librazor/root.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Feb 16 17:33:47 2012 +0000 (2012-02-16)
changeset 424 8cbc438cc298
parent 406 5ab137def3d1
child 425 0c8bdd8dc942
permissions -rw-r--r--
Allow multiple atomic transactions to be used with one root object.
This allows transactions that include barriers to be performed
while holding an exclusive system lock.
richard@300
     1
/*
richard@300
     2
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
richard@300
     3
 * Copyright (C) 2008  Red Hat, Inc
ali@424
     4
 * Copyright (C) 2009, 2011, 2012  J. Ali Harlow <ali@juiblex.co.uk>
richard@300
     5
 *
richard@300
     6
 * This program is free software; you can redistribute it and/or modify
richard@300
     7
 * it under the terms of the GNU General Public License as published by
richard@300
     8
 * the Free Software Foundation; either version 2 of the License, or
richard@300
     9
 * (at your option) any later version.
richard@300
    10
 *
richard@300
    11
 * This program is distributed in the hope that it will be useful,
richard@300
    12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
richard@300
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
richard@300
    14
 * GNU General Public License for more details.
richard@300
    15
 *
richard@300
    16
 * You should have received a copy of the GNU General Public License along
richard@300
    17
 * with this program; if not, write to the Free Software Foundation, Inc.,
richard@300
    18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
richard@300
    19
 */
richard@300
    20
ali@340
    21
#include "config.h"
ali@340
    22
krh@248
    23
#include <stdlib.h>
krh@248
    24
#include <stdint.h>
krh@248
    25
#include <stdio.h>
krh@317
    26
#include <string.h>
krh@248
    27
#include <sys/stat.h>
krh@248
    28
#include <dirent.h>
krh@248
    29
#include <unistd.h>
krh@248
    30
#include <fcntl.h>
ali@325
    31
#include <limits.h>
richard@301
    32
#include <assert.h>
ali@340
    33
#ifdef MSWIN_API
ali@340
    34
#include <shlobj.h>
ali@340
    35
#endif
richard@301
    36
krh@248
    37
#include "razor.h"
krh@248
    38
#include "razor-internal.h"
krh@248
    39
ali@345
    40
#ifndef O_BINARY
ali@345
    41
#define O_BINARY	0
ali@345
    42
#endif
ali@345
    43
richard@310
    44
static const char system_repo_filename[] = "system.rzdb";
ali@388
    45
/*
ali@388
    46
 * system_lock_filename is chosen to be the same as the pre v0.3
ali@388
    47
 * next_repo_filename. This means that once a system has been
ali@388
    48
 * updated by a v0.3+ copy of razor all pre v0.3 versions of razor
ali@388
    49
 * will see the system as permenantly locked.
ali@388
    50
 */
ali@388
    51
static const char system_lock_filename[] = "system-next.rzdb";
ali@340
    52
#ifdef MSWIN_API
ali@340
    53
#define RAZOR_ROOT_PATH	NULL
ali@340
    54
#else
ali@340
    55
#define RAZOR_ROOT_PATH	"/var/lib/razor"
ali@340
    56
#endif
ali@340
    57
static const char *razor_root_path = RAZOR_ROOT_PATH;
krh@248
    58
krh@248
    59
struct razor_root {
krh@248
    60
	struct razor_set *system;
ali@424
    61
	char *path;
krh@248
    62
};
krh@248
    63
ali@340
    64
static void
ali@340
    65
razor_root_init(void)
ali@340
    66
{
ali@340
    67
#ifdef MSWIN_API
ali@340
    68
	static char root_path[MAX_PATH];
ali@340
    69
	if (!razor_root_path) {
ali@340
    70
		SHGetFolderPath(NULL,
ali@340
    71
			CSIDL_COMMON_APPDATA | CSIDL_FLAG_DONT_VERIFY, NULL, 0,
ali@340
    72
			root_path);
ali@340
    73
		strcat(root_path, "\\Razor");
ali@340
    74
		razor_root_path = root_path;
ali@340
    75
	}
ali@340
    76
#endif
ali@340
    77
}
ali@340
    78
krh@269
    79
RAZOR_EXPORT int
krh@248
    80
