Add -lole32 to link libraries.
This fixes a problem when compiling with mingw-headers version 3.3
where the use of SHGetFolderPath() expands to a call to CoTaskMemFree()
which is defined in libole32.dll:
/usr/x86_64-w64-mingw32/sys-root/mingw/include/shobjidl.h:29954: undefined reference to `__imp_CoTaskMemFree'
2 * Copyright (C) 2012, 2014 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>
33 #include "razor-internal.h"
36 * Emulated atomic support
38 * This implementation is better than nothing, but is certainly not atomic.
39 * It does have a couple of advantages over atomic-none:
40 * - If a file operation fails while a package is being installed we
41 * have a good chance of being able to rollback the transaction to
43 * - We behave similarly to atomic-ktm in that changes are not visible
44 * on disk to non-atomic operations (eg., scripts) until the atomic
45 * is committed. This makes the testsuite more likely to pick up
46 * problems that would otherwise only be found when using razor on
47 * an MS-Windows system which supports KTM.
54 static void recursive_remove(const char *directory)
60 dp = opendir(directory);
61 while((dirp = readdir(dp))) {
62 if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
63 buf = malloc(strlen(directory) + strlen(dirp->d_name)
65 sprintf(buf, "%s/%s", directory, dirp->d_name);
67 recursive_remove(buf);
77 RAZOR_EXPORT struct razor_atomic *razor_atomic_open(const char *description)
79 struct razor_atomic *atomic;
81 atomic = zalloc(sizeof *atomic);
83 atomic->description = strdup(description);
88 RAZOR_EXPORT int razor_atomic_commit(struct razor_atomic *atomic)
90 struct atomic_action *actions;
92 if (razor_atomic_in_error_state(atomic))
95 if (atomic->actions) {
96 actions = atomic_action_list_reverse(atomic->actions);
97 atomic->actions = NULL;
98 actions = atomic_action_do(atomic, actions);
99 atomic_action_free(actions);
102 if (atomic->toplevel) {
103 recursive_remove(atomic->toplevel);
104 free(atomic->toplevel);
105 atomic->toplevel = NULL;
108 return razor_atomic_in_error_state(atomic);
111 RAZOR_EXPORT void razor_atomic_destroy(struct razor_atomic *atomic)
113 if (atomic->actions) {
114 atomic_action_free(atomic->actions);
115 atomic->actions = NULL;
118 if (atomic->toplevel) {
119 recursive_remove(atomic->toplevel);
120 free(atomic->toplevel);
121 atomic->toplevel = NULL;
125 razor_error_free(atomic->error);
127 free(atomic->description);
133 static char *absolute_path(const char *path)
136 char *result, *subpath, *p, *s, *t;
138 result = realpath(path, NULL);
140 if (!result && errno == ENOENT) {
146 result = strdup("/");
151 subpath = realpath(p, NULL);
155 len = strlen(subpath);
156 result = malloc(len + strlen(s) + 1);
157 memcpy(result, subpath, len);
158 strcpy(result + len, s);
161 } else if (errno != ENOENT)
170 result = realpath(".", NULL);
180 * We need a toplevel directory in which to hold temporary files
181 * before they are committed. Since we can generally assume that
182 * we have write permissions anywhere on the filesystem in
183 * question, the best location is at the relevant mount point.
184 * The most common case where this assumption fails is when
185 * testing, when the current directory is a good choice.
189 razor_atomic_set_toplevel_from_path(struct razor_atomic *atomic,
197 if (razor_atomic_in_error_state(atomic))
200 if (atomic->toplevel)
204 if (path[0]=='\\' && path[1]=='\\' && path[2] && path[2]!='\\'
205 && strchr(path+3,'\\')) {
206 /* We have a UNC path: \\servername\sharename... */
207 const char *sharename, *root;
210 sharename = strchr(path+3,'\\')+1;
211 root = strchr(sharename,'\\');
213 disklen = root - path;
215 disklen = strlen(path);
218 malloc(disklen + strlen("\\atomic-XXXXXX") + 1);
219 memcpy(atomic->toplevel, path, disklen);
220 strcpy(atomic->toplevel + disklen, "\\atomic-XXXXXX");
221 } else if ((*path>='A' && *path<='Z' || *path>='a' && *path<='z') &&
223 atomic->toplevel = strdup("X:\\atomic-XXXXXX");
224 *atomic->toplevel = *path;
230 n = GetCurrentDirectoryW(0, NULL);
231 buf = malloc(n * sizeof(wchar_t));
233 if (GetCurrentDirectoryW(n, buf)) {
234 dir = razor_utf16_to_utf8(buf, n - 1);
235 razor_atomic_set_toplevel_from_path(atomic, dir);
241 atomic->toplevel = strdup("C:\\atomic-XXXXXX");
248 * Find the mount point (assuming we can write to the
249 * whole filesystem). Otherwise stop at the first
250 * unwritable directory and take one step back.
252 char *s, *abspath, saved;
253 int len, can_step_back = 0;
255 abspath = absolute_path(path);
257 atomic->error = razor_error_new_posix(path);
261 if (stat(abspath, &buf) < 0) {
265 atomic->error = razor_error_new_posix(abspath);
270 filesystem = buf.st_dev;
272 len = strlen(abspath);
273 while(len > 1 && (s = strrchr(abspath, '/'))) {
277 len = s + 1 - abspath;
283 if (stat(abspath, &buf) < 0) {
287 atomic->error = razor_error_new_posix(abspath);
291 } else if (!filesystem)
292 filesystem = buf.st_dev;
294 if (buf.st_dev != filesystem || access(abspath, W_OK)) {
301 len = strlen(abspath);
308 len = 0; /* Avoid an unslightly double slash. */
309 atomic->toplevel = malloc(len + strlen("/.atomic-XXXXXX") + 1);
310 memcpy(atomic->toplevel, abspath, len);
311 strcpy(atomic->toplevel + len, "/.atomic-XXXXXX");
317 if (!mkdtemp(atomic->toplevel)) {
322 char *s = strdup("atomic-XXXXXX");
325 if (stat(".", &buf) < 0) {
326 atomic->error = razor_error_new_posix(".");
328 free(atomic->toplevel);
329 atomic->toplevel = NULL;
332 if (buf.st_dev != filesystem)
334 * Don't use a different filesystem. It will
335 * only fail later on (in rename) and cause
336 * an unhelpful error message (EXDEV).
342 free(atomic->toplevel);
343 atomic->toplevel = s;
350 atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, err,
354 free(atomic->toplevel);
355 atomic->toplevel = NULL;
358 return !atomic->toplevel;
362 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
365 struct atomic_action *a;
367 razor_atomic_set_toplevel_from_path(atomic, *root ? root : path);
369 if (razor_atomic_in_error_state(atomic))
372 a = atomic_action_new(ACTION_MAKE_DIRS);
373 a->args.path = strdup(path);
374 a->args.u.make_dirs.root = strdup(root);
375 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
381 razor_atomic_remove(struct razor_atomic *atomic, const char *path)
383 struct atomic_action *a;
385 razor_atomic_set_toplevel_from_path(atomic, path);
387 if (razor_atomic_in_error_state(atomic))
390 a = atomic_action_new(ACTION_REMOVE);
391 a->args.path = strdup(path);
392 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
398 razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
401 struct atomic_action *a;
403 razor_atomic_set_toplevel_from_path(atomic, newpath);
405 if (razor_atomic_in_error_state(atomic))
408 a = atomic_action_new(ACTION_MOVE);
409 a->args.path = strdup(oldpath);
410 a->args.u.move.dest = strdup(newpath);
411 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
417 razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
420 struct atomic_action *a;
422 razor_atomic_set_toplevel_from_path(atomic, dirname);
424 if (razor_atomic_in_error_state(atomic))
427 a = atomic_action_new(ACTION_CREATE_DIR);
428 a->args.path = strdup(dirname);
429 a->args.u.create_dir.mode = mode;
430 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
436 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
440 struct atomic_action *a;
442 razor_atomic_set_toplevel_from_path(atomic, path);
445 if (razor_atomic_in_error_state(atomic))
449 a = atomic_action_new(ACTION_CREATE_SYMLINK);
450 a->args.path = strdup(path);
451 a->args.u.create_symlink.target = strdup(target);
452 atomic->actions = atomic_action_list_prepend(atomic->actions, a);
456 atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, ENOSYS, NULL,
457 "Symbolic links not supported "
465 razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
469 struct atomic_action *a;
472 razor_atomic_set_toplevel_from_path(atomic, filename);
474 if (razor_atomic_in_error_state(atomic))
477 tmpnam = atomic_action_attic_tmpnam(atomic);
478 fd = open(tmpnam, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
479 mode & (S_IRWXU | S_IRWXG | S_IRWXO));
482 atomic->error = razor_error_new_posix(filename);
484 a = atomic_action_new(ACTION_MOVE);
485 a->args.path = tmpnam;
486 a->args.u.move.dest = strdup(filename);
487 atomic->actions = atomic_action_list_prepend(atomic->actions,
494 #endif /* ENABLE_ATOMIC && !HAVE_WINDOWS_KTM */