Add a test for relocations that are valid paths but would be invalid URIs if mis-interpreted
2 * Copyright (C) 2012, 2014, 2016 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
28 #include "razor-internal.h"
31 * Emulated atomic support
33 * This implementation is better than nothing, but is certainly not atomic.
34 * It does have a couple of advantages over atomic-none:
35 * - If a file operation fails while a package is being installed we
36 * have a good chance of being able to rollback the transaction to
38 * - We behave similarly to atomic-ktm in that changes are not visible
39 * on disk to non-atomic operations (eg., scripts) until the atomic
40 * is committed. This makes the testsuite more likely to pick up
41 * problems that would otherwise only be found when using razor on
42 * an MS-Windows system which supports KTM.
49 static void recursive_remove(const char *directory)
54 dp = razor_uri_opendir(directory, NULL);
57 while((name = razor_uri_readdir(dp, NULL))) {
58 buf = razor_concat(directory, "/", name, NULL);
60 if (razor_uri_unlink(buf, NULL) < 0)
61 recursive_remove(buf);
65 razor_uri_closedir(dp, NULL);
71 RAZOR_EXPORT struct razor_atomic *razor_atomic_open(const char *description)
73 struct razor_atomic *atomic;
75 atomic = zalloc(sizeof *atomic);
77 atomic->description = strdup(description);
82 RAZOR_EXPORT int razor_atomic_commit(struct razor_atomic *atomic)
84 struct atomic_action *actions;
86 if (razor_atomic_in_error_state(atomic))
89 if (atomic->actions) {
90 actions = atomic_action_list_reverse(atomic->actions);
91 atomic->actions = NULL;
92 actions = atomic_action_do(atomic, actions);
93 atomic_action_free(actions);
96 if (atomic->toplevel) {
97 recursive_remove(atomic->toplevel);
98 free(atomic->toplevel);
99 atomic->toplevel = NULL;
102 return razor_atomic_in_error_state(atomic);
105 RAZOR_EXPORT void razor_atomic_destroy(struct razor_atomic *atomic)
107 if (atomic->actions) {
108 atomic_action_free(atomic->actions);
109 atomic->actions = NULL;
112 if (atomic->toplevel) {
113 recursive_remove(atomic->toplevel);
114 free(atomic->toplevel);
115 atomic->toplevel = NULL;
119 razor_error_free(atomic->error);
121 free(atomic->description);
127 * We need a toplevel directory in which to hold temporary files
128 * before they are committed. Since we can generally assume that
129 * we have write permissions anywhere on the filesystem in
130 * question, the best location is at the relevant mount point.
131 * The most common case where this assumption fails is when
132 * testing, when the current directory is a good choice.
136 razor_atomic_set_toplevel_from_uri(struct razor_atomic *atomic,
139 if (razor_atomic_in_error_state(atomic))
142 if (atomic->toplevel)
146 atomic->toplevel = razor_uri_mkdtemp_near(uri, "atomic-XXXXXX",
149 atomic->toplevel = razor_uri_mkdtemp_near(uri, ".atomic-XXXXXX",
153 return !atomic->toplevel;
157 * If root_uri is empty, then uri can be a URI (but not a relative-ref; ie.,
158 * a scheme must be specified). If root_uri is non-empty, then it must be a
159 * URI (but not a relative-ref) and uri must be a non-empty path.
162 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root_uri,
165 struct atomic_action *a;
167 struct razor_error *tmp_error = NULL;
168 char *fakeroot = NULL;
170 razor_atomic_set_toplevel_from_uri(atomic, *root_uri ? root_uri : uri);
172 if (razor_atomic_in_error_state(atomic))
176 if (razor_uri_parse(&ru, uri, &tmp_error)) {
177 razor_atomic_propagate_error(atomic, tmp_error, NULL);
180 if (*ru.path == '/') {
182 ru.path = strdup("/");
185 ru.path = strdup("");
187 fakeroot = razor_uri_recompose(&ru);
188 razor_uri_destroy(&ru);
190 uri += strlen(fakeroot);
193 a = atomic_action_new(ACTION_MAKE_DIRS);
194 a->args.uri = strdup(uri);
195 a->args.u.make_dirs.root = strdup(root_uri);
196 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
205 razor_atomic_remove(struct razor_atomic *atomic, const char *uri)
207 struct atomic_action *a;
209 razor_atomic_set_toplevel_from_uri(atomic, uri);
211 if (razor_atomic_in_error_state(atomic))
214 a = atomic_action_new(ACTION_REMOVE);
215 a->args.uri = strdup(uri);
216 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
222 razor_atomic_rename_file(struct razor_atomic *atomic, const char *old_uri,
225 struct atomic_action *a;
227 razor_atomic_set_toplevel_from_uri(atomic, new_uri);
229 if (razor_atomic_in_error_state(atomic))
232 a = atomic_action_new(ACTION_MOVE);
233 a->args.uri = strdup(old_uri);
234 a->args.u.move.dest = strdup(new_uri);
235 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
241 razor_atomic_create_dir(struct razor_atomic *atomic, const char *uri,
244 struct atomic_action *a;
246 razor_atomic_set_toplevel_from_uri(atomic, uri);
248 if (razor_atomic_in_error_state(atomic))
251 a = atomic_action_new(ACTION_CREATE_DIR);
252 a->args.uri = strdup(uri);
253 a->args.u.create_dir.mode = mode;
254 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
260 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
264 struct atomic_action *a;
266 razor_atomic_set_toplevel_from_uri(atomic, uri);
269 if (razor_atomic_in_error_state(atomic))
273 a = atomic_action_new(ACTION_CREATE_SYMLINK);
274 a->args.uri = strdup(uri);
275 a->args.u.create_symlink.target = strdup(target);
276 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
280 atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, ENOSYS, NULL,
281 "Symbolic links not supported "
289 razor_atomic_create_file(struct razor_atomic *atomic, const char *uri,
293 struct atomic_action *a;
296 razor_atomic_set_toplevel_from_uri(atomic, uri);
298 if (razor_atomic_in_error_state(atomic))
301 tmpnam = atomic_action_attic_tmpnam(atomic);
302 fd = razor_uri_open(tmpnam, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
303 mode & (S_IRWXU | S_IRWXG | S_IRWXO),
307 a = atomic_action_new(ACTION_MOVE);
308 a->args.uri = tmpnam;
309 a->args.u.move.dest = strdup(uri);
310 atomic->actions = atomic_action_list_prepend(atomic->actions,
317 #endif /* ENABLE_ATOMIC && !HAVE_WINDOWS_KTM */