librazor/root.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Jan 27 07:55:30 2012 +0000 (2012-01-27)
changeset 405 f960eb19dca2
parent 388 6a6462ce8a08
child 406 5ab137def3d1
permissions -rw-r--r--
Start 0.5.1
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@403
     4
 * Copyright (C) 2009, 2011  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@388
    52
static const char system_tmp_filename[] = "system.tmp";
ali@340
    53
#ifdef MSWIN_API
ali@340
    54
#define RAZOR_ROOT_PATH	NULL
ali@340
    55
#else
ali@340
    56
#define RAZOR_ROOT_PATH	"/var/lib/razor"
ali@340
    57
#endif
ali@340
    58
static const char *razor_root_path = RAZOR_ROOT_PATH;
krh@248
    59
krh@248
    60
struct razor_root {
krh@248
    61
	struct razor_set *system;
krh@248
    62
	struct razor_set *next;
ali@403
    63
	struct razor_atomic *atomic;
ali@403
    64
	int handle;
ali@403
    65
	char *path, *new_path;
krh@248
    66
};
krh@248
    67
ali@340
    68
static void
ali@340
    69
razor_root_init(void)
ali@340
    70
{
ali@340
    71
#ifdef MSWIN_API
ali@340
    72
	static char root_path[MAX_PATH];
ali@340
    73
	if (!razor_root_path) {
ali@340
    74
		SHGetFolderPath(NULL,
ali@340
    75
			CSIDL_COMMON_APPDATA | CSIDL_FLAG_DONT_VERIFY, NULL, 0,
ali@340
    76
			root_path);
ali@340
    77
		strcat(root_path, "\\Razor");
ali@340
    78
		razor_root_path = root_path;
ali@340
    79
	}
ali@340
    80
#endif
ali@340
    81
}
ali@340
    82
krh@269
    83
RAZOR_EXPORT int
krh@248
    84
razor_root_create(const char *root)
krh@248
    85
{
ali@403
    86
	int retval;
krh@248
    87
	struct stat buf;
krh@248
    88
	struct razor_set *set;
ali@403
    89
	struct razor_atomic *atomic;
ali@403
    90
	char *path;
krh@248
    91
richard@301
    92
	assert (root != NULL);
richard@301
    93
ali@340
    94
	razor_root_init();
krh@317
    95
	if (root[0] == '\0') {
krh@317
    96
		/* root is file system root */
krh@317
    97
	} else if (stat(root, &buf) < 0) {
krh@248
    98
		if (mkdir(root, 0777) < 0) {
krh@248
    99
			fprintf(stderr,
krh@248
   100
				"could not create install root \"%s\"\n",
krh@248
   101
				root);
krh@248
   102
			return -1;
krh@248
   103
		}
krh@248
   104
		fprintf(stderr, "created install root \"%s\"\n", root);
krh@248
   105
	} else if (!S_ISDIR(buf.st_mode)) {
krh@248
   106
		fprintf(stderr,
krh@248
   107
			"install root \"%s\" exists, but is not a directory\n",
krh@248
   108
			root);
krh@248
   109
		return -1;
krh@248
   110
	}
krh@248
   111
ali@403
   112
	path = razor_concat(root, razor_root_path, "/", system_repo_filename,
ali@403
   113
			    NULL);
ali@403
   114
	retval = !stat(path, &buf);
ali@403
   115
	free(path);
ali@403
   116
	if (retval) {
ali@403
   117
		fprintf(stderr,
ali@403
   118
			"a razor install root is already initialized\n");
ali@403
   119
		return retval;
krh@248
   120
	}
krh@248
   121
ali@403
   122
	atomic = razor_atomic_open("Create initial package set");
ali@403
   123
	path = razor_concat(razor_root_path, "/", system_repo_filename, NULL);
ali@403
   124
	razor_atomic_make_dirs(atomic, root, path);
krh@248
   125
	set = razor_set_create();
ali@403
   126
	razor_set_write(set, atomic, path, RAZOR_SECTION_ALL);
ali@403
   127
	free(path);
ali@403
   128
	retval = razor_atomic_commit(atomic);
ali@403
   129
	if (retval)
krh@248
   130
		fprintf(stderr, "could not write initial package set\n");
ali@403
   131
	razor_set_unref(set);
ali@403
   132
	razor_atomic_destroy(atomic);
krh@248
   133
ali@403
   134
	return retval;
krh@248
   135
}
krh@248
   136
