/* * Copyright (C) 2012, 2014, 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 * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "config.h" #if ENABLE_ATOMIC && !HAVE_WINDOWS_KTM #include #include #include #include #include #include "razor-internal.h" /* * Emulated atomic support * * This implementation is better than nothing, but is certainly not atomic. * It does have a couple of advantages over atomic-none: * - If a file operation fails while a package is being installed we * have a good chance of being able to rollback the transaction to * a well-known state. * - We behave similarly to atomic-ktm in that changes are not visible * on disk to non-atomic operations (eg., scripts) until the atomic * is committed. This makes the testsuite more likely to pick up * problems that would otherwise only be found when using razor on * an MS-Windows system which supports KTM. */ #ifndef O_BINARY #define O_BINARY 0 #endif static void recursive_remove(const char *directory) { void *dp; char *name, *buf; dp = razor_uri_opendir(directory, NULL); if (dp) { while((name = razor_uri_readdir(dp, NULL))) { buf = razor_concat(directory, "/", name, NULL); free(name); if (razor_uri_unlink(buf, NULL) < 0) recursive_remove(buf); free(buf); } razor_uri_closedir(dp, NULL); } rmdir(directory); } RAZOR_EXPORT struct razor_atomic *razor_atomic_open(const char *description) { struct razor_atomic *atomic; atomic = zalloc(sizeof *atomic); atomic->description = strdup(description); return atomic; } RAZOR_EXPORT int razor_atomic_commit(struct razor_atomic *atomic) { struct atomic_action *actions; if (razor_atomic_in_error_state(atomic)) return -1; if (atomic->actions) { actions = atomic_action_list_reverse(atomic->actions); atomic->actions = NULL; actions = atomic_action_do(atomic, actions); atomic_action_free(actions); } if (atomic->toplevel) { recursive_remove(atomic->toplevel); free(atomic->toplevel); atomic->toplevel = NULL; } return razor_atomic_in_error_state(atomic); } RAZOR_EXPORT void razor_atomic_destroy(struct razor_atomic *atomic) { if (atomic->actions) { atomic_action_free(atomic->actions); atomic->actions = NULL; } if (atomic->toplevel) { recursive_remove(atomic->toplevel); free(atomic->toplevel); atomic->toplevel = NULL; } if (atomic->error) razor_error_free(atomic->error); free(atomic->description); free(atomic); } /* * We need a toplevel directory in which to hold temporary files * before they are committed. Since we can generally assume that * we have write permissions anywhere on the filesystem in * question, the best location is at the relevant mount point. * The most common case where this assumption fails is when * testing, when the current directory is a good choice. */ static int razor_atomic_set_toplevel_from_uri(struct razor_atomic *atomic, const char *uri) { if (razor_atomic_in_error_state(atomic)) return -1; if (atomic->toplevel) return 0; #ifdef MSWIN_API atomic->toplevel = razor_uri_mkdtemp_near(uri, "atomic-XXXXXX", &atomic->error); #else atomic->toplevel = razor_uri_mkdtemp_near(uri, ".atomic-XXXXXX", &atomic->error); #endif return !atomic->toplevel; } /* * If root_uri is empty, then uri can be a URI (but not a relative-ref; ie., * a scheme must be specified). If root_uri is non-empty, then it must be a * URI (but not a relative-ref) and uri must be a non-empty path. */ RAZOR_EXPORT int razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root_uri, const char *uri) { struct atomic_action *a; struct razor_uri ru; struct razor_error *tmp_error = NULL; char *fakeroot = NULL; razor_atomic_set_toplevel_from_uri(atomic, *root_uri ? root_uri : uri); if (razor_atomic_in_error_state(atomic)) return -1; if (!*root_uri) { if (razor_uri_parse(&ru, uri, &tmp_error)) { razor_atomic_propagate_error(atomic, tmp_error, NULL); return -1; } if (*ru.path == '/') { free(ru.path); ru.path = strdup("/"); } else { free(ru.path); ru.path = strdup(""); } fakeroot = razor_uri_recompose(&ru); razor_uri_destroy(&ru); root_uri = fakeroot; uri += strlen(fakeroot); } a = atomic_action_new(ACTION_MAKE_DIRS); a->args.uri = strdup(uri); a->args.u.make_dirs.root = strdup(root_uri); atomic->actions = atomic_action_list_prepend(atomic->actions, a); if (fakeroot) free(fakeroot); return 0; } RAZOR_EXPORT int razor_atomic_remove(struct razor_atomic *atomic, const char *uri) { struct atomic_action *a; razor_atomic_set_toplevel_from_uri(atomic, uri); if (razor_atomic_in_error_state(atomic)) return -1; a = atomic_action_new(ACTION_REMOVE); a->args.uri = strdup(uri); atomic->actions = atomic_action_list_prepend(atomic->actions, a); return 0; } RAZOR_EXPORT int razor_atomic_rename_file(struct razor_atomic *atomic, const char *old_uri, const char *new_uri) { struct atomic_action *a; razor_atomic_set_toplevel_from_uri(atomic, new_uri); if (razor_atomic_in_error_state(atomic)) return -1; a = atomic_action_new(ACTION_MOVE); a->args.uri = strdup(old_uri); a->args.u.move.dest = strdup(new_uri); atomic->actions = atomic_action_list_prepend(atomic->actions, a); return 0; } RAZOR_EXPORT int razor_atomic_create_dir(struct razor_atomic *atomic, const char *uri, mode_t mode) { struct atomic_action *a; razor_atomic_set_toplevel_from_uri(atomic, uri); if (razor_atomic_in_error_state(atomic)) return -1; a = atomic_action_new(ACTION_CREATE_DIR); a->args.uri = strdup(uri); a->args.u.create_dir.mode = mode; atomic->actions = atomic_action_list_prepend(atomic->actions, a); return 0; } RAZOR_EXPORT int razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target, const char *uri) { #if HAVE_SYMLINK struct atomic_action *a; razor_atomic_set_toplevel_from_uri(atomic, uri); #endif if (razor_atomic_in_error_state(atomic)) return -1; #if HAVE_SYMLINK a = atomic_action_new(ACTION_CREATE_SYMLINK); a->args.uri = strdup(uri); a->args.u.create_symlink.target = strdup(target); atomic->actions = atomic_action_list_prepend(atomic->actions, a); return 0; #else atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, ENOSYS, NULL, "Symbolic links not supported " "on this platform"); return -1; #endif } RAZOR_EXPORT int razor_atomic_create_file(struct razor_atomic *atomic, const char *uri, mode_t mode) { int fd; struct atomic_action *a; char *tmpnam; razor_atomic_set_toplevel_from_uri(atomic, uri); if (razor_atomic_in_error_state(atomic)) return -1; tmpnam = atomic_action_attic_tmpnam(atomic); fd = razor_uri_open(tmpnam, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode & (S_IRWXU | S_IRWXG | S_IRWXO), &atomic->error); if (fd >= 0) { a = atomic_action_new(ACTION_MOVE); a->args.uri = tmpnam; a->args.u.move.dest = strdup(uri); atomic->actions = atomic_action_list_prepend(atomic->actions, a); } return fd; } #endif /* ENABLE_ATOMIC && !HAVE_WINDOWS_KTM */