librazor/root.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Jan 09 18:59:38 2009 +0000 (2009-01-09)
changeset 343 8bce3ff205bb
parent 340 5962a461a5a3
child 345 edd9b0fa63ca
permissions -rw-r--r--
Include test.xml in the distribution since it is needed for make check.
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
richard@310
    40
static const char system_repo_filename[] = "system.rzdb";
richard@310
    41
static const char system_repo_details_filename[] = "system-details.rzdb";
richard@310
    42
static const char system_repo_files_filename[] = "system-files.rzdb";
jbowes@273
    43
richard@310
    44
static const char next_repo_filename[] = "system-next.rzdb";
ali@340
    45
#ifdef MSWIN_API
ali@340
    46
#define RAZOR_ROOT_PATH	NULL
ali@340
    47
#else
ali@340
    48
#define RAZOR_ROOT_PATH	"/var/lib/razor"
ali@340
    49
#endif
ali@340
    50
static const char *razor_root_path = RAZOR_ROOT_PATH;
krh@248
    51
krh@248
    52
struct razor_root {
krh@248
    53
	struct razor_set *system;
krh@248
    54
	struct razor_set *next;
krh@248
    55
	int fd;
krh@317
    56
	char root[PATH_MAX];
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;
jbowes@273
    81
	char path[PATH_MAX], details_path[PATH_MAX], files_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);
jbowes@273
   114
	snprintf(details_path, sizeof details_path, "%s%s/%s",
jbowes@273
   115
		 root, razor_root_path, system_repo_details_filename);
jbowes@273
   116
	snprintf(files_path, sizeof files_path, "%s%s/%s",
jbowes@273
   117
		 root, razor_root_path, system_repo_files_filename);
krh@248
   118
	if (stat(path, &buf) == 0) {
krh@248
   119
		fprintf(stderr,
krh@248
   120
			"a razor install root is already initialized\n");
krh@248
   121
		return -1;
krh@248
   122
	}
jbowes@273
   123
	if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
jbowes@273
   124
	    razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
jbowes@273
   125
	    razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
krh@248
   126
		fprintf(stderr, "could not write initial package set\n");
krh@248
   127
		return -1;
krh@248
   128
	}
krh@248
   129
	razor_set_destroy(set);
krh@248
   130
krh@248
   131
	return 0;
krh@248
   132
}
krh@248
   133
krh@269
   134
RAZOR_EXPORT struct razor_root *
krh@250
   135
