2 * Copyright (C) 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.
21 #if ENABLE_ATOMIC && !HAVE_WINDOWS_KTM
27 #include <sys/types.h>
32 #include "razor-internal.h"
35 * Emulated atomic support
37 * This implementation is better than nothing, but is certainly not atomic.
38 * It does have a couple of advantages over atomic-none:
39 * - If a file operation fails while a package is being installed we
40 * have a good chance of being able to rollback the transaction to
42 * - We behave similarly to atomic-ktm in that changes are not visible
43 * on disk to non-atomic operations (eg., scripts) until the atomic
44 * is committed. This makes the testsuite more likely to pick up
45 * problems that would otherwise only be found when using razor on
46 * an MS-Windows system which supports KTM.
53 static void recursive_remove(const char *directory)
59 dp = opendir(directory);
60 while((dirp = readdir(dp))) {
61 if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
62 buf = malloc(strlen(directory) + strlen(dirp->d_name)
64 sprintf(buf, "%s/%s", directory, dirp->d_name);
66 recursive_remove(buf);
74 RAZOR_EXPORT struct razor_atomic *razor_atomic_open(const char *description)
76 struct razor_atomic *atomic;
78 atomic = zalloc(sizeof *atomic);
80 atomic->toplevel = strdup(".atomic-XXXXXX");
81 if (!mkdtemp(atomic->toplevel)) {
82 free(atomic->toplevel);
87 atomic->description = strdup(description);
92 RAZOR_EXPORT int razor_atomic_commit(struct razor_atomic *atomic)
94 struct atomic_action *actions;
96 if (razor_atomic_in_error_state(atomic))
99 if (atomic->actions) {
100 actions = atomic_action_list_reverse(atomic->actions);
101 atomic->actions = NULL;
102 actions = atomic_action_do(atomic, actions);
103 atomic_action_free(actions);
106 if (atomic->toplevel) {
107 recursive_remove(atomic->toplevel);
108 free(atomic->toplevel);
109 atomic->toplevel = NULL;
112 return razor_atomic_in_error_state(atomic);
115 RAZOR_EXPORT void razor_atomic_destroy(struct razor_atomic *atomic)
117 if (atomic->toplevel) {
118 recursive_remove(atomic->toplevel);
119 free(atomic->toplevel);
120 atomic->toplevel = NULL;
124 razor_error_free(atomic->error);
130 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
133 struct atomic_action *a;
135 if (razor_atomic_in_error_state(atomic))
138 a = atomic_action_new(ACTION_MAKE_DIRS);
139 a->args.path = strdup(path);
140 a->args.u.make_dirs.root = strdup(root);
141 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
147 razor_atomic_remove(struct razor_atomic *atomic, const char *path)
149 struct atomic_action *a;
151 if (razor_atomic_in_error_state(atomic))
154 a = atomic_action_new(ACTION_REMOVE);
155 a->args.path = strdup(path);
156 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
162 razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
165 struct atomic_action *a;
167 if (razor_atomic_in_error_state(atomic))
170 a = atomic_action_new(ACTION_MOVE);
171 a->args.path = strdup(oldpath);
172 a->args.u.move.dest = strdup(newpath);
173 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
179 razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
182 struct atomic_action *a;
184 if (razor_atomic_in_error_state(atomic))
187 a = atomic_action_new(ACTION_CREATE_DIR);
188 a->args.path = strdup(dirname);
189 a->args.u.create_dir.mode = mode;
190 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
196 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
200 struct atomic_action *a;
203 if (razor_atomic_in_error_state(atomic))
207 a = atomic_action_new(ACTION_CREATE_SYMLINK);
208 a->args.path = strdup(path);
209 a->args.u.create_symlink.target = strdup(target);
210 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
214 atomic->error = razor_error_new_str(NULL,
215 "Symbolic links not supported "
223 razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
227 struct atomic_action *a;
230 if (razor_atomic_in_error_state(atomic))
233 tmpnam = atomic_action_attic_tmpnam(atomic);
234 fd = open(tmpnam, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
235 mode & (S_IRWXU | S_IRWXG | S_IRWXO));
238 atomic->error = razor_error_new_str(filename, strerror(errno));
240 a = atomic_action_new(ACTION_MOVE);
241 a->args.path = tmpnam;
242 a->args.u.move.dest = strdup(filename);
243 atomic->actions = atomic_action_list_prepend(atomic->actions,
250 #endif /* ENABLE_ATOMIC && !HAVE_WINDOWS_KTM */