2 * Copyright (C) 2011, 2012, 2014 J. Ali Harlow <ali@juiblex.co.uk>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37 #include "razor-internal.h"
40 razor_valid_root_name2(const wchar_t *name)
42 if (razor_allow_all_root_names())
43 return !wcschr(name, '/');
45 return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
54 static struct razor_wstr *
55 razor_wstr_create(const char *init, int len)
58 struct razor_wstr *wstr;
60 wstr = malloc(sizeof(struct razor_wstr));
62 n = MultiByteToWideChar(CP_UTF8, 0, init, len, NULL, 0);
63 if (len >= 0 && init[len])
68 wstr->allocated = n * 2;
69 wstr->str = malloc(wstr->allocated * sizeof(wchar_t));
75 (void)MultiByteToWideChar(CP_UTF8, 0, init, len, wstr->str, n);
76 if (len >= 0 && init[len])
77 wstr->str[wstr->len] = 0;
83 razor_wstr_append(struct razor_wstr *wstr, const char *s, int len)
88 n = MultiByteToWideChar(CP_UTF8, 0, s, len, NULL, 0);
89 if (len < 0 || !s[len])
92 if (wstr->allocated <= wstr->len + n) {
93 allocated = (wstr->len + n + 1) * 2;
94 str = realloc(wstr->str, allocated * sizeof(wchar_t));
97 wstr->allocated = allocated;
101 (void)MultiByteToWideChar(CP_UTF8, 0, s, len, wstr->str + wstr->len, n);
103 wstr->str[wstr->len] = 0;
109 razor_wstr_destroy(struct razor_wstr *wstr)
115 RAZOR_EXPORT struct razor_atomic *
116 razor_atomic_open(const char *description)
119 struct razor_atomic *atomic;
121 atomic = zalloc(sizeof *atomic);
122 buf = razor_utf8_to_utf16(description, -1);
123 atomic->transaction = CreateTransaction(NULL, 0,
124 TRANSACTION_DO_NOT_PROMOTE,
132 razor_atomic_commit(struct razor_atomic *atomic)
136 if (razor_atomic_in_error_state(atomic))
139 retval = !CommitTransaction(atomic->transaction);
142 razor_set_error_mswin(&atomic->error, NULL, GetLastError());
143 RollbackTransaction(atomic->transaction);
146 CloseHandle(atomic->transaction);
147 atomic->transaction = INVALID_HANDLE_VALUE;
153 razor_atomic_destroy(struct razor_atomic *atomic)
157 for(i = 0; i < atomic->n_files; i++) {
158 if (atomic->files[i].h != INVALID_HANDLE_VALUE) {
159 CloseHandle(atomic->files[i].h);
160 free(atomic->files[i].path);
164 if (atomic->transaction != INVALID_HANDLE_VALUE) {
165 RollbackTransaction(atomic->transaction);
166 CloseHandle(atomic->transaction);
169 razor_error_free(atomic->error);
174 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
177 struct razor_wstr *buffer;
178 const char *slash, *next;
179 WIN32_FILE_ATTRIBUTE_DATA fa;
183 if (razor_atomic_in_error_state(atomic))
186 buffer = razor_wstr_create(root, -1);
187 slash = buffer->len ? SKIP_DRIVE_LETTER(path) : path;
189 for (; *slash != '\0'; slash = next) {
190 next = strpbrk(slash + 1, "/\\");
194 razor_wstr_append(buffer, slash, next - slash);
197 if (razor_valid_root_name2(buffer->str))
200 r = GetFileAttributesTransactedW(buffer->str,
201 GetFileExInfoStandard,
203 atomic->transaction);
206 err = GetLastError();
207 if (err == ERROR_FILE_NOT_FOUND) {
210 razor_set_error_mswin(&atomic->error,
212 razor_wstr_destroy(buffer);
215 } else if (!(fa.dwFileAttributes&
216 FILE_ATTRIBUTE_DIRECTORY)) {
217 razor_set_error2(&atomic->error, buffer->str,
219 razor_wstr_destroy(buffer);
224 if (!CreateDirectoryTransactedW(NULL, buffer->str, NULL,
225 atomic->transaction)) {
226 razor_set_error_mswin(&atomic->error,
229 razor_wstr_destroy(buffer);
233 /* FIXME: What to do about permissions for dirs we
234 * have to create but are not in the cpio archive? */
238 razor_wstr_destroy(buffer);
244 razor_atomic_remove(struct razor_atomic *atomic, const char *path)
249 if (razor_atomic_in_error_state(atomic))
252 buf = razor_utf8_to_utf16(path, -1);
254 if (DeleteFileTransactedW(buf, atomic->transaction)) {
259 err = GetLastError();
260 if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) {
265 if (SetFileAttributesTransactedW(buf, FILE_ATTRIBUTE_NORMAL,
266 atomic->transaction)) {
267 if (DeleteFileTransactedW(buf, atomic->transaction)) {
271 err = GetLastError();
274 if (RemoveDirectoryTransactedW(buf, atomic->transaction) ||
275 GetLastError() == ERROR_DIR_NOT_EMPTY) {
281 * It would be tempting to use:
282 * MoveFileEx(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)
283 * but unless we can guarantee that the system will be rebooted
284 * before we (or some other application) write another file with the
285 * same path, this is likely to cause more problems than it solves.
288 razor_set_error_mswin(&atomic->error, buf, err);
294 razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
297 wchar_t *oldbuf, *newbuf;
298 const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
300 if (razor_atomic_in_error_state(atomic))
303 newbuf = razor_utf8_to_utf16(newpath, -1);
304 oldbuf = razor_utf8_to_utf16(oldpath, -1);
307 * Passing MOVEFILE_REPLACE_EXISTING to MoveFileTransaction() will
308 * cover every case we care about _except_ replacing an empty
309 * directory with a file. Calling RemoveDirectoryTransacted() will deal
310 * with this case while having no effect in all other cases.
312 (void)RemoveDirectoryTransactedW(newbuf, atomic->transaction);
314 if (!MoveFileTransactedW(oldbuf, newbuf, NULL, NULL, flags,
315 atomic->transaction))
316 razor_set_error_mswin(&atomic->error, newbuf, GetLastError());
321 return razor_atomic_in_error_state(atomic);
325 razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
330 WIN32_FILE_ATTRIBUTE_DATA fa;
332 if (razor_atomic_in_error_state(atomic))
335 buf = razor_utf8_to_utf16(dirname, -1);
337 if (!CreateDirectoryTransactedW(NULL, buf, NULL, atomic->transaction)) {
338 err = GetLastError();
339 if (err != ERROR_FILE_EXISTS && err != ERROR_ALREADY_EXISTS) {
341 razor_set_error_mswin(&atomic->error, buf, err);
346 if (!GetFileAttributesTransactedW(buf, GetFileExInfoStandard,
347 &fa, atomic->transaction))
350 if (!(fa.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) {
351 if (razor_atomic_remove(atomic, dirname)) {
355 if (!CreateDirectoryTransactedW(NULL, buf, NULL,
356 atomic->transaction)) {
357 err = GetLastError();
369 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
372 if (razor_atomic_in_error_state(atomic))
376 * This isn't true, but symbolic links under Windows 7
377 * need to know whether the target is a directory or not
378 * and we don't always know that at the time when the
379 * link is created, so it's a convienent lie for now.
381 razor_set_error(&atomic->error, NULL,
382 "Symbolic links not supported on this platform");
388 razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
392 struct razor_atomic_file *files;
393 int i = atomic->n_files;
395 if (razor_atomic_in_error_state(atomic))
398 files = realloc(atomic->files,
399 (atomic->n_files+1) * sizeof(struct razor_atomic_file));
401 razor_set_error(&atomic->error, NULL, "Not enough memory");
405 atomic->files = files;
407 files[i].path = razor_utf8_to_utf16(filename, -1);
410 * Passing CREATE_ALWAYS to CreateFileTransacted() will cover
411 * every case we care about _except_ replacing an empty directory
412 * with a file. Calling RemoveDirectoryTransacted() will deal
413 * with this case while having no effect in all other cases.
415 (void)RemoveDirectoryTransactedW(files[i].path, atomic->transaction);
418 attribs = FILE_ATTRIBUTE_NORMAL;
420 attribs = FILE_ATTRIBUTE_READONLY;
422 files[i].h = CreateFileTransactedW(files[i].path, GENERIC_WRITE,
423 0, NULL, CREATE_ALWAYS, attribs,
424 NULL, atomic->transaction, NULL,
427 if (files[i].h == INVALID_HANDLE_VALUE) {
428 razor_set_error_mswin(&atomic->error, files[i].path,
439 razor_atomic_write(struct razor_atomic *atomic, int handle, const void *data,
444 if (razor_atomic_in_error_state(atomic))
447 assert(handle < atomic->n_files);
448 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
451 if (!WriteFile(atomic->files[handle].h, data, size, &written,
453 razor_set_error_mswin(&atomic->error,
454 atomic->files[handle].path,
457 (void)CloseHandle(atomic->files[handle].h);
458 free(atomic->files[handle].path);
459 atomic->files[handle].path = NULL;
460 atomic->files[handle].h = INVALID_HANDLE_VALUE;
473 razor_atomic_sync(struct razor_atomic *atomic, int handle)
477 if (razor_atomic_in_error_state(atomic))
480 assert(handle < atomic->n_files);
481 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
483 if (!CloseHandle(atomic->files[handle].h)) {
484 razor_set_error_mswin(&atomic->error,
485 atomic->files[handle].path,
487 free(atomic->files[handle].path);
488 atomic->files[handle].path = NULL;
489 atomic->files[handle].h = INVALID_HANDLE_VALUE;
493 h = CreateFileTransactedW(atomic->files[handle].path, GENERIC_WRITE, 0,
494 NULL, OPEN_EXISTING, 0, NULL,
495 atomic->transaction, NULL, NULL);
496 atomic->files[handle].h = h;
498 if (atomic->files[handle].h == INVALID_HANDLE_VALUE) {
499 razor_set_error_mswin(&atomic->error,
500 atomic->files[handle].path,
502 free(atomic->files[handle].path);
503 atomic->files[handle].path = NULL;
507 return razor_atomic_in_error_state(atomic);
511 razor_atomic_close(struct razor_atomic *atomic, int handle)
513 if (razor_atomic_in_error_state(atomic))
516 assert(handle < atomic->n_files);
517 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
519 if (!CloseHandle(atomic->files[handle].h))
520 razor_set_error_mswin(&atomic->error,
521 atomic->files[handle].path,
524 free(atomic->files[handle].path);
525 atomic->files[handle].path = NULL;
526 atomic->files[handle].h = INVALID_HANDLE_VALUE;
528 while(atomic->n_files > 0 &&
529 atomic->files[atomic->n_files-1].h == INVALID_HANDLE_VALUE)
532 return razor_atomic_in_error_state(atomic);
535 #endif /* HAVE_WINDOWS_KTM */