librazor/root.c
author James Bowes <jbowes@redhat.com>
Thu Jun 26 10:37:45 2008 -0400 (2008-06-26)
changeset 287 026c47621214
parent 269 03fc85294bc9
child 300 455eaa569767
permissions -rw-r--r--
Get api docs building
krh@248
     1
#include <stdlib.h>
krh@248
     2
#include <stdint.h>
krh@248
     3
#include <stdio.h>
krh@248
     4
#include <sys/stat.h>
krh@248
     5
#include <dirent.h>
krh@248
     6
#include <unistd.h>
krh@248
     7
#include <fcntl.h>
krh@248
     8
#include "razor.h"
krh@248
     9
#include "razor-internal.h"
krh@248
    10
krh@248
    11
static const char system_repo_filename[] = "system.repo";
jbowes@273
    12
static const char system_repo_details_filename[] = "system-details.repo";
jbowes@273
    13
static const char system_repo_files_filename[] = "system-files.repo";
jbowes@273
    14
krh@248
    15
static const char next_repo_filename[] = "system-next.repo";
krh@248
    16
static const char razor_root_path[] = "/var/lib/razor";
krh@248
    17
krh@248
    18
struct razor_root {
krh@248
    19
	struct razor_set *system;
krh@248
    20
	struct razor_set *next;
krh@248
    21
	int fd;
krh@248
    22
	char path[PATH_MAX];
krh@248
    23
	char new_path[PATH_MAX];
krh@248
    24
};
krh@248
    25
krh@269
    26
RAZOR_EXPORT int
krh@248
    27
razor_root_create(const char *root)
krh@248
    28
{
krh@248
    29
	struct stat buf;
krh@248
    30
	struct razor_set *set;
jbowes@273
    31
	char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
krh@248
    32
krh@248
    33
	if (stat(root, &buf) < 0) {
krh@248
    34
		if (mkdir(root, 0777) < 0) {
krh@248
    35
			fprintf(stderr,
krh@248
    36
				"could not create install root \"%s\"\n",
krh@248
    37
				root);
krh@248
    38
			return -1;
krh@248
    39
		}
krh@248
    40
		fprintf(stderr, "created install root \"%s\"\n", root);
krh@248
    41
	} else if (!S_ISDIR(buf.st_mode)) {
krh@248
    42
		fprintf(stderr,
krh@248
    43
			"install root \"%s\" exists, but is not a directory\n",
krh@248
    44
			root);
krh@248
    45
		return -1;
krh@248
    46
	}
krh@248
    47
krh@248
    48
	snprintf(path, sizeof path, "%s/%s",
krh@248
    49
		 razor_root_path, system_repo_filename);
krh@248
    50
	if (razor_create_dir(root, path) < 0) {
krh@248
    51
		fprintf(stderr, "could not create %s%s\n",
krh@248
    52
			root, razor_root_path);
krh@248
    53
		return -1;
krh@248
    54
	}
krh@248
    55
krh@248
    56
	set = razor_set_create();
krh@248
    57
	snprintf(path, sizeof path, "%s%s/%s",
krh@248
    58
		 root, razor_root_path, system_repo_filename);
jbowes@273
    59
	snprintf(details_path, sizeof details_path, "%s%s/%s",
jbowes@273
    60
		 root, razor_root_path, system_repo_details_filename);
jbowes@273
    61
	snprintf(files_path, sizeof files_path, "%s%s/%s",
jbowes@273
    62
		 root, razor_root_path, system_repo_files_filename);
krh@248
    63
	if (stat(path, &buf) == 0) {
krh@248
    64
		fprintf(stderr,
krh@248
    65
			"a razor install root is already initialized\n");
krh@248
    66
		return -1;
krh@248
    67
	}
jbowes@273
    68
	if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
jbowes@273
    69
	    razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
jbowes@273
    70
	    razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
krh@248
    71
		fprintf(stderr, "could not write initial package set\n");
krh@248
    72
		return -1;
krh@248
    73
	}
krh@248
    74
	razor_set_destroy(set);
krh@248
    75
krh@248
    76
	return 0;
krh@248
    77
}
krh@248
    78