krh@269
   137
RAZOR_EXPORT struct razor_root *
ali@403
   138
razor_root_open(const char *root, struct razor_atomic *atomic)
krh@248
   139
{
krh@248
   140
	struct razor_root *image;
ali@403
   141
	char *lock_path;
ali@403
   142
	int r;
krh@248
   143
richard@301
   144
	assert (root != NULL);
richard@301
   145
ali@340
   146
	razor_root_init();
krh@248
   147
	image = malloc(sizeof *image);
ali@403
   148
	if (image == NULL) {
ali@403
   149
		razor_atomic_abort(atomic, "Not enough memory");
krh@248
   150
		return NULL;
ali@403
   151
	}
ali@403
   152
ali@403
   153
	image->atomic = atomic;
krh@248
   154
ali@388
   155
	image->system = razor_set_create_without_root();
ali@388
   156
	if (image->system == NULL) {
ali@388
   157
		free(image);
ali@403
   158
		razor_atomic_abort(atomic, "Not enough memory");
ali@388
   159
		return NULL;
ali@388
   160
	}
ali@388
   161
ali@403
   162
	lock_path = razor_concat(root, razor_root_path, "/",
ali@403
   163
				 system_lock_filename, NULL);
ali@388
   164
ali@403
   165
	r = razor_set_aquire_lock(image->system, lock_path, 1);
ali@403
   166
ali@403
   167
	free(lock_path);
ali@403
   168
ali@403
   169
	if (r < 0) {
ali@403
   170
		razor_atomic_abort(atomic,
ali@403
   171
				   "Failed to aquire exclusive system lock");
ali@403
   172
		razor_set_unref(image->system);
ali@388
   173
		free(image);
ali@388
   174
		return NULL;
ali@388
   175
	}
ali@388
   176
ali@403
   177
	image->new_path = razor_concat(root, razor_root_path, "/",
ali@403
   178
				       system_tmp_filename, NULL);
ali@403
   179
	image->handle = razor_atomic_create_file(atomic, image->new_path,
ali@403
   180
						 S_IRWXU | S_IRWXG | S_IRWXO);
ali@403
   181
	if (image->handle < 0) {
ali@403
   182
		free(image->new_path);
ali@403
   183
		razor_set_unref(image->system);
krh@248
   184
		free(image);
krh@248
   185
		return NULL;
krh@248
   186
	}
krh@248
   187
ali@403
   188
	image->path = razor_concat(root, razor_root_path, "/",
ali@403
   189
				   system_repo_filename, NULL);
krh@317
   190
ali@403
   191
	if (razor_set_bind_sections(image->system, atomic, image->path)) {
krh@248
   192
		unlink(image->new_path);
ali@403
   193
		free(image->new_path);
ali@403
   194
		free(image->path);
ali@403
   195
		razor_set_unref(image->system);
krh@248
   196
		free(image);
krh@248
   197
		return NULL;
krh@248
   198
	}
krh@248
   199
krh@248
   200
	return image;
krh@248
   201
}
krh@248
   202
krh@269
   203
RAZOR_EXPORT struct razor_set *
ali@403
   204
