librazor/root.c
author Richard Hughes <richard@hughsie.com>
Sun Jun 29 10:55:17 2008 +0100 (2008-06-29)
changeset 300 455eaa569767
parent 273 3e37dc49d6b7
child 301 4124c37fd953
permissions -rw-r--r--
add missing licencing information at the top of each source file

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