librazor/atomic-ktm.c
author J. Ali Harlow <ali@juiblex.co.uk>
Tue Jul 05 09:40:01 2016 +0100 (2016-07-05)
changeset 477 6ba4c0f3c9d1
parent 447 0a5e583393e1
child 478 8e4bf84a7bb8
permissions -rw-r--r--
Add public URI resolver
     1 /*
     2  * Copyright (C) 2011, 2012, 2014  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 
    19 #include "config.h"
    20 
    21 #if HAVE_WINDOWS_KTM
    22 
    23 #include <stdlib.h>
    24 #include <windows.h>
    25 #include <stdio.h>
    26 #include <limits.h>
    27 #include <unistd.h>
    28 #include <errno.h>
    29 #include <string.h>
    30 #include <assert.h>
    31 #include <wchar.h>
    32 #include <ktmw32.h>
    33 
    34 #include "razor.h"
    35 #include "razor-internal.h"
    36 
    37 static int
    38 razor_valid_root_name2(const wchar_t *name)
    39 {
    40 	if (razor_allow_all_root_names())
    41 		return !wcschr(name, '/');
    42 
    43 	return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
    44 	       name[2] == '\0';
    45 }
    46 
    47 struct razor_wstr {
    48 	wchar_t *str;
    49 	int len, allocated;
    50 };
    51 
    52 static struct razor_wstr *
    53 razor_wstr_create(const char *init, int len)
    54 {
    55 	int n;
    56 	struct razor_wstr *wstr;
    57 
    58 	wstr = malloc(sizeof(struct razor_wstr));
    59 
    60 	n = MultiByteToWideChar(CP_UTF8, 0, init, len, NULL, 0);
    61 	if (len >= 0 && init[len])
    62 		wstr->len = n++;
    63 	else
    64 		wstr->len = n - 1;
    65 
    66 	wstr->allocated = n * 2;
    67 	wstr->str = malloc(wstr->allocated * sizeof(wchar_t));
    68 	if (!wstr->str) {
    69 		free(wstr);
    70 		return NULL;
    71 	}
    72 
    73 	(void)MultiByteToWideChar(CP_UTF8, 0, init, len, wstr->str, n);
    74 	if (len >= 0 && init[len])
    75 		wstr->str[wstr->len] = 0;
    76 
    77 	return wstr;
    78 }
    79 
    80 static int
    81 razor_wstr_append(struct razor_wstr *wstr, const char *s, int len)
    82 {
    83 	int n, allocated;
    84 	wchar_t *str;
    85 
    86 	n = MultiByteToWideChar(CP_UTF8, 0, s, len, NULL, 0);
    87 	if (len < 0 || !s[len])
    88 		n--;
    89 
    90 	if (wstr->allocated <= wstr->len + n) {
    91 		allocated = (wstr->len + n + 1) * 2;
    92 		str = realloc(wstr->str, allocated * sizeof(wchar_t));
    93 		if (!str)
    94 			return -1;
    95 		wstr->allocated = allocated;
    96 		wstr->str = str;
    97 	}
    98 
    99 	(void)MultiByteToWideChar(CP_UTF8, 0, s, len, wstr->str + wstr->len, n);
   100 	wstr->len += n;
   101 	wstr->str[wstr->len] = 0;
   102 
   103 	return 0;
   104 }
   105 
   106 static void
   107 razor_wstr_destroy(struct razor_wstr *wstr)
   108 {
   109 	free(wstr->str);
   110 	free(wstr);
   111 }
   112 
   113 RAZOR_EXPORT struct razor_atomic *
   114 razor_atomic_open(const char *description)
   115 {
   116 	wchar_t *buf;
   117 	struct razor_atomic *atomic;
   118 
   119 	atomic = zalloc(sizeof *atomic);
   120 	buf = razor_utf8_to_utf16(description, -1);
   121 	atomic->transaction = CreateTransaction(NULL, 0,
   122 						TRANSACTION_DO_NOT_PROMOTE,
   123 						0, 0, 0, buf);
   124 	free(buf);
   125 
   126 	return atomic;
   127 }
   128 
   129 RAZOR_EXPORT int
   130 razor_atomic_commit(struct razor_atomic *atomic)
   131 {
   132 	int retval;
   133 
   134 	if (razor_atomic_in_error_state(atomic))
   135 		return -1;
   136 
   137 	retval = !CommitTransaction(atomic->transaction);
   138 
   139 	if (retval) {
   140 		razor_set_error_mswin(&atomic->error, NULL, GetLastError());
   141 		RollbackTransaction(atomic->transaction);
   142 	}
   143 
   144 	CloseHandle(atomic->transaction);
   145 	atomic->transaction = INVALID_HANDLE_VALUE;
   146 
   147 	return retval;
   148 }
   149 
   150 RAZOR_EXPORT void
   151 razor_atomic_destroy(struct razor_atomic *atomic)
   152 {
   153 	int i;
   154 
   155 	for(i = 0; i < atomic->n_files; i++) {
   156 		if (atomic->files[i].h != INVALID_HANDLE_VALUE) {
   157 			CloseHandle(atomic->files[i].h);
   158 			free(atomic->files[i].path);
   159 		}
   160 	}
   161 	free(atomic->files);
   162 	if (atomic->transaction != INVALID_HANDLE_VALUE) {
   163 		RollbackTransaction(atomic->transaction);
   164 		CloseHandle(atomic->transaction);
   165 	}
   166 	if (atomic->error)
   167 		razor_error_free(atomic->error);
   168 	free(atomic);
   169 }
   170 
   171 RAZOR_EXPORT int
   172 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
   173 		       const char *path)
   174 {
   175 	struct razor_wstr *buffer;
   176 	const char *slash, *next;
   177 	WIN32_FILE_ATTRIBUTE_DATA fa;
   178 	DWORD err;
   179 	int r, creating = 0;
   180 
   181 	if (razor_atomic_in_error_state(atomic))
   182 		return -1;
   183 
   184 	buffer = razor_wstr_create(root, -1);
   185 	slash = buffer->len ? SKIP_DRIVE_LETTER(path) : path;
   186 
   187 	for (; *slash != '\0'; slash = next) {
   188 		next = strpbrk(slash + 1, "/\\");
   189 		if (next == NULL)
   190 			break;
   191 
   192 		razor_wstr_append(buffer, slash, next - slash);
   193 
   194 		if (!creating) {
   195 			if (razor_valid_root_name2(buffer->str))
   196 				continue;
   197 
   198 			r = GetFileAttributesTransactedW(buffer->str,
   199 							 GetFileExInfoStandard,
   200 							 &fa,
   201 							 atomic->transaction);
   202 
   203 			if (!r) {
   204 				err = GetLastError();
   205 				if (err == ERROR_FILE_NOT_FOUND) {
   206 					creating = 1;
   207 				} else {
   208 					razor_set_error_mswin(&atomic->error,
   209 							      buffer->str, err);
   210 					razor_wstr_destroy(buffer);
   211 					return -1;
   212 				}
   213 			} else if (!(fa.dwFileAttributes&
   214 				     FILE_ATTRIBUTE_DIRECTORY)) {
   215 				razor_set_error2(&atomic->error,
   216 						 RAZOR_MSWIN_ERROR,
   217 						 ERROR_DIRECTORY, buffer->str,
   218 						 "Not a directory");
   219 				razor_wstr_destroy(buffer);
   220 				return -1;
   221 			}
   222 		}
   223 		if (creating) {
   224 			if (!CreateDirectoryTransactedW(NULL, buffer->str, NULL,
   225 							atomic->transaction)) {
   226 				razor_set_error_mswin(&atomic->error,
   227 						      buffer->str,
   228 						      GetLastError());
   229 				razor_wstr_destroy(buffer);
   230 				return -1;
   231 			}
   232 
   233 			/* FIXME: What to do about permissions for dirs we
   234 			 * have to create but are not in the cpio archive? */
   235 		}
   236 	}
   237 
   238 	razor_wstr_destroy(buffer);
   239 
   240 	return 0;
   241 }
   242 
   243 RAZOR_EXPORT int
   244 razor_atomic_remove(struct razor_atomic *atomic, const char *path)
   245 {
   246 	wchar_t *buf;
   247 	DWORD err;
   248 
   249 	if (razor_atomic_in_error_state(atomic))
   250 		return -1;
   251 
   252 	buf = razor_utf8_to_utf16(path, -1);
   253 
   254 	if (DeleteFileTransactedW(buf, atomic->transaction)) {
   255 		free(buf);
   256 		return 0;
   257 	}
   258 
   259 	err = GetLastError();
   260 	if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) {
   261 		free(buf);
   262 		return 0;
   263 	}
   264 
   265 	if (SetFileAttributesTransactedW(buf, FILE_ATTRIBUTE_NORMAL,
   266 					 atomic->transaction)) {
   267 		if (DeleteFileTransactedW(buf, atomic->transaction)) {
   268 			free(buf);
   269 			return 0;
   270 		}
   271 		err = GetLastError();
   272 	}
   273 
   274 	if (RemoveDirectoryTransactedW(buf, atomic->transaction) ||
   275 	    GetLastError() == ERROR_DIR_NOT_EMPTY) {
   276 		free(buf);
   277 		return 0;
   278 	}
   279 
   280 	/*
   281 	 * It would be tempting to use:
   282 	 * 	MoveFileEx(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)
   283 	 * but unless we can guarantee that the system will be rebooted
   284 	 * before we (or some other application) write another file with the
   285 	 * same path, this is likely to cause more problems than it solves.
   286 	 */
   287 
   288 	razor_set_error_mswin(&atomic->error, buf, err);
   289 	free(buf);
   290 	return -1;
   291 }
   292 
   293 RAZOR_EXPORT int
   294 razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
   295 			 const char *newpath)
   296 {
   297 	wchar_t *oldbuf, *newbuf;
   298 	const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
   299 
   300 	if (razor_atomic_in_error_state(atomic))
   301 		return -1;
   302 
   303 	newbuf = razor_utf8_to_utf16(newpath, -1);
   304 	oldbuf = razor_utf8_to_utf16(oldpath, -1);
   305 
   306 	/*
   307 	 * Passing MOVEFILE_REPLACE_EXISTING to MoveFileTransaction() will
   308 	 * cover every case we care about _except_ replacing an empty
   309 	 * directory with a file. Calling RemoveDirectoryTransacted() will deal
   310 	 * with this case while having no effect in all other cases.
   311 	 */
   312 	(void)RemoveDirectoryTransactedW(newbuf, atomic->transaction);
   313 
   314 	if (!MoveFileTransactedW(oldbuf, newbuf, NULL, NULL, flags,
   315 			         atomic->transaction))
   316 		razor_set_error_mswin(&atomic->error, newbuf, GetLastError());
   317 
   318 	free(newbuf);
   319 	free(oldbuf);
   320 
   321 	return razor_atomic_in_error_state(atomic);
   322 }
   323 
   324 RAZOR_EXPORT int
   325 razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
   326 			mode_t mode)
   327 {
   328 	wchar_t *buf;
   329 	DWORD err;
   330 	WIN32_FILE_ATTRIBUTE_DATA fa;
   331 
   332 	if (razor_atomic_in_error_state(atomic))
   333 		return -1;
   334 
   335 	buf = razor_utf8_to_utf16(dirname, -1);
   336 
   337 	if (!CreateDirectoryTransactedW(NULL, buf, NULL, atomic->transaction)) {
   338 		err = GetLastError();
   339 		if (err != ERROR_FILE_EXISTS && err != ERROR_ALREADY_EXISTS) {
   340 abort:
   341 			razor_set_error_mswin(&atomic->error, buf, err);
   342 			free(buf);
   343 			return -1;
   344 		}
   345 
   346 		if (!GetFileAttributesTransactedW(buf, GetFileExInfoStandard,
   347 						  &fa, atomic->transaction))
   348 			goto abort;
   349 
   350 		if (!(fa.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) {
   351 			if (razor_atomic_remove(atomic, dirname)) {
   352 				free(buf);
   353 				return -1;
   354 			}
   355 			if (!CreateDirectoryTransactedW(NULL, buf, NULL,
   356 							atomic->transaction)) {
   357 				err = GetLastError();
   358 				goto abort;
   359 			}
   360 		}
   361 	}
   362 
   363 	free(buf);
   364 
   365 	return 0;
   366 }
   367 
   368 RAZOR_EXPORT int
   369 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
   370 			    const char *path)
   371 {
   372 	if (razor_atomic_in_error_state(atomic))
   373 		return -1;
   374 
   375 	/*
   376 	 * This isn't true, but symbolic links under Windows 7
   377 	 * need to know whether the target is a directory or not
   378 	 * and we don't always know that at the time when the
   379 	 * link is created, so it's a convienent lie for now.
   380 	 */
   381 	razor_set_error(&atomic->error, RAZOR_MSWIN_ERROR, ERROR_NOT_SUPPORTED,
   382 			NULL, "Symbolic links not supported on this platform");
   383 
   384 	return -1;
   385 }
   386 
   387 RAZOR_EXPORT int
   388 razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
   389 			 mode_t mode)
   390 {
   391 	DWORD attribs;
   392 	struct razor_atomic_file *files;
   393 	int i = atomic->n_files;
   394 
   395 	if (razor_atomic_in_error_state(atomic))
   396 		return -1;
   397 
   398 	files = realloc(atomic->files,
   399 			(atomic->n_files+1) * sizeof(struct razor_atomic_file));
   400 	if (!files) {
   401 		razor_set_error(&atomic->error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
   402 				"Not enough memory");
   403 		return -1;
   404 	}
   405 	atomic->n_files++;
   406 	atomic->files = files;
   407 
   408 	files[i].path = razor_utf8_to_utf16(filename, -1);
   409 
   410 	/*
   411 	 * Passing CREATE_ALWAYS to CreateFileTransacted() will cover
   412 	 * every case we care about _except_ replacing an empty directory
   413 	 * with a file. Calling RemoveDirectoryTransacted() will deal
   414 	 * with this case while having no effect in all other cases.
   415 	 */
   416 	(void)RemoveDirectoryTransactedW(files[i].path, atomic->transaction);
   417 
   418 	if (mode & S_IWUSR)
   419 		attribs = FILE_ATTRIBUTE_NORMAL;
   420 	else
   421 		attribs = FILE_ATTRIBUTE_READONLY;
   422 
   423 	files[i].h = CreateFileTransactedW(files[i].path, GENERIC_WRITE,
   424 					   0, NULL, CREATE_ALWAYS, attribs,
   425 					   NULL, atomic->transaction, NULL,
   426 					   NULL);
   427 
   428 	if (files[i].h == INVALID_HANDLE_VALUE) {
   429 		razor_set_error_mswin(&atomic->error, files[i].path,
   430 				      GetLastError());
   431 		free(files[i].path);
   432 		atomic->n_files--;
   433 		return -1;
   434 	}
   435 
   436 	return i;
   437 }
   438 
   439 RAZOR_EXPORT int
   440 razor_atomic_write(struct razor_atomic *atomic, int handle, const void *data,
   441 		   size_t size)
   442 {
   443 	DWORD written;
   444 
   445 	if (razor_atomic_in_error_state(atomic))
   446 		return -1;
   447 
   448 	assert(handle < atomic->n_files);
   449 	assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
   450 
   451 	while(size) {
   452 		if (!WriteFile(atomic->files[handle].h, data, size, &written,
   453 			       NULL)) {
   454 			razor_set_error_mswin(&atomic->error,
   455 					      atomic->files[handle].path,
   456 					      GetLastError());
   457 
   458 			(void)CloseHandle(atomic->files[handle].h);
   459 			free(atomic->files[handle].path);
   460 			atomic->files[handle].path = NULL;
   461 			atomic->files[handle].h = INVALID_HANDLE_VALUE;
   462 
   463 			return -1;
   464 		}
   465 
   466 		data += written;
   467 		size -= written;
   468 	}
   469 
   470 	return 0;
   471 }
   472 
   473 RAZOR_EXPORT int
   474 razor_atomic_sync(struct razor_atomic *atomic, int handle)
   475 {
   476 	HANDLE h;
   477 
   478 	if (razor_atomic_in_error_state(atomic))
   479 		return -1;
   480 
   481 	assert(handle < atomic->n_files);
   482 	assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
   483 
   484 	if (!CloseHandle(atomic->files[handle].h)) {
   485 		razor_set_error_mswin(&atomic->error,
   486 				      atomic->files[handle].path,
   487 				      GetLastError());
   488 		free(atomic->files[handle].path);
   489 		atomic->files[handle].path = NULL;
   490 		atomic->files[handle].h = INVALID_HANDLE_VALUE;
   491 		return -1;
   492 	}
   493 
   494 	h = CreateFileTransactedW(atomic->files[handle].path, GENERIC_WRITE, 0,
   495 				  NULL, OPEN_EXISTING, 0, NULL,
   496 				  atomic->transaction, NULL, NULL);
   497 	atomic->files[handle].h = h;
   498 
   499 	if (atomic->files[handle].h == INVALID_HANDLE_VALUE) {
   500 		razor_set_error_mswin(&atomic->error,
   501 				      atomic->files[handle].path,
   502 				      GetLastError());
   503 		free(atomic->files[handle].path);
   504 		atomic->files[handle].path = NULL;
   505 		return -1;
   506 	}
   507 
   508 	return razor_atomic_in_error_state(atomic);
   509 }
   510 
   511 RAZOR_EXPORT int
   512 razor_atomic_close(struct razor_atomic *atomic, int handle)
   513 {
   514 	if (razor_atomic_in_error_state(atomic))
   515 		return -1;
   516 
   517 	assert(handle < atomic->n_files);
   518 	assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
   519 
   520 	if (!CloseHandle(atomic->files[handle].h))
   521 		razor_set_error_mswin(&atomic->error,
   522 				      atomic->files[handle].path,
   523 				      GetLastError());
   524 
   525 	free(atomic->files[handle].path);
   526 	atomic->files[handle].path = NULL;
   527 	atomic->files[handle].h = INVALID_HANDLE_VALUE;
   528 
   529 	while(atomic->n_files > 0 &&
   530 	      atomic->files[atomic->n_files-1].h == INVALID_HANDLE_VALUE)
   531 		atomic->n_files--;
   532 
   533 	return razor_atomic_in_error_state(atomic);
   534 }
   535 
   536 #endif		/* HAVE_WINDOWS_KTM */