razor_root_open(const char *root)
krh@248
   136
{
krh@248
   137
	struct razor_root *image;
krh@315
   138
	char details_path[PATH_MAX], files_path[PATH_MAX];
krh@248
   139
richard@301
   140
	assert (root != NULL);
richard@301
   141
ali@340
   142
	razor_root_init();
krh@248
   143
	image = malloc(sizeof *image);
krh@248
   144
	if (image == NULL)
krh@248
   145
		return NULL;
krh@248
   146
krh@248
   147
	/* Create the new next repo file up front to ensure exclusive
krh@248
   148
	 * access. */
krh@248
   149
	snprintf(image->new_path, sizeof image->new_path,
krh@248
   150
		 "%s%s/%s", root, razor_root_path, next_repo_filename);
krh@248
   151
	image->fd = open(image->new_path,
krh@248
   152
			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
krh@248
   153
	if (image->fd < 0) {
krh@248
   154
		fprintf(stderr, "failed to get lock file, "
krh@248
   155
			"maybe previous operation crashed?\n");
krh@248
   156
krh@248
   157
		/* FIXME: Use fcntl advisory locking on the system
krh@248
   158
		 * package set file to figure out whether previous
krh@248
   159
		 * operation crashed or is still in progress. */
krh@248
   160
krh@248
   161
		free(image);
krh@248
   162
		return NULL;
krh@248
   163
	}
krh@248
   164
krh@248
   165
	snprintf(image->path, sizeof image->path,
krh@248
   166
		 "%s%s/%s", root, razor_root_path, system_repo_filename);
krh@315
   167
	snprintf(details_path, sizeof details_path,
krh@315
   168
		 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
krh@315
   169
	snprintf(files_path, sizeof files_path,
krh@315
   170
		 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
krh@315
   171
krh@317
   172
	/* FIXME: We store the root path to make the hack in
krh@317
   173
	 * razor_root_update() work.  Need to get rid of this. */
krh@317
   174
	strcpy(image->root, root);
krh@317
   175
krh@248
   176
	image->system = razor_set_open(image->path);
krh@315
   177
	if (image->system == NULL ||
krh@315
   178
	    razor_set_open_details(image->system, details_path) ||
krh@315
   179
	    razor_set_open_files(image->system, files_path)) {
krh@248
   180
		unlink(image->new_path);
krh@248
   181
		close(image->fd);
krh@248
   182
		free(image);
krh@248
   183
		return NULL;
krh@248
   184
	}
krh@248
   185
krh@248
   186
	return image;
krh@248
   187
}
krh@248
   188
krh@269
   189
RAZOR_EXPORT struct razor_set *
krh@248
   190
razor_root_open_read_only(const char *root)
krh@248
   191
{
krh@317
   192
	char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
krh@317
   193
	struct razor_set *set;
krh@248
   194
richard@301
   195
	assert (root != NULL);
richard@301
   196
ali@340
   197
	razor_root_init();
krh@248
   198
	snprintf(path, sizeof path, "%s%s/%s",
krh@248
   199
		 root, razor_root_path, system_repo_filename);
krh@317
   200
	snprintf(details_path, sizeof details_path,
krh@317
   201
		 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
krh@317
   202
	snprintf(files_path, sizeof files_path,
krh@317
   203
		 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
krh@248
   204
krh@317
   205
krh@317
   206
	set = razor_set_open(path);
krh@317
   207
	if (set == NULL)
krh@317
   208
		return NULL;
krh@317
   209
krh@317
   210
	if (razor_set_open_details(set, details_path) ||
krh@317
   211
	    razor_set_open_files(set, files_path)) {
krh@317
   212
		razor_set_destroy(set);
krh@317
   213
		return NULL;
krh@317
   214
	}
krh@317
   215
krh@317
   216
	return set;
krh@248
   217
}
krh@248
   218
krh@269
   219
RAZOR_EXPORT struct razor_set *
krh@250
   220
razor_root_get_system_set(struct razor_root *root)
krh@248
   221
{
richard@301
   222
	assert (root != NULL);
richard@301
   223
krh@250
   224
	return root->system;
krh@248
   225
}
krh@248
   226
krh@269
   227
RAZOR_EXPORT int
krh@250
   228
razor_root_close(struct razor_root *root)
krh@248
   229
{
richard@301
   230
	assert (root != NULL);
richard@301
   231
krh@250
   232
	razor_set_destroy(root->system);
ali@341
   233
	close(root->fd);
krh@250
   234
	unlink(root->new_path);
krh@250
   235
	free(root);
krh@248
   236
krh@248
   237
	return 0;
krh@248
   238
}
krh@248
   239
krh@269
   240
RAZOR_EXPORT void
krh@248
   241
razor_root_update(struct razor_root *root, struct razor_set *next)
krh@248
   242
{
krh@317
   243
	char path[PATH_MAX];
krh@317
   244
richard@301
   245
	assert (root != NULL);
richard@301
   246
	assert (next != NULL);
richard@301
   247
ali@340
   248
	razor_root_init();
jbowes@258
   249
	razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
krh@248
   250
	root->next = next;
krh@248
   251
krh@317
   252
	/* FIXME: This is a pretty bad hack that just overwrites the
krh@317
   253
	 * system details and files rzdb files before the transaction
krh@317
   254
	 * succeeds.  We need to fix this by merging the separate
krh@317
   255
	 * details and files rzdb files back into the main rzdb
krh@317
   256
	 * file. */
krh@317
   257
	snprintf(path, sizeof path,
krh@317
   258
		 "%s%s/%s", root->root, razor_root_path, system_repo_details_filename);
krh@317
   259
	razor_set_write(next, path, RAZOR_REPO_FILE_DETAILS);
krh@317
   260
	snprintf(path, sizeof path,
krh@317
   261
		 "%s%s/%s", root->root, razor_root_path, system_repo_files_filename);
krh@317
   262
	razor_set_write(next, path, RAZOR_REPO_FILE_FILES);
krh@317
   263
krh@248
   264
	/* Sync the new repo file so the new package set is on disk
krh@248
   265
	 * before we start upgrading. */
krh@248
   266
	fsync(root->fd);
krh@248
   267
	printf("wrote %s\n", root->new_path);
krh@248
   268
}
krh@248
   269
krh@269
   270
RAZOR_EXPORT int
krh@250
   271
razor_root_commit(struct razor_root *root)
krh@248
   272
{
richard@301
   273
	assert (root != NULL);
richard@301
   274
krh@248
   275
	/* Make it so. */
krh@250
   276
	rename(root->new_path, root->path);
krh@250
   277
	printf("renamed %s to %s\n", root->new_path, root->path);
krh@250
   278
	razor_set_destroy(root->system);
krh@250
   279
	close(root->fd);
krh@250
   280
	free(root);
krh@248
   281
krh@248
   282
	return 0;
krh@248
   283
}