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;
107 razor_wstr_destroy(struct razor_wstr *wstr)
113 RAZOR_EXPORT struct razor_atomic *
114 razor_atomic_open(const char *description)
117 struct razor_atomic *atomic;
119 atomic = zalloc(sizeof *atomic);
120 buf = razor_utf8_to_utf16(description, -1);
121 atomic->transaction = CreateTransaction(NULL, 0,
122 TRANSACTION_DO_NOT_PROMOTE,
130 razor_atomic_commit(struct razor_atomic *atomic)
134 if (razor_atomic_in_error_state(atomic))
137 retval = !CommitTransaction(atomic->transaction);
140 razor_set_error_mswin(&atomic->error, NULL, GetLastError());
141 RollbackTransaction(atomic->transaction);
144 CloseHandle(atomic->transaction);
145 atomic->transaction = INVALID_HANDLE_VALUE;
151 razor_atomic_destroy(struct razor_atomic *atomic)
155 for(i = 0; i < atomic->n_files; i++) {
156 if (atomic->files[i].h != INVALID_HANDLE_VALUE) {
157 CloseHandle(atomic->files[i].h);
158 free(atomic->files[i].path);
162 if (atomic->transaction != INVALID_HANDLE_VALUE) {
163 RollbackTransaction(atomic->transaction);
164 CloseHandle(atomic->transaction);
167 razor_error_free(atomic->error);
172 * If root_uri is empty, then uri can be a URI (but not a relative-ref; ie.,
173 * a scheme must be specified). If root_uri is non-empty, then it must be a
174 * URI (but not a relative-ref) and uri must be a non-empty path.
177 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root_uri,
180 struct razor_wstr *buffer;
181 const char *slash, *next;
182 WIN32_FILE_ATTRIBUTE_DATA fa;
186 struct razor_error *tmp_error = NULL;
187 char *s, *root, *path;
189 if (razor_atomic_in_error_state(atomic))
193 if (razor_uri_parse(&ru, uri, &tmp_error)) {
194 razor_atomic_propagate_error(atomic, tmp_error, NULL);
197 s = SKIP_DRIVE_LETTER(ru.path);
201 s = razor_uri_recompose(&ru);
204 root = razor_path_from_parsed_uri(&ru, &tmp_error);
205 razor_uri_destroy(&ru);
207 razor_atomic_propagate_error(atomic, tmp_error, NULL);
210 path = razor_path_from_uri(uri, &tmp_error);
212 razor_atomic_propagate_error(atomic, tmp_error, NULL);
217 root = razor_path_from_uri(root_uri, &tmp_error);
219 razor_atomic_propagate_error(atomic, tmp_error, NULL);
222 s = razor_concat("file:", uri, NULL);
223 path = razor_path_from_uri(s, &tmp_error);
226 razor_atomic_propagate_error(atomic, tmp_error, NULL);
231 * Arguably, razor_atomic_make_dirs(a, "file:///", "c:/xxx")
232 * is wrong, and we should be called as:
233 * razor_atomic_make_dirs(a, "file:///c:/", "xxx")
234 * but cope with it anyway.
236 s = SKIP_DRIVE_LETTER(path);
237 if (!strcmp(root, "/") && *s == '/') {
243 buffer = razor_wstr_create(root, -1);
245 slash = buffer->len ? SKIP_DRIVE_LETTER(path) : path;
247 for (; *slash != '\0'; slash = next) {
248 next = strchr(slash + 1, '/');
252 razor_wstr_append(buffer, slash, next - slash);
255 if (razor_valid_root_name2(buffer->str))
258 r = GetFileAttributesTransactedW(buffer->str,
259 GetFileExInfoStandard,
261 atomic->transaction);
264 err = GetLastError();
265 if (err == ERROR_FILE_NOT_FOUND) {
268 razor_set_error_mswin(&atomic->error,
270 razor_wstr_destroy(buffer);
274 } else if (!(fa.dwFileAttributes&
275 FILE_ATTRIBUTE_DIRECTORY)) {
276 razor_set_error2(&atomic->error,
278 ERROR_DIRECTORY, buffer->str,
280 razor_wstr_destroy(buffer);
286 if (!CreateDirectoryTransactedW(NULL, buffer->str, NULL,
287 atomic->transaction)) {
288 razor_set_error_mswin(&atomic->error,
291 razor_wstr_destroy(buffer);
296 /* FIXME: What to do about permissions for dirs we
297 * have to create but are not in the cpio archive? */
301 razor_wstr_destroy(buffer);
308 razor_atomic_remove(struct razor_atomic *atomic, const char *uri)
310 struct razor_error *tmp_error = NULL;
315 if (razor_atomic_in_error_state(atomic))
318 path = razor_path_from_uri(uri, &tmp_error);
320 razor_atomic_propagate_error(atomic, tmp_error, NULL);
324 buf = razor_utf8_to_utf16(path, -1);
328 if (DeleteFileTransactedW(buf, atomic->transaction)) {
333 err = GetLastError();
334 if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) {
339 if (SetFileAttributesTransactedW(buf, FILE_ATTRIBUTE_NORMAL,
340 atomic->transaction)) {
341 if (DeleteFileTransactedW(buf, atomic->transaction)) {
345 err = GetLastError();
348 if (RemoveDirectoryTransactedW(buf, atomic->transaction) ||
349 GetLastError() == ERROR_DIR_NOT_EMPTY) {
355 * It would be tempting to use:
356 * MoveFileEx(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)
357 * but unless we can guarantee that the system will be rebooted
358 * before we (or some other application) write another file with the
359 * same path, this is likely to cause more problems than it solves.
362 razor_set_error_mswin(&atomic->error, buf, err);
368 razor_atomic_rename_file(struct razor_atomic *atomic, const char *old_uri,
371 struct razor_error *tmp_error = NULL;
372 char *oldpath, *newpath;
373 wchar_t *oldbuf, *newbuf;
374 const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
376 if (razor_atomic_in_error_state(atomic))
379 newpath = razor_path_from_uri(new_uri, &tmp_error);
381 razor_atomic_propagate_error(atomic, tmp_error, NULL);
385 oldpath = razor_path_from_uri(old_uri, &tmp_error);
387 razor_atomic_propagate_error(atomic, tmp_error, NULL);
392 newbuf = razor_utf8_to_utf16(newpath, -1);
393 oldbuf = razor_utf8_to_utf16(oldpath, -1);
399 * Passing MOVEFILE_REPLACE_EXISTING to MoveFileTransaction() will
400 * cover every case we care about _except_ replacing an empty
401 * directory with a file. Calling RemoveDirectoryTransacted() will deal
402 * with this case while having no effect in all other cases.
404 (void)RemoveDirectoryTransactedW(newbuf, atomic->transaction);
406 if (!MoveFileTransactedW(oldbuf, newbuf, NULL, NULL, flags,
407 atomic->transaction))
408 razor_set_error_mswin(&atomic->error, newbuf, GetLastError());
413 return razor_atomic_in_error_state(atomic);
417 razor_atomic_create_dir(struct razor_atomic *atomic, const char *uri,
420 struct razor_error *tmp_error = NULL;
424 WIN32_FILE_ATTRIBUTE_DATA fa;
426 if (razor_atomic_in_error_state(atomic))
429 dirname = razor_path_from_uri(uri, &tmp_error);
431 razor_atomic_propagate_error(atomic, tmp_error, NULL);
435 buf = razor_utf8_to_utf16(dirname, -1);
439 if (!CreateDirectoryTransactedW(NULL, buf, NULL, atomic->transaction)) {
440 err = GetLastError();
441 if (err != ERROR_FILE_EXISTS && err != ERROR_ALREADY_EXISTS) {
443 razor_set_error_mswin(&atomic->error, buf, err);
448 if (!GetFileAttributesTransactedW(buf, GetFileExInfoStandard,
449 &fa, atomic->transaction))
452 if (!(fa.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) {
453 if (razor_atomic_remove(atomic, dirname)) {
457 if (!CreateDirectoryTransactedW(NULL, buf, NULL,
458 atomic->transaction)) {
459 err = GetLastError();
471 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
474 if (razor_atomic_in_error_state(atomic))
478 * This isn't true, but symbolic links under Windows 7
479 * need to know whether the target is a directory or not
480 * and we don't always know that at the time when the
481 * link is created, so it's a convienent lie for now.
483 razor_set_error(&atomic->error, RAZOR_MSWIN_ERROR, ERROR_NOT_SUPPORTED,
484 NULL, "Symbolic links not supported on this platform");
490 razor_atomic_create_file(struct razor_atomic *atomic, const char *uri,
493 struct razor_error *tmp_error = NULL;
496 struct razor_atomic_file *files;
497 int i = atomic->n_files;
499 if (razor_atomic_in_error_state(atomic))
502 filename = razor_path_from_uri(uri, &tmp_error);
504 razor_atomic_propagate_error(atomic, tmp_error, NULL);
508 files = realloc(atomic->files,
509 (atomic->n_files+1) * sizeof(struct razor_atomic_file));
511 razor_set_error(&atomic->error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
512 "Not enough memory");
517 atomic->files = files;
519 files[i].path = razor_utf8_to_utf16(filename, -1);
524 * Passing CREATE_ALWAYS to CreateFileTransacted() will cover
525 * every case we care about _except_ replacing an empty directory
526 * with a file. Calling RemoveDirectoryTransacted() will deal
527 * with this case while having no effect in all other cases.
529 (void)RemoveDirectoryTransactedW(files[i].path, atomic->transaction);
532 attribs = FILE_ATTRIBUTE_NORMAL;
534 attribs = FILE_ATTRIBUTE_READONLY;
536 files[i].h = CreateFileTransactedW(files[i].path, GENERIC_WRITE,
537 0, NULL, CREATE_ALWAYS, attribs,
538 NULL, atomic->transaction, NULL,
541 if (files[i].h == INVALID_HANDLE_VALUE) {
542 razor_set_error_mswin(&atomic->error, files[i].path,
553 razor_atomic_write(struct razor_atomic *atomic, int handle, const void *data,
558 if (razor_atomic_in_error_state(atomic))
561 assert(handle < atomic->n_files);
562 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
565 if (!WriteFile(atomic->files[handle].h, data, size, &written,
567 razor_set_error_mswin(&atomic->error,
568 atomic->files[handle].path,
571 (void)CloseHandle(atomic->files[handle].h);
572 free(atomic->files[handle].path);
573 atomic->files[handle].path = NULL;
574 atomic->files[handle].h = INVALID_HANDLE_VALUE;
587 razor_atomic_sync(struct razor_atomic *atomic, int handle)
591 if (razor_atomic_in_error_state(atomic))
594 assert(handle < atomic->n_files);
595 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
597 if (!CloseHandle(atomic->files[handle].h)) {
598 razor_set_error_mswin(&atomic->error,
599 atomic->files[handle].path,
601 free(atomic->files[handle].path);
602 atomic->files[handle].path = NULL;
603 atomic->files[handle].h = INVALID_HANDLE_VALUE;
607 h = CreateFileTransactedW(atomic->files[handle].path, GENERIC_WRITE, 0,
608 NULL, OPEN_EXISTING, 0, NULL,
609 atomic->transaction, NULL, NULL);
610 atomic->files[handle].h = h;
612 if (atomic->files[handle].h == INVALID_HANDLE_VALUE) {
613 razor_set_error_mswin(&atomic->error,
614 atomic->files[handle].path,
616 free(atomic->files[handle].path);
617 atomic->files[handle].path = NULL;
621 return razor_atomic_in_error_state(atomic);
625 razor_atomic_close(struct razor_atomic *atomic, int handle)
627 if (razor_atomic_in_error_state(atomic))
630 assert(handle < atomic->n_files);
631 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
633 if (!CloseHandle(atomic->files[handle].h))
634 razor_set_error_mswin(&atomic->error,
635 atomic->files[handle].path,
638 free(atomic->files[handle].path);
639 atomic->files[handle].path = NULL;
640 atomic->files[handle].h = INVALID_HANDLE_VALUE;
642 while(atomic->n_files > 0 &&
643 atomic->files[atomic->n_files-1].h == INVALID_HANDLE_VALUE)
646 return razor_atomic_in_error_state(atomic);
649 #endif /* HAVE_WINDOWS_KTM */