librazor/atomic-emulate.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Feb 17 18:35:10 2012 +0000 (2012-02-17)
changeset 426 2e896ad9754b
parent 416 d0aa9e0a6d04
child 435 275a4428c13b
permissions -rw-r--r--
razor_rpm_open() should take an error parameter, not an atomic
ali@416
     1
/*
ali@416
     2
 * Copyright (C) 2012  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@416
    32
#include "razor-internal.h"
ali@416
    33
ali@416
    34
/*
ali@416
    35
 * Emulated atomic support
ali@416
    36
 *
ali@416
    37
 * This implementation is better than nothing, but is certainly not atomic.
ali@416
    38
 * It does have a couple of advantages over atomic-none:
ali@416
    39
 *	- If a file operation fails while a package is being installed we
ali@416
    40
 *	  have a good chance of being able to rollback the transaction to
ali@416
    41
 *	  a well-known state.
ali@416
    42
 *	- We behave similarly to atomic-ktm in that changes are not visible
ali@416
    43
 *	  on disk to non-atomic operations (eg., scripts) until the atomic
ali@416
    44
 *	  is committed. This makes the testsuite more likely to pick up
ali@416
    45
 *	  problems that would otherwise only be found when using razor on
ali@416
    46
 *	  an MS-Windows system which supports KTM.
ali@416
    47
 */
ali@416
    48
ali@416
    49
#ifndef O_BINARY
ali@416
    50
#define O_BINARY	0
ali@416
    51
#endif
ali@416
    52
ali@416
    53
static void recursive_remove(const char *directory)
ali@416
    54
{
ali@416
    55
	DIR *dp;
ali@416
    56
	struct dirent *dirp;
ali@416
    57
	char *buf;
ali@416
    58
ali@416
    59
	dp = opendir(directory);
ali@416
    60
	while((dirp = readdir(dp))) {
ali@416
    61
		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
ali@416
    62
			buf = malloc(strlen(directory) + strlen(dirp->d_name)
ali@416
    63
				     + 2);
ali@416
    64
			sprintf(buf, "%s/%s", directory, dirp->d_name);
ali@416
    65
			if (remove(buf) < 0)
ali@416
    66
				recursive_remove(buf);
ali@416
    67
			free(buf);
ali@416
    68
		}
ali@416
    69
	}
ali@416
    70
ali@416
    71
	rmdir(directory);
ali@416
    72
}
ali@416
    73
ali@416
    74
RAZOR_EXPORT struct razor_atomic *razor_atomic_open(const char *description)
ali@416
    75
{
ali@416
    76
	struct razor_atomic *atomic;
ali@416
    77
ali@416
    78
	atomic = zalloc(sizeof *atomic);
ali@416
    79
ali@416
    80
	atomic->toplevel = strdup(".atomic-XXXXXX");
ali@416
    81
	if (!mkdtemp(atomic->toplevel)) {
ali@416
    82
		free(atomic->toplevel);
ali@416
    83
		free(atomic);
ali@416
    84
		return NULL;
ali@416
    85
	}
ali@416
    86
ali@416
    87
	atomic->description = strdup(description);
ali@416
    88
ali@416
    89
	return atomic;
ali@416
    90
}
ali@416
    91
ali@416
    92
RAZOR_EXPORT int razor_atomic_commit(struct razor_atomic *atomic)
ali@416
    93
{
ali@416
    94
	struct atomic_action *actions;
ali@416
    95
ali@416
    96
	if (razor_atomic_in_error_state(atomic))
ali@416
    97
		return -1;
ali@416
    98
ali@416
    99
	if (atomic->actions) {
ali@416
   100
		actions = atomic_action_list_reverse(atomic->actions);
ali@416
   101
		atomic->actions = NULL;
ali@416
   102
		actions = atomic_action_do(atomic, actions);
ali@416
   103
		atomic_action_free(actions);
ali@416
   104
	}
ali@416
   105
ali@416
   106
	if (atomic->toplevel) {
ali@416
   107
		recursive_remove(atomic->toplevel);
ali@416
   108
		free(atomic->toplevel);
ali@416
   109
		atomic->toplevel = NULL;
ali@416
   110
	}
ali@416
   111
ali@423
   112
	return razor_atomic_in_error_state(atomic);
ali@416
   113
}
ali@416
   114
ali@416
   115
RAZOR_EXPORT void razor_atomic_destroy(struct razor_atomic *atomic)
ali@416
   116
{
ali@416
   117
	if (atomic->toplevel) {
ali@416
   118
		recursive_remove(atomic->toplevel);
ali@416
   119
		free(atomic->toplevel);
ali@416
   120
		atomic->toplevel = NULL;
ali@416
   121
	}
ali@416
   122
ali@423
   123
	if (atomic->error)
ali@423
   124
		razor_error_free(atomic->error);
ali@423
   125
ali@416
   126
	free(atomic);
ali@416
   127
}
ali@416
   128
ali@416
   129
RAZOR_EXPORT int
ali@416
   130
razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
ali@416
   131
		       const char *path)
