librazor/root.c
author Kristian H?gsberg <krh@redhat.com>
Tue Jul 08 22:02:58 2008 -0400 (2008-07-08)
changeset 316 5ebed314390c
parent 310 9a7691262ce6
child 317 019a53b65271
permissions -rw-r--r--
Change the install iterator API to what we want.
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@248
    23
#include <sys/stat.h>
krh@248
    24
#include <dirent.h>
krh@248
    25
#include <unistd.h>
krh@248
    26
#include <fcntl.h>
richard@301
    27
#include <assert.h>
richard@301
    28
krh@248
    29
#include "razor.h"
krh@248
    30
#include "razor-internal.h"
krh@248
    31
richard@310
    32
static const char system_repo_filename[] = "system.rzdb";
richard@310
    33
static const char system_repo_details_filename[] = "system-details.rzdb";
richard@310
    34
static const char system_repo_files_filename[] = "system-files.rzdb";
jbowes@273
    35
richard@310
    36
static const char next_repo_filename[] = "system-next.rzdb";
krh@248
    37
static const char razor_root_path[] = "/var/lib/razor";
krh@248
    38
krh@248
    39
struct razor_root {
krh@248
    40
	struct razor_set *system;
krh@248
    41
	struct razor_set *next;
krh@248
    42
	int fd;
krh@248
    43
	char path[PATH_MAX];
krh@248
    44
	char new_path[PATH_MAX];
krh@248
    45
};
krh@248
    46
krh@269
    47
RAZOR_EXPORT int
krh@248
    48
razor_root_create(const char *root)
krh@248
    49
{
krh@248
    50
	struct stat buf;
krh@248
    51
	struct razor_set *set;
jbowes@273
    52
	char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
krh@248
    53
richard@301
    54
	assert (root != NULL);
richard@301
    55
krh@248
    56
	if (stat(root, &buf) < 0) {
krh@248
    57
		if (mkdir(root, 0777) < 0) {
krh@248
    58
			fprintf(stderr,
krh@248
    59
				"could not create install root \"%s\"\n",
krh@248
    60
				root);
krh@248
    61
			return -1;
krh@248
    62
		}
krh@248
    63
		fprintf(stderr, "created install root \"%s\"\n", root);
krh@248
    64
	} else if (!S_ISDIR(buf.st_mode)) {
krh@248
    65
		fprintf(stderr,
krh@248
    66
			"install root \"%s\" exists, but is not a directory\n",
krh@248
    67
			root);
krh@248
    68
		return -1;
krh@248
    69
	}
krh@248
    70
krh@248
    71
	snprintf(path, sizeof path, "%s/%s",
krh@248
    72
		 razor_root_path, system_repo_filename);
krh@248
    73
	if (razor_create_dir(root, path) < 0) {
krh@248
    74
		fprintf(stderr, "could not create %s%s\n",
krh@248
    75
			root, razor_root_path);
krh@248
    76
		return -1;
krh@248
    77
	}
krh@248
    78
krh@248
    79
	set = razor_set_create();
krh@248
    80
	snprintf(path, sizeof path, "%s%s/%s",
krh@248
    81
		 root, razor_root_path, system_repo_filename);
jbowes@273
    82
	snprintf(details_path, sizeof details_path, "%s%s/%s",
jbowes@273
    83
		 root, razor_root_path, system_repo_details_filename);
jbowes@273
    84
	snprintf(files_path, sizeof files_path, "%s%s/%s",
jbowes@273
    85
		 root, razor_root_path, system_repo_files_filename);
krh@248
    86
	if (stat(path, &buf) == 0) {
krh@248
    87
		fprintf(stderr,
krh@248
    88
			"a razor install root is already initialized\n");
krh@248
    89
		return -1;
krh@248
    90
	}
jbowes@273
    91
	if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
jbowes@273
    92
	    razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
jbowes@273
    93
	    razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
krh@248
    94
		fprintf(stderr, "could not write initial package set\n");
krh@248
    95
		return -1;
krh@248
    96
	}
krh@248
    97
	razor_set_destroy(set);
krh@248
    98
krh@248
    99
	return 0;
krh@248
   100
}
krh@248
   101
krh@269
   102
RAZOR_EXPORT struct razor_root *
krh@250
   103
