librazor/root.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Oct 01 20:02:23 2009 +0100 (2009-10-01)
changeset 390 297c6c8be2d1
parent 373 fda83d91e600
child 403 e63951c1d0f8
permissions -rw-r--r--
Don't touch v2+ rzdb files
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";
ali@388
    45
/*
ali@388
    46
 * system_lock_filename is chosen to be the same as the pre v0.3
ali@388
    47
 * next_repo_filename. This means that once a system has been
ali@388
    48
 * updated by a v0.3+ copy of razor all pre v0.3 versions of razor
ali@388
    49
 * will see the system as permenantly locked.
ali@388
    50
 */
ali@388
    51
static const char system_lock_filename[] = "system-next.rzdb";
ali@388
    52
static const char system_tmp_filename[] = "system.tmp";
ali@340
    53
#ifdef MSWIN_API
ali@340
    54
#define RAZOR_ROOT_PATH	NULL
ali@340
    55
#else
ali@340
    56
#define RAZOR_ROOT_PATH	"/var/lib/razor"
ali@340
    57
#endif
ali@340
    58
static const char *razor_root_path = RAZOR_ROOT_PATH;
krh@248
    59
krh@248
    60
struct razor_root {
krh@248
    61
	struct razor_set *system;
krh@248
    62
	struct razor_set *next;
krh@248
    63
	int fd;
krh@248
    64
	char path[PATH_MAX];
krh@248
    65
	char new_path[PATH_MAX];
krh@248
    66
};
krh@248
    67
ali@340
    68
static void
ali@340
    69
razor_root_init(void)
ali@340
    70
{
ali@340
    71
#ifdef MSWIN_API
ali@340
    72
	static char root_path[MAX_PATH];
ali@340
    73
	if (!razor_root_path) {
ali@340
    74
		SHGetFolderPath(NULL,
ali@340
    75
			CSIDL_COMMON_APPDATA | CSIDL_FLAG_DONT_VERIFY, NULL, 0,
ali@340
    76
			root_path);
ali@340
    77
		strcat(root_path, "\\Razor");
ali@340
    78
		razor_root_path = root_path;
ali@340
    79
	}
ali@340
    80
#endif
ali@340
    81
}
ali@340
    82
krh@269
    83
RAZOR_EXPORT int
krh@248
    84
razor_root_create(const char *root)
krh@248
    85
{
krh@248
    86
	struct stat buf;
krh@248
    87
	struct razor_set *set;
krh@373
    88
	char path[PATH_MAX];
krh@248
    89
richard@301
    90
	assert (root != NULL);
richard@301
    91
ali@340
    92
	razor_root_init();
krh@317
    93
	if (root[0] == '\0') {
krh@317
    94
		/* root is file system root */
krh@317
    95
	} else if (stat(root, &buf) < 0) {
krh@248
    96
		if (mkdir(root, 0777) < 0) {
krh@248
    97
			fprintf(stderr,
krh@248
    98
				"could not create install root \"%s\"\n",
krh@248
    99
				root);
krh@248
   100
			return -1;
krh@248
   101
		}
krh@248
   102
		fprintf(stderr, "created install root \"%s\"\n", root);
krh@248
   103
	} else if (!S_ISDIR(buf.st_mode)) {
krh@248
   104
		fprintf(stderr,
krh@248
   105
			"install root \"%s\" exists, but is not a directory\n",
krh@248
   106
			root);
krh@248
   107
		return -1;
krh@248
   108
	}
krh@248
   109
krh@248
   110
	snprintf(path, sizeof path, "%s/%s",
krh@248
   111
		 razor_root_path, system_repo_filename);
krh@248
   112
	if (razor_create_dir(root, path) < 0) {
krh@248
   113
		fprintf(stderr, "could not create %s%s\n",
krh@248
   114
			root, razor_root_path);
krh@248
   115
		return -1;
krh@248
   116
	}
krh@248
   117
krh@248
   118
	set = razor_set_create();
krh@248
   119
	snprintf(path, sizeof path, "%s%s/%s",
krh@248
   120
		 root, razor_root_path, system_repo_filename);
krh@248
   121
	if (stat(path, &buf) == 0) {
krh@248
   122
		fprintf(stderr,
krh@248
   123
			"a razor install root is already initialized\n");
krh@248
   124
		return -1;
krh@248
   125
	}
krh@373
   126
	if (razor_set_write(set, path, RAZOR_SECTION_ALL) < 0) {
krh@248
   127
		fprintf(stderr, "could not write initial package set\n");
krh@248
   128
		return -1;
krh@248
   129
	}
krh@248
   130
	razor_set_destroy(set);
krh@248
   131
krh@248
   132
	return 0;
krh@248
   133
}
krh@248
   134
krh@269
   135
RAZOR_EXPORT struct razor_root *
krh@250
   136
