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