librazor/root.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Jan 08 15:18:01 2009 +0000 (2009-01-08)
changeset 330 2af301268803
parent 317 019a53b65271
child 340 5962a461a5a3
permissions -rw-r--r--
Only include sys/wait.h on platforms that have it.
richard@300
     1
/*
richard@300
     2
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
richard@300
     3
 * Copyright (C) 2008  Red Hat, Inc
richard@300
     4
 *
richard@300
     5
 * This program is free software; you can redistribute it and/or modify
richard@300
     6
 * it under the terms of the GNU General Public License as published by
richard@300
     7
 * the Free Software Foundation; either version 2 of the License, or
richard@300
     8
 * (at your option) any later version.
richard@300
     9
 *
richard@300
    10
 * This program is distributed in the hope that it will be useful,
richard@300
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
richard@300
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
richard@300
    13
 * GNU General Public License for more details.
richard@300
    14
 *
richard@300
    15
 * You should have received a copy of the GNU General Public License along
richard@300
    16
 * with this program; if not, write to the Free Software Foundation, Inc.,
richard@300
    17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
richard@300
    18
 */
richard@300
    19
krh@248
    20
#include <stdlib.h>
krh@248
    21
#include <stdint.h>
krh@248
    22
#include <stdio.h>
krh@317
    23
#include <string.h>
krh@248
    24
#include <sys/stat.h>
krh@248
    25
#include <dirent.h>
krh@248
    26
#include <unistd.h>
krh@248
    27
#include <fcntl.h>
ali@325
    28
#include <limits.h>
richard@301
    29
#include <assert.h>
richard@301
    30
krh@248
    31
#include "razor.h"
krh@248
    32
#include "razor-internal.h"
krh@248
    33
richard@310
    34
static const char system_repo_filename[] = "system.rzdb";
richard@310
    35
static const char system_repo_details_filename[] = "system-details.rzdb";
richard@310
    36
static const char system_repo_files_filename[] = "system-files.rzdb";
jbowes@273
    37
richard@310
    38
static const char next_repo_filename[] = "system-next.rzdb";
krh@248
    39
static const char razor_root_path[] = "/var/lib/razor";
krh@248
    40
krh@248
    41
struct razor_root {
krh@248
    42
	struct razor_set *system;
krh@248
    43
	struct razor_set *next;
krh@248
    44
	int fd;
krh@317
    45
	char root[PATH_MAX];
krh@248
    46
	char path[PATH_MAX];
krh@248
    47
	char new_path[PATH_MAX];
krh@248
    48
};
krh@248
    49
krh@269
    50
RAZOR_EXPORT int
krh@248
    51
razor_root_create(const char *root)
krh@248
    52
{
krh@248
    53
	struct stat buf;
krh@248
    54
	struct razor_set *set;
jbowes@273
    55
	char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
krh@248
    56
richard@301
    57
	assert (root != NULL);
richard@301
    58
krh@317
    59
	if (root[0] == '\0') {
krh@317
    60
		/* root is file system root */
krh@317
    61
	} else if (stat(root, &buf) < 0) {
krh@248
    62
		if (mkdir(root, 0777) < 0) {
krh@248
    63
			fprintf(stderr,
krh@248
    64
				"could not create install root \"%s\"\n",
krh@248
    65
				root);
krh@248
    66
			return -1;
krh@248
    67
		}
krh@248
    68
		fprintf(stderr, "created install root \"%s\"\n", root);
krh@248
    69
	} else if (!S_ISDIR(buf.st_mode)) {
krh@248
    70
		fprintf(stderr,
krh@248
    71
			"install root \"%s\" exists, but is not a directory\n",
krh@248
    72
			root);
krh@248
    73
		return -1;
krh@248
    74
	}
krh@248
    75
krh@248
    76
	snprintf(path, sizeof path, "%s/%s",
krh@248
    77
		 razor_root_path, system_repo_filename);
krh@248
    78
	if (razor_create_dir(root, path) < 0) {
krh@248
    79
		fprintf(stderr, "could not create %s%s\n",
krh@248
    80
			root, razor_root_path);
krh@248
    81
		return -1;
krh@248
    82
	}
krh@248
    83
krh@248
    84
	set = razor_set_create();
krh@248
    85
	snprintf(path, sizeof path, "%s%s/%s",
krh@248
    86
		 root, razor_root_path, system_repo_filename);
jbowes@273
    87
	snprintf(details_path, sizeof details_path, "%s%s/%s",
jbowes@273
    88
		 root, razor_root_path, system_repo_details_filename);
jbowes@273
    89
	snprintf(files_path, sizeof files_path, "%s%s/%s",
jbowes@273
    90
		 root, razor_root_path, system_repo_files_filename);
krh@248
    91
	if (stat(path, &buf) == 0) {
krh@248
    92
		fprintf(stderr,
krh@248
    93
			"a razor install root is already initialized\n");
krh@248
    94
		return -1;
krh@248
    95
	}
jbowes@273
    96
	if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
jbowes@273
    97
	    razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
jbowes@273
    98
	    razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
krh@248
    99
		fprintf(stderr, "could not write initial package set\n");
krh@248
   100
		return -1;
krh@248
   101
	}
