Drop drive letter from path to razor root when RAZOR_ROOT set.
If the RAZOR_ROOT environment variable was set to eg., /root then on
Microsoft Windows we were trying to use paths such as /rootC:/Programs
which is obviously wrong. Instead we should drop the drive letter
giving paths of the form /root/Programs. Note that the drive letter is
_not_ migrated to C:/root/Programs: If a root of C:/root was desired
then RAZOR_ROOT would have been set to C:/root.
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->description = strdup(description);
85 RAZOR_EXPORT int razor_atomic_commit(struct razor_atomic *atomic)
87 struct atomic_action *actions;
89 if (razor_atomic_in_error_state(atomic))
92 if (atomic->actions) {
93 actions = atomic_action_list_reverse(atomic->actions);
94 atomic->actions = NULL;
95 actions = atomic_action_do(atomic, actions);
96 atomic_action_free(actions);
99 if (atomic->toplevel) {
100 recursive_remove(atomic->toplevel);
101 free(atomic->toplevel);
102 atomic->toplevel = NULL;
105 return razor_atomic_in_error_state(atomic);
108 RAZOR_EXPORT void razor_atomic_destroy(struct razor_atomic *atomic)
110 if (atomic->toplevel) {
111 recursive_remove(atomic->toplevel);
112 free(atomic->toplevel);
113 atomic->toplevel = NULL;
117 razor_error_free(atomic->error);
123 * We need a toplevel directory in which to hold temporary files
124 * before they are committed. Since we can generally assume that
125 * we have write permissions anywhere on the disk in question,
126 * the best location is in the relevant root directory. The most
127 * common case where this assumption fails is when testing, when
128 * the current directory is a good choice.
129 * It might be even better to find a mount point above path instead
130 * but this is hard to do, and probably not worth the effort.
134 razor_atomic_set_toplevel_from_path(struct razor_atomic *atomic,
137 if (razor_atomic_in_error_state(atomic))
140 if (atomic->toplevel)
144 if (path[0]=='\\' && path[1]=='\\' && path[2] && path[2]!='\\'
145 && strchr(path+3,'\\')) {
146 /* We have a UNC path: \\servername\sharename... */
147 const char *sharename, *root;
150 sharename = strchr(path+3,'\\')+1;
151 root = strchr(sharename,'\\');
153 disklen = root - path;
155 disklen = strlen(path);
158 malloc(disklen + strlen("\\atomic-XXXXXX") + 1);
159 memcpy(atomic->toplevel, path, disklen);
160 strcpy(atomic->toplevel + disklen, "\\atomic-XXXXXX");
161 } else if ((*path>='A' && *path<='Z' || *path>='a' && *path<='z') &&
163 atomic->toplevel = strdup("X:\\atomic-XXXXXX");
164 *atomic->toplevel = *path;
170 n = GetCurrentDirectoryW(0, NULL);
171 buf = malloc(n * sizeof(wchar_t));
173 if (GetCurrentDirectoryW(n, buf)) {
174 dir = razor_utf16_to_utf8(buf, n - 1);
175 razor_atomic_set_toplevel_from_path(atomic, dir);
181 atomic->toplevel = strdup("C:\\atomic-XXXXXX");
186 atomic->toplevel = strdup("/.atomic-XXXXXX");
189 if (!mkdtemp(atomic->toplevel)) {
194 char *s = strdup("atomic-XXXXXX");
197 free(atomic->toplevel);
198 atomic->toplevel = s;
205 atomic->error = razor_error_new_str(atomic->toplevel,
208 free(atomic->toplevel);
209 atomic->toplevel = NULL;
212 return !atomic->toplevel;
216 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
219 struct atomic_action *a;
221 razor_atomic_set_toplevel_from_path(atomic, *root ? root : path);
223 if (razor_atomic_in_error_state(atomic))
226 a = atomic_action_new(ACTION_MAKE_DIRS);
227 a->args.path = strdup(path);
228 a->args.u.make_dirs.root = strdup(root);
229 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
235 razor_atomic_remove(struct razor_atomic *atomic, const char *path)
237 struct atomic_action *a;
239 razor_atomic_set_toplevel_from_path(atomic, path);
241 if (razor_atomic_in_error_state(atomic))
244 a = atomic_action_new(ACTION_REMOVE);
245 a->args.path = strdup(path);
246 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
252 razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
255 struct atomic_action *a;
257 razor_atomic_set_toplevel_from_path(atomic, newpath);
259 if (razor_atomic_in_error_state(atomic))
262 a = atomic_action_new(ACTION_MOVE);
263 a->args.path = strdup(oldpath);
264 a->args.u.move.dest = strdup(newpath);
265 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
271 razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
274 struct atomic_action *a;
276 razor_atomic_set_toplevel_from_path(atomic, dirname);
278 if (razor_atomic_in_error_state(atomic))
281 a = atomic_action_new(ACTION_CREATE_DIR);
282 a->args.path = strdup(dirname);
283 a->args.u.create_dir.mode = mode;
284 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
290 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
294 struct atomic_action *a;
296 razor_atomic_set_toplevel_from_path(atomic, path);
299 if (razor_atomic_in_error_state(atomic))
303 a = atomic_action_new(ACTION_CREATE_SYMLINK);
304 a->args.path = strdup(path);
305 a->args.u.create_symlink.target = strdup(target);
306 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
310 atomic->error = razor_error_new_str(NULL,
311 "Symbolic links not supported "
319 razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
323 struct atomic_action *a;
326 razor_atomic_set_toplevel_from_path(atomic, filename);
328 if (razor_atomic_in_error_state(atomic))
331 tmpnam = atomic_action_attic_tmpnam(atomic);
332 fd = open(tmpnam, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
333 mode & (S_IRWXU | S_IRWXG | S_IRWXO));
336 atomic->error = razor_error_new_str(filename, strerror(errno));
338 a = atomic_action_new(ACTION_MOVE);
339 a->args.path = tmpnam;
340 a->args.u.move.dest = strdup(filename);
341 atomic->actions = atomic_action_list_prepend(atomic->actions,
348 #endif /* ENABLE_ATOMIC && !HAVE_WINDOWS_KTM */