Add an error object.
This is intended to dis-entangle the two roles that the atomic
object has evolved into so that atomic need only be used where
atomic actions are actually being undertaken.
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_commit(struct razor_atomic *atomic)
139 if (razor_atomic_in_error_state(atomic))
142 retval = !CommitTransaction(atomic->transaction);
145 atomic->error = razor_error_new_mswin(NULL, GetLastError());
146 RollbackTransaction(atomic->transaction);
149 CloseHandle(atomic->transaction);
150 atomic->transaction = INVALID_HANDLE_VALUE;
156 razor_atomic_destroy(struct razor_atomic *atomic)
160 for(i = 0; i < atomic->n_files; i++) {
161 if (atomic->files[i].h != INVALID_HANDLE_VALUE) {
162 CloseHandle(atomic->files[i].h);
163 free(atomic->files[i].path);
167 if (atomic->transaction != INVALID_HANDLE_VALUE) {
168 RollbackTransaction(atomic->transaction);
169 CloseHandle(atomic->transaction);
172 razor_error_free(atomic->error);
177 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
180 struct razor_wstr *buffer;
181 const char *slash, *next;
182 WIN32_FILE_ATTRIBUTE_DATA fa;
186 if (razor_atomic_in_error_state(atomic))
189 buffer = razor_wstr_create(root, -1);
192 for (; *slash != '\0'; slash = next) {
193 next = strpbrk(slash + 1, "/\\");
197 razor_wstr_append(buffer, slash, next - slash);
200 if (razor_valid_root_name2(buffer->str))
203 r = GetFileAttributesTransactedW(buffer->str,
204 GetFileExInfoStandard,
206 atomic->transaction);
209 err = GetLastError();
210 if (err == ERROR_FILE_NOT_FOUND) {
213 atomic->error = razor_error_new_mswin(buffer->str,
215 razor_wstr_destroy(buffer);
218 } else if (!(fa.dwFileAttributes&
219 FILE_ATTRIBUTE_DIRECTORY)) {
220 atomic->error = razor_error_new_str(buffer->str,
222 razor_wstr_destroy(buffer);
227 if (!CreateDirectoryTransactedW(NULL, buffer->str, NULL,
228 atomic->transaction)) {
229 atomic->error = razor_error_new_mswin(buffer->str,
231 razor_wstr_destroy(buffer);
235 /* FIXME: What to do about permissions for dirs we
236 * have to create but are not in the cpio archive? */
240 razor_wstr_destroy(buffer);
246 razor_atomic_remove(struct razor_atomic *atomic, const char *path)
251 if (razor_atomic_in_error_state(atomic))
254 buf = razor_utf8_to_utf16(path, -1);
256 if (DeleteFileTransactedW(buf, atomic->transaction)) {
261 err = GetLastError();
262 if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) {
267 if (SetFileAttributesTransactedW(buf, FILE_ATTRIBUTE_NORMAL,
268 atomic->transaction)) {
269 if (DeleteFileTransactedW(buf, atomic->transaction)) {
273 err = GetLastError();
276 if (RemoveDirectoryTransactedW(buf, atomic->transaction) ||
277 GetLastError() == ERROR_DIR_NOT_EMPTY) {
283 * It would be tempting to use:
284 * MoveFileEx(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)
285 * but unless we can guarantee that the system will be rebooted
286 * before we (or some other application) write another file with the
287 * same path, this is likely to cause more problems than it solves.
290 atomic->error = razor_error_new_mswin(buf, err);
296 razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
299 wchar_t *oldbuf, *newbuf;
300 const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
302 if (razor_atomic_in_error_state(atomic))
305 newbuf = razor_utf8_to_utf16(newpath, -1);
306 oldbuf = razor_utf8_to_utf16(oldpath, -1);
309 * Passing MOVEFILE_REPLACE_EXISTING to MoveFileTransaction() will
310 * cover every case we care about _except_ replacing an empty
311 * directory with a file. Calling RemoveDirectoryTransacted() will deal
312 * with this case while having no effect in all other cases.
314 (void)RemoveDirectoryTransactedW(newbuf, atomic->transaction);
316 if (!MoveFileTransactedW(oldbuf, newbuf, NULL, NULL, flags,
317 atomic->transaction))
318 atomic->error = razor_error_new_mswin(newbuf, GetLastError());
323 return razor_atomic_in_error_state(atomic);
327 razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
332 WIN32_FILE_ATTRIBUTE_DATA fa;
334 if (razor_atomic_in_error_state(atomic))
337 buf = razor_utf8_to_utf16(dirname, -1);
339 if (!CreateDirectoryTransactedW(NULL, buf, NULL, atomic->transaction)) {
340 err = GetLastError();
341 if (err != ERROR_FILE_EXISTS && err != ERROR_ALREADY_EXISTS) {
343 atomic->error = razor_error_new_mswin(buf, err);
348 if (!GetFileAttributesTransactedW(buf, GetFileExInfoStandard,
349 &fa, atomic->transaction))
352 if (!(fa.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) {
353 if (razor_atomic_remove(atomic, dirname)) {
357 if (!CreateDirectoryTransactedW(NULL, buf, NULL,
358 atomic->transaction)) {
359 err = GetLastError();
371 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
374 if (razor_atomic_in_error_state(atomic))
378 * This isn't true, but symbolic links under Windows 7
379 * need to know whether the target is a directory or not
380 * and we don't always know that at the time when the
381 * link is created, so it's a convienent lie for now.
383 atomic->error = razor_error_new_str(NULL,
384 "Symbolic links not supported "
391 razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
395 struct razor_atomic_file *files;
396 int i = atomic->n_files;
398 if (razor_atomic_in_error_state(atomic))
401 files = realloc(atomic->files,
402 (atomic->n_files+1) * sizeof(struct razor_atomic_file));
404 atomic->error = razor_error_new_str(NULL, "Not enough memory");
408 atomic->files = files;
410 files[i].path = razor_utf8_to_utf16(filename, -1);
413 * Passing CREATE_ALWAYS to CreateFileTransacted() will cover
414 * every case we care about _except_ replacing an empty directory
415 * with a file. Calling RemoveDirectoryTransacted() will deal
416 * with this case while having no effect in all other cases.
418 (void)RemoveDirectoryTransactedW(files[i].path, atomic->transaction);
421 attribs = FILE_ATTRIBUTE_NORMAL;
423 attribs = FILE_ATTRIBUTE_READONLY;
425 files[i].h = CreateFileTransactedW(files[i].path, GENERIC_WRITE,
426 0, NULL, CREATE_ALWAYS, attribs,
427 NULL, atomic->transaction, NULL,
430 if (files[i].h == INVALID_HANDLE_VALUE) {
431 atomic->error = razor_error_new_mswin(files[i].path,
442 razor_atomic_write(struct razor_atomic *atomic, int handle, const void *data,
447 if (razor_atomic_in_error_state(atomic))
450 assert(handle < atomic->n_files);
451 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
454 if (!WriteFile(atomic->files[handle].h, data, size, &written,
456 atomic->error = razor_error_new_mswin(atomic->files[handle].path,
459 (void)CloseHandle(atomic->files[handle].h);
460 free(atomic->files[handle].path);
461 atomic->files[handle].path = NULL;
462 atomic->files[handle].h = INVALID_HANDLE_VALUE;
475 razor_atomic_sync(struct razor_atomic *atomic, int handle)
479 if (razor_atomic_in_error_state(atomic))
482 assert(handle < atomic->n_files);
483 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
485 if (!CloseHandle(atomic->files[handle].h)) {
486 atomic->error = razor_error_new_mswin(atomic->files[handle].path,
488 free(atomic->files[handle].path);
489 atomic->files[handle].path = NULL;
490 atomic->files[handle].h = INVALID_HANDLE_VALUE;
494 h = CreateFileTransactedW(atomic->files[handle].path, GENERIC_WRITE, 0,
495 NULL, OPEN_EXISTING, 0, NULL,
496 atomic->transaction, NULL, NULL);
497 atomic->files[handle].h = h;
499 if (atomic->files[handle].h == INVALID_HANDLE_VALUE) {
500 atomic->error = razor_error_new_mswin(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 atomic->error = razor_error_new_mswin(atomic->files[handle].path,
523 free(atomic->files[handle].path);
524 atomic->files[handle].path = NULL;
525 atomic->files[handle].h = INVALID_HANDLE_VALUE;
527 while(atomic->n_files > 0 &&
528 atomic->files[atomic->n_files-1].h == INVALID_HANDLE_VALUE)
531 return razor_atomic_in_error_state(atomic);
534 #endif /* HAVE_WINDOWS_KTM */