2 * Copyright (C) 2011-2012 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"
39 #define RAZOR_ASCII_ISALPHA(c) \
40 ((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
43 razor_valid_root_name2(const wchar_t *name)
45 if (razor_allow_all_root_names())
46 return !wcschr(name, '/');
48 return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
57 static struct razor_wstr *
58 razor_wstr_create(const char *init, int len)
61 struct razor_wstr *wstr;
63 wstr = malloc(sizeof(struct razor_wstr));
65 n = MultiByteToWideChar(CP_UTF8, 0, init, len, NULL, 0);
66 if (len >= 0 && init[len])
71 wstr->allocated = n * 2;
72 wstr->str = malloc(wstr->allocated * sizeof(wchar_t));
78 (void)MultiByteToWideChar(CP_UTF8, 0, init, len, wstr->str, n);
79 if (len >= 0 && init[len])
80 wstr->str[wstr->len] = 0;
86 razor_wstr_append(struct razor_wstr *wstr, const char *s, int len)
91 n = MultiByteToWideChar(CP_UTF8, 0, s, len, NULL, 0);
92 if (len < 0 || !s[len])
95 if (wstr->allocated <= wstr->len + n) {
96 allocated = (wstr->len + n + 1) * 2;
97 str = realloc(wstr->str, allocated * sizeof(wchar_t));
100 wstr->allocated = allocated;
104 (void)MultiByteToWideChar(CP_UTF8, 0, s, len, wstr->str + wstr->len, n);
106 wstr->str[wstr->len] = 0;
112 razor_wstr_destroy(struct razor_wstr *wstr)
118 RAZOR_EXPORT struct razor_atomic *
119 razor_atomic_open(const char *description)
122 struct razor_atomic *atomic;
124 atomic = zalloc(sizeof *atomic);
125 buf = razor_utf8_to_utf16(description, -1);
126 atomic->transaction = CreateTransaction(NULL, 0,
127 TRANSACTION_DO_NOT_PROMOTE,
135 razor_atomic_set_error_str(struct razor_atomic *atomic, const wchar_t *path,
138 assert(!atomic->error_str);
140 free(atomic->error_path);
143 atomic->error_path = razor_utf16_to_utf8(path, -1);
145 atomic->error_path = NULL;
147 atomic->error_str = strdup(str);
151 razor_atomic_set_error(struct razor_atomic *atomic, const wchar_t *path,
156 assert(!atomic->error_str);
158 free(atomic->error_path);
161 atomic->error_path = razor_utf16_to_utf8(path, -1);
163 atomic->error_path = NULL;
165 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
166 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
167 NULL, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
168 (LPWSTR)&buf, 0, NULL);
169 atomic->error_str = razor_utf16_to_utf8(buf, -1);
174 razor_atomic_commit(struct razor_atomic *atomic)
178 if (razor_atomic_in_error_state(atomic))
181 retval = !CommitTransaction(atomic->transaction);
184 razor_atomic_set_error(atomic, NULL, GetLastError());
185 RollbackTransaction(atomic->transaction);
188 CloseHandle(atomic->transaction);
189 atomic->transaction = INVALID_HANDLE_VALUE;
195 razor_atomic_destroy(struct razor_atomic *atomic)
199 for(i = 0; i < atomic->n_files; i++) {
200 if (atomic->files[i].h != INVALID_HANDLE_VALUE) {
201 CloseHandle(atomic->files[i].h);
202 free(atomic->files[i].path);
206 if (atomic->transaction != INVALID_HANDLE_VALUE) {
207 RollbackTransaction(atomic->transaction);
208 CloseHandle(atomic->transaction);
210 free(atomic->error_path);
211 free(atomic->error_str);
212 free(atomic->error_msg);
217 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
220 struct razor_wstr *buffer;
221 const char *slash, *next;
222 WIN32_FILE_ATTRIBUTE_DATA fa;
226 if (razor_atomic_in_error_state(atomic))
229 buffer = razor_wstr_create(root, -1);
232 for (; *slash != '\0'; slash = next) {
233 next = strpbrk(slash + 1, "/\\");
237 razor_wstr_append(buffer, slash, next - slash);
240 if (razor_valid_root_name2(buffer->str))
243 r = GetFileAttributesTransactedW(buffer->str,
244 GetFileExInfoStandard,
246 atomic->transaction);
249 err = GetLastError();
250 if (err == ERROR_FILE_NOT_FOUND) {
253 razor_atomic_set_error(atomic,
256 razor_wstr_destroy(buffer);
259 } else if (!(fa.dwFileAttributes&
260 FILE_ATTRIBUTE_DIRECTORY)) {
261 razor_atomic_set_error_str(atomic, buffer->str,
263 razor_wstr_destroy(buffer);
268 if (!CreateDirectoryTransactedW(NULL, buffer->str, NULL,
269 atomic->transaction)) {
270 razor_atomic_set_error(atomic, buffer->str,
272 razor_wstr_destroy(buffer);
276 /* FIXME: What to do about permissions for dirs we
277 * have to create but are not in the cpio archive? */
281 razor_wstr_destroy(buffer);
287 razor_atomic_remove(struct razor_atomic *atomic, const char *path)
292 if (razor_atomic_in_error_state(atomic))
295 buf = razor_utf8_to_utf16(path, -1);
297 if (DeleteFileTransactedW(buf, atomic->transaction)) {
302 err = GetLastError();
303 if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) {
308 if (SetFileAttributesTransactedW(buf, FILE_ATTRIBUTE_NORMAL,
309 atomic->transaction)) {
310 if (DeleteFileTransactedW(buf, atomic->transaction)) {
314 err = GetLastError();
317 if (RemoveDirectoryTransactedW(buf, atomic->transaction) ||
318 GetLastError() == ERROR_DIR_NOT_EMPTY) {
324 * It would be tempting to use:
325 * MoveFileEx(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)
326 * but unless we can guarantee that the system will be rebooted
327 * before we (or some other application) write another file with the
328 * same path, this is likely to cause more problems than it solves.
331 razor_atomic_set_error(atomic, buf, err);
337 razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
340 wchar_t *oldbuf, *newbuf;
341 const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
343 if (razor_atomic_in_error_state(atomic))
346 newbuf = razor_utf8_to_utf16(newpath, -1);
347 oldbuf = razor_utf8_to_utf16(oldpath, -1);
350 * Passing MOVEFILE_REPLACE_EXISTING to MoveFileTransaction() will
351 * cover every case we care about _except_ replacing an empty
352 * directory with a file. Calling RemoveDirectoryTransacted() will deal
353 * with this case while having no effect in all other cases.
355 (void)RemoveDirectoryTransactedW(newbuf, atomic->transaction);
357 if (!MoveFileTransactedW(oldbuf, newbuf, NULL, NULL, flags,
358 atomic->transaction))
359 razor_atomic_set_error(atomic, newbuf, GetLastError());
364 return !!atomic->error_str;
368 razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
373 WIN32_FILE_ATTRIBUTE_DATA fa;
375 if (razor_atomic_in_error_state(atomic))
378 buf = razor_utf8_to_utf16(dirname, -1);
380 if (!CreateDirectoryTransactedW(NULL, buf, NULL, atomic->transaction)) {
381 err = GetLastError();
382 if (err != ERROR_FILE_EXISTS && err != ERROR_ALREADY_EXISTS) {
384 razor_atomic_set_error(atomic, buf, err);
389 if (!GetFileAttributesTransactedW(buf, GetFileExInfoStandard,
390 &fa, atomic->transaction))
393 if (!(fa.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) {
394 if (razor_atomic_remove(atomic, dirname)) {
398 if (!CreateDirectoryTransactedW(NULL, buf, NULL,
399 atomic->transaction)) {
400 err = GetLastError();
412 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
415 if (razor_atomic_in_error_state(atomic))
419 * This isn't true, but symbolic links under Windows 7
420 * need to know whether the target is a directory or not
421 * and we don't always know that at the time when the
422 * link is created, so it's a convienent lie for now.
424 razor_atomic_set_error_str(atomic, NULL, "Symbolic links not supported "
431 razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
435 struct razor_atomic_file *files;
436 int i = atomic->n_files;
438 if (razor_atomic_in_error_state(atomic))
441 files = realloc(atomic->files,
442 (atomic->n_files+1) * sizeof(struct razor_atomic_file));
444 razor_atomic_set_error_str(atomic, NULL, "Not enough memory");
448 atomic->files = files;
450 files[i].path = razor_utf8_to_utf16(filename, -1);
453 * Passing CREATE_ALWAYS to CreateFileTransacted() will cover
454 * every case we care about _except_ replacing an empty directory
455 * with a file. Calling RemoveDirectoryTransacted() will deal
456 * with this case while having no effect in all other cases.
458 (void)RemoveDirectoryTransactedW(files[i].path, atomic->transaction);
461 attribs = FILE_ATTRIBUTE_NORMAL;
463 attribs = FILE_ATTRIBUTE_READONLY;
465 files[i].h = CreateFileTransactedW(files[i].path, GENERIC_WRITE,
466 0, NULL, CREATE_ALWAYS, attribs,
467 NULL, atomic->transaction, NULL,
470 if (files[i].h == INVALID_HANDLE_VALUE) {
471 razor_atomic_set_error(atomic, files[i].path, GetLastError());
481 razor_atomic_write(struct razor_atomic *atomic, int handle, const void *data,
486 if (razor_atomic_in_error_state(atomic))
489 assert(handle < atomic->n_files);
490 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
493 if (!WriteFile(atomic->files[handle].h, data, size, &written,
495 razor_atomic_set_error(atomic,
496 atomic->files[handle].path,
499 (void)CloseHandle(atomic->files[handle].h);
500 free(atomic->files[handle].path);
501 atomic->files[handle].path = NULL;
502 atomic->files[handle].h = INVALID_HANDLE_VALUE;
515 razor_atomic_sync(struct razor_atomic *atomic, int handle)
519 if (razor_atomic_in_error_state(atomic))
522 assert(handle < atomic->n_files);
523 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
525 if (!CloseHandle(atomic->files[handle].h)) {
526 razor_atomic_set_error(atomic, atomic->files[handle].path,
528 free(atomic->files[handle].path);
529 atomic->files[handle].path = NULL;
530 atomic->files[handle].h = INVALID_HANDLE_VALUE;
534 h = CreateFileTransactedW(atomic->files[handle].path, GENERIC_WRITE, 0,
535 NULL, OPEN_EXISTING, 0, NULL,
536 atomic->transaction, NULL, NULL);
537 atomic->files[handle].h = h;
539 if (atomic->files[handle].h == INVALID_HANDLE_VALUE) {
540 razor_atomic_set_error(atomic, atomic->files[handle].path,
542 free(atomic->files[handle].path);
543 atomic->files[handle].path = NULL;
547 return !!atomic->error_str;
551 razor_atomic_close(struct razor_atomic *atomic, int handle)
553 if (razor_atomic_in_error_state(atomic))
556 assert(handle < atomic->n_files);
557 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
559 if (!CloseHandle(atomic->files[handle].h))
560 razor_atomic_set_error(atomic, atomic->files[handle].path,
563 free(atomic->files[handle].path);
564 atomic->files[handle].path = NULL;
565 atomic->files[handle].h = INVALID_HANDLE_VALUE;
567 while(atomic->n_files > 0 &&
568 atomic->files[atomic->n_files-1].h == INVALID_HANDLE_VALUE)
571 return !!atomic->error_str;
574 #endif /* HAVE_WINDOWS_KTM */