librazor/atomic-emulate.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Oct 17 10:13:48 2014 +0100 (2014-10-17)
changeset 459 5576a85e174a
parent 449 f3baf790a815
child 461 e1b95d57dd54
permissions -rw-r--r--
Add missing files
     1 /*
     2  * Copyright (C) 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 ENABLE_ATOMIC && !HAVE_WINDOWS_KTM
    22 
    23 #include <stdlib.h>
    24 #include <stdio.h>
    25 #include <string.h>
    26 #include <unistd.h>
    27 #include <sys/types.h>
    28 #include <sys/stat.h>
    29 #include <fcntl.h>
    30 #include <dirent.h>
    31 #include <errno.h>
    32 #include <unistd.h>
    33 #include "razor-internal.h"
    34 
    35 /*
    36  * Emulated atomic support
    37  *
    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
    42  *	  a well-known state.
    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.
    48  */
    49 
    50 #ifndef O_BINARY
    51 #define O_BINARY	0
    52 #endif
    53 
    54 static void recursive_remove(const char *directory)
    55 {
    56 	DIR *dp;
    57 	struct dirent *dirp;
    58 	char *buf;
    59 
    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)
    64 				     + 2);
    65 			sprintf(buf, "%s/%s", directory, dirp->d_name);
    66 			if (remove(buf) < 0)
    67 				recursive_remove(buf);
    68 			free(buf);
    69 		}
    70 	}
    71 
    72 	closedir(dp);
    73 
    74 	rmdir(directory);
    75 }
    76 
    77 RAZOR_EXPORT struct razor_atomic *razor_atomic_open(const char *description)
    78 {
    79 	struct razor_atomic *atomic;
    80 
    81 	atomic = zalloc(sizeof *atomic);
    82 
    83 	atomic->description = strdup(description);
    84 
    85 	return atomic;
    86 }
    87 
    88 RAZOR_EXPORT int razor_atomic_commit(struct razor_atomic *atomic)
    89 {
    90 	struct atomic_action *actions;
    91 
    92 	if (razor_atomic_in_error_state(atomic))
    93 		return -1;
    94 
    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);
   100 	}
   101 
   102 	if (atomic->toplevel) {
   103 		recursive_remove(atomic->toplevel);
   104 		free(atomic->toplevel);
   105 		atomic->toplevel = NULL;
   106 	}
   107 
   108 	return razor_atomic_in_error_state(atomic);
   109 }
   110 
   111 RAZOR_EXPORT void razor_atomic_destroy(struct razor_atomic *atomic)
   112 {
   113 	if (atomic->actions) {
   114 		atomic_action_free(atomic->actions);
   115 		atomic->actions = NULL;
   116 	}
   117 
   118 	if (atomic->toplevel) {
   119 		recursive_remove(atomic->toplevel);
   120 		free(atomic->toplevel);
   121 		atomic->toplevel = NULL;
   122 	}
   123 
   124 	if (atomic->error)
   125 		razor_error_free(atomic->error);
   126 
   127 	free(atomic->description);
   128 
   129 	free(atomic);
   130 }
   131 
   132 #ifndef MSWIN_API
   133 static char *absolute_path(const char *path)
   134 {
   135 	int len;
   136 	char *result, *subpath, *p, *s, *t;
   137 
   138 	result = realpath(path, NULL);
   139 
   140 	if (!result && errno == ENOENT) {
   141 		p = strdup(path);
   142 		s = strrchr(p, '/');
   143 
   144 		while (s) {
   145 			if (s == p) {
   146 				result = strdup("/");
   147 				break;
   148 			}
   149 
   150 			*s = '\0';
   151 			subpath = realpath(p, NULL);
   152 
   153 			if (subpath) {
   154 				*s = '/';
   155 				len = strlen(subpath);
   156 				result = malloc(len + strlen(s) + 1);
   157 				memcpy(result, subpath, len);
   158 				strcpy(result + len, s);
   159 				break;
   160 			} else if (errno != ENOENT)
   161 				break;
   162 
   163 			t = strrchr(p, '/');
   164 			*s = '/';
   165 			s = t;
   166 		}
   167 
   168 		if (!s)
   169 			result = realpath(".", NULL);
   170 
   171 		free(p);
   172 	}
   173 
   174 	return result;
   175 }
   176 #endif
   177 
   178 /*
   179  * We need a toplevel directory in which to hold temporary files
   180  * before they are committed. Since we can generally assume that
   181  * we have write permissions anywhere on the filesystem in
   182  * question, the best location is at the relevant mount point.
   183  * The most common case where this assumption fails is when
   184  * testing, when the current directory is a good choice.
   185  */
   186 
   187 static int
   188 razor_atomic_set_toplevel_from_path(struct razor_atomic *atomic,
   189 				    const char *path)
   190 {
   191 #ifndef MSWIN_API
   192 	dev_t filesystem;
   193 	struct stat buf;
   194 #endif
   195 
   196 	if (razor_atomic_in_error_state(atomic))
   197 		return -1;
   198 
   199 	if (atomic->toplevel)
   200 		return 0;
   201 
   202 #ifdef MSWIN_API
   203 	if (path[0]=='\\' && path[1]=='\\' && path[2] && path[2]!='\\'
   204 	    && strchr(path+3,'\\')) {
   205 		/* We have a UNC path: \\servername\sharename... */
   206 		const char *sharename, *root;
   207 		int disklen;
   208 
   209 		sharename = strchr(path+3,'\\')+1;
   210 		root = strchr(sharename,'\\');
   211 		if (root)
   212 		    disklen = root - path;
   213 		else
   214 		    disklen = strlen(path);
   215 
   216 		atomic->toplevel =
   217 		  malloc(disklen + strlen("\\atomic-XXXXXX") + 1);
   218 		memcpy(atomic->toplevel, path, disklen);
   219 		strcpy(atomic->toplevel + disklen, "\\atomic-XXXXXX");
   220 	} else if ((*path>='A' && *path<='Z' || *path>='a' && *path<='z') &&
   221 		    path[1]==':') {
   222 		atomic->toplevel = strdup("X:\\atomic-XXXXXX");
   223 		*atomic->toplevel = *path;
   224 	} else {
   225 		DWORD n;
   226 		wchar_t *buf;
   227 		char *dir;
   228 
   229 		n = GetCurrentDirectoryW(0, NULL);
   230 		buf = malloc(n * sizeof(wchar_t));
   231 
   232 		if (GetCurrentDirectoryW(n, buf)) {
   233 			dir = razor_utf16_to_utf8(buf, n - 1);
   234 			razor_atomic_set_toplevel_from_path(atomic, dir);
   235 
   236 			free(dir);
   237 			free(buf);
   238 			return;
   239 		} else
   240 			atomic->toplevel = strdup("C:\\atomic-XXXXXX");
   241 
   242 		free(buf);
   243 	}
   244 #else
   245 	{
   246 		/*
   247 		 * Find the mount point (assuming we can write to the
   248 		 * whole filesystem). Otherwise stop at the first
   249 		 * unwritable directory and take one step back.
   250 		 */
   251 		char *s, *abspath, saved;
   252 		int len, can_step_back = 0;
   253 
   254 		abspath = absolute_path(path);
   255 		if (!abspath) {
   256 			atomic->error = razor_error_new_posix(path);
   257 			return -1;
   258 		}
   259 
   260 		if (stat(abspath, &buf) < 0) {
   261 			if (errno == ENOENT)
   262 				filesystem = 0;
   263 			else {
   264 				atomic->error = razor_error_new_posix(abspath);
   265 				free(abspath);
   266 				return -1;
   267 			}
   268 		} else
   269 			filesystem = buf.st_dev;
   270 
   271 		len = strlen(abspath);
   272 		while(len > 1 && (s = strrchr(abspath, '/'))) {
   273 			if (s == abspath) {
   274 				saved = s[1];
   275 				s[1] = '\0';
   276 				len = s + 1 - abspath;
   277 			} else {
   278 				s[0] = '\0';
   279 				len = s - abspath;
   280 			}
   281 
   282 			if (stat(abspath, &buf) < 0) {
   283 				if (errno == ENOENT)
   284 					continue;
   285 				else {
   286 				    atomic->error = razor_error_new_posix(abspath);
   287 				    free(abspath);
   288 				    return -1;
   289 				}
   290 			} else if (!filesystem)
   291 				filesystem = buf.st_dev;
   292 
   293 			if (buf.st_dev != filesystem || access(abspath, W_OK)) {
   294 				if (can_step_back) {
   295 					if (s == abspath)
   296 						s[1] = saved;
   297 					else
   298 						s[0] = '/';
   299 				}
   300 				len = strlen(abspath);
   301 				break;
   302 			} else
   303 				can_step_back = 1;
   304 		}
   305 
   306 		if (len == 1)
   307 			len = 0;	/* Avoid an unslightly double slash. */
   308 		atomic->toplevel = malloc(len + strlen("/.atomic-XXXXXX") + 1);
   309 		memcpy(atomic->toplevel, abspath, len);
   310 		strcpy(atomic->toplevel + len, "/.atomic-XXXXXX");
   311 
   312 		free(abspath);
   313 	}
   314 #endif
   315 
   316 	if (!mkdtemp(atomic->toplevel)) {
   317 		int err = errno;
   318 
   319 #ifdef EACCES
   320 		if (err == EACCES) {
   321 			char *s = strdup("atomic-XXXXXX");
   322 
   323 #ifndef MSWIN_API
   324 			if (stat(".", &buf) < 0) {
   325 				atomic->error = razor_error_new_posix(".");
   326 				free(s);
   327 				free(atomic->toplevel);
   328 				atomic->toplevel = NULL;
   329 				return -1;
   330 			}
   331 			if (buf.st_dev != filesystem)
   332 				/*
   333 				 * Don't use a different filesystem. It will
   334 				 * only fail later on (in rename) and cause
   335 				 * an unhelpful error message (EXDEV).
   336 				 */
   337 				free(s);
   338 			else
   339 #endif
   340 			if (mkdtemp(s)) {
   341 				free(atomic->toplevel);
   342 				atomic->toplevel = s;
   343 				return 0;
   344 			} else
   345 				free(s);
   346 		}
   347 #endif
   348 
   349 		atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, err,
   350 						    atomic->toplevel,
   351 						    strerror(err));
   352 
   353 		free(atomic->toplevel);
   354 		atomic->toplevel = NULL;
   355 	}
   356 
   357 	return !atomic->toplevel;
   358 }
   359 
   360 RAZOR_EXPORT int
   361 razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
   362 		       const char *path)
   363 {
   364 	struct atomic_action *a;
   365 
   366 	razor_atomic_set_toplevel_from_path(atomic, *root ? root : path);
   367 
   368 	if (razor_atomic_in_error_state(atomic))
   369 		return -1;
   370 
   371 	a = atomic_action_new(ACTION_MAKE_DIRS);
   372 	a->args.path = strdup(path);
   373 	a->args.u.make_dirs.root = strdup(root);
   374 	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
   375 
   376 	return 0;
   377 }
   378 
   379 RAZOR_EXPORT int
   380 razor_atomic_remove(struct razor_atomic *atomic, const char *path)
   381 {
   382 	struct atomic_action *a;
   383 
   384 	razor_atomic_set_toplevel_from_path(atomic, path);
   385 
   386 	if (razor_atomic_in_error_state(atomic))
   387 		return -1;
   388 
   389 	a = atomic_action_new(ACTION_REMOVE);
   390 	a->args.path = strdup(path);
   391 	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
   392 
   393 	return 0;
   394 }
   395 
   396 RAZOR_EXPORT int
   397 razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
   398 			 const char *newpath)
   399 {
   400 	struct atomic_action *a;
   401 
   402 	razor_atomic_set_toplevel_from_path(atomic, newpath);
   403 
   404 	if (razor_atomic_in_error_state(atomic))
   405 		return -1;
   406 
   407 	a = atomic_action_new(ACTION_MOVE);
   408 	a->args.path = strdup(oldpath);
   409 	a->args.u.move.dest = strdup(newpath);
   410 	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
   411 
   412 	return 0;
   413 }
   414 
   415 RAZOR_EXPORT int
   416 razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
   417 			mode_t mode)
   418 {
   419 	struct atomic_action *a;
   420 
   421 	razor_atomic_set_toplevel_from_path(atomic, dirname);
   422 
   423 	if (razor_atomic_in_error_state(atomic))
   424 		return -1;
   425 
   426 	a = atomic_action_new(ACTION_CREATE_DIR);
   427 	a->args.path = strdup(dirname);
   428 	a->args.u.create_dir.mode = mode;
   429 	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
   430 
   431 	return 0;
   432 }
   433 
   434 RAZOR_EXPORT int
   435 razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
   436 			    const char *path)
   437 {
   438 #if HAVE_SYMLINK
   439 	struct atomic_action *a;
   440 
   441 	razor_atomic_set_toplevel_from_path(atomic, path);
   442 #endif
   443 
   444 	if (razor_atomic_in_error_state(atomic))
   445 		return -1;
   446 
   447 #if HAVE_SYMLINK
   448 	a = atomic_action_new(ACTION_CREATE_SYMLINK);
   449 	a->args.path = strdup(path);
   450 	a->args.u.create_symlink.target = strdup(target);
   451 	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
   452 
   453 	return 0;
   454 #else
   455 	atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, ENOSYS, NULL,
   456 					    "Symbolic links not supported "
   457 					    "on this platform");
   458 
   459 	return -1;
   460 #endif
   461 }
   462 
   463 RAZOR_EXPORT int
   464 razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
   465                          mode_t mode)
   466 {
   467 	int fd;
   468 	struct atomic_action *a;
   469 	char *tmpnam;
   470 
   471 	razor_atomic_set_toplevel_from_path(atomic, filename);
   472 
   473 	if (razor_atomic_in_error_state(atomic))
   474 		return -1;
   475 
   476 	tmpnam = atomic_action_attic_tmpnam(atomic);
   477 	fd = open(tmpnam, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
   478 		  mode & (S_IRWXU | S_IRWXG | S_IRWXO));
   479 
   480 	if (fd == -1)
   481 		atomic->error = razor_error_new_posix(filename);
   482 	else {
   483 		a = atomic_action_new(ACTION_MOVE);
   484 		a->args.path = tmpnam;
   485 		a->args.u.move.dest = strdup(filename);
   486 		atomic->actions = atomic_action_list_prepend(atomic->actions,
   487 							     a);
   488 	}
   489 
   490 	return fd;
   491 }
   492 
   493 #endif	/* ENABLE_ATOMIC && !HAVE_WINDOWS_KTM */