ali@416
   132
{
ali@416
   133
	struct atomic_action *a;
ali@416
   134
ali@416
   135
	if (razor_atomic_in_error_state(atomic))
ali@416
   136
		return -1;
ali@416
   137
ali@416
   138
	a = atomic_action_new(ACTION_MAKE_DIRS);
ali@416
   139
	a->args.path = strdup(path);
ali@416
   140
	a->args.u.make_dirs.root = strdup(root);
ali@416
   141
	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
ali@416
   142
ali@416
   143
	return 0;
ali@416
   144
}
ali@416
   145
ali@416
   146
RAZOR_EXPORT int
ali@416
   147
razor_atomic_remove(struct razor_atomic *atomic, const char *path)
ali@416
   148
{
ali@416
   149
	struct atomic_action *a;
ali@416
   150
ali@416
   151
	if (razor_atomic_in_error_state(atomic))
ali@416
   152
		return -1;
ali@416
   153
ali@416
   154
	a = atomic_action_new(ACTION_REMOVE);
ali@416
   155
	a->args.path = strdup(path);
ali@416
   156
	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
ali@416
   157
ali@416
   158
	return 0;
ali@416
   159
}
ali@416
   160
ali@416
   161
RAZOR_EXPORT int
ali@416
   162
razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
ali@416
   163
			 const char *newpath)
ali@416
   164
{
ali@416
   165
	struct atomic_action *a;
ali@416
   166
ali@416
   167
	if (razor_atomic_in_error_state(atomic))
ali@416
   168
		return -1;
ali@416
   169
ali@416
   170
	a = atomic_action_new(ACTION_MOVE);
ali@416
   171
	a->args.path = strdup(oldpath);
ali@416
   172
	a->args.u.move.dest = strdup(newpath);
ali@416
   173
	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
ali@416
   174
ali@416
   175
	return 0;
ali@416
   176
}
ali@416
   177
ali@416
   178
RAZOR_EXPORT int
ali@416
   179
razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
ali@416
   180
			mode_t mode)
ali@416
   181
{
ali@416
   182
	struct atomic_action *a;
ali@416
   183
ali@416
   184
	if (razor_atomic_in_error_state(atomic))
ali@416
   185
		return -1;
ali@416
   186
ali@416
   187
	a = atomic_action_new(ACTION_CREATE_DIR);
ali@416
   188
	a->args.path = strdup(dirname);
ali@416
   189
	a->args.u.create_dir.mode = mode;
ali@416
   190
	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
ali@416
   191
ali@416
   192
	return 0;
ali@416
   193
}
ali@416
   194
ali@416
   195
RAZOR_EXPORT int
ali@416
   196
razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
ali@416
   197
			    const char *path)
ali@416
   198
{
ali@416
   199
#if HAVE_SYMLINK
ali@416
   200
	struct atomic_action *a;
ali@416
   201
#endif
ali@416
   202
ali@416
   203
	if (razor_atomic_in_error_state(atomic))
ali@416
   204
		return -1;
ali@416
   205
ali@416
   206
#if HAVE_SYMLINK
ali@416
   207
	a = atomic_action_new(ACTION_CREATE_SYMLINK);
ali@416
   208
	a->args.path = strdup(path);
ali@416
   209
	a->args.u.create_symlink.target = strdup(target);
ali@416
   210
	atomic->actions = atomic_action_list_prepend(atomic->actions, a);
ali@416
   211
ali@416
   212
	return 0;
ali@416
   213
#else
ali@423
   214
	atomic->error = razor_error_new_str(NULL,
ali@423
   215
					    "Symbolic links not supported "
ali@423
   216
					    "on this platform");
ali@416
   217
ali@416
   218
	return -1;
ali@416
   219
#endif
ali@416
   220
}
ali@416
   221
ali@416
   222
RAZOR_EXPORT int
ali@416
   223
razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
ali@416
   224
                         mode_t mode)
ali@416
   225
{
ali@416
   226
	int fd;
ali@416
   227
	struct atomic_action *a;
ali@416
   228
	char *tmpnam;
ali@416
   229
ali@416
   230
	if (razor_atomic_in_error_state(atomic))
ali@416
   231
		return -1;
ali@416
   232
ali@416
   233
	tmpnam = atomic_action_attic_tmpnam(atomic);
ali@416
   234
	fd = open(tmpnam, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
ali@416
   235
		  mode & (S_IRWXU | S_IRWXG | S_IRWXO));
ali@416
   236
ali@416
   237
	if (fd == -1)
ali@423
   238
		atomic->error = razor_error_new_str(filename, strerror(errno));
ali@416
   239
	else {
ali@416
   240
		a = atomic_action_new(ACTION_MOVE);
ali@416
   241
		a->args.path = tmpnam;
ali@416
   242
		a->args.u.move.dest = strdup(filename);
ali@416
   243
		atomic->actions = atomic_action_list_prepend(atomic->actions,
ali@416
   244
							     a);
ali@416
   245
	}
ali@416
   246
ali@416
   247
	return fd;
ali@416
   248
}
ali@416
   249
ali@416
   250
#endif	/* ENABLE_ATOMIC && !HAVE_WINDOWS_KTM */