librazor/root.c
author J. Ali Harlow <ali@juiblex.co.uk>
Tue Mar 27 21:32:46 2012 +0100 (2012-03-27)
changeset 435 275a4428c13b
parent 424 8cbc438cc298
child 439 f28bb31024b4
permissions -rw-r--r--
Don't assume the current directory is writable
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@424
     4
 * Copyright (C) 2009, 2011, 2012  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@340
    52
#ifdef MSWIN_API
ali@340
    53
#define RAZOR_ROOT_PATH	NULL
ali@340
    54
#else
ali@340
    55
#define RAZOR_ROOT_PATH	"/var/lib/razor"
ali@340
    56
#endif
ali@340
    57
static const char *razor_root_path = RAZOR_ROOT_PATH;
krh@248
    58
krh@248
    59
struct razor_root {
krh@248
    60
	struct razor_set *system;
ali@424
    61
	char *path;
krh@248
    62
};
krh@248
    63
ali@340
    64
static void
ali@340
    65
razor_root_init(void)
ali@340
    66
{
ali@340
    67
#ifdef MSWIN_API
ali@340
    68
	static char root_path[MAX_PATH];
ali@340
    69
	if (!razor_root_path) {
ali@340
    70
		SHGetFolderPath(NULL,
ali@340
    71
			CSIDL_COMMON_APPDATA | CSIDL_FLAG_DONT_VERIFY, NULL, 0,
ali@340
    72
			root_path);
ali@340
    73
		strcat(root_path, "\\Razor");
ali@340
    74
		razor_root_path = root_path;
ali@340
    75
	}
ali@340
    76
#endif
ali@340
    77
}
ali@340
    78
krh@269
    79
RAZOR_EXPORT int
ali@425
    80
razor_root_create(const char *root, struct razor_error **error)
krh@248
    81
{
ali@403
    82
	int retval;
krh@248
    83
	struct stat buf;
krh@248
    84
	struct razor_set *set;
ali@403
    85
	struct razor_atomic *atomic;
ali@406
    86
	char *file, *path;
krh@248
    87
richard@301
    88
	assert (root != NULL);
richard@301
    89
ali@340
    90
	razor_root_init();
krh@317
    91
	if (root[0] == '\0') {
krh@317
    92
		/* root is file system root */
krh@317
    93
	} else if (stat(root, &buf) < 0) {
krh@248
    94
		if (mkdir(root, 0777) < 0) {
ali@425
    95
			razor_set_error(error, root,
ali@425
    96
					"Could not create install root");
krh@248
    97
			return -1;
krh@248
    98
		}
krh@248
    99
	} else if (!S_ISDIR(buf.st_mode)) {
ali@425
   100
		razor_set_error(error, root, "Not a directory");
krh@248
   101
		return -1;
krh@248
   102
	}
krh@248
   103
ali@406
   104
	file = razor_concat(razor_root_path, "/", system_repo_filename, NULL);
ali@406
   105
	path = razor_concat(root, file, NULL);
ali@403
   106
	retval = !stat(path, &buf);
ali@403
   107
	if (retval) {
ali@425
   108
		razor_set_error(error, NULL,
ali@425
   109
				"A razor install root is already initialized");
ali@406
   110
		free(path);
ali@406
   111
		free(file);
ali@403
   112
		return retval;
krh@248
   113
	}
krh@248
   114
ali@403
   115
	atomic = razor_atomic_open("Create initial package set");
ali@406
   116
	razor_atomic_make_dirs(atomic, root, file);
krh@248
   117
	set = razor_set_create();
ali@403
   118
	razor_set_write(set, atomic, path, RAZOR_SECTION_ALL);
ali@403
   119
	free(path);
ali@406
   120
	free(file);
ali@403
   121
	retval = razor_atomic_commit(atomic);
ali@403
   122
	if (retval)
ali@425
   123
		razor_set_error(error, NULL,
ali@425
   124
				"Could not write initial package set");
ali@403
   125
	razor_set_unref(set);
ali@403
   126
	razor_atomic_destroy(atomic);
krh@248
   127
ali@403
   128
	return retval;
krh@248
   129
}
krh@248
   130
krh@269
   131
RAZOR_EXPORT struct razor_root *
ali@424
   132
