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