librazor/root.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Jan 27 07:55:30 2012 +0000 (2012-01-27)
changeset 405 f960eb19dca2
parent 388 6a6462ce8a08
child 406 5ab137def3d1
permissions -rw-r--r--
Start 0.5.1
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  * Copyright (C) 2009, 2011  J. Ali Harlow <ali@juiblex.co.uk>
     5  *
     6  * This program is free software; you can redistribute it and/or modify
     7  * it under the terms of the GNU General Public License as published by
     8  * the Free Software Foundation; either version 2 of the License, or
     9  * (at your option) any later version.
    10  *
    11  * This program is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along
    17  * with this program; if not, write to the Free Software Foundation, Inc.,
    18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    19  */
    20 
    21 #include "config.h"
    22 
    23 #include <stdlib.h>
    24 #include <stdint.h>
    25 #include <stdio.h>
    26 #include <string.h>
    27 #include <sys/stat.h>
    28 #include <dirent.h>
    29 #include <unistd.h>
    30 #include <fcntl.h>
    31 #include <limits.h>
    32 #include <assert.h>
    33 #ifdef MSWIN_API
    34 #include <shlobj.h>
    35 #endif
    36 
    37 #include "razor.h"
    38 #include "razor-internal.h"
    39 
    40 #ifndef O_BINARY
    41 #define O_BINARY	0
    42 #endif
    43 
    44 static const char system_repo_filename[] = "system.rzdb";
    45 /*
    46  * system_lock_filename is chosen to be the same as the pre v0.3
    47  * next_repo_filename. This means that once a system has been
    48  * updated by a v0.3+ copy of razor all pre v0.3 versions of razor
    49  * will see the system as permenantly locked.
    50  */
    51 static const char system_lock_filename[] = "system-next.rzdb";
    52 static const char system_tmp_filename[] = "system.tmp";
    53 #ifdef MSWIN_API
    54 #define RAZOR_ROOT_PATH	NULL
    55 #else
    56 #define RAZOR_ROOT_PATH	"/var/lib/razor"
    57 #endif
    58 static const char *razor_root_path = RAZOR_ROOT_PATH;
    59 
    60 struct razor_root {
    61 	struct razor_set *system;
    62 	struct razor_set *next;
    63 	struct razor_atomic *atomic;
    64 	int handle;
    65 	char *path, *new_path;
    66 };
    67 
    68 static void
    69 razor_root_init(void)
    70 {
    71 #ifdef MSWIN_API
    72 	static char root_path[MAX_PATH];
    73 	if (!razor_root_path) {
    74 		SHGetFolderPath(NULL,
    75 			CSIDL_COMMON_APPDATA | CSIDL_FLAG_DONT_VERIFY, NULL, 0,
    76 			root_path);
    77 		strcat(root_path, "\\Razor");
    78 		razor_root_path = root_path;
    79 	}
    80 #endif
    81 }
    82 
    83 RAZOR_EXPORT int
    84 razor_root_create(const char *root)
    85 {
    86 	int retval;
    87 	struct stat buf;
    88 	struct razor_set *set;
    89 	struct razor_atomic *atomic;
    90 	char *path;
    91 
    92 	assert (root != NULL);
    93 
    94 	razor_root_init();
    95 	if (root[0] == '\0') {
    96 		/* root is file system root */
    97 	} else if (stat(root, &buf) < 0) {
    98 		if (mkdir(root, 0777) < 0) {
    99 			fprintf(stderr,
   100 				"could not create install root \"%s\"\n",
   101 				root);
   102 			return -1;
   103 		}
   104 		fprintf(stderr, "created install root \"%s\"\n", root);
   105 	} else if (!S_ISDIR(buf.st_mode)) {
   106 		fprintf(stderr,
   107 			"install root \"%s\" exists, but is not a directory\n",
   108 			root);
   109 		return -1;
   110 	}
   111 
   112 	path = razor_concat(root, razor_root_path, "/", system_repo_filename,
   113 			    NULL);
   114 	retval = !stat(path, &buf);
   115 	free(path);
   116 	if (retval) {
   117 		fprintf(stderr,
   118 			"a razor install root is already initialized\n");
   119 		return retval;
   120 	}
   121 
   122 	atomic = razor_atomic_open("Create initial package set");
   123 	path = razor_concat(razor_root_path, "/", system_repo_filename, NULL);
   124 	razor_atomic_make_dirs(atomic, root, path);
   125 	set = razor_set_create();
   126 	razor_set_write(set, atomic, path, RAZOR_SECTION_ALL);
   127 	free(path);
   128 	retval = razor_atomic_commit(atomic);
   129 	if (retval)
   130 		fprintf(stderr, "could not write initial package set\n");
   131 	razor_set_unref(set);
   132 	razor_atomic_destroy(atomic);
   133 
   134 	return retval;
   135 }
   136 
   137 RAZOR_EXPORT struct razor_root *
   138 razor_root_open(const char *root, struct razor_atomic *atomic)
   139 {
   140 	struct razor_root *image;
   141 	char *lock_path;
   142 	int r;
   143 
   144 	assert (root != NULL);
   145 
   146 	razor_root_init();
   147 	image = malloc(sizeof *image);
   148 	if (image == NULL) {
   149 		razor_atomic_abort(atomic, "Not enough memory");
   150 		return NULL;
   151 	}
   152 
   153 	image->atomic = atomic;
   154 
   155 	image->system = razor_set_create_without_root();
   156 	if (image->system == NULL) {
   157 		free(image);
   158 		razor_atomic_abort(atomic, "Not enough memory");
   159 		return NULL;
   160 	}
   161 
   162 	lock_path = razor_concat(root, razor_root_path, "/",
   163 				 system_lock_filename, NULL);
   164 
   165 	r = razor_set_aquire_lock(image->system, lock_path, 1);
   166 
   167 	free(lock_path);
   168 
   169 	if (r < 0) {
   170 		razor_atomic_abort(atomic,
   171 				   "Failed to aquire exclusive system lock");
   172 		razor_set_unref(image->system);
   173 		free(image);
   174 		return NULL;
   175 	}
   176 
   177 	image->new_path = razor_concat(root, razor_root_path, "/",
   178 				       system_tmp_filename, NULL);
   179 	image->handle = razor_atomic_create_file(atomic, image->new_path,
   180 						 S_IRWXU | S_IRWXG | S_IRWXO);
   181 	if (image->handle < 0) {
   182 		free(image->new_path);
   183 		razor_set_unref(image->system);
   184 		free(image);
   185 		return NULL;
   186 	}
   187 
   188 	image->path = razor_concat(root, razor_root_path, "/",
   189 				   system_repo_filename, NULL);
   190 
   191 	if (razor_set_bind_sections(image->system, atomic, image->path)) {
   192 		unlink(image->new_path);
   193 		free(image->new_path);
   194 		free(image->path);
   195 		razor_set_unref(image->system);
   196 		free(image);
   197 		return NULL;
   198 	}
   199 
   200 	return image;
   201 }
   202 
   203 RAZOR_EXPORT struct razor_set *
   204 razor_root_open_read_only(const char *root, struct razor_atomic *atomic)
   205 {
   206 	char *path;
   207 	struct razor_set *set;
   208 
   209 	assert (root != NULL);
   210 
   211 	razor_root_init();
   212 	set = razor_set_create_without_root();
   213 	if (set == NULL) {
   214 		razor_atomic_abort(atomic, "Not enough memory");
   215 		return NULL;
   216 	}
   217 
   218 	path = razor_concat(root, razor_root_path, "/", system_lock_filename,
   219 			    NULL);
   220 	if (razor_set_aquire_lock(set, path, 0) < 0) {
   221 		razor_atomic_abort(atomic, "Failed to aquire non-exclusive "
   222 					   "system lock");
   223 		free(path);
   224 		razor_set_unref(set);
   225 		return NULL;
   226 	}
   227 
   228 	free(path);
   229 	path = razor_concat(root, razor_root_path, "/", system_repo_filename,
   230 			    NULL);
   231 
   232 	if (razor_set_bind_sections(set, atomic, path)) {
   233 		razor_set_unref(set);
   234 		set = NULL;
   235 	}
   236 
   237 	free(path);
   238 
   239 	return set;
   240 }
   241 
   242 RAZOR_EXPORT struct razor_set *
   243 razor_root_get_system_set(struct razor_root *root)
   244 {
   245 	assert (root != NULL);
   246 
   247 	return root->system;
   248 }
   249 
   250 RAZOR_EXPORT int
   251 razor_root_close(struct razor_root *root)
   252 {
   253 	assert (root != NULL);
   254 
   255 	razor_set_unref(root->system);
   256 	razor_atomic_close(root->atomic, root->handle);
   257 	razor_atomic_remove(root->atomic, root->new_path);
   258 	free(root->path);
   259 	free(root->new_path);
   260 	free(root);
   261 
   262 	return 0;
   263 }
   264 
   265 RAZOR_EXPORT void
   266 razor_root_update(struct razor_root *root, struct razor_set *next)
   267 {
   268 	assert (root != NULL);
   269 	assert (next != NULL);
   270 
   271 	razor_root_init();
   272 	razor_set_write_to_handle(next, root->atomic, root->handle,
   273 				  RAZOR_SECTION_ALL);
   274 	root->next = next;
   275 
   276 	/* Sync the new repo file so the new package set is on disk
   277 	 * before we start upgrading. */
   278 	razor_atomic_sync(root->atomic, root->handle);
   279 }
   280 
   281 RAZOR_EXPORT int
   282 razor_root_commit(struct razor_root *root)
   283 {
   284 	int retval;
   285 	assert (root != NULL);
   286 
   287 	razor_atomic_close(root->atomic, root->handle);
   288 	retval = razor_atomic_rename_file(root->atomic, root->new_path,
   289 					  root->path);
   290 	razor_set_unref(root->system);
   291 	free(root->path);
   292 	free(root->new_path);
   293 	free(root);
   294 
   295 	return retval;
   296 }