razor_root_create(const char *root)
krh@248
    81
{
ali@403
    82
	int retval;
krh@248
    83
	struct stat buf;
krh@248
    84
	struct razor_set *set;
ali@403
    85
	struct razor_atomic *atomic;
ali@406
    86
	char *file, *path;
krh@248
    87
richard@301
    88
	assert (root != NULL);
richard@301
    89
ali@340
    90
	razor_root_init();
krh@317
    91
	if (root[0] == '\0') {
krh@317
    92
		/* root is file system root */
krh@317
    93
	} else if (stat(root, &buf) < 0) {
krh@248
    94
		if (mkdir(root, 0777) < 0) {
krh@248
    95
			fprintf(stderr,
krh@248
    96
				"could not create install root \"%s\"\n",
krh@248
    97
				root);
krh@248
    98
			return -1;
krh@248
    99
		}
krh@248
   100
		fprintf(stderr, "created install root \"%s\"\n", root);
krh@248
   101
	} else if (!S_ISDIR(buf.st_mode)) {
krh@248
   102
		fprintf(stderr,
krh@248
   103
			"install root \"%s\" exists, but is not a directory\n",
krh@248
   104
			root);
krh@248
   105
		return -1;
krh@248
   106
	}
krh@248
   107
ali@406
   108
	file = razor_concat(razor_root_path, "/", system_repo_filename, NULL);
ali@406
   109
	path = razor_concat(root, file, NULL);
ali@403
   110
	retval = !stat(path, &buf);
ali@403
   111
	if (retval) {
ali@403
   112
		fprintf(stderr,
ali@403
   113
			"a razor install root is already initialized\n");
ali@406
   114
		free(path);
ali@406
   115
		free(file);
ali@403
   116
		return retval;
krh@248
   117
	}
krh@248
   118
ali@403
   119
	atomic = razor_atomic_open("Create initial package set");
ali@406
   120
	razor_atomic_make_dirs(atomic, root, file);
krh@248
   121
	set = razor_set_create();
ali@403
   122
	razor_set_write(set, atomic, path, RAZOR_SECTION_ALL);
ali@403
   123
	free(path);
ali@406
   124
	free(file);
ali@403
   125
	retval = razor_atomic_commit(atomic);
ali@403
   126
	if (retval)
krh@248
   127
		fprintf(stderr, "could not write initial package set\n");
ali@403
   128
	razor_set_unref(set);
ali@403
   129
	razor_atomic_destroy(atomic);
krh@248
   130
ali@403
   131
	return retval;
krh@248
   132
}
krh@248
   133
krh@269
   134
RAZOR_EXPORT struct razor_root *
ali@424
   135
razor_root_open(const char *root, struct razor_error **error)
krh@248
   136
{
krh@248
   137
	struct razor_root *image;
ali@403
   138
	char *lock_path;
ali@403
   139
	int r;
krh@248
   140
richard@301
   141
	assert (root != NULL);
richard@301
   142
ali@340
   143
	razor_root_init();
krh@248
   144
	image = malloc(sizeof *image);
ali@403
   145
	if (image == NULL) {
ali@424
   146
		razor_set_error(error, NULL, "Not enough memory");
krh@248
   147
		return NULL;
ali@403
   148
	}
ali@403
   149
ali@388
   150
	image->system = razor_set_create_without_root();
ali@388
   151
	if (image->system == NULL) {
ali@388
   152
		free(image);
ali@424
   153
		razor_set_error(error, NULL, "Not enough memory");
ali@388
   154
		return NULL;
ali@388
   155
	}
ali@388
   156
ali@403
   157
	lock_path = razor_concat(root, razor_root_path, "/",
ali@403
   158
				 system_lock_filename, NULL);
ali@388
   159
ali@403
   160
	r = razor_set_aquire_lock(image->system, lock_path, 1);
ali@403
   161
ali@403
   162
	free(lock_path);
ali@403
   163
ali@403
   164
	if (r < 0) {
ali@424
   165
		razor_set_error(error, NULL,
ali@424
   166
				"Failed to aquire exclusive system lock");
ali@403
   167
		razor_set_unref(image->system);
krh@248
   168
		free(image);
krh@248
   169
		return NULL;
krh@248
   170
	}
krh@248
   171
ali@403
   172
	image->path = razor_concat(root, razor_root_path, "/",
ali@403
   173
				   system_repo_filename, NULL);
krh@317
   174
ali@424
   175
	if (razor_set_bind_sections(image->system, image->path,
ali@424
   176
				    RAZOR_SET_PRIVATE, error)) {
ali@403
   177
		free(image->path);
ali@403
   178
		razor_set_unref(image->system);
krh@248
   179
		free(image);
krh@248
   180
		return NULL;
krh@248
   181
	}
krh@248
   182
krh@248
   183
	return image;
krh@248
   184
}
krh@248
   185
