2 * Copyright (C) 2011-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.
26 #include <sys/types.h>
33 #include "razor-internal.h"
39 static int allow_all_root_names = 0;
42 * Primarily intended for testing named roots under UNIX platforms.
45 razor_disable_root_name_checks(int disable)
47 allow_all_root_names = disable;
51 razor_allow_all_root_names(void)
53 return allow_all_root_names;
56 RAZOR_EXPORT const char *
57 razor_atomic_get_error_msg(struct razor_atomic *atomic)
59 if (!atomic->error_msg) {
60 if (atomic->error_path)
61 atomic->error_msg = razor_concat(atomic->error_path,
66 atomic->error_msg = strdup(atomic->error_str);
69 return atomic->error_msg;
73 razor_atomic_abort(struct razor_atomic *atomic, const char *error_msg)
75 if (!atomic->error_str)
76 razor_atomic_set_error_str(atomic, NULL, error_msg);
80 razor_atomic_in_error_state(struct razor_atomic *atomic)
82 return atomic->error_str && !atomic->in_undo;
88 * Common code with atomic-none and atomic-emulate
91 #define RAZOR_ASCII_ISALPHA(c) \
92 ((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
95 razor_valid_root_name(const char *name)
97 if (razor_allow_all_root_names()) {
99 return !strpbrk(name, "/\\");
101 return !strchr(name, '/');
106 return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
109 return name[0] == '\0';
115 razor_atomic_set_error_mswin(struct razor_atomic *atomic, const wchar_t *path,
120 assert(!atomic->error_str);
122 free(atomic->error_path);
125 atomic->error_path = razor_utf16_to_utf8(path, -1);
127 atomic->error_path = NULL;
129 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
130 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
131 NULL, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
132 (LPWSTR)&buf, 0, NULL);
133 atomic->error_str = razor_utf16_to_utf8(buf, -1);
139 razor_atomic_set_error_str(struct razor_atomic *atomic, const char *path,
142 assert(!atomic->error_str);
144 atomic->error_path = path ? strdup(path) : NULL;
145 atomic->error_str = strdup(str);
149 razor_atomic_write(struct razor_atomic *atomic, int fd, const void *data,
154 if (razor_atomic_in_error_state(atomic))
158 written = write(fd, data, size);
160 razor_atomic_set_error_str(atomic, NULL,
176 razor_atomic_sync(struct razor_atomic *atomic, int handle)
178 if (razor_atomic_in_error_state(atomic))
181 if (fsync(handle) < 0) {
182 razor_atomic_set_error_str(atomic, NULL, strerror(errno));
186 free(atomic->error_path);
187 atomic->error_path = NULL;
193 razor_atomic_close(struct razor_atomic *atomic, int fd)
195 if (razor_atomic_in_error_state(atomic))
199 razor_atomic_set_error_str(atomic, NULL, strerror(errno));
203 free(atomic->error_path);
204 atomic->error_path = NULL;
209 #endif /* !HAVE_WINDOWS_KTM */