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.
35 #include "razor-internal.h"
38 razor_valid_root_name2(const wchar_t *name)
40 if (razor_allow_all_root_names())
41 return !wcschr(name, '/');
43 return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
52 static struct razor_wstr *
53 razor_wstr_create(const char *init, int len)
56 struct razor_wstr *wstr;
58 wstr = malloc(sizeof(struct razor_wstr));
60 n = MultiByteToWideChar(CP_UTF8, 0, init, len, NULL, 0);
61 if (len >= 0 && init[len])
66 wstr->allocated = n * 2;
67 wstr->str = malloc(wstr->allocated * sizeof(wchar_t));
73 (void)MultiByteToWideChar(CP_UTF8, 0, init, len, wstr->str, n);
74 if (len >= 0 && init[len])
75 wstr->str[wstr->len] = 0;
81 razor_wstr_append(struct razor_wstr *wstr, const char *s, int len)
86 n = MultiByteToWideChar(CP_UTF8, 0, s, len, NULL, 0);
87 if (len < 0 || !s[len])
90 if (wstr->allocated <= wstr->len + n) {
91 allocated = (wstr->len + n + 1) * 2;
92 str = realloc(wstr->str, allocated * sizeof(wchar_t));
95 wstr->allocated = allocated;
99 (void)MultiByteToWideChar(CP_UTF8, 0, s, len, wstr->str + wstr->len, n);
101 wstr->str[wstr->len] = 0;
108 razor_wstr_contents(struct razor_wstr *wstr, int len)
113 if (len < 0 || len > wstr->len)
116 n = WideCharToMultiByte(CP_UTF8, 0, wstr->str, len, NULL, 0, NULL,
121 (void)WideCharToMultiByte(CP_UTF8, 0, wstr->str, wstr->len, s, n, NULL,
131 razor_wstr_destroy(struct razor_wstr *wstr)
137 RAZOR_EXPORT struct razor_atomic *
138 razor_atomic_open(const char *description)
141 struct razor_atomic *atomic;
143 atomic = zalloc(sizeof *atomic);
144 buf = razor_utf8_to_utf16(description, -1);
145 atomic->transaction = CreateTransaction(NULL, 0,
146 TRANSACTION_DO_NOT_PROMOTE,
154 razor_atomic_commit(struct razor_atomic *atomic)
158 if (razor_atomic_in_error_state(atomic))
161 retval = !CommitTransaction(atomic->transaction);
164 razor_set_error_mswin(&atomic->error, NULL, GetLastError());
165 RollbackTransaction(atomic->transaction);
168 CloseHandle(atomic->transaction);
169 atomic->transaction = INVALID_HANDLE_VALUE;
175 razor_atomic_destroy(struct razor_atomic *atomic)
179 for(i = 0; i < atomic->n_files; i++) {
180 if (atomic->files[i].h != INVALID_HANDLE_VALUE) {
181 CloseHandle(atomic->files[i].h);
182 free(atomic->files[i].path);
186 if (atomic->transaction != INVALID_HANDLE_VALUE) {
187 RollbackTransaction(atomic->transaction);
188 CloseHandle(atomic->transaction);
191 razor_error_free(atomic->error);
196 * If root_uri is empty, then uri can be a URI (but not a relative-ref; ie.,
197 * a scheme must be specified). If root_uri is non-empty, then it must be a
198 * URI (but not a relative-ref) and uri must be a non-empty path.
201 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root_uri,
204 struct razor_wstr *buffer;
205 const char *slash, *next;
206 WIN32_FILE_ATTRIBUTE_DATA fa;
210 struct razor_error *tmp_error = NULL;
211 char *s, *root, *path;
213 if (razor_atomic_in_error_state(atomic))
217 if (razor_uri_parse(&ru, uri, &tmp_error)) {
218 razor_atomic_propagate_error(atomic, tmp_error, NULL);
224 s = SKIP_DRIVE_LETTER(ru.path + 1);
225 if (s > ru.path + 1 && *s == '/')
229 s = razor_uri_recompose(&ru);
232 root = razor_path_from_parsed_uri(&ru, &tmp_error);
233 razor_uri_destroy(&ru);
235 razor_atomic_propagate_error(atomic, tmp_error, NULL);
238 s = razor_concat("file:", uri, NULL);
239 path = razor_path_from_uri(s, &tmp_error);
242 razor_atomic_propagate_error(atomic, tmp_error, NULL);
247 root = razor_path_from_uri(root_uri, &tmp_error);
249 razor_atomic_propagate_error(atomic, tmp_error, NULL);
252 s = razor_concat("file:", uri, NULL);
253 path = razor_path_from_uri(s, &tmp_error);
256 razor_atomic_propagate_error(atomic, tmp_error, NULL);
261 * Arguably, razor_atomic_make_dirs(a, "file:///", "c:/xxx")
262 * is wrong, and we should be called as:
263 * razor_atomic_make_dirs(a, "file:///c:/", "xxx")
264 * but cope with it anyway.
266 s = SKIP_DRIVE_LETTER(path);
267 if (!strcmp(root, "/") && *s == '/') {
273 buffer = razor_wstr_create(root, -1);
275 slash = buffer->len ? SKIP_DRIVE_LETTER(path) : path;
277 for (; *slash != '\0'; slash = next) {
278 next = strchr(slash + 1, '/');
282 razor_wstr_append(buffer, slash, next - slash);
285 if (razor_valid_root_name2(buffer->str))
288 r = GetFileAttributesTransactedW(buffer->str,
289 GetFileExInfoStandard,
291 atomic->transaction);
294 err = GetLastError();
295 if (err == ERROR_FILE_NOT_FOUND) {
298 razor_set_error_mswin(&atomic->error,
300 razor_wstr_destroy(buffer);
304 } else if (!(fa.dwFileAttributes&
305 FILE_ATTRIBUTE_DIRECTORY)) {
306 razor_set_error2(&atomic->error,
308 ERROR_DIRECTORY, buffer->str,
310 razor_wstr_destroy(buffer);
316 if (!CreateDirectoryTransactedW(NULL, buffer->str, NULL,
317 atomic->transaction)) {
318 razor_set_error_mswin(&atomic->error,
321 razor_wstr_destroy(buffer);
326 /* FIXME: What to do about permissions for dirs we
327 * have to create but are not in the cpio archive? */
331 razor_wstr_destroy(buffer);
338 razor_atomic_remove(struct razor_atomic *atomic, const char *uri)
340 struct razor_error *tmp_error = NULL;
345 if (razor_atomic_in_error_state(atomic))
348 path = razor_path_from_uri(uri, &tmp_error);
350 razor_atomic_propagate_error(atomic, tmp_error, NULL);
354 buf = razor_utf8_to_utf16(path, -1);
358 if (DeleteFileTransactedW(buf, atomic->transaction)) {
363 err = GetLastError();
364 if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) {
369 if (SetFileAttributesTransactedW(buf, FILE_ATTRIBUTE_NORMAL,
370 atomic->transaction)) {
371 if (DeleteFileTransactedW(buf, atomic->transaction)) {
375 err = GetLastError();
378 if (RemoveDirectoryTransactedW(buf, atomic->transaction) ||
379 GetLastError() == ERROR_DIR_NOT_EMPTY) {
385 * It would be tempting to use:
386 * MoveFileEx(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)
387 * but unless we can guarantee that the system will be rebooted
388 * before we (or some other application) write another file with the
389 * same path, this is likely to cause more problems than it solves.
392 razor_set_error_mswin(&atomic->error, buf, err);
398 razor_atomic_rename_file(struct razor_atomic *atomic, const char *old_uri,
401 struct razor_error *tmp_error = NULL;
402 char *oldpath, *newpath;
403 wchar_t *oldbuf, *newbuf;
404 const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
406 if (razor_atomic_in_error_state(atomic))
409 newpath = razor_path_from_uri(new_uri, &tmp_error);
411 razor_atomic_propagate_error(atomic, tmp_error, NULL);
415 oldpath = razor_path_from_uri(old_uri, &tmp_error);
417 razor_atomic_propagate_error(atomic, tmp_error, NULL);
422 newbuf = razor_utf8_to_utf16(newpath, -1);
423 oldbuf = razor_utf8_to_utf16(oldpath, -1);
429 * Passing MOVEFILE_REPLACE_EXISTING to MoveFileTransaction() will
430 * cover every case we care about _except_ replacing an empty
431 * directory with a file. Calling RemoveDirectoryTransacted() will deal
432 * with this case while having no effect in all other cases.
434 (void)RemoveDirectoryTransactedW(newbuf, atomic->transaction);
436 if (!MoveFileTransactedW(oldbuf, newbuf, NULL, NULL, flags,
437 atomic->transaction))
438 razor_set_error_mswin(&atomic->error, newbuf, GetLastError());
443 return razor_atomic_in_error_state(atomic);
447 razor_atomic_create_dir(struct razor_atomic *atomic, const char *uri,
450 struct razor_error *tmp_error = NULL;
454 WIN32_FILE_ATTRIBUTE_DATA fa;
456 if (razor_atomic_in_error_state(atomic))
459 dirname = razor_path_from_uri(uri, &tmp_error);
461 razor_atomic_propagate_error(atomic, tmp_error, NULL);
465 buf = razor_utf8_to_utf16(dirname, -1);
469 if (!CreateDirectoryTransactedW(NULL, buf, NULL, atomic->transaction)) {
470 err = GetLastError();
471 if (err != ERROR_FILE_EXISTS && err != ERROR_ALREADY_EXISTS) {
473 razor_set_error_mswin(&atomic->error, buf, err);
478 if (!GetFileAttributesTransactedW(buf, GetFileExInfoStandard,
479 &fa, atomic->transaction))
482 if (!(fa.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) {
483 if (razor_atomic_remove(atomic, uri)) {
487 if (!CreateDirectoryTransactedW(NULL, buf, NULL,
488 atomic->transaction)) {
489 err = GetLastError();
501 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
504 if (razor_atomic_in_error_state(atomic))
508 * This isn't true, but symbolic links under Windows 7
509 * need to know whether the target is a directory or not
510 * and we don't always know that at the time when the
511 * link is created, so it's a convienent lie for now.
513 razor_set_error(&atomic->error, RAZOR_MSWIN_ERROR, ERROR_NOT_SUPPORTED,
514 NULL, "Symbolic links not supported on this platform");
520 razor_atomic_create_file(struct razor_atomic *atomic, const char *uri,
523 struct razor_error *tmp_error = NULL;
526 struct razor_atomic_file *files;
527 int i = atomic->n_files;
529 if (razor_atomic_in_error_state(atomic))
532 filename = razor_path_from_uri(uri, &tmp_error);
534 razor_atomic_propagate_error(atomic, tmp_error, NULL);
538 files = realloc(atomic->files,
539 (atomic->n_files+1) * sizeof(struct razor_atomic_file));
541 razor_set_error(&atomic->error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
542 "Not enough memory");
547 atomic->files = files;
549 files[i].path = razor_utf8_to_utf16(filename, -1);
554 * Passing CREATE_ALWAYS to CreateFileTransacted() will cover
555 * every case we care about _except_ replacing an empty directory
556 * with a file. Calling RemoveDirectoryTransacted() will deal
557 * with this case while having no effect in all other cases.
559 (void)RemoveDirectoryTransactedW(files[i].path, atomic->transaction);
562 attribs = FILE_ATTRIBUTE_NORMAL;
564 attribs = FILE_ATTRIBUTE_READONLY;
566 files[i].h = CreateFileTransactedW(files[i].path, GENERIC_WRITE,
567 0, NULL, CREATE_ALWAYS, attribs,
568 NULL, atomic->transaction, NULL,
571 if (files[i].h == INVALID_HANDLE_VALUE) {
572 razor_set_error_mswin(&atomic->error, files[i].path,
583 razor_atomic_write(struct razor_atomic *atomic, int handle, const void *data,
588 if (razor_atomic_in_error_state(atomic))
591 assert(handle < atomic->n_files);
592 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
595 if (!WriteFile(atomic->files[handle].h, data, size, &written,
597 razor_set_error_mswin(&atomic->error,
598 atomic->files[handle].path,
601 (void)CloseHandle(atomic->files[handle].h);
602 free(atomic->files[handle].path);
603 atomic->files[handle].path = NULL;
604 atomic->files[handle].h = INVALID_HANDLE_VALUE;
617 razor_atomic_sync(struct razor_atomic *atomic, int handle)
621 if (razor_atomic_in_error_state(atomic))
624 assert(handle < atomic->n_files);
625 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
627 if (!CloseHandle(atomic->files[handle].h)) {
628 razor_set_error_mswin(&atomic->error,
629 atomic->files[handle].path,
631 free(atomic->files[handle].path);
632 atomic->files[handle].path = NULL;
633 atomic->files[handle].h = INVALID_HANDLE_VALUE;
637 h = CreateFileTransactedW(atomic->files[handle].path, GENERIC_WRITE, 0,
638 NULL, OPEN_EXISTING, 0, NULL,
639 atomic->transaction, NULL, NULL);
640 atomic->files[handle].h = h;
642 if (atomic->files[handle].h == INVALID_HANDLE_VALUE) {
643 razor_set_error_mswin(&atomic->error,
644 atomic->files[handle].path,
646 free(atomic->files[handle].path);
647 atomic->files[handle].path = NULL;
651 return razor_atomic_in_error_state(atomic);
655 razor_atomic_close(struct razor_atomic *atomic, int handle)
657 if (razor_atomic_in_error_state(atomic))
660 assert(handle < atomic->n_files);
661 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
663 if (!CloseHandle(atomic->files[handle].h))
664 razor_set_error_mswin(&atomic->error,
665 atomic->files[handle].path,
668 free(atomic->files[handle].path);
669 atomic->files[handle].path = NULL;
670 atomic->files[handle].h = INVALID_HANDLE_VALUE;
672 while(atomic->n_files > 0 &&
673 atomic->files[atomic->n_files-1].h == INVALID_HANDLE_VALUE)
676 return razor_atomic_in_error_state(atomic);
679 #endif /* HAVE_WINDOWS_KTM */