librazor/atomic-emulate.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Oct 17 10:10:57 2014 +0100 (2014-10-17)
changeset 458 3f841a46eab5
parent 449 f3baf790a815
child 461 e1b95d57dd54
permissions -rw-r--r--
Fix multiple memory allocation problems (found with valgrind)
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@444
   159
				break;
ali@444
   160
			} else if (errno != ENOENT)
ali@444
   161
				break;
ali@444
   162
ali@444
   163
			t = strrchr(p, '/');
ali@444
   164
			*s = '/';
ali@444
   165
			s = t;
ali@444
   166
		}
ali@444
   167
ali@444
   168
		if (!s)
ali@444
   169
			result = realpath(".", NULL);
ali@444
   170
ali@444
   171
		free(p);
ali@444
   172
	}
ali@444
   173
ali@444
   174
	return result;
ali@444
   175
}
ali@444
   176
#endif
ali@444
   177
ali@435
   178
/*
ali@435
   179
 * We need a toplevel directory in which to hold temporary files
ali@435
   180
 * before they are committed. Since we can generally assume that
ali@444
   181
 * we have write permissions anywhere on the filesystem in
ali@444
   182
 * question, the best location is at the relevant mount point.
ali@444
   183
 * The most common case where this assumption fails is when
ali@444
   184
 * testing, when the current directory is a good choice.
ali@435
   185
 */
ali@435
   186
ali@435
   187
static int
ali@435
   188
razor_atomic_set_toplevel_from_path(struct razor_atomic *atomic,
ali@435
   189
				    const char *path)
