librazor/atomic.c
changeset 414 404c18183801
child 416 d0aa9e0a6d04
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/librazor/atomic.c	Wed Feb 01 13:00:40 2012 +0000
     1.3 @@ -0,0 +1,1002 @@
     1.4 +/*
     1.5 + * Copyright (C) 2011  J. Ali Harlow <ali@juiblex.co.uk>
     1.6 + *
     1.7 + * This program is free software; you can redistribute it and/or modify
     1.8 + * it under the terms of the GNU General Public License as published by
     1.9 + * the Free Software Foundation; either version 2 of the License, or
    1.10 + * (at your option) any later version.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License along
    1.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
    1.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    1.20 + */
    1.21 +
    1.22 +#include "config.h"
    1.23 +
    1.24 +#include <stdlib.h>
    1.25 +#ifdef MSWIN_API
    1.26 +#include <windows.h>
    1.27 +#endif
    1.28 +#include <stdio.h>
    1.29 +#include <limits.h>
    1.30 +#include <errno.h>
    1.31 +#include <unistd.h>
    1.32 +#include <fcntl.h>
    1.33 +#include <sys/stat.h>
    1.34 +#include <string.h>
    1.35 +#include <assert.h>
    1.36 +#if HAVE_WINDOWS_KTM
    1.37 +#include <wchar.h>
    1.38 +#include <ktmw32.h>
    1.39 +#endif
    1.40 +
    1.41 +#include "razor.h"
    1.42 +#include "razor-internal.h"
    1.43 +
    1.44 +/*
    1.45 + * Atomic transactions
    1.46 + */
    1.47 +
    1.48 +#ifndef O_BINARY
    1.49 +#define O_BINARY	0
    1.50 +#endif
    1.51 +
    1.52 +#define RAZOR_ASCII_ISALPHA(c)	\
    1.53 +			((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
    1.54 +
    1.55 +static int allow_all_root_names = 0;
    1.56 +
    1.57 +/*
    1.58 + * Primarily intended for testing named roots under UNIX platforms.
    1.59 + */
    1.60 +RAZOR_EXPORT void
    1.61 +razor_disable_root_name_checks(int disable)
    1.62 +{
    1.63 +	allow_all_root_names = disable;
    1.64 +}
    1.65 +
    1.66 +#ifdef MSWIN_API
    1.67 +
    1.68 +static char *
    1.69 +razor_utf16_to_utf8(const wchar_t *utf16, int len)
    1.70 +{
    1.71 +	int n;
    1.72 +	char *utf8;
    1.73 +
    1.74 +	n = WideCharToMultiByte(CP_UTF8, 0, utf16, len, NULL, 0, NULL, NULL);
    1.75 +	if (len >= 0 && utf16[len])
    1.76 +		n++;
    1.77 +	utf8 = malloc(n);
    1.78 +	(void)WideCharToMultiByte(CP_UTF8, 0, utf16, len, utf8, n, NULL, NULL);
    1.79 +	if (len >= 0 && utf16[len])
    1.80 +		utf8[n - 1] = 0;
    1.81 +
    1.82 +	return utf8;
    1.83 +}
    1.84 +
    1.85 +static wchar_t *
    1.86 +razor_utf8_to_utf16(const char *utf8, int len)
    1.87 +{
    1.88 +	int n;
    1.89 +	wchar_t *utf16;
    1.90 +
    1.91 +	n = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0);
    1.92 +	if (len >= 0 && utf8[len])
    1.93 +		n++;
    1.94 +	utf16 = malloc(n * sizeof(wchar_t));
    1.95 +	(void)MultiByteToWideChar(CP_UTF8, 0, utf8, len, utf16, n);
    1.96 +	if (len >= 0 && utf8[len])
    1.97 +		utf16[n - 1] = 0;
    1.98 +
    1.99 +	return utf16;
   1.100 +}
   1.101 +
   1.102 +#endif	/* MSWIN_API */
   1.103 +
   1.104 +#if HAVE_WINDOWS_KTM
   1.105 +
   1.106 +static int
   1.107 +razor_valid_root_name(const wchar_t *name)
   1.108 +{
   1.109 +	if (allow_all_root_names)
   1.110 +		return !wcschr(name, '/');
   1.111 +
   1.112 +	return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
   1.113 +	       name[2] == '\0';
   1.114 +}
   1.115 +
   1.116 +struct razor_atomic {
   1.117 +	HANDLE transaction;
   1.118 +	int n_files;
   1.119 +	struct razor_atomic_file {
   1.120 +		wchar_t *path;
   1.121 +		HANDLE h;
   1.122 +	} *files;
   1.123 +	char *error_path;
   1.124 +	char *error_str;
   1.125 +	char *error_msg;
   1.126 +};
   1.127 +
   1.128 +struct razor_wstr {
   1.129 +	wchar_t *str;
   1.130 +	int len, allocated;
   1.131 +};
   1.132 +
   1.133 +static struct razor_wstr *
   1.134 +razor_wstr_create(const char *init, int len)
   1.135 +{
   1.136 +	int n;
   1.137 +	struct razor_wstr *wstr;
   1.138 +
   1.139 +	wstr = malloc(sizeof(struct razor_wstr));
   1.140 +
   1.141 +	n = MultiByteToWideChar(CP_UTF8, 0, init, len, NULL, 0);
   1.142 +	if (len >= 0 && init[len])
   1.143 +		wstr->len = n++;
   1.144 +	else
   1.145 +		wstr->len = n - 1;
   1.146 +
   1.147 +	wstr->allocated = n * 2;
   1.148 +	wstr->str = malloc(wstr->allocated * sizeof(wchar_t));
   1.149 +	if (!wstr->str) {
   1.150 +		free(wstr);
   1.151 +		return NULL;
   1.152 +	}
   1.153 +
   1.154 +	(void)MultiByteToWideChar(CP_UTF8, 0, init, len, wstr->str, n);
   1.155 +	if (len >= 0 && init[len])
   1.156 +		wstr->str[wstr->len] = 0;
   1.157 +
   1.158 +	return wstr;
   1.159 +}
   1.160 +
   1.161 +static int
   1.162 +razor_wstr_append(struct razor_wstr *wstr, const char *s, int len)
   1.163 +{
   1.164 +	int n, allocated;
   1.165 +	wchar_t *str;
   1.166 +
   1.167 +	n = MultiByteToWideChar(CP_UTF8, 0, s, len, NULL, 0);
   1.168 +	if (len < 0 || !s[len])
   1.169 +		n--;
   1.170 +
   1.171 +	if (wstr->allocated <= wstr->len + n) {
   1.172 +		allocated = (wstr->len + n + 1) * 2;
   1.173 +		str = realloc(wstr->str, allocated * sizeof(wchar_t));
   1.174 +		if (!str)
   1.175 +			return -1;
   1.176 +		wstr->allocated = allocated;
   1.177 +		wstr->str = str;
   1.178 +	}
   1.179 +
   1.180 +	(void)MultiByteToWideChar(CP_UTF8, 0, s, len, wstr->str + wstr->len, n);
   1.181 +	wstr->len += n;
   1.182 +	wstr->str[wstr->len] = 0;
   1.183 +
   1.184 +	return 0;
   1.185 +}
   1.186 +
   1.187 +static void
   1.188 +razor_wstr_destroy(struct razor_wstr *wstr)
   1.189 +{
   1.190 +	free(wstr->str);
   1.191 +	free(wstr);
   1.192 +}
   1.193 +
   1.194 +RAZOR_EXPORT struct razor_atomic *
   1.195 +razor_atomic_open(const char *description)
   1.196 +{
   1.197 +	wchar_t *buf;
   1.198 +	struct razor_atomic *atomic;
   1.199 +
   1.200 +	atomic = zalloc(sizeof *atomic);
   1.201 +	buf = razor_utf8_to_utf16(description, -1);
   1.202 +	atomic->transaction = CreateTransaction(NULL, 0,
   1.203 +						TRANSACTION_DO_NOT_PROMOTE,
   1.204 +						0, 0, 0, buf);
   1.205 +	free(buf);
   1.206 +
   1.207 +	return atomic;
   1.208 +}
   1.209 +
   1.210 +static void
   1.211 +razor_atomic_set_error_str(struct razor_atomic *atomic, const wchar_t *path,
   1.212 +			   const char *str)
   1.213 +{
   1.214 +	assert(!atomic->error_str);
   1.215 +
   1.216 +	free(atomic->error_path);
   1.217 +
   1.218 +	if (path)
   1.219 +		atomic->error_path = razor_utf16_to_utf8(path, -1);
   1.220 +	else
   1.221 +		atomic->error_path = NULL;
   1.222 +
   1.223 +	atomic->error_str = strdup(str);
   1.224 +}
   1.225 +
   1.226 +static void
   1.227 +razor_atomic_set_error(struct razor_atomic *atomic, const wchar_t *path,
   1.228 +		       DWORD error)
   1.229 +{
   1.230 +	wchar_t *buf;
   1.231 +
   1.232 +	assert(!atomic->error_str);
   1.233 +
   1.234 +	free(atomic->error_path);
   1.235 +
   1.236 +	if (path)
   1.237 +		atomic->error_path = razor_utf16_to_utf8(path, -1);
   1.238 +	else
   1.239 +		atomic->error_path = NULL;
   1.240 +
   1.241 +	FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
   1.242 +		       FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
   1.243 +		       NULL, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
   1.244 +		       (LPWSTR)&buf, 0, NULL);
   1.245 +	atomic->error_str = razor_utf16_to_utf8(buf, -1);
   1.246 +	LocalFree(buf);
   1.247 +}
   1.248 +
   1.249 +RAZOR_EXPORT int
   1.250 +razor_atomic_commit(struct razor_atomic *atomic)
   1.251 +{
   1.252 +	int retval;
   1.253 +
   1.254 +	if (atomic->error_str)
   1.255 +		return -1;
   1.256 +
   1.257 +	retval = !CommitTransaction(atomic->transaction);
   1.258 +
   1.259 +	if (retval) {
   1.260 +		razor_atomic_set_error(atomic, NULL, GetLastError());
   1.261 +		RollbackTransaction(atomic->transaction);
   1.262 +	}
   1.263 +
   1.264 +	CloseHandle(atomic->transaction);
   1.265 +	atomic->transaction = INVALID_HANDLE_VALUE;
   1.266 +
   1.267 +	return retval;
   1.268 +}
   1.269 +
   1.270 +RAZOR_EXPORT void
   1.271 +razor_atomic_destroy(struct razor_atomic *atomic)
   1.272 +{
   1.273 +	int i;
   1.274 +
   1.275 +	for(i = 0; i < atomic->n_files; i++) {
   1.276 +		if (atomic->files[i].h != INVALID_HANDLE_VALUE) {
   1.277 +			CloseHandle(atomic->files[i].h);
   1.278 +			free(atomic->files[i].path);
   1.279 +		}
   1.280 +	}
   1.281 +	free(atomic->files);
   1.282 +	if (atomic->transaction != INVALID_HANDLE_VALUE) {
   1.283 +		RollbackTransaction(atomic->transaction);
   1.284 +		CloseHandle(atomic->transaction);
   1.285 +	}
   1.286 +	free(atomic->error_path);
   1.287 +	free(atomic->error_str);
   1.288 +	free(atomic->error_msg);
   1.289 +	free(atomic);
   1.290 +}
   1.291 +
   1.292 +RAZOR_EXPORT int
   1.293 +razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
   1.294 +		       const char *path)
   1.295 +{
   1.296 +	struct razor_wstr *buffer;
   1.297 +	const char *slash, *s, *next;
   1.298 +	WIN32_FILE_ATTRIBUTE_DATA fa;
   1.299 +	DWORD err;
   1.300 +	int r, creating = 0;
   1.301 +
   1.302 +	if (atomic->error_str)
   1.303 +		return -1;
   1.304 +
   1.305 +	buffer = razor_wstr_create(root, -1);
   1.306 +	slash = path;
   1.307 +
   1.308 +	for (; *slash != '\0'; slash = next) {
   1.309 +		next = strpbrk(slash + 1, "/\\");
   1.310 +		if (next == NULL)
   1.311 +			break;
   1.312 +
   1.313 +		razor_wstr_append(buffer, slash, next - slash);
   1.314 +
   1.315 +		if (!creating) {
   1.316 +			if (razor_valid_root_name(buffer->str))
   1.317 +				continue;
   1.318 +
   1.319 +			r = GetFileAttributesTransactedW(buffer->str,
   1.320 +							 GetFileExInfoStandard,
   1.321 +							 &fa,
   1.322 +							 atomic->transaction);
   1.323 +
   1.324 +			if (!r) {
   1.325 +				err = GetLastError();
   1.326 +				if (err == ERROR_FILE_NOT_FOUND) {
   1.327 +					creating = 1;
   1.328 +				} else {
   1.329 +					razor_atomic_set_error(atomic,
   1.330 +							       buffer->str,
   1.331 +							       err);
   1.332 +					razor_wstr_destroy(buffer);
   1.333 +					return -1;
   1.334 +				}
   1.335 +			} else if (!(fa.dwFileAttributes&
   1.336 +				     FILE_ATTRIBUTE_DIRECTORY)) {
   1.337 +				razor_atomic_set_error_str(atomic, buffer->str,
   1.338 +							   "Not a directory");
   1.339 +				razor_wstr_destroy(buffer);
   1.340 +				return -1;
   1.341 +			}
   1.342 +		}
   1.343 +		if (creating) {
   1.344 +			if (!CreateDirectoryTransactedW(NULL, buffer->str, NULL,
   1.345 +							atomic->transaction)) {
   1.346 +				razor_atomic_set_error(atomic, buffer->str,
   1.347 +						       GetLastError());
   1.348 +				razor_wstr_destroy(buffer);
   1.349 +				return -1;
   1.350 +			}
   1.351 +
   1.352 +			/* FIXME: What to do about permissions for dirs we
   1.353 +			 * have to create but are not in the cpio archive? */
   1.354 +		}
   1.355 +	}
   1.356 +
   1.357 +	razor_wstr_destroy(buffer);
   1.358 +
   1.359 +	return 0;
   1.360 +}
   1.361 +
   1.362 +RAZOR_EXPORT int
   1.363 +razor_atomic_remove(struct razor_atomic *atomic, const char *path)
   1.364 +{
   1.365 +	wchar_t *buf;
   1.366 +	DWORD err;
   1.367 +
   1.368 +	if (atomic->error_str)
   1.369 +		return -1;
   1.370 +
   1.371 +	buf = razor_utf8_to_utf16(path, -1);
   1.372 +
   1.373 +	if (DeleteFileTransactedW(buf, atomic->transaction)) {
   1.374 +		free(buf);
   1.375 +		return 0;
   1.376 +	}
   1.377 +
   1.378 +	err = GetLastError();
   1.379 +	if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) {
   1.380 +		free(buf);
   1.381 +		return 0;
   1.382 +	}
   1.383 +
   1.384 +	if (SetFileAttributesTransactedW(buf, FILE_ATTRIBUTE_NORMAL,
   1.385 +					 atomic->transaction)) {
   1.386 +		if (DeleteFileTransactedW(buf, atomic->transaction)) {
   1.387 +			free(buf);
   1.388 +			return 0;
   1.389 +		}
   1.390 +		err = GetLastError();
   1.391 +	}
   1.392 +
   1.393 +	if (RemoveDirectoryTransactedW(buf, atomic->transaction) ||
   1.394 +	    GetLastError() == ERROR_DIR_NOT_EMPTY) {
   1.395 +		free(buf);
   1.396 +		return 0;
   1.397 +	}
   1.398 +
   1.399 +	/*
   1.400 +	 * It would be tempting to use:
   1.401 +	 * 	MoveFileEx(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)
   1.402 +	 * but unless we can guarantee that the system will be rebooted
   1.403 +	 * before we (or some other application) write another file with the
   1.404 +	 * same path, this is likely to cause more problems than it solves.
   1.405 +	 */
   1.406 +
   1.407 +	razor_atomic_set_error(atomic, buf, err);
   1.408 +	free(buf);
   1.409 +	return -1;
   1.410 +}
   1.411 +
   1.412 +RAZOR_EXPORT int
   1.413 +razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
   1.414 +			 const char *newpath)
   1.415 +{
   1.416 +	wchar_t *oldbuf, *newbuf;
   1.417 +	const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
   1.418 +
   1.419 +	if (atomic->error_str)
   1.420 +		return -1;
   1.421 +
   1.422 +	newbuf = razor_utf8_to_utf16(newpath, -1);
   1.423 +	oldbuf = razor_utf8_to_utf16(oldpath, -1);
   1.424 +
   1.425 +	/*
   1.426 +	 * Passing MOVEFILE_REPLACE_EXISTING to MoveFileTransaction() will
   1.427 +	 * cover every case we care about _except_ replacing an empty
   1.428 +	 * directory with a file. Calling RemoveDirectoryTransacted() will deal
   1.429 +	 * with this case while having no effect in all other cases.
   1.430 +	 */
   1.431 +	(void)RemoveDirectoryTransactedW(newbuf, atomic->transaction);
   1.432 +
   1.433 +	if (!MoveFileTransactedW(oldbuf, newbuf, NULL, NULL, flags,
   1.434 +			         atomic->transaction))
   1.435 +		razor_atomic_set_error(atomic, newbuf, GetLastError());
   1.436 +
   1.437 +	free(newbuf);
   1.438 +	free(oldbuf);
   1.439 +
   1.440 +	return !!atomic->error_str;
   1.441 +}
   1.442 +
   1.443 +RAZOR_EXPORT int
   1.444 +razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
   1.445 +			mode_t mode)
   1.446 +{
   1.447 +	wchar_t *buf;
   1.448 +	DWORD err;
   1.449 +	WIN32_FILE_ATTRIBUTE_DATA fa;
   1.450 +
   1.451 +	if (atomic->error_str)
   1.452 +		return -1;
   1.453 +
   1.454 +	buf = razor_utf8_to_utf16(dirname, -1);
   1.455 +
   1.456 +	if (!CreateDirectoryTransactedW(NULL, buf, NULL, atomic->transaction)) {
   1.457 +		err = GetLastError();
   1.458 +		if (err != ERROR_FILE_EXISTS && err != ERROR_ALREADY_EXISTS) {
   1.459 +abort:
   1.460 +			razor_atomic_set_error(atomic, buf, err);
   1.461 +			free(buf);
   1.462 +			return -1;
   1.463 +		}
   1.464 +
   1.465 +		if (!GetFileAttributesTransactedW(buf, GetFileExInfoStandard,
   1.466 +						  &fa, atomic->transaction))
   1.467 +			goto abort;
   1.468 +
   1.469 +		if (!(fa.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) {
   1.470 +			if (razor_atomic_remove(atomic, dirname)) {
   1.471 +				free(buf);
   1.472 +				return -1;
   1.473 +			}
   1.474 +			if (!CreateDirectoryTransactedW(NULL, buf, NULL,
   1.475 +							atomic->transaction)) {
   1.476 +				err = GetLastError();
   1.477 +				goto abort;
   1.478 +			}
   1.479 +		}
   1.480 +	}
   1.481 +
   1.482 +	free(buf);
   1.483 +
   1.484 +	return 0;
   1.485 +}
   1.486 +
   1.487 +RAZOR_EXPORT int
   1.488 +razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
   1.489 +			    const char *path)
   1.490 +{
   1.491 +	if (atomic->error_str)
   1.492 +		return -1;
   1.493 +
   1.494 +	/*
   1.495 +	 * This isn't true, but symbolic links under Windows 7
   1.496 +	 * need to know whether the target is a directory or not
   1.497 +	 * and we don't always know that at the time when the
   1.498 +	 * link is created, so it's a convienent lie for now.
   1.499 +	 */
   1.500 +	razor_atomic_set_error_str(atomic, NULL, "Symbolic links not supported "
   1.501 +						 "on this platform");
   1.502 +
   1.503 +	return -1;
   1.504 +}
   1.505 +
   1.506 +RAZOR_EXPORT int
   1.507 +razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
   1.508 +			 mode_t mode)
   1.509 +{
   1.510 +	DWORD attribs;
   1.511 +	struct razor_atomic_file *files;
   1.512 +	int i = atomic->n_files;
   1.513 +
   1.514 +	if (atomic->error_str)
   1.515 +		return -1;
   1.516 +
   1.517 +	files = realloc(atomic->files,
   1.518 +			(atomic->n_files+1) * sizeof(struct razor_atomic_file));
   1.519 +	if (!files) {
   1.520 +		razor_atomic_set_error_str(atomic, NULL, "Not enough memory");
   1.521 +		return -1;
   1.522 +	}
   1.523 +	atomic->n_files++;
   1.524 +	atomic->files = files;
   1.525 +
   1.526 +	files[i].path = razor_utf8_to_utf16(filename, -1);
   1.527 +
   1.528 +	/*
   1.529 +	 * Passing CREATE_ALWAYS to CreateFileTransacted() will cover
   1.530 +	 * every case we care about _except_ replacing an empty directory
   1.531 +	 * with a file. Calling RemoveDirectoryTransacted() will deal
   1.532 +	 * with this case while having no effect in all other cases.
   1.533 +	 */
   1.534 +	(void)RemoveDirectoryTransactedW(files[i].path, atomic->transaction);
   1.535 +
   1.536 +	if (mode & S_IWUSR)
   1.537 +		attribs = FILE_ATTRIBUTE_NORMAL;
   1.538 +	else
   1.539 +		attribs = FILE_ATTRIBUTE_READONLY;
   1.540 +
   1.541 +	files[i].h = CreateFileTransactedW(files[i].path, GENERIC_WRITE,
   1.542 +					   0, NULL, CREATE_ALWAYS, attribs,
   1.543 +					   NULL, atomic->transaction, NULL,
   1.544 +					   NULL);
   1.545 +
   1.546 +	if (files[i].h == INVALID_HANDLE_VALUE) {
   1.547 +		razor_atomic_set_error(atomic, files[i].path, GetLastError());
   1.548 +		free(files[i].path);
   1.549 +		atomic->n_files--;
   1.550 +		return -1;
   1.551 +	}
   1.552 +
   1.553 +	return i;
   1.554 +}
   1.555 +
   1.556 +RAZOR_EXPORT int
   1.557 +razor_atomic_write(struct razor_atomic *atomic, int handle, const void *data,
   1.558 +		   size_t size)
   1.559 +{
   1.560 +	DWORD written;
   1.561 +
   1.562 +	if (atomic->error_str)
   1.563 +		return -1;
   1.564 +
   1.565 +	assert(handle < atomic->n_files);
   1.566 +	assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
   1.567 +
   1.568 +	while(size) {
   1.569 +		if (!WriteFile(atomic->files[handle].h, data, size, &written,
   1.570 +			       NULL)) {
   1.571 +			razor_atomic_set_error(atomic,
   1.572 +					       atomic->files[handle].path,
   1.573 +					       GetLastError());
   1.574 +
   1.575 +			(void)CloseHandle(atomic->files[handle].h);
   1.576 +			free(atomic->files[handle].path);
   1.577 +			atomic->files[handle].path = NULL;
   1.578 +			atomic->files[handle].h = INVALID_HANDLE_VALUE;
   1.579 +
   1.580 +			return -1;
   1.581 +		}
   1.582 +
   1.583 +		data += written;
   1.584 +		size -= written;
   1.585 +	}
   1.586 +
   1.587 +	return 0;
   1.588 +}
   1.589 +
   1.590 +RAZOR_EXPORT int
   1.591 +razor_atomic_sync(struct razor_atomic *atomic, int handle)
   1.592 +{
   1.593 +	HANDLE h;
   1.594 +
   1.595 +	if (atomic->error_str)
   1.596 +		return -1;
   1.597 +
   1.598 +	assert(handle < atomic->n_files);
   1.599 +	assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
   1.600 +
   1.601 +	if (!CloseHandle(atomic->files[handle].h)) {
   1.602 +		razor_atomic_set_error(atomic, atomic->files[handle].path,
   1.603 +				       GetLastError());
   1.604 +		free(atomic->files[handle].path);
   1.605 +		atomic->files[handle].path = NULL;
   1.606 +		atomic->files[handle].h = INVALID_HANDLE_VALUE;
   1.607 +		return -1;
   1.608 +	}
   1.609 +
   1.610 +	h = CreateFileTransactedW(atomic->files[handle].path, GENERIC_WRITE, 0,
   1.611 +				  NULL, OPEN_EXISTING, 0, NULL,
   1.612 +				  atomic->transaction, NULL, NULL);
   1.613 +	atomic->files[handle].h = h;
   1.614 +
   1.615 +	if (atomic->files[handle].h == INVALID_HANDLE_VALUE) {
   1.616 +		razor_atomic_set_error(atomic, atomic->files[handle].path,
   1.617 +				       GetLastError());
   1.618 +		free(atomic->files[handle].path);
   1.619 +		atomic->files[handle].path = NULL;
   1.620 +		return -1;
   1.621 +	}
   1.622 +
   1.623 +	return !!atomic->error_str;
   1.624 +}
   1.625 +
   1.626 +RAZOR_EXPORT int
   1.627 +razor_atomic_close(struct razor_atomic *atomic, int handle)
   1.628 +{
   1.629 +	if (atomic->error_str)
   1.630 +		return -1;
   1.631 +
   1.632 +	assert(handle < atomic->n_files);
   1.633 +	assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
   1.634 +
   1.635 +	if (!CloseHandle(atomic->files[handle].h))
   1.636 +		razor_atomic_set_error(atomic, atomic->files[handle].path,
   1.637 +				       GetLastError());
   1.638 +
   1.639 +	free(atomic->files[handle].path);
   1.640 +	atomic->files[handle].path = NULL;
   1.641 +	atomic->files[handle].h = INVALID_HANDLE_VALUE;
   1.642 +
   1.643 +	while(atomic->n_files > 0 &&
   1.644 +	      atomic->files[atomic->n_files-1].h == INVALID_HANDLE_VALUE)
   1.645 +		atomic->n_files--;
   1.646 +
   1.647 +	return !!atomic->error_str;
   1.648 +}
   1.649 +
   1.650 +#else		/* HAVE_WINDOWS_KVM */
   1.651 +
   1.652 +static int
   1.653 +razor_valid_root_name(const char *name)
   1.654 +{
   1.655 +	if (allow_all_root_names) {
   1.656 +#ifdef MSWIN_API
   1.657 +		return !strpbrk(name, "/\\");
   1.658 +#else
   1.659 +		return !strchr(name, '/');
   1.660 +#endif
   1.661 +	}
   1.662 +
   1.663 +#ifdef MSWIN_API
   1.664 +	return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
   1.665 +	       name[2] == '\0';
   1.666 +#else
   1.667 +	return name[0] == '\0';
   1.668 +#endif
   1.669 +}
   1.670 +
   1.671 +struct razor_atomic {
   1.672 +	char *error_path;
   1.673 +	char *error_str;
   1.674 +	char *error_msg;
   1.675 +};
   1.676 +
   1.677 +RAZOR_EXPORT struct razor_atomic *
   1.678 +razor_atomic_open(const char *description)
   1.679 +{
   1.680 +	struct razor_atomic *atomic;
   1.681 +
   1.682 +	atomic = zalloc(sizeof *atomic);
   1.683 +
   1.684 +	return atomic;
   1.685 +}
   1.686 +
   1.687 +static void
   1.688 +razor_atomic_set_error_str(struct razor_atomic *atomic, const char *path,
   1.689 +			   const char *str)
   1.690 +{
   1.691 +	assert(!atomic->error_str);
   1.692 +
   1.693 +	atomic->error_path = path ? strdup(path) : NULL;
   1.694 +	atomic->error_str = strdup(str);
   1.695 +}
   1.696 +
   1.697 +#ifdef MSWIN_API
   1.698 +static void
   1.699 +razor_atomic_set_error_mswin(struct razor_atomic *atomic, const wchar_t *path,
   1.700 +		       DWORD error)
   1.701 +{
   1.702 +	wchar_t *buf;
   1.703 +
   1.704 +	assert(!atomic->error_str);
   1.705 +
   1.706 +	free(atomic->error_path);
   1.707 +
   1.708 +	if (path)
   1.709 +		atomic->error_path = razor_utf16_to_utf8(path, -1);
   1.710 +	else
   1.711 +		atomic->error_path = NULL;
   1.712 +
   1.713 +	FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
   1.714 +		       FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
   1.715 +		       NULL, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
   1.716 +		       (LPWSTR)&buf, 0, NULL);
   1.717 +	atomic->error_str = razor_utf16_to_utf8(buf, -1);
   1.718 +	LocalFree(buf);
   1.719 +}
   1.720 +#endif
   1.721 +
   1.722 +RAZOR_EXPORT int
   1.723 +razor_atomic_commit(struct razor_atomic *atomic)
   1.724 +{
   1.725 +	return !!atomic->error_str;
   1.726 +}
   1.727 +
   1.728 +RAZOR_EXPORT void
   1.729 +razor_atomic_destroy(struct razor_atomic *atomic)
   1.730 +{
   1.731 +	free(atomic->error_path);
   1.732 +	free(atomic->error_str);
   1.733 +	free(atomic->error_msg);
   1.734 +	free(atomic);
   1.735 +}
   1.736 +
   1.737 +RAZOR_EXPORT int
   1.738 +razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
   1.739 +		       const char *path)
   1.740 +{
   1.741 +	char buffer[PATH_MAX], *p;
   1.742 +	const char *slash, *next;
   1.743 +	struct stat buf;
   1.744 +
   1.745 +	if (atomic->error_str)
   1.746 +		return -1;
   1.747 +
   1.748 +	strcpy(buffer, root);
   1.749 +	p = buffer + strlen(buffer);
   1.750 +	slash = path;
   1.751 +	for (slash = path; *slash != '\0'; slash = next) {
   1.752 +#ifdef MSWIN_API
   1.753 +		next = strpbrk(slash + 1, "/\\");
   1.754 +#else
   1.755 +		next = strchr(slash + 1, '/');
   1.756 +#endif
   1.757 +		if (next == NULL)
   1.758 +			break;
   1.759 +
   1.760 +		memcpy(p, slash, next - slash);
   1.761 +		p += next - slash;
   1.762 +		*p = '\0';
   1.763 +
   1.764 +		if (razor_valid_root_name(buffer))
   1.765 +			continue;
   1.766 +
   1.767 +		if (stat(buffer, &buf) == 0) {
   1.768 +			if (!S_ISDIR(buf.st_mode)) {
   1.769 +				razor_atomic_set_error_str(atomic, buffer,
   1.770 +							   "Not a directory");
   1.771 +				return -1;
   1.772 +			}
   1.773 +		} else if (mkdir(buffer, 0777) < 0) {
   1.774 +			razor_atomic_set_error_str(atomic, buffer,
   1.775 +						   strerror(errno));
   1.776 +			return -1;
   1.777 +		}
   1.778 +	}
   1.779 +
   1.780 +	return 0;
   1.781 +}
   1.782 +
   1.783 +RAZOR_EXPORT int
   1.784 +razor_atomic_remove(struct razor_atomic *atomic, const char *path)
   1.785 +{
   1.786 +#ifdef MSWIN_API
   1.787 +	wchar_t *buf;
   1.788 +	DWORD err;
   1.789 +#endif
   1.790 +
   1.791 +	if (atomic->error_str)
   1.792 +		return -1;
   1.793 +
   1.794 +#ifdef MSWIN_API
   1.795 +	buf = razor_utf8_to_utf16(path, -1);
   1.796 +
   1.797 +	if (!DeleteFileW(buf)) {
   1.798 +		err = GetLastError();
   1.799 +		if (err != ERROR_FILE_NOT_FOUND &&
   1.800 +		    err != ERROR_PATH_NOT_FOUND &&
   1.801 +		    !(SetFileAttributesW(buf, FILE_ATTRIBUTE_NORMAL) &&
   1.802 +		      DeleteFileW(buf)) &&
   1.803 +		    !RemoveDirectoryW(buf) &&
   1.804 +		    GetLastError() != ERROR_DIR_NOT_EMPTY)
   1.805 +			razor_atomic_set_error_mswin(atomic, buf, err);
   1.806 +	}
   1.807 +
   1.808 +	free(buf);
   1.809 +#else
   1.810 +	if (remove(path))
   1.811 +		razor_atomic_set_error_str(atomic, path, strerror(errno));
   1.812 +#endif
   1.813 +
   1.814 +	return !!atomic->error_str;
   1.815 +}
   1.816 +
   1.817 +RAZOR_EXPORT int
   1.818 +razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
   1.819 +			 const char *newpath)
   1.820 +{
   1.821 +#ifdef MSWIN_API
   1.822 +	wchar_t *oldbuf, *newbuf;
   1.823 +	const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
   1.824 +#endif
   1.825 +
   1.826 +	if (atomic->error_str)
   1.827 +		return -1;
   1.828 +
   1.829 +#ifdef MSWIN_API
   1.830 +	newbuf = razor_utf8_to_utf16(newpath, -1);
   1.831 +	oldbuf = razor_utf8_to_utf16(oldpath, -1);
   1.832 +
   1.833 +	/*
   1.834 +	 * Passing MOVEFILE_REPLACE_EXISTING to MoveFileEx() will
   1.835 +	 * cover every case we care about _except_ replacing an empty
   1.836 +	 * directory with a file. Calling RemoveDirectory() will deal
   1.837 +	 * with this case while having no effect in all other cases.
   1.838 +	 */
   1.839 +	(void)RemoveDirectoryW(newbuf);
   1.840 +
   1.841 +	if (!MoveFileExW(oldbuf, newbuf, flags))
   1.842 +		razor_atomic_set_error_mswin(atomic, newbuf, GetLastError());
   1.843 +
   1.844 +	free(newbuf);
   1.845 +	free(oldbuf);
   1.846 +#else
   1.847 +	if (rename(oldpath, newpath))
   1.848 +		razor_atomic_set_error_str(atomic, newpath, strerror(errno));
   1.849 +#endif
   1.850 +
   1.851 +	return !!atomic->error_str;
   1.852 +}
   1.853 +
   1.854 +RAZOR_EXPORT int
   1.855 +razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
   1.856 +			mode_t mode)
   1.857 +{
   1.858 +	if (atomic->error_str)
   1.859 +		return -1;
   1.860 +
   1.861 +	if (!mkdir(dirname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)))
   1.862 +		return 0;
   1.863 +
   1.864 +	if (errno != EEXIST) {
   1.865 +		razor_atomic_set_error_str(atomic, dirname, strerror(errno));
   1.866 +		return -1;
   1.867 +	}
   1.868 +
   1.869 +	if (chmod(dirname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
   1.870 +		razor_atomic_set_error_str(atomic, dirname, strerror(errno));
   1.871 +		return -1;
   1.872 +	}
   1.873 +
   1.874 +	return 0;
   1.875 +}
   1.876 +
   1.877 +RAZOR_EXPORT int
   1.878 +razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
   1.879 +			    const char *path)
   1.880 +{
   1.881 +	if (atomic->error_str)
   1.882 +		return -1;
   1.883 +
   1.884 +#if HAVE_SYMLINK
   1.885 +	if (symlink(target, path) < 0) {
   1.886 +		razor_atomic_set_error_str(atomic, NULL, strerror(errno));
   1.887 +		return -1;
   1.888 +	}
   1.889 +#else
   1.890 +	razor_atomic_set_error_str(atomic, NULL, "Symbolic links not supported "
   1.891 +						 "on this platform");
   1.892 +#endif
   1.893 +
   1.894 +	return 0;
   1.895 +}
   1.896 +
   1.897 +RAZOR_EXPORT int
   1.898 +razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
   1.899 +			 mode_t mode)
   1.900 +{
   1.901 +	int fd;
   1.902 +
   1.903 +	if (atomic->error_str)
   1.904 +		return -1;
   1.905 +
   1.906 +	atomic->error_path = strdup(filename);
   1.907 +	fd = open(atomic->error_path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
   1.908 +		  mode & (S_IRWXU | S_IRWXG | S_IRWXO));
   1.909 +
   1.910 +	if (fd == -1)
   1.911 +		razor_atomic_set_error_str(atomic, NULL, strerror(errno));
   1.912 +
   1.913 +	return fd;
   1.914 +}
   1.915 +
   1.916 +RAZOR_EXPORT int
   1.917 +razor_atomic_write(struct razor_atomic *atomic, int fd, const void *data,
   1.918 +		   size_t size)
   1.919 +{
   1.920 +	int written;
   1.921 +
   1.922 +	if (atomic->error_str)
   1.923 +		return -1;
   1.924 +
   1.925 +	while(size) {
   1.926 +		written = write(fd, data, size);
   1.927 +		if (written < 0) {
   1.928 +			razor_atomic_set_error_str(atomic, NULL, strerror(errno));
   1.929 +
   1.930 +			(void)close(fd);
   1.931 +
   1.932 +			return -1;
   1.933 +		}
   1.934 +
   1.935 +		data += written;
   1.936 +		size -= written;
   1.937 +	}
   1.938 +
   1.939 +	return 0;
   1.940 +}
   1.941 +
   1.942 +RAZOR_EXPORT int
   1.943 +razor_atomic_sync(struct razor_atomic *atomic, int handle)
   1.944 +{
   1.945 +	if (atomic->error_str)
   1.946 +		return -1;
   1.947 +
   1.948 +	if (fsync(handle) < 0) {
   1.949 +		razor_atomic_set_error_str(atomic, NULL, strerror(errno));
   1.950 +		return -1;
   1.951 +	}
   1.952 +
   1.953 +	free(atomic->error_path);
   1.954 +	atomic->error_path = NULL;
   1.955 +
   1.956 +	return 0;
   1.957 +}
   1.958 +
   1.959 +RAZOR_EXPORT int
   1.960 +razor_atomic_close(struct razor_atomic *atomic, int fd)
   1.961 +{
   1.962 +	if (atomic->error_str)
   1.963 +		return -1;
   1.964 +
   1.965 +	if (close(fd) < 0) {
   1.966 +		razor_atomic_set_error_str(atomic, NULL, strerror(errno));
   1.967 +		return -1;
   1.968 +	}
   1.969 +
   1.970 +	free(atomic->error_path);
   1.971 +	atomic->error_path = NULL;
   1.972 +
   1.973 +	return 0;
   1.974 +}
   1.975 +
   1.976 +#endif		/* HAVE_WINDOWS_KVM */
   1.977 +
   1.978 +RAZOR_EXPORT const char *
   1.979 +razor_atomic_get_error_msg(struct razor_atomic *atomic)
   1.980 +{
   1.981 +	if (!atomic->error_msg) {
   1.982 +		if (atomic->error_path)
   1.983 +			atomic->error_msg = razor_concat(atomic->error_path,
   1.984 +							 ": ",
   1.985 +							 atomic->error_str,
   1.986 +							 NULL);
   1.987 +		else
   1.988 +			atomic->error_msg = strdup(atomic->error_str);
   1.989 +	}
   1.990 +
   1.991 +	return atomic->error_msg;
   1.992 +}
   1.993 +
   1.994 +RAZOR_EXPORT void
   1.995 +razor_atomic_abort(struct razor_atomic *atomic, const char *error_msg)
   1.996 +{
   1.997 +	if (!atomic->error_str)
   1.998 +		razor_atomic_set_error_str(atomic, NULL, error_msg);
   1.999 +}
  1.1000 +
  1.1001 +RAZOR_EXPORT int
  1.1002 +razor_atomic_in_error_state(struct razor_atomic *atomic)
  1.1003 +{
  1.1004 +	return !!atomic->error_str;
  1.1005 +}