razor_root_open(const char *root)
krh@248
   137
{
krh@248
   138
	struct razor_root *image;
ali@388
   139
	char lock_path[PATH_MAX];
krh@248
   140
richard@301
   141
	assert (root != NULL);
richard@301
   142
ali@340
   143
	razor_root_init();
krh@248
   144
	image = malloc(sizeof *image);
krh@248
   145
	if (image == NULL)
krh@248
   146
		return NULL;
krh@248
   147
ali@388
   148
	image->system = razor_set_create_without_root();
ali@388
   149
	if (image->system == NULL) {
ali@388
   150
		free(image);
ali@388
   151
		return NULL;
ali@388
   152
	}
ali@388
   153
ali@388
   154
	snprintf(lock_path, sizeof lock_path,
ali@388
   155
		 "%s%s/%s", root, razor_root_path, system_lock_filename);
ali@388
   156
ali@388
   157
	if (razor_set_aquire_lock(image->system, lock_path, 1) < 0) {
ali@388
   158
		razor_set_destroy(image->system);
ali@388
   159
		free(image);
ali@388
   160
		return NULL;
ali@388
   161
	}
ali@388
   162
krh@248
   163
	snprintf(image->new_path, sizeof image->new_path,
ali@388
   164
		 "%s%s/%s", root, razor_root_path, system_tmp_filename);
krh@248
   165
	image->fd = open(image->new_path,
ali@388
   166
			 O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,
ali@345
   167
			 0666);
krh@248
   168
	if (image->fd < 0) {
ali@388
   169
		razor_set_destroy(image->system);
krh@248
   170
		free(image);
krh@248
   171
		return NULL;
krh@248
   172
	}
krh@248
   173
krh@248
   174
	snprintf(image->path, sizeof image->path,
krh@248
   175
		 "%s%s/%s", root, razor_root_path, system_repo_filename);
krh@317
   176
ali@388
   177
	if (razor_set_bind_sections(image->system, image->path)) {
ali@388
   178
		close(image->fd);
krh@248
   179
		unlink(image->new_path);
ali@388
   180
		razor_set_destroy(image->system);
krh@248
   181
		free(image);
krh@248
   182
		return NULL;
krh@248
   183
	}
krh@248
   184
krh@248
   185
	return image;
krh@248
   186
}
krh@248
   187
krh@269
   188
RAZOR_EXPORT struct razor_set *
krh@248
   189
razor_root_open_read_only(const char *root)
krh@248
   190
{
krh@373
   191
	char path[PATH_MAX];
ali@388
   192
	struct razor_set *set;
krh@248
   193
richard@301
   194
	assert (root != NULL);
richard@301
   195
ali@340
   196
	razor_root_init();
ali@388
   197
	set = razor_set_create_without_root();
ali@388
   198
	if (set == NULL)
ali@388
   199
		return NULL;
ali@388
   200
ali@388
   201
	snprintf(path, sizeof path,
ali@388
   202
		 "%s%s/%s", root, razor_root_path, system_lock_filename);
ali@388
   203
	if (razor_set_aquire_lock(set, path, 0) < 0) {
ali@388
   204
		razor_set_destroy(set);
ali@388
   205
		return NULL;
ali@388
   206
	}
ali@388
   207
krh@248
   208
	snprintf(path, sizeof path, "%s%s/%s",
krh@248
   209
		 root, razor_root_path, system_repo_filename);
krh@248
   210
ali@388
   211
	if (razor_set_bind_sections(set, path)) {
ali@388
   212
		razor_set_destroy(set);
ali@388
   213
		return NULL;
ali@388
   214
	}
ali@388
   215
ali@388
   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
{
richard@301
   243
	assert (root != NULL);
richard@301
   244
	assert (next != NULL);
richard@301
   245
ali@340
   246
	razor_root_init();
krh@373
   247
	razor_set_write_to_fd(next, root->fd, RAZOR_SECTION_ALL);
krh@248
   248
	root->next = next;
krh@248
   249
krh@248
   250
	/* Sync the new repo file so the new package set is on disk
krh@248
   251
	 * before we start upgrading. */
krh@248
   252
	fsync(root->fd);
krh@248
   253
	printf("wrote %s\n", root->new_path);
krh@248
   254
}
krh@248
   255
krh@269
   256
RAZOR_EXPORT int
krh@250
   257
razor_root_commit(struct razor_root *root)
krh@248
   258
{
ali@346
   259
	int retval;
richard@301
   260
	assert (root != NULL);
richard@301
   261
krh@248
   262
	/* Make it so. */
ali@346
   263
	close(root->fd);
ali@346
   264
#ifdef MSWIN_API
ali@346
   265
	/* Rename is not atomic under MS-Windows */
ali@346
   266
	remove(root->path);
ali@346
   267
#endif
ali@346
   268
	retval = rename(root->new_path, root->path);
ali@346
   269
	if (retval)
ali@346
   270
		perror(root->path);
ali@346
   271
	else
ali@346
   272
		printf("renamed %s to %s\n", root->new_path, root->path);
krh@250
   273
	razor_set_destroy(root->system);
krh@250
   274
	free(root);
krh@248
   275
ali@346
   276
	return retval;
krh@248
   277
}