ali@435
   190
{
ali@444
   191
#ifndef MSWIN_API
ali@444
   192
	dev_t filesystem;
ali@444
   193
	struct stat buf;
ali@444
   194
#endif
ali@444
   195
ali@435
   196
	if (razor_atomic_in_error_state(atomic))
ali@435
   197
		return -1;
ali@435
   198
ali@435
   199
	if (atomic->toplevel)
ali@435
   200
		return 0;
ali@435
   201
ali@435
   202
#ifdef MSWIN_API
ali@435
   203
	if (path[0]=='\\' && path[1]=='\\' && path[2] && path[2]!='\\'
ali@435
   204
	    && strchr(path+3,'\\')) {
ali@435
   205
		/* We have a UNC path: \\servername\sharename... */
ali@435
   206
		const char *sharename, *root;
ali@435
   207
		int disklen;
ali@435
   208
ali@435
   209
		sharename = strchr(path+3,'\\')+1;
ali@435
   210
		root = strchr(sharename,'\\');
ali@435
   211
		if (root)
ali@435
   212
		    disklen = root - path;
ali@435
   213
		else
ali@435
   214
		    disklen = strlen(path);
ali@435
   215
ali@435
   216
		atomic->toplevel =
ali@435
   217
		  malloc(disklen + strlen("\\atomic-XXXXXX") + 1);
ali@435
   218
		memcpy(atomic->toplevel, path, disklen);
ali@435
   219
		strcpy(atomic->toplevel + disklen, "\\atomic-XXXXXX");
ali@435
   220
	} else if ((*path>='A' && *path<='Z' || *path>='a' && *path<='z') &&
ali@435
   221
		    path[1]==':') {
ali@435
   222
		atomic->toplevel = strdup("X:\\atomic-XXXXXX");
ali@435
   223
		*atomic->toplevel = *path;
ali@435
   224
	} else {
ali@435
   225
		DWORD n;
ali@435
   226
		wchar_t *buf;
ali@435
   227
		char *dir;
ali@435
   228
ali@435
   229
		n = GetCurrentDirectoryW(0, NULL);
ali@435
   230
		buf = malloc(n * sizeof(wchar_t));
ali@435
   231
ali@435
   232
		if (GetCurrentDirectoryW(n, buf)) {
ali@435
   233
			dir = razor_utf16_to_utf8(buf, n - 1);
ali@435
   234
			razor_atomic_set_toplevel_from_path(atomic, dir);
ali@435
   235
ali@435
   236
			free(dir);
ali@435
   237
			free(buf);
ali@435
   238
			return;
ali@435
   239
		} else
ali@435
   240
			atomic->toplevel = strdup("C:\\atomic-XXXXXX");
ali@435
   241
ali@435
   242
		free(buf);
ali@435
   243
	}
ali@435
   244
#else
ali@444
   245
	{
ali@444
   246
		/*
ali@444
   247
		 * Find the mount point (assuming we can write to the
ali@444
   248
		 * whole filesystem). Otherwise stop at the first
ali@444
   249
		 * unwritable directory and take one step back.
ali@444
   250
		 */
ali@444
   251
		char *s, *abspath, saved;
ali@449
   252
		int len, can_step_back = 0;
ali@444
   253
ali@444
   254
		abspath = absolute_path(path);
ali@444
   255
		if (!abspath) {
ali@447
   256
			atomic->error = razor_error_new_posix(path);
ali@444
   257
			return -1;
ali@444
   258
		}
ali@444
   259
ali@444
   260
		if (stat(abspath, &buf) < 0) {
ali@449
   261
			if (errno == ENOENT)
ali@449
   262
				filesystem = 0;
ali@449
   263
			else {
ali@449
   264
				atomic->error = razor_error_new_posix(abspath);
ali@449
   265
				free(abspath);
ali@449
   266
				return -1;
ali@449
   267
			}
ali@449
   268
		} else
ali@449
   269
			filesystem = buf.st_dev;
ali@444
   270
ali@444
   271
		len = strlen(abspath);
ali@444
   272
		while(len > 1 && (s = strrchr(abspath, '/'))) {
ali@444
   273
			if (s == abspath) {
ali@444
   274
				saved = s[1];
ali@444
   275
				s[1] = '\0';
ali@444
   276
				len = s + 1 - abspath;
ali@444
   277
			} else {
ali@444
   278
				s[0] = '\0';
ali@444
   279
				len = s - abspath;
ali@444
   280
			}
ali@444
   281
ali@444
   282
			if (stat(abspath, &buf) < 0) {
ali@449
   283
				if (errno == ENOENT)
ali@449
   284
					continue;
ali@449
   285
				else {
ali@449
   286
				    atomic->error = razor_error_new_posix(abspath);
ali@449
   287
				    free(abspath);
ali@449
   288
				    return -1;
ali@449
   289
				}
ali@449
   290
			} else if (!filesystem)
ali@449
   291
				filesystem = buf.st_dev;
ali@444
   292
ali@444
   293
			if (buf.st_dev != filesystem || access(abspath, W_OK)) {
ali@449
   294
				if (can_step_back) {
ali@449
   295
					if (s == abspath)
ali@449
   296
						s[1] = saved;
ali@449
   297
					else
ali@449
   298
						s[0] = '/';
ali@449
   299
				}
ali@444
   300
				len = strlen(abspath);
ali@444
   301
				break;
ali@449
   302
			} else
ali@449
   303
				can_step_back = 1;
ali@444
   304
		}
ali@444
   305
ali@444
   306
		if (len == 1)
ali@444
   307
			len = 0;	/* Avoid an unslightly double slash. */
ali@444
   308
		atomic->toplevel = malloc(len + strlen("/.atomic-XXXXXX") + 1);
ali@444
   309
		memcpy(atomic->toplevel, abspath, len);
ali@444
   310
		strcpy(atomic->toplevel + len, "/.atomic-XXXXXX");
ali@444
   311
ali@444
   312
		free(abspath);
ali@444
   313
	}
ali@435
   314
#endif
ali@435
   315
ali@435
   316
	if (!mkdtemp(atomic->toplevel)) {
ali@435
   317
		int err = errno;
ali@435
   318
ali@435
   319
#ifdef EACCES
ali@435
   320
		if (err == EACCES) {
ali@435
   321
			char *s = strdup("atomic-XXXXXX");
ali@435
   322
ali@444
   323
#ifndef MSWIN_API
ali@444
   324
			if (stat(".", &buf) < 0) {
ali@447
   325
				atomic->error = razor_error_new_posix(".");
ali@444
   326
				free(s);
ali@444
   327
				free(atomic->toplevel);
ali@444
   328
				atomic->toplevel = NULL;
ali@444
   329
				return -1;
ali@444
   330
			}
ali@444
   331
			if (buf.st_dev != filesystem)
ali@444
   332
				/*
ali@444
   333
				 * Don't use a different filesystem. It will
ali@444
   334
				 * only fail later on (in rename) and cause
ali@444
   335
				 * an unhelpful error message (EXDEV).
ali@444
   336
				 */
ali@444
   337
				free(s);
ali@444
   338
			else
ali@444
   339
#endif
ali@435
   340
			if (mkdtemp(s)) {
ali@435
   341
				free(atomic->toplevel);
ali@435
   342
				atomic->toplevel = s;
ali@435
   343
				return 0;
ali@435
   344
			} else
ali@435
   345
				free(s);
ali@435
   346
		}
ali@435
   347
#endif
ali@435
   348
ali@447
   349
		atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, err,
ali@447
   350
						    atomic->toplevel,
ali@435
   351
						    strerror(err));
ali@435
   352
ali@435
   353
		free(atomic->toplevel);
ali@435
   354
		atomic->toplevel = NULL;
ali@435
   355
	}
ali@435
   356
ali@435
   357
	return !atomic->toplevel;
ali@435
   358
}
ali@435
   359
ali@416
   360
RAZOR_EXPORT int
ali@416
   361
razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
ali@416
   362
		       const char *path)
ali@416
   363
{
ali@416
   364
	struct atomic_action *a;
ali@416
   365
ali@435
   366
	razor_atomic_set_toplevel_from_path(atomic, *root ? root : path);
ali@435
   367
ali@416
   368
	if (razor_atomic_in_error_state(atomic))
ali@416
   369
		return -1;
ali@416
   370
ali@416
   371
	a = atomic_action_new(ACTION_MAKE_DIRS);
ali@416
   372
	a->args.path = strdup(path);
ali@416
   373
	a->args.u.make_dirs.root = strdup(root);
ali@416
   374
	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
ali@416
   375
ali@416
   376
	return 0;
ali@416
   377
}
ali@416
   378
