diff -r a3e5e3eaf224 -r c3722fff46c7 librazor/atomic-actions.c --- a/librazor/atomic-actions.c Thu Jun 09 17:37:09 2016 +0100 +++ b/librazor/atomic-actions.c Fri Jul 08 15:54:09 2016 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012 J. Ali Harlow + * Copyright (C) 2012, 2016 J. Ali Harlow * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,9 +25,6 @@ #include #include #include -#include -#include -#include #include #include "razor-internal.h" @@ -87,7 +84,7 @@ while(action) { a = atomic_action_list_pop_head(&action); - free(a->args.path); + free(a->args.uri); switch(a->type) { case ACTION_MAKE_DIRS: @@ -166,13 +163,11 @@ strcpy(buffer, action->args.u.make_dirs.root); p = buffer + strlen(buffer); - slash = action->args.path; + slash = action->args.uri; + if (p > buffer && p[-1] != ':' && p[-1] != '/' && *slash != '/') + *p++ = '/'; for (; *slash != '\0'; slash = next) { -#ifdef MSWIN_API - next = strpbrk(slash + 1, "/\\"); -#else next = strchr(slash + 1, '/'); -#endif if (next == NULL) break; @@ -184,7 +179,7 @@ continue; prim = atomic_action_new(ACTION_CREATE_DIR); - prim->args.path = strdup(buffer); + prim->args.uri = strdup(buffer); prim->args.u.create_dir.mode = S_IRWXU | S_IRWXG | S_IRWXO; primitives = atomic_action_list_prepend(primitives, prim); } @@ -198,12 +193,8 @@ static struct atomic_action * atomic_action_remove(struct razor_atomic *atomic, struct atomic_action *action) { -#ifdef MSWIN_API - wchar_t *path; - _WDIR *dir; -#else - DIR *dir; -#endif + void *dir; + char *name; struct atomic_action *prim; if (razor_atomic_in_error_state(atomic)) { @@ -214,27 +205,20 @@ /* * Non-empty directories should NOT be removed */ -#ifdef MSWIN_API - path = razor_utf8_to_utf16(action->args.path, -1); - - dir = _wopendir(path); - if (dir && _wreaddir(dir)) { - atomic_action_free(action); - action = NULL; + dir = razor_uri_opendir(action->args.uri, NULL); + if (dir) { + name = razor_uri_readdir(dir, NULL); + razor_uri_closedir(dir, NULL); + if (name) { + free(name); + atomic_action_free(action); + action = NULL; + } } - _wclosedir(dir); -#else - dir = opendir(action->args.path); - if (dir && readdir(dir)) { - atomic_action_free(action); - action = NULL; - } - closedir(dir); -#endif if (action) { prim = atomic_action_new(ACTION_MOVE); - prim->args.path = strdup(action->args.path); + prim->args.uri = strdup(action->args.uri); prim->args.u.move.dest = atomic_action_attic_tmpnam(atomic); atomic_action_free(action); @@ -249,7 +233,7 @@ struct atomic_action *action) { mode_t mode; - struct stat buf; + struct razor_error **error; if (razor_atomic_in_error_state(atomic)) { atomic_action_free(action); @@ -258,21 +242,14 @@ mode = action->args.u.create_dir.mode & (S_IRWXU | S_IRWXG | S_IRWXO); - if (!mkdir(action->args.path, mode)) + if (atomic->error) + error = NULL; + else + error = &atomic->error; + + if (!razor_uri_mkdir(action->args.uri, mode, error)) return action; - if (errno != EEXIST || stat(action->args.path, &buf)) { - if (!atomic->error) - atomic->error = razor_error_new_posix(action->args.path); - atomic_action_free(action); - return NULL; - } - - if (!S_ISDIR(buf.st_mode) && !atomic->error) - atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, EEXIST, - action->args.path, - "Not a directory"); - atomic_action_free(action); return NULL; } @@ -285,9 +262,9 @@ return NULL; } - if (rmdir(action->args.path) < 0) { + if (rmdir(action->args.uri) < 0) { if (!atomic->error) - atomic->error = razor_error_new_posix(action->args.path); + atomic->error = razor_error_new_posix(action->args.uri); atomic_action_free(action); return NULL; } else @@ -306,10 +283,10 @@ return NULL; } - r = symlink(action->args.u.create_symlink.target, action->args.path); + r = symlink(action->args.u.create_symlink.target, action->args.uri); if (r < 0) { if (!atomic->error) - atomic->error = razor_error_new_posix(action->args.path); + atomic->error = razor_error_new_posix(action->args.uri); atomic_action_free(action); return NULL; } @@ -321,14 +298,19 @@ atomic_action_remove_symlink(struct razor_atomic *atomic, struct atomic_action *action) { + struct razor_error **error; + if (razor_atomic_in_error_state(atomic)) { atomic_action_free(action); return NULL; } - if (unlink(action->args.path) < 0) { - if (!atomic->error) - atomic->error = razor_error_new_posix(action->args.path); + if (atomic->error) + error = NULL; + else + error = &atomic->error; + + if (razor_uri_unlink(action->args.uri, error)) { atomic_action_free(action); return NULL; } @@ -338,52 +320,16 @@ #endif static int -move_file(struct razor_atomic *atomic, const char *path, const char *dest) +move_file(struct razor_atomic *atomic, const char *uri, const char *dest) { -#ifdef MSWIN_API - wchar_t *oldbuf, *newbuf; - const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING; + struct razor_error **error; - newbuf = razor_utf8_to_utf16(dest, -1); - oldbuf = razor_utf8_to_utf16(path, -1); + if (atomic->error) + error = NULL; + else + error = &atomic->error; - /* - * Passing MOVEFILE_REPLACE_EXISTING to MoveFileEx() will - * cover every case we care about _except_ replacing an empty - * directory with a file. Calling RemoveDirectory() will deal - * with this case while having no effect in all other cases. - */ - (void)RemoveDirectoryW(newbuf); - - if (!MoveFileExW(oldbuf, newbuf, flags)) { - if (!atomic->error) - atomic->error = razor_error_new_mswin(newbuf, - GetLastError()); - return -1; - } - - free(newbuf); - free(oldbuf); -#else - int code; - const char *object; - - if (rename(path, dest)) { - if (!atomic->error) { - code = errno; - if (access(path, F_OK) < 0) - object = path; - else - object = dest; - atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, - code, object, - strerror(code)); - } - return -1; - } -#endif - - return 0; + return razor_uri_move(uri, dest, error); } static struct atomic_action * @@ -394,7 +340,7 @@ return NULL; } - if (move_file(atomic, action->args.path, action->args.u.move.dest)) { + if (move_file(atomic, action->args.uri, action->args.u.move.dest)) { atomic_action_free(action); return NULL; } @@ -410,7 +356,7 @@ return NULL; } - if (move_file(atomic, action->args.u.move.dest, action->args.path)) { + if (move_file(atomic, action->args.u.move.dest, action->args.uri)) { atomic_action_free(action); return NULL; }