librazor/root.c
author Kristian H?gsberg <krh@redhat.com>
Wed Jul 02 14:37:38 2008 -0400 (2008-07-02)
changeset 311 cd292c2de0b6
parent 301 4124c37fd953
child 315 1c52b84bfc33
permissions -rw-r--r--
Move DEPSOLVE.txt and REPO.txt into docbook.

A little messy, but it's a first step towards pulling all the docs into
a nice self-contained document.
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@248
   106
richard@301
   107
	assert (root != NULL);
richard@301
   108
krh@248
   109
	image = malloc(sizeof *image);
krh@248
   110
	if (image == NULL)
krh@248
   111
		return NULL;
krh@248
   112
krh@248
   113
	/* Create the new next repo file up front to ensure exclusive
krh@248
   114
	 * access. */
krh@248
   115
	snprintf(image->new_path, sizeof image->new_path,
krh@248
   116
		 "%s%s/%s", root, razor_root_path, next_repo_filename);
krh@248
   117
	image->fd = open(image->new_path,
krh@248
   118
			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
krh@248
   119
	if (image->fd < 0) {
krh@248
   120
		fprintf(stderr, "failed to get lock file, "
krh@248
   121
			"maybe previous operation crashed?\n");
krh@248
   122
krh@248
   123
		/* FIXME: Use fcntl advisory locking on the system
krh@248
   124
		 * package set file to figure out whether previous
krh@248
   125
		 * operation crashed or is still in progress. */
krh@248
   126
krh@248
   127
		free(image);
krh@248
   128
		return NULL;
krh@248
   129
	}
krh@248
   130
krh@248
   131
	snprintf(image->path, sizeof image->path,
krh@248
   132
		 "%s%s/%s", root, razor_root_path, system_repo_filename);
krh@248
   133
	image->system = razor_set_open(image->path);
krh@248
   134
	if (image->system == NULL) {
krh@248
   135
		unlink(image->new_path);
krh@248
   136
		close(image->fd);
krh@248
   137
		free(image);
krh@248
   138
		return NULL;
krh@248
   139
	}
krh@248
   140
krh@248
   141
	return image;
krh@248
   142
}
krh@248
   143
krh@269
   144
RAZOR_EXPORT struct razor_set *
krh@248
   145
razor_root_open_read_only(const char *root)
krh@248
   146
{
krh@248
   147
	char path[PATH_MAX];
krh@248
   148
richard@301
   149
	assert (root != NULL);
richard@301
   150
krh@248
   151
	snprintf(path, sizeof path, "%s%s/%s",
krh@248
   152
		 root, razor_root_path, system_repo_filename);
krh@248
   153
krh@248
   154
	return razor_set_open(path);
krh@248
   155
}
krh@248
   156
krh@269
   157
RAZOR_EXPORT struct razor_set *
krh@250
   158
razor_root_get_system_set(struct razor_root *root)
krh@248
   159
{
richard@301
   160
	assert (root != NULL);
richard@301
   161
krh@250
   162
	return root->system;
krh@248
   163
}
krh@248
   164
krh@269
   165
RAZOR_EXPORT int
krh@250
   166
razor_root_close(struct razor_root *root)
krh@248
   167
{
richard@301
   168
	assert (root != NULL);
richard@301
   169
krh@250
   170
	razor_set_destroy(root->system);
krh@250
   171
	unlink(root->new_path);
krh@250
   172
	close(root->fd);
krh@250
   173
	free(root);
krh@248
   174
krh@248
   175
	return 0;
krh@248
   176
}
krh@248
   177
krh@269
   178
RAZOR_EXPORT void
krh@248
   179
razor_root_update(struct razor_root *root, struct razor_set *next)
krh@248
   180
{
richard@301
   181
	assert (root != NULL);
richard@301
   182
	assert (next != NULL);
richard@301
   183
jbowes@258
   184
	razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
krh@248
   185
	root->next = next;
krh@248
   186
krh@248
   187
	/* Sync the new repo file so the new package set is on disk
krh@248
   188
	 * before we start upgrading. */
krh@248
   189
	fsync(root->fd);
krh@248
   190
	printf("wrote %s\n", root->new_path);
krh@248
   191
}
krh@248
   192
krh@269
   193
RAZOR_EXPORT int
krh@250
   194
razor_root_commit(struct razor_root *root)
krh@248
   195
{
richard@301
   196
	assert (root != NULL);
richard@301
   197
krh@248
   198
	/* Make it so. */
krh@250
   199
	rename(root->new_path, root->path);
krh@250
   200
	printf("renamed %s to %s\n", root->new_path, root->path);
krh@250
   201
	razor_set_destroy(root->system);
krh@250
   202
	close(root->fd);
krh@250
   203
	free(root);
krh@248
   204
krh@248
   205
	return 0;
krh@248
   206
}