razor_root_open(const char *root)
krh@248
   104
{
krh@248
   105
	struct razor_root *image;
krh@315
   106
	char details_path[PATH_MAX], files_path[PATH_MAX];
krh@248
   107
richard@301
   108
	assert (root != NULL);
richard@301
   109
krh@248
   110
	image = malloc(sizeof *image);
krh@248
   111
	if (image == NULL)
krh@248
   112
		return NULL;
krh@248
   113
krh@248
   114
	/* Create the new next repo file up front to ensure exclusive
krh@248
   115
	 * access. */
krh@248
   116
	snprintf(image->new_path, sizeof image->new_path,
krh@248
   117
		 "%s%s/%s", root, razor_root_path, next_repo_filename);
krh@248
   118
	image->fd = open(image->new_path,
krh@248
   119
			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
krh@248
   120
	if (image->fd < 0) {
krh@248
   121
		fprintf(stderr, "failed to get lock file, "
krh@248
   122
			"maybe previous operation crashed?\n");
krh@248
   123
krh@248
   124
		/* FIXME: Use fcntl advisory locking on the system
krh@248
   125
		 * package set file to figure out whether previous
krh@248
   126
		 * operation crashed or is still in progress. */
krh@248
   127
krh@248
   128
		free(image);
krh@248
   129
		return NULL;
krh@248
   130
	}
krh@248
   131
krh@248
   132
	snprintf(image->path, sizeof image->path,
krh@248
   133
		 "%s%s/%s", root, razor_root_path, system_repo_filename);
krh@315
   134
	snprintf(details_path, sizeof details_path,
krh@315
   135
		 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
krh@315
   136
	snprintf(files_path, sizeof files_path,
krh@315
   137
		 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
krh@315
   138
krh@248
   139
	image->system = razor_set_open(image->path);
krh@315
   140
	if (image->system == NULL ||
krh@315
   141
	    razor_set_open_details(image->system, details_path) ||
krh@315
   142
	    razor_set_open_files(image->system, files_path)) {
krh@248
   143
		unlink(image->new_path);
krh@248
   144
		close(image->fd);
krh@248
   145
		free(image);
krh@248
   146
		return NULL;
krh@248
   147
	}
krh@248
   148
krh@248
   149
	return image;
krh@248
   150
}
krh@248
   151
krh@269
   152
RAZOR_EXPORT struct razor_set *
krh@248
   153
razor_root_open_read_only(const char *root)
krh@248
   154
{
krh@248
   155
	char path[PATH_MAX];
krh@248
   156
richard@301
   157
	assert (root != NULL);
richard@301
   158
krh@248
   159
	snprintf(path, sizeof path, "%s%s/%s",
krh@248
   160
		 root, razor_root_path, system_repo_filename);
krh@248
   161
krh@248
   162
	return razor_set_open(path);
krh@248
   163
}
krh@248
   164
krh@269
   165
RAZOR_EXPORT struct razor_set *
krh@250
   166
razor_root_get_system_set(struct razor_root *root)
krh@248
   167
{
richard@301
   168
	assert (root != NULL);
richard@301
   169
krh@250
   170
	return root->system;
krh@248
   171
}
krh@248
   172
krh@269
   173
RAZOR_EXPORT int
krh@250
   174
razor_root_close(struct razor_root *root)
krh@248
   175
{
richard@301
   176
	assert (root != NULL);
richard@301
   177
krh@250
   178
	razor_set_destroy(root->system);
krh@250
   179
	unlink(root->new_path);
krh@250
   180
	close(root->fd);
krh@250
   181
	free(root);
krh@248
   182
krh@248
   183
	return 0;
krh@248
   184
}
krh@248
   185
krh@269
   186
RAZOR_EXPORT void
krh@248
   187
razor_root_update(struct razor_root *root, struct razor_set *next)
krh@248
   188
{
richard@301
   189
	assert (root != NULL);
richard@301
   190
	assert (next != NULL);
richard@301
   191
jbowes@258
   192
	razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
krh@248
   193
	root->next = next;
krh@248
   194
krh@248
   195
	/* Sync the new repo file so the new package set is on disk
krh@248
   196
	 * before we start upgrading. */
krh@248
   197
	fsync(root->fd);
krh@248
   198
	printf("wrote %s\n", root->new_path);
krh@248
   199
}
krh@248
   200
krh@269
   201
RAZOR_EXPORT int
krh@250
   202
razor_root_commit(struct razor_root *root)
krh@248
   203
{
richard@301
   204
	assert (root != NULL);
richard@301
   205
krh@248
   206
	/* Make it so. */
krh@250
   207
	rename(root->new_path, root->path);
krh@250
   208
	printf("renamed %s to %s\n", root->new_path, root->path);
krh@250
   209
	razor_set_destroy(root->system);
krh@250
   210
	close(root->fd);
krh@250
   211
	free(root);
krh@248
   212
krh@248
   213
	return 0;
krh@248
   214
}