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