2 * Copyright (C) 2011 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.
39 #include "razor-internal.h"
49 #define RAZOR_ASCII_ISALPHA(c) \
50 ((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
52 static int allow_all_root_names = 0;
55 * Primarily intended for testing named roots under UNIX platforms.
58 razor_disable_root_name_checks(int disable)
60 allow_all_root_names = disable;
66 razor_utf16_to_utf8(const wchar_t *utf16, int len)
71 n = WideCharToMultiByte(CP_UTF8, 0, utf16, len, NULL, 0, NULL, NULL);
72 if (len >= 0 && utf16[len])
75 (void)WideCharToMultiByte(CP_UTF8, 0, utf16, len, utf8, n, NULL, NULL);
76 if (len >= 0 && utf16[len])
83 razor_utf8_to_utf16(const char *utf8, int len)
88 n = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0);
89 if (len >= 0 && utf8[len])
91 utf16 = malloc(n * sizeof(wchar_t));
92 (void)MultiByteToWideChar(CP_UTF8, 0, utf8, len, utf16, n);
93 if (len >= 0 && utf8[len])
99 #endif /* MSWIN_API */
104 razor_valid_root_name(const wchar_t *name)
106 if (allow_all_root_names)
107 return !wcschr(name, '/');
109 return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
113 struct razor_atomic {
116 struct razor_atomic_file {
130 static struct razor_wstr *
131 razor_wstr_create(const char *init, int len)
134 struct razor_wstr *wstr;
136 wstr = malloc(sizeof(struct razor_wstr));
138 n = MultiByteToWideChar(CP_UTF8, 0, init, len, NULL, 0);
139 if (len >= 0 && init[len])
144 wstr->allocated = n * 2;
145 wstr->str = malloc(wstr->allocated * sizeof(wchar_t));
151 (void)MultiByteToWideChar(CP_UTF8, 0, init, len, wstr->str, n);
152 if (len >= 0 && init[len])
153 wstr->str[wstr->len] = 0;
159 razor_wstr_append(struct razor_wstr *wstr, const char *s, int len)
164 n = MultiByteToWideChar(CP_UTF8, 0, s, len, NULL, 0);
165 if (len < 0 || !s[len])
168 if (wstr->allocated <= wstr->len + n) {
169 allocated = (wstr->len + n + 1) * 2;
170 str = realloc(wstr->str, allocated * sizeof(wchar_t));
173 wstr->allocated = allocated;
177 (void)MultiByteToWideChar(CP_UTF8, 0, s, len, wstr->str + wstr->len, n);
179 wstr->str[wstr->len] = 0;
185 razor_wstr_destroy(struct razor_wstr *wstr)
191 RAZOR_EXPORT struct razor_atomic *
192 razor_atomic_open(const char *description)
195 struct razor_atomic *atomic;
197 atomic = zalloc(sizeof *atomic);
198 buf = razor_utf8_to_utf16(description, -1);
199 atomic->transaction = CreateTransaction(NULL, 0,
200 TRANSACTION_DO_NOT_PROMOTE,
208 razor_atomic_set_error_str(struct razor_atomic *atomic, const wchar_t *path,
211 assert(!atomic->error_str);
213 free(atomic->error_path);
216 atomic->error_path = razor_utf16_to_utf8(path, -1);
218 atomic->error_path = NULL;
220 atomic->error_str = strdup(str);
224 razor_atomic_set_error(struct razor_atomic *atomic, const wchar_t *path,
229 assert(!atomic->error_str);
231 free(atomic->error_path);
234 atomic->error_path = razor_utf16_to_utf8(path, -1);
236 atomic->error_path = NULL;
238 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
239 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
240 NULL, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
241 (LPWSTR)&buf, 0, NULL);
242 atomic->error_str = razor_utf16_to_utf8(buf, -1);
247 razor_atomic_commit(struct razor_atomic *atomic)
251 if (atomic->error_str)
254 retval = !CommitTransaction(atomic->transaction);
257 razor_atomic_set_error(atomic, NULL, GetLastError());
258 RollbackTransaction(atomic->transaction);
261 CloseHandle(atomic->transaction);
262 atomic->transaction = INVALID_HANDLE_VALUE;
268 razor_atomic_destroy(struct razor_atomic *atomic)
272 for(i = 0; i < atomic->n_files; i++) {
273 if (atomic->files[i].h != INVALID_HANDLE_VALUE) {
274 CloseHandle(atomic->files[i].h);
275 free(atomic->files[i].path);
279 if (atomic->transaction != INVALID_HANDLE_VALUE) {
280 RollbackTransaction(atomic->transaction);
281 CloseHandle(atomic->transaction);
283 free(atomic->error_path);
284 free(atomic->error_str);
285 free(atomic->error_msg);
290 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
293 struct razor_wstr *buffer;
294 const char *slash, *s, *next;
295 WIN32_FILE_ATTRIBUTE_DATA fa;
299 if (atomic->error_str)
302 buffer = razor_wstr_create(root, -1);
305 for (; *slash != '\0'; slash = next) {
306 next = strpbrk(slash + 1, "/\\");
310 razor_wstr_append(buffer, slash, next - slash);
313 if (razor_valid_root_name(buffer->str))
316 r = GetFileAttributesTransactedW(buffer->str,
317 GetFileExInfoStandard,
319 atomic->transaction);
322 err = GetLastError();
323 if (err == ERROR_FILE_NOT_FOUND) {
326 razor_atomic_set_error(atomic,
329 razor_wstr_destroy(buffer);
332 } else if (!(fa.dwFileAttributes&
333 FILE_ATTRIBUTE_DIRECTORY)) {
334 razor_atomic_set_error_str(atomic, buffer->str,
336 razor_wstr_destroy(buffer);
341 if (!CreateDirectoryTransactedW(NULL, buffer->str, NULL,
342 atomic->transaction)) {
343 razor_atomic_set_error(atomic, buffer->str,
345 razor_wstr_destroy(buffer);
349 /* FIXME: What to do about permissions for dirs we
350 * have to create but are not in the cpio archive? */
354 razor_wstr_destroy(buffer);
360 razor_atomic_remove(struct razor_atomic *atomic, const char *path)
365 if (atomic->error_str)
368 buf = razor_utf8_to_utf16(path, -1);
370 if (DeleteFileTransactedW(buf, atomic->transaction)) {
375 err = GetLastError();
376 if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) {
381 if (SetFileAttributesTransactedW(buf, FILE_ATTRIBUTE_NORMAL,
382 atomic->transaction)) {
383 if (DeleteFileTransactedW(buf, atomic->transaction)) {
387 err = GetLastError();
390 if (RemoveDirectoryTransactedW(buf, atomic->transaction) ||
391 GetLastError() == ERROR_DIR_NOT_EMPTY) {
397 * It would be tempting to use:
398 * MoveFileEx(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)
399 * but unless we can guarantee that the system will be rebooted
400 * before we (or some other application) write another file with the
401 * same path, this is likely to cause more problems than it solves.
404 razor_atomic_set_error(atomic, buf, err);
410 razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
413 wchar_t *oldbuf, *newbuf;
414 const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
416 if (atomic->error_str)
419 newbuf = razor_utf8_to_utf16(newpath, -1);
420 oldbuf = razor_utf8_to_utf16(oldpath, -1);
423 * Passing MOVEFILE_REPLACE_EXISTING to MoveFileTransaction() will
424 * cover every case we care about _except_ replacing an empty
425 * directory with a file. Calling RemoveDirectoryTransacted() will deal
426 * with this case while having no effect in all other cases.
428 (void)RemoveDirectoryTransactedW(newbuf, atomic->transaction);
430 if (!MoveFileTransactedW(oldbuf, newbuf, NULL, NULL, flags,
431 atomic->transaction))
432 razor_atomic_set_error(atomic, newbuf, GetLastError());
437 return !!atomic->error_str;
441 razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
446 WIN32_FILE_ATTRIBUTE_DATA fa;
448 if (atomic->error_str)
451 buf = razor_utf8_to_utf16(dirname, -1);
453 if (!CreateDirectoryTransactedW(NULL, buf, NULL, atomic->transaction)) {
454 err = GetLastError();
455 if (err != ERROR_FILE_EXISTS && err != ERROR_ALREADY_EXISTS) {
457 razor_atomic_set_error(atomic, buf, err);
462 if (!GetFileAttributesTransactedW(buf, GetFileExInfoStandard,
463 &fa, atomic->transaction))
466 if (!(fa.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) {
467 if (razor_atomic_remove(atomic, dirname)) {
471 if (!CreateDirectoryTransactedW(NULL, buf, NULL,
472 atomic->transaction)) {
473 err = GetLastError();
485 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
488 if (atomic->error_str)
492 * This isn't true, but symbolic links under Windows 7
493 * need to know whether the target is a directory or not
494 * and we don't always know that at the time when the
495 * link is created, so it's a convienent lie for now.
497 razor_atomic_set_error_str(atomic, NULL, "Symbolic links not supported "
504 razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
508 struct razor_atomic_file *files;
509 int i = atomic->n_files;
511 if (atomic->error_str)
514 files = realloc(atomic->files,
515 (atomic->n_files+1) * sizeof(struct razor_atomic_file));
517 razor_atomic_set_error_str(atomic, NULL, "Not enough memory");
521 atomic->files = files;
523 files[i].path = razor_utf8_to_utf16(filename, -1);
526 * Passing CREATE_ALWAYS to CreateFileTransacted() will cover
527 * every case we care about _except_ replacing an empty directory
528 * with a file. Calling RemoveDirectoryTransacted() will deal
529 * with this case while having no effect in all other cases.
531 (void)RemoveDirectoryTransactedW(files[i].path, atomic->transaction);
534 attribs = FILE_ATTRIBUTE_NORMAL;
536 attribs = FILE_ATTRIBUTE_READONLY;
538 files[i].h = CreateFileTransactedW(files[i].path, GENERIC_WRITE,
539 0, NULL, CREATE_ALWAYS, attribs,
540 NULL, atomic->transaction, NULL,
543 if (files[i].h == INVALID_HANDLE_VALUE) {
544 razor_atomic_set_error(atomic, files[i].path, GetLastError());
554 razor_atomic_write(struct razor_atomic *atomic, int handle, const void *data,
559 if (atomic->error_str)
562 assert(handle < atomic->n_files);
563 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
566 if (!WriteFile(atomic->files[handle].h, data, size, &written,
568 razor_atomic_set_error(atomic,
569 atomic->files[handle].path,
572 (void)CloseHandle(atomic->files[handle].h);
573 free(atomic->files[handle].path);
574 atomic->files[handle].path = NULL;
575 atomic->files[handle].h = INVALID_HANDLE_VALUE;
588 razor_atomic_sync(struct razor_atomic *atomic, int handle)
592 if (atomic->error_str)
595 assert(handle < atomic->n_files);
596 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
598 if (!CloseHandle(atomic->files[handle].h)) {
599 razor_atomic_set_error(atomic, 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_atomic_set_error(atomic, atomic->files[handle].path,
615 free(atomic->files[handle].path);
616 atomic->files[handle].path = NULL;
620 return !!atomic->error_str;
624 razor_atomic_close(struct razor_atomic *atomic, int handle)
626 if (atomic->error_str)
629 assert(handle < atomic->n_files);
630 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
632 if (!CloseHandle(atomic->files[handle].h))
633 razor_atomic_set_error(atomic, atomic->files[handle].path,
636 free(atomic->files[handle].path);
637 atomic->files[handle].path = NULL;
638 atomic->files[handle].h = INVALID_HANDLE_VALUE;
640 while(atomic->n_files > 0 &&
641 atomic->files[atomic->n_files-1].h == INVALID_HANDLE_VALUE)
644 return !!atomic->error_str;
647 #else /* HAVE_WINDOWS_KVM */
650 razor_valid_root_name(const char *name)
652 if (allow_all_root_names) {
654 return !strpbrk(name, "/\\");
656 return !strchr(name, '/');
661 return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
664 return name[0] == '\0';
668 struct razor_atomic {
674 RAZOR_EXPORT struct razor_atomic *
675 razor_atomic_open(const char *description)
677 struct razor_atomic *atomic;
679 atomic = zalloc(sizeof *atomic);
685 razor_atomic_set_error_str(struct razor_atomic *atomic, const char *path,
688 assert(!atomic->error_str);
690 atomic->error_path = path ? strdup(path) : NULL;
691 atomic->error_str = strdup(str);
696 razor_atomic_set_error_mswin(struct razor_atomic *atomic, const wchar_t *path,
701 assert(!atomic->error_str);
703 free(atomic->error_path);
706 atomic->error_path = razor_utf16_to_utf8(path, -1);
708 atomic->error_path = NULL;
710 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
711 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
712 NULL, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
713 (LPWSTR)&buf, 0, NULL);
714 atomic->error_str = razor_utf16_to_utf8(buf, -1);
720 razor_atomic_commit(struct razor_atomic *atomic)
722 return !!atomic->error_str;
726 razor_atomic_destroy(struct razor_atomic *atomic)
728 free(atomic->error_path);
729 free(atomic->error_str);
730 free(atomic->error_msg);
735 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
738 char buffer[PATH_MAX], *p;
739 const char *slash, *next;
742 if (atomic->error_str)
745 strcpy(buffer, root);
746 p = buffer + strlen(buffer);
748 for (slash = path; *slash != '\0'; slash = next) {
750 next = strpbrk(slash + 1, "/\\");
752 next = strchr(slash + 1, '/');
757 memcpy(p, slash, next - slash);
761 if (razor_valid_root_name(buffer))
764 if (stat(buffer, &buf) == 0) {
765 if (!S_ISDIR(buf.st_mode)) {
766 razor_atomic_set_error_str(atomic, buffer,
770 } else if (mkdir(buffer, 0777) < 0) {
771 razor_atomic_set_error_str(atomic, buffer,
781 razor_atomic_remove(struct razor_atomic *atomic, const char *path)
788 if (atomic->error_str)
792 buf = razor_utf8_to_utf16(path, -1);
794 if (!DeleteFileW(buf)) {
795 err = GetLastError();
796 if (err != ERROR_FILE_NOT_FOUND &&
797 err != ERROR_PATH_NOT_FOUND &&
798 !(SetFileAttributesW(buf, FILE_ATTRIBUTE_NORMAL) &&
800 !RemoveDirectoryW(buf) &&
801 GetLastError() != ERROR_DIR_NOT_EMPTY)
802 razor_atomic_set_error_mswin(atomic, buf, err);
808 razor_atomic_set_error_str(atomic, path, strerror(errno));
811 return !!atomic->error_str;
815 razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
819 wchar_t *oldbuf, *newbuf;
820 const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
823 if (atomic->error_str)
827 newbuf = razor_utf8_to_utf16(newpath, -1);
828 oldbuf = razor_utf8_to_utf16(oldpath, -1);
831 * Passing MOVEFILE_REPLACE_EXISTING to MoveFileEx() will
832 * cover every case we care about _except_ replacing an empty
833 * directory with a file. Calling RemoveDirectory() will deal
834 * with this case while having no effect in all other cases.
836 (void)RemoveDirectoryW(newbuf);
838 if (!MoveFileExW(oldbuf, newbuf, flags))
839 razor_atomic_set_error_mswin(atomic, newbuf, GetLastError());
844 if (rename(oldpath, newpath))
845 razor_atomic_set_error_str(atomic, newpath, strerror(errno));
848 return !!atomic->error_str;
852 razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
855 if (atomic->error_str)
858 if (!mkdir(dirname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)))
861 if (errno != EEXIST) {
862 razor_atomic_set_error_str(atomic, dirname, strerror(errno));
866 if (chmod(dirname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
867 razor_atomic_set_error_str(atomic, dirname, strerror(errno));
875 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
878 if (atomic->error_str)
882 if (symlink(target, path) < 0) {
883 razor_atomic_set_error_str(atomic, NULL, strerror(errno));
887 razor_atomic_set_error_str(atomic, NULL, "Symbolic links not supported "
895 razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
900 if (atomic->error_str)
903 atomic->error_path = strdup(filename);
904 fd = open(atomic->error_path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
905 mode & (S_IRWXU | S_IRWXG | S_IRWXO));
908 razor_atomic_set_error_str(atomic, NULL, strerror(errno));
914 razor_atomic_write(struct razor_atomic *atomic, int fd, const void *data,
919 if (atomic->error_str)
923 written = write(fd, data, size);
925 razor_atomic_set_error_str(atomic, NULL, strerror(errno));
940 razor_atomic_sync(struct razor_atomic *atomic, int handle)
942 if (atomic->error_str)
945 if (fsync(handle) < 0) {
946 razor_atomic_set_error_str(atomic, NULL, strerror(errno));
950 free(atomic->error_path);
951 atomic->error_path = NULL;
957 razor_atomic_close(struct razor_atomic *atomic, int fd)
959 if (atomic->error_str)
963 razor_atomic_set_error_str(atomic, NULL, strerror(errno));
967 free(atomic->error_path);
968 atomic->error_path = NULL;
973 #endif /* HAVE_WINDOWS_KVM */
975 RAZOR_EXPORT const char *
976 razor_atomic_get_error_msg(struct razor_atomic *atomic)
978 if (!atomic->error_msg) {
979 if (atomic->error_path)
980 atomic->error_msg = razor_concat(atomic->error_path,
985 atomic->error_msg = strdup(atomic->error_str);
988 return atomic->error_msg;
992 razor_atomic_abort(struct razor_atomic *atomic, const char *error_msg)
994 if (!atomic->error_str)
995 razor_atomic_set_error_str(atomic, NULL, error_msg);
999 razor_atomic_in_error_state(struct razor_atomic *atomic)
1001 return !!atomic->error_str;