krh@269
    79
RAZOR_EXPORT struct razor_root *
krh@250
    80
razor_root_open(const char *root)
krh@248
    81
{
krh@248
    82
	struct razor_root *image;
krh@248
    83
krh@248
    84
	image = malloc(sizeof *image);
krh@248
    85
	if (image == NULL)
krh@248
    86
		return NULL;
krh@248
    87
krh@248
    88
	/* Create the new next repo file up front to ensure exclusive
krh@248
    89
	 * access. */
krh@248
    90
	snprintf(image->new_path, sizeof image->new_path,
krh@248
    91
		 "%s%s/%s", root, razor_root_path, next_repo_filename);
krh@248
    92
	image->fd = open(image->new_path,
krh@248
    93
			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
krh@248
    94
	if (image->fd < 0) {
krh@248
    95
		fprintf(stderr, "failed to get lock file, "
krh@248
    96
			"maybe previous operation crashed?\n");
krh@248
    97
krh@248
    98
		/* FIXME: Use fcntl advisory locking on the system
krh@248
    99
		 * package set file to figure out whether previous
krh@248
   100
		 * operation crashed or is still in progress. */
krh@248
   101
krh@248
   102
		free(image);
krh@248
   103
		return NULL;
krh@248
   104
	}
krh@248
   105
krh@248
   106
	snprintf(image->path, sizeof image->path,
krh@248
   107
		 "%s%s/%s", root, razor_root_path, system_repo_filename);
krh@248
   108
	image->system = razor_set_open(image->path);
krh@248
   109
	if (image->system == NULL) {
krh@248
   110
		unlink(image->new_path);
krh@248
   111
		close(image->fd);
krh@248
   112
		free(image);
krh@248
   113
		return NULL;
krh@248
   114
	}
krh@248
   115
krh@248
   116
	return image;
krh@248
   117
}
krh@248
   118
krh@269
   119
RAZOR_EXPORT struct razor_set *
krh@248
   120
razor_root_open_read_only(const char *root)
krh@248
   121
{
krh@248
   122
	char path[PATH_MAX];
krh@248
   123
krh@248
   124
	snprintf(path, sizeof path, "%s%s/%s",
krh@248
   125
		 root, razor_root_path, system_repo_filename);
krh@248
   126
krh@248
   127
	return razor_set_open(path);
krh@248
   128
}
krh@248
   129
krh@269
   130
RAZOR_EXPORT struct razor_set *
krh@250
   131
razor_root_get_system_set(struct razor_root *root)
krh@248
   132
{
krh@250
   133
	return root->system;
krh@248
   134
}
krh@248
   135
krh@269
   136
RAZOR_EXPORT int
krh@250
   137
razor_root_close(struct razor_root *root)
krh@248
   138
{
krh@250
   139
	razor_set_destroy(root->system);
krh@250
   140
	unlink(root->new_path);
krh@250
   141
	close(root->fd);
krh@250
   142
	free(root);
krh@248
   143
krh@248
   144
	return 0;
krh@248
   145
}
krh@248
   146
krh@269
   147
RAZOR_EXPORT void
krh@248
   148
razor_root_update(struct razor_root *root, struct razor_set *next)
krh@248
   149
{
jbowes@258
   150
	razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
krh@248
   151
	root->next = next;
krh@248
   152
krh@248
   153
	/* Sync the new repo file so the new package set is on disk
krh@248
   154
	 * before we start upgrading. */
krh@248
   155
	fsync(root->fd);
krh@248
   156
	printf("wrote %s\n", root->new_path);
krh@248
   157
}
krh@248
   158
krh@269
   159
RAZOR_EXPORT int
krh@250
   160
razor_root_commit(struct razor_root *root)
krh@248
   161
{
krh@248
   162
	/* Make it so. */
krh@250
   163
	rename(root->new_path, root->path);
krh@250
   164
	printf("renamed %s to %s\n", root->new_path, root->path);
krh@250
   165
	razor_set_destroy(root->system);
krh@250
   166
	close(root->fd);
krh@250
   167
	free(root);
krh@248
   168
krh@248
   169
	return 0;
krh@248
   170
}