ali@416
   379
RAZOR_EXPORT int
ali@416
   380
razor_atomic_remove(struct razor_atomic *atomic, const char *path)
ali@416
   381
{
ali@416
   382
	struct atomic_action *a;
ali@416
   383
ali@435
   384
	razor_atomic_set_toplevel_from_path(atomic, path);
ali@435
   385
ali@416
   386
	if (razor_atomic_in_error_state(atomic))
ali@416
   387
		return -1;
ali@416
   388
ali@416
   389
	a = atomic_action_new(ACTION_REMOVE);
ali@416
   390
	a->args.path = strdup(path);
ali@416
   391
	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
ali@416
   392
ali@416
   393
	return 0;
ali@416
   394
}
ali@416
   395
ali@416
   396
RAZOR_EXPORT int
ali@416
   397
razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
ali@416
   398
			 const char *newpath)
ali@416
   399
{
ali@416
   400
	struct atomic_action *a;
ali@416
   401
ali@435
   402
	razor_atomic_set_toplevel_from_path(atomic, newpath);
ali@435
   403
ali@416
   404
	if (razor_atomic_in_error_state(atomic))
ali@416
   405
		return -1;
ali@416
   406
ali@416
   407
	a = atomic_action_new(ACTION_MOVE);
ali@416
   408
	a->args.path = strdup(oldpath);
ali@416
   409
	a->args.u.move.dest = strdup(newpath);
ali@416
   410
	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
ali@416
   411
ali@416
   412
	return 0;
ali@416
   413
}
ali@416
   414
ali@416
   415
RAZOR_EXPORT int
ali@416
   416
razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
ali@416
   417
			mode_t mode)
ali@416
   418
{
ali@416
   419
	struct atomic_action *a;
ali@416
   420
ali@435
   421
	razor_atomic_set_toplevel_from_path(atomic, dirname);
ali@435
   422
ali@416
   423
	if (razor_atomic_in_error_state(atomic))
ali@416
   424
		return -1;
ali@416
   425
ali@416
   426
	a = atomic_action_new(ACTION_CREATE_DIR);
ali@416
   427
	a->args.path = strdup(dirname);
ali@416
   428
	a->args.u.create_dir.mode = mode;
ali@416
   429
	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
ali@416
   430
ali@416
   431
	return 0;
ali@416
   432
}
ali@416
   433
ali@416
   434
RAZOR_EXPORT int
ali@416
   435
razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
ali@416
   436
			    const char *path)
ali@416
   437
{
ali@416
   438
#if HAVE_SYMLINK
ali@416
   439
	struct atomic_action *a;
ali@435
   440
ali@435
   441
	razor_atomic_set_toplevel_from_path(atomic, path);
ali@416
   442
#endif
ali@416
   443
ali@416
   444
	if (razor_atomic_in_error_state(atomic))
ali@416
   445
		return -1;
ali@416
   446
ali@416
   447
#if HAVE_SYMLINK
ali@416
   448
	a = atomic_action_new(ACTION_CREATE_SYMLINK);
ali@416
   449
	a->args.path = strdup(path);
ali@416
   450
	a->args.u.create_symlink.target = strdup(target);
ali@416
   451
	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
ali@416
   452
ali@416
   453
	return 0;
ali@416
   454
#else
ali@447
   455
	atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, ENOSYS, NULL,
ali@423
   456
					    "Symbolic links not supported "
ali@423
   457
					    "on this platform");
ali@416
   458
ali@416
   459
	return -1;
ali@416
   460
#endif
ali@416
   461
}
ali@416
   462
ali@416
   463
RAZOR_EXPORT int
ali@416
   464
razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
ali@416
   465
                         mode_t mode)
ali@416
   466
{
ali@416
   467
	int fd;
ali@416
   468
	struct atomic_action *a;
ali@416
   469
	char *tmpnam;
ali@416
   470
ali@435
   471
	razor_atomic_set_toplevel_from_path(atomic, filename);
ali@435
   472
ali@416
   473
	if (razor_atomic_in_error_state(atomic))
ali@416
   474
		return -1;
ali@416
   475
ali@416
   476
	tmpnam = atomic_action_attic_tmpnam(atomic);
ali@416
   477
	fd = open(tmpnam, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
ali@416
   478
		  mode & (S_IRWXU | S_IRWXG | S_IRWXO));
ali@416
   479
ali@416
   480
	if (fd == -1)
ali@447
   481
		atomic->error = razor_error_new_posix(filename);
ali@416
   482
	else {
ali@416
   483
		a = atomic_action_new(ACTION_MOVE);
ali@416
   484
		a->args.path = tmpnam;
ali@416
   485
		a->args.u.move.dest = strdup(filename);
ali@416
   486
		atomic->actions = atomic_action_list_prepend(atomic->actions,
ali@416
   487
							     a);
ali@416
   488
	}
ali@416
   489
ali@416
   490
	return fd;
ali@416
   491
}
ali@416
   492
ali@416
   493
#endif	/* ENABLE_ATOMIC && !HAVE_WINDOWS_KTM */