razor_root_open(const char *root, struct razor_error **error)
krh@248
   133
{
krh@248
   134
	struct razor_root *image;
ali@403
   135
	char *lock_path;
ali@403
   136
	int r;
krh@248
   137
richard@301
   138
	assert (root != NULL);
richard@301
   139
ali@340
   140
	razor_root_init();
krh@248
   141
	image = malloc(sizeof *image);
ali@403
   142
	if (image == NULL) {
ali@424
   143
		razor_set_error(error, NULL, "Not enough memory");
krh@248
   144
		return NULL;
ali@403
   145
	}
ali@403
   146
ali@388
   147
	image->system = razor_set_create_without_root();
ali@388
   148
	if (image->system == NULL) {
ali@388
   149
		free(image);
ali@424
   150
		razor_set_error(error, NULL, "Not enough memory");
ali@388
   151
		return NULL;
ali@388
   152
	}
ali@388
   153
ali@403
   154
	lock_path = razor_concat(root, razor_root_path, "/",
ali@403
   155
				 system_lock_filename, NULL);
ali@388
   156
ali@403
   157
	r = razor_set_aquire_lock(image->system, lock_path, 1);
ali@403
   158
ali@403
   159
	free(lock_path);
ali@403
   160
ali@403
   161
	if (r < 0) {
ali@424
   162
		razor_set_error(error, NULL,
ali@424
   163
				"Failed to aquire exclusive system lock");
ali@403
   164
		razor_set_unref(image->system);
krh@248
   165
		free(image);
krh@248
   166
		return NULL;
krh@248
   167
	}
krh@248
   168
ali@403
   169
	image->path = razor_concat(root, razor_root_path, "/",
ali@403
   170
				   system_repo_filename, NULL);
krh@317
   171
ali@424
   172
	if (razor_set_bind_sections(image->system, image->path,
ali@424
   173
				    RAZOR_SET_PRIVATE, error)) {
ali@403
   174
		free(image->path);
ali@403
   175
		razor_set_unref(image->system);
krh@248
   176
		free(image);
krh@248
   177
		return NULL;
krh@248
   178
	}
krh@248
   179
krh@248
   180
	return image;
krh@248
   181
}
krh@248
   182
krh@269
   183
RAZOR_EXPORT struct razor_set *
ali@424
   184
razor_root_open_read_only(const char *root, struct razor_error **error)
krh@248
   185
{
ali@403
   186
	char *path;
ali@388
   187
	struct razor_set *set;
krh@248
   188
richard@301
   189
	assert (root != NULL);
richard@301
   190
ali@340
   191
	razor_root_init();
ali@388
   192
	set = razor_set_create_without_root();
ali@403
   193
	if (set == NULL) {
ali@424
   194
		razor_set_error(error, NULL, "Not enough memory");
ali@388
   195
		return NULL;
ali@388
   196
	}
ali@388
   197
ali@403
   198
	path = razor_concat(root, razor_root_path, "/", system_lock_filename,
ali@403
   199
			    NULL);
ali@403
   200
	if (razor_set_aquire_lock(set, path, 0) < 0) {
ali@424
   201
		razor_set_error(error, NULL,
ali@424
   202
				"Failed to aquire non-exclusive system lock");
ali@403
   203
		free(path);
ali@403
   204
		razor_set_unref(set);
ali@388
   205
		return NULL;
ali@388
   206
	}
ali@388
   207
ali@403
   208
	free(path);
ali@403
   209
	path = razor_concat(root, razor_root_path, "/", system_repo_filename,
ali@403
   210
			    NULL);
ali@403
   211
ali@424
   212
	if (razor_set_bind_sections(set, path, 0, error)) {
ali@403
   213
		razor_set_unref(set);
ali@403
   214
		set = NULL;
ali@403
   215
	}
ali@403
   216
ali@403
   217
	free(path);
ali@403
   218
ali@388
   219
	return set;
krh@248
   220
}
krh@248
   221
krh@269
   222
RAZOR_EXPORT struct razor_set *
krh@250
   223
razor_root_get_system_set(struct razor_root *root)
krh@248
   224
{
richard@301
   225
	assert (root != NULL);
richard@301
   226
krh@250
   227
	return root->system;
krh@248
   228
}
krh@248
   229
krh@269
   230
RAZOR_EXPORT int
krh@250
   231
razor_root_close(struct razor_root *root)
krh@248
   232
{
richard@301
   233
	assert (root != NULL);
richard@301
   234
ali@403
   235
	razor_set_unref(root->system);
ali@403
   236
	free(root->path);
krh@250
   237
	free(root);
krh@248
   238
krh@248
   239
	return 0;
krh@248
   240
}
krh@248
   241
ali@424
   242
RAZOR_EXPORT int
ali@424
   243
razor_root_update(struct razor_root *root, struct razor_set *next,
ali@424
   244
		  struct razor_atomic *atomic)
krh@248
   245
{
ali@424
   246
	int handle, retval;
ali@424
   247
richard@301
   248
	assert (root != NULL);
richard@301
   249
	assert (next != NULL);
richard@301
   250
ali@424
   251
	handle = razor_atomic_create_file(atomic, root->path,
ali@424
   252
					  S_IRWXU | S_IRWXG | S_IRWXO);
ali@424
   253
	if (handle < 0)
ali@424
   254
		return handle;
krh@248
   255
ali@424
   256
	razor_set_write_to_handle(next, atomic, handle, RAZOR_SECTION_ALL);
krh@248
   257
ali@424
   258
	retval = razor_atomic_close(atomic, handle);
richard@301
   259
ali@424
   260
	if (!retval) {
ali@424
   261
		razor_set_unref(root->system);
ali@424
   262
		root->system = razor_set_ref(next);
ali@424
   263
	}
krh@248
   264
ali@346
   265
	return retval;
krh@248
   266
}