krh@269
   186
RAZOR_EXPORT struct razor_set *
ali@424
   187
razor_root_open_read_only(const char *root, struct razor_error **error)
krh@248
   188
{
ali@403
   189
	char *path;
ali@388
   190
	struct razor_set *set;
krh@248
   191
richard@301
   192
	assert (root != NULL);
richard@301
   193
ali@340
   194
	razor_root_init();
ali@388
   195
	set = razor_set_create_without_root();
ali@403
   196
	if (set == NULL) {
ali@424
   197
		razor_set_error(error, NULL, "Not enough memory");
ali@388
   198
		return NULL;
ali@388
   199
	}
ali@388
   200
ali@403
   201
	path = razor_concat(root, razor_root_path, "/", system_lock_filename,
ali@403
   202
			    NULL);
ali@403
   203
	if (razor_set_aquire_lock(set, path, 0) < 0) {
ali@424
   204
		razor_set_error(error, NULL,
ali@424
   205
				"Failed to aquire non-exclusive system lock");
ali@403
   206
		free(path);
ali@403
   207
		razor_set_unref(set);
ali@388
   208
		return NULL;
ali@388
   209
	}
ali@388
   210
ali@403
   211
	free(path);
ali@403
   212
	path = razor_concat(root, razor_root_path, "/", system_repo_filename,
ali@403
   213
			    NULL);
ali@403
   214
ali@424
   215
	if (razor_set_bind_sections(set, path, 0, error)) {
ali@403
   216
		razor_set_unref(set);
ali@403
   217
		set = NULL;
ali@403
   218
	}
ali@403
   219
ali@403
   220
	free(path);
ali@403
   221
ali@388
   222
	return set;
krh@248
   223
}
krh@248
   224
krh@269
   225
RAZOR_EXPORT struct razor_set *
krh@250
   226
razor_root_get_system_set(struct razor_root *root)
krh@248
   227
{
richard@301
   228
	assert (root != NULL);
richard@301
   229
krh@250
   230
	return root->system;
krh@248
   231
}
krh@248
   232
krh@269
   233
RAZOR_EXPORT int
krh@250
   234
razor_root_close(struct razor_root *root)
krh@248
   235
{
richard@301
   236
	assert (root != NULL);
richard@301
   237
ali@403
   238
	razor_set_unref(root->system);
ali@403
   239
	free(root->path);
krh@250
   240
	free(root);
krh@248
   241
krh@248
   242
	return 0;
krh@248
   243
}
krh@248
   244
ali@424
   245
RAZOR_EXPORT int
ali@424
   246
razor_root_update(struct razor_root *root, struct razor_set *next,
ali@424
   247
		  struct razor_atomic *atomic)
krh@248
   248
{
ali@424
   249
	int handle, retval;
ali@424
   250
richard@301
   251
	assert (root != NULL);
richard@301
   252
	assert (next != NULL);
richard@301
   253
ali@424
   254
	handle = razor_atomic_create_file(atomic, root->path,
ali@424
   255
					  S_IRWXU | S_IRWXG | S_IRWXO);
ali@424
   256
	if (handle < 0)
ali@424
   257
		return handle;
krh@248
   258
ali@424
   259
	razor_set_write_to_handle(next, atomic, handle, RAZOR_SECTION_ALL);
krh@248
   260
ali@424
   261
	retval = razor_atomic_close(atomic, handle);
richard@301
   262
ali@424
   263
	if (!retval) {
ali@424
   264
		razor_set_unref(root->system);
ali@424
   265
		root->system = razor_set_ref(next);
ali@424
   266
	}
krh@248
   267
ali@346
   268
	return retval;
krh@248
   269
}