razor_root_open_read_only(const char *root, struct razor_atomic *atomic)
krh@248
   205
{
ali@403
   206
	char *path;
ali@388
   207
	struct razor_set *set;
krh@248
   208
richard@301
   209
	assert (root != NULL);
richard@301
   210
ali@340
   211
	razor_root_init();
ali@388
   212
	set = razor_set_create_without_root();
ali@403
   213
	if (set == NULL) {
ali@403
   214
		razor_atomic_abort(atomic, "Not enough memory");
ali@388
   215
		return NULL;
ali@388
   216
	}
ali@388
   217
ali@403
   218
	path = razor_concat(root, razor_root_path, "/", system_lock_filename,
ali@403
   219
			    NULL);
ali@403
   220
	if (razor_set_aquire_lock(set, path, 0) < 0) {
ali@403
   221
		razor_atomic_abort(atomic, "Failed to aquire non-exclusive "
ali@403
   222
					   "system lock");
ali@403
   223
		free(path);
ali@403
   224
		razor_set_unref(set);
ali@388
   225
		return NULL;
ali@388
   226
	}
ali@388
   227
ali@403
   228
	free(path);
ali@403
   229
	path = razor_concat(root, razor_root_path, "/", system_repo_filename,
ali@403
   230
			    NULL);
ali@403
   231
ali@403
   232
	if (razor_set_bind_sections(set, atomic, path)) {
ali@403
   233
		razor_set_unref(set);
ali@403
   234
		set = NULL;
ali@403
   235
	}
ali@403
   236
ali@403
   237
	free(path);
ali@403
   238
ali@388
   239
	return set;
krh@248
   240
}
krh@248
   241
krh@269
   242
RAZOR_EXPORT struct razor_set *
krh@250
   243
razor_root_get_system_set(struct razor_root *root)
krh@248
   244
{
richard@301
   245
	assert (root != NULL);
richard@301
   246
krh@250
   247
	return root->system;
krh@248
   248
}
krh@248
   249
krh@269
   250
RAZOR_EXPORT int
krh@250
   251
razor_root_close(struct razor_root *root)
krh@248
   252
{
richard@301
   253
	assert (root != NULL);
richard@301
   254
ali@403
   255
	razor_set_unref(root->system);
ali@403
   256
	razor_atomic_close(root->atomic, root->handle);
ali@403
   257
	razor_atomic_remove(root->atomic, root->new_path);
ali@403
   258
	free(root->path);
ali@403
   259
	free(root->new_path);
krh@250
   260
	free(root);
krh@248
   261
krh@248
   262
	return 0;
krh@248
   263
}
krh@248
   264
krh@269
   265
RAZOR_EXPORT void
krh@248
   266
razor_root_update(struct razor_root *root, struct razor_set *next)
krh@248
   267
{
richard@301
   268
	assert (root != NULL);
richard@301
   269
	assert (next != NULL);
richard@301
   270
ali@340
   271
	razor_root_init();
ali@403
   272
	razor_set_write_to_handle(next, root->atomic, root->handle,
ali@403
   273
				  RAZOR_SECTION_ALL);
krh@248
   274
	root->next = next;
krh@248
   275
krh@248
   276
	/* Sync the new repo file so the new package set is on disk
krh@248
   277
	 * before we start upgrading. */
ali@403
   278
	razor_atomic_sync(root->atomic, root->handle);
krh@248
   279
}
krh@248
   280
krh@269
   281
RAZOR_EXPORT int
krh@250
   282
razor_root_commit(struct razor_root *root)
krh@248
   283
{
ali@346
   284
	int retval;
richard@301
   285
	assert (root != NULL);
richard@301
   286
ali@403
   287
	razor_atomic_close(root->atomic, root->handle);
ali@403
   288
	retval = razor_atomic_rename_file(root->atomic, root->new_path,
ali@403
   289
					  root->path);
ali@403
   290
	razor_set_unref(root->system);
ali@403
   291
	free(root->path);
ali@403
   292
	free(root->new_path);
krh@250
   293
	free(root);
krh@248
   294
ali@346
   295
	return retval;
krh@248
   296
}