krh@248
   102
	razor_set_destroy(set);
krh@248
   103
krh@248
   104
	return 0;
krh@248
   105
}
krh@248
   106
krh@269
   107
RAZOR_EXPORT struct razor_root *
krh@250
   108
razor_root_open(const char *root)
krh@248
   109
{
krh@248
   110
	struct razor_root *image;
krh@315
   111
	char details_path[PATH_MAX], files_path[PATH_MAX];
krh@248
   112
richard@301
   113
	assert (root != NULL);
richard@301
   114
krh@248
   115
	image = malloc(sizeof *image);
krh@248
   116
	if (image == NULL)
krh@248
   117
		return NULL;
krh@248
   118
krh@248
   119
	/* Create the new next repo file up front to ensure exclusive
krh@248
   120
	 * access. */
krh@248
   121
	snprintf(image->new_path, sizeof image->new_path,
krh@248
   122
		 "%s%s/%s", root, razor_root_path, next_repo_filename);
krh@248
   123
	image->fd = open(image->new_path,
krh@248
   124
			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
krh@248
   125
	if (image->fd < 0) {
krh@248
   126
		fprintf(stderr, "failed to get lock file, "
krh@248
   127
			"maybe previous operation crashed?\n");
krh@248
   128
krh@248
   129
		/* FIXME: Use fcntl advisory locking on the system
krh@248
   130
		 * package set file to figure out whether previous
krh@248
   131
		 * operation crashed or is still in progress. */
krh@248
   132
krh@248
   133
		free(image);
krh@248
   134
		return NULL;
krh@248
   135
	}
krh@248
   136
krh@248
   137
	snprintf(image->path, sizeof image->path,
krh@248
   138
		 "%s%s/%s", root, razor_root_path, system_repo_filename);
krh@315
   139
	snprintf(details_path, sizeof details_path,
krh@315
   140
		 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
krh@315
   141
	snprintf(files_path, sizeof files_path,
krh@315
   142
		 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
krh@315
   143
krh@317
   144
	/* FIXME: We store the root path to make the hack in
krh@317
   145
	 * razor_root_update() work.  Need to get rid of this. */
krh@317
   146
	strcpy(image->root, root);
krh@317
   147
krh@248
   148
	image->system = razor_set_open(image->path);
krh@315
   149
	if (image->system == NULL ||
krh@315
   150
	    razor_set_open_details(image->system, details_path) ||
krh@315
   151
	    razor_set_open_files(image->system, files_path)) {
krh@248
   152
		unlink(image->new_path);
krh@248
   153
		close(image->fd);
krh@248
   154
		free(image);
krh@248
   155
		return NULL;
krh@248
   156
	}
krh@248
   157
krh@248
   158
	return image;
krh@248
   159
}
krh@248
   160
krh@269
   161
RAZOR_EXPORT struct razor_set *
krh@248
   162
razor_root_open_read_only(const char *root)
krh@248
   163
{
krh@317
   164
	char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
krh@317
   165
	struct razor_set *set;
krh@248
   166
richard@301
   167
	assert (root != NULL);
richard@301
   168
krh@248
   169
	snprintf(path, sizeof path, "%s%s/%s",
krh@248
   170
		 root, razor_root_path, system_repo_filename);
krh@317
   171
	snprintf(details_path, sizeof details_path,
krh@317
   172
		 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
krh@317
   173
	snprintf(files_path, sizeof files_path,
krh@317
   174
		 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
krh@248
   175
krh@317
   176
krh@317
   177
	set = razor_set_open(path);
krh@317
   178
	if (set == NULL)
krh@317
   179
		return NULL;
krh@317
   180
krh@317
   181
	if (razor_set_open_details(set, details_path) ||
krh@317
   182
	    razor_set_open_files(set, files_path)) {
krh@317
   183
		razor_set_destroy(set);
krh@317
   184
		return NULL;
krh@317
   185
	}
krh@317
   186
krh@317
   187
	return set;
krh@248
   188
}
krh@248
   189
krh@269
   190
RAZOR_EXPORT struct razor_set *
krh@250
   191
razor_root_get_system_set(struct razor_root *root)
krh@248
   192
{
richard@301
   193
	assert (root != NULL);
richard@301
   194
krh@250
   195
	return root->system;
krh@248
   196
}
krh@248
   197
krh@269
   198
RAZOR_EXPORT int
krh@250
   199
razor_root_close(struct razor_root *root)
krh@248
   200
{
richard@301
   201
	assert (root != NULL);
richard@301
   202
krh@250
   203
	razor_set_destroy(root->system);
krh@250
   204
	unlink(root->new_path);
krh@250
   205
	close(root->fd);
krh@250
   206
	free(root);
krh@248
   207
krh@248
   208
	return 0;
krh@248
   209
}
krh@248
   210
krh@269
   211
RAZOR_EXPORT void
krh@248
   212
razor_root_update(struct razor_root *root, struct razor_set *next)
krh@248
   213
{
krh@317
   214
	char path[PATH_MAX];
krh@317
   215
richard@301
   216
	assert (root != NULL);
richard@301
   217
	assert (next != NULL);
richard@301
   218
jbowes@258
   219
	razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
krh@248
   220
	root->next = next;
krh@248
   221
krh@317
   222
	/* FIXME: This is a pretty bad hack that just overwrites the
krh@317
   223
	 * system details and files rzdb files before the transaction
krh@317
   224
	 * succeeds.  We need to fix this by merging the separate
krh@317
   225
	 * details and files rzdb files back into the main rzdb
krh@317
   226
	 * file. */
krh@317
   227
	snprintf(path, sizeof path,
krh@317
   228
		 "%s%s/%s", root->root, razor_root_path, system_repo_details_filename);
krh@317
   229
	razor_set_write(next, path, RAZOR_REPO_FILE_DETAILS);
krh@317
   230
	snprintf(path, sizeof path,
krh@317
   231
		 "%s%s/%s", root->root, razor_root_path, system_repo_files_filename);
krh@317
   232
	razor_set_write(next, path, RAZOR_REPO_FILE_FILES);
krh@317
   233
krh@248
   234
	/* Sync the new repo file so the new package set is on disk
krh@248
   235
	 * before we start upgrading. */
krh@248
   236
	fsync(root->fd);
krh@248
   237
	printf("wrote %s\n", root->new_path);
krh@248
   238
}
krh@248
   239
krh@269
   240
RAZOR_EXPORT int
krh@250
   241
razor_root_commit(struct razor_root *root)
krh@248
   242
{
richard@301
   243
	assert (root != NULL);
richard@301
   244
krh@248
   245
	/* Make it so. */
krh@250
   246
	rename(root->new_path, root->path);
krh@250
   247
	printf("renamed %s to %s\n", root->new_path, root->path);
krh@250
   248
	razor_set_destroy(root->system);
krh@250
   249
	close(root->fd);
krh@250
   250
	free(root);
krh@248
   251
krh@248
   252
	return 0;
krh@248
   253
}