Add facility to specify razor command when running tests.
This makes it easy to run under eg., valgrind as:
./details "libtool --mode=execute valgrind --leak-check=yes ../src/razor"
2 * Copyright (C) 2011, 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.
37 #include "razor-internal.h"
43 RAZOR_EXPORT struct razor_atomic *
44 razor_atomic_open(const char *description)
46 struct razor_atomic *atomic;
48 atomic = zalloc(sizeof *atomic);
54 razor_atomic_commit(struct razor_atomic *atomic)
56 return razor_atomic_in_error_state(atomic);
60 razor_atomic_destroy(struct razor_atomic *atomic)
63 razor_error_free(atomic->error);
69 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
72 char buffer[PATH_MAX], *p;
73 const char *slash, *next;
76 if (razor_atomic_in_error_state(atomic))
80 p = buffer + strlen(buffer);
81 slash = (p > buffer) ? SKIP_DRIVE_PATH(path) : path;
83 for (slash = path; *slash != '\0'; slash = next) {
85 next = strpbrk(slash + 1, "/\\");
87 next = strchr(slash + 1, '/');
92 memcpy(p, slash, next - slash);
96 if (razor_valid_root_name(buffer))
99 if (stat(buffer, &buf) == 0) {
100 if (!S_ISDIR(buf.st_mode)) {
101 atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR,
107 } else if (mkdir(buffer, 0777) < 0) {
108 atomic->error = razor_error_new_posix(buffer);
117 razor_atomic_remove(struct razor_atomic *atomic, const char *path)
124 if (razor_atomic_in_error_state(atomic))
128 buf = razor_utf8_to_utf16(path, -1);
130 if (!DeleteFileW(buf)) {
131 err = GetLastError();
132 if (err != ERROR_FILE_NOT_FOUND &&
133 err != ERROR_PATH_NOT_FOUND &&
134 !(SetFileAttributesW(buf, FILE_ATTRIBUTE_NORMAL) &&
136 !RemoveDirectoryW(buf) &&
137 GetLastError() != ERROR_DIR_NOT_EMPTY)
138 atomic->error = razor_error_new_mswin(buf, err);
144 atomic->error = razor_error_new_posix(path);
147 return razor_atomic_in_error_state(atomic);
151 razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
155 wchar_t *oldbuf, *newbuf;
156 const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
159 if (razor_atomic_in_error_state(atomic))
163 newbuf = razor_utf8_to_utf16(newpath, -1);
164 oldbuf = razor_utf8_to_utf16(oldpath, -1);
167 * Passing MOVEFILE_REPLACE_EXISTING to MoveFileEx() will
168 * cover every case we care about _except_ replacing an empty
169 * directory with a file. Calling RemoveDirectory() will deal
170 * with this case while having no effect in all other cases.
172 (void)RemoveDirectoryW(newbuf);
174 if (!MoveFileExW(oldbuf, newbuf, flags))
175 atomic->error = razor_error_new_mswin(newbuf, GetLastError());
180 if (rename(oldpath, newpath))
181 atomic->error = razor_error_new_posix(newpath);
184 return razor_atomic_in_error_state(atomic);
188 razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
193 if (razor_atomic_in_error_state(atomic))
196 if (!mkdir(dirname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)))
199 if (errno != EEXIST || stat(dirname, &buf)) {
200 atomic->error = razor_error_new_posix(dirname);
204 if (!S_ISDIR(buf.st_mode)) {
205 atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, EEXIST,
215 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
218 if (razor_atomic_in_error_state(atomic))
222 if (symlink(target, path) < 0) {
223 atomic->error = razor_error_new_posix(path);
229 atomic->error = razor_error_new_str(RAZOR_GENERAL_ERROR,
230 RAZOR_GENERAL_ERROR_FAILED, NULL,
231 "Symbolic links not supported "
239 razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
244 if (razor_atomic_in_error_state(atomic))
247 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
248 mode & (S_IRWXU | S_IRWXG | S_IRWXO));
251 atomic->error = razor_error_new_posix(filename);
256 #endif /* !ENABLE_ATOMIC */