librazor/root.c
author J. Ali Harlow <ali@juiblex.co.uk>
Tue Sep 09 15:04:24 2014 +0100 (2014-09-09)
changeset 446 4277359896dc
parent 441 cf499fd51df7
child 447 0a5e583393e1
permissions -rw-r--r--
Add razor_transaction_unsatisfied()
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  * Copyright (C) 2009, 2011, 2012, 2014  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 #ifndef FALSE
    45 #define FALSE 0
    46 #endif
    47 
    48 #ifndef TRUE
    49 #define TRUE (!FALSE)
    50 #endif
    51 
    52 static const char system_repo_filename[] = "system.rzdb";
    53 /*
    54  * system_lock_filename is chosen to be the same as the pre v0.3
    55  * next_repo_filename. This means that once a system has been
    56  * updated by a v0.3+ copy of razor all pre v0.3 versions of razor
    57  * will see the system as permenantly locked.
    58  */
    59 static const char system_lock_filename[] = "system-next.rzdb";
    60 #ifdef MSWIN_API
    61 #define RAZOR_DATABASE_PATH	NULL
    62 #else
    63 #define RAZOR_DATABASE_PATH	"/var/lib/razor"
    64 #endif
    65 static char *razor_database_path = RAZOR_DATABASE_PATH;
    66 static int razor_database_path_alloced = FALSE;
    67 
    68 struct razor_root {
    69 	struct razor_set *system;
    70 	char *path;
    71 };
    72 
    73 static void
    74 razor_root_init(void)
    75 {
    76 #ifdef MSWIN_API
    77 	static char database_path[MAX_PATH];
    78 	if (!razor_database_path) {
    79 		SHGetFolderPath(NULL,
    80 			CSIDL_COMMON_APPDATA | CSIDL_FLAG_DONT_VERIFY, NULL, 0,
    81 			database_path);
    82 		strcat(database_path, "\\Razor");
    83 		razor_database_path = database_path;
    84 		razor_database_path_alloced = FALSE;
    85 	}
    86 #endif
    87 }
    88 
    89 RAZOR_EXPORT const char *
    90 razor_get_database_path()
    91 {
    92 	razor_root_init();
    93 
    94 	return razor_database_path;
    95 }
    96 
    97 RAZOR_EXPORT void
    98 razor_set_database_path(const char *database_path)
    99 {
   100 	if (razor_database_path_alloced)
   101 		free(razor_database_path);
   102 
   103 	if (database_path) {
   104 		razor_database_path = strdup(database_path);
   105 		razor_database_path_alloced = TRUE;
   106 	} else {
   107 		razor_database_path = RAZOR_DATABASE_PATH;
   108 		razor_database_path_alloced = FALSE;
   109 	}
   110 }
   111 
   112 RAZOR_EXPORT int
   113 razor_root_create(const char *root, struct razor_error **error)
   114 {
   115 	int retval;
   116 	struct stat buf;
   117 	struct razor_set *set;
   118 	struct razor_atomic *atomic;
   119 	char *file, *path;
   120 
   121 	assert (root != NULL);
   122 
   123 	razor_root_init();
   124 	if (root[0] == '\0') {
   125 		/* root is file system root */
   126 	} else if (stat(root, &buf) < 0) {
   127 		if (mkdir(root, 0777) < 0) {
   128 			razor_set_error(error, root,
   129 					"Could not create install root");
   130 			return -1;
   131 		}
   132 	} else if (!S_ISDIR(buf.st_mode)) {
   133 		razor_set_error(error, root, "Not a directory");
   134 		return -1;
   135 	}
   136 
   137 	file = razor_concat(razor_database_path, "/", system_repo_filename,
   138 			    NULL);
   139 	path = razor_path_add_root(file, root);
   140 	retval = !stat(path, &buf);
   141 	if (retval) {
   142 		razor_set_error(error, NULL,
   143 				"A razor install root is already initialized");
   144 		free(path);
   145 		free(file);
   146 		return retval;
   147 	}
   148 
   149 	atomic = razor_atomic_open("Create initial package set");
   150 	razor_atomic_make_dirs(atomic, root, file);
   151 	set = razor_set_create();
   152 	razor_set_write(set, atomic, path, RAZOR_SECTION_ALL);
   153 	free(path);
   154 	free(file);
   155 	retval = razor_atomic_commit(atomic);
   156 	if (retval)
   157 		razor_propagate_error(error,
   158 				      razor_atomic_get_error(atomic),
   159 				      "Could not write initial package set");
   160 	razor_set_unref(set);
   161 	razor_atomic_destroy(atomic);
   162 
   163 	return retval;
   164 }
   165 
   166 RAZOR_EXPORT struct razor_root *
   167 razor_root_open(const char *root, struct razor_error **error)
   168 {
   169 	struct razor_root *image;
   170 	char *s, *lock_path;
   171 	int r;
   172 
   173 	assert (root != NULL);
   174 
   175 	razor_root_init();
   176 	image = malloc(sizeof *image);
   177 	if (image == NULL) {
   178 		razor_set_error(error, NULL, "Not enough memory");
   179 		return NULL;
   180 	}
   181 
   182 	image->system = razor_set_create_without_root();
   183 	if (image->system == NULL) {
   184 		free(image);
   185 		razor_set_error(error, NULL, "Not enough memory");
   186 		return NULL;
   187 	}
   188 
   189 	s = razor_concat(razor_database_path, "/", system_lock_filename, NULL);
   190 	lock_path = razor_path_add_root(s, root);
   191 	free(s);
   192 
   193 	r = razor_set_aquire_lock(image->system, lock_path, 1);
   194 
   195 	free(lock_path);
   196 
   197 	if (r < 0) {
   198 		razor_set_error(error, NULL,
   199 				"Failed to aquire exclusive system lock");
   200 		razor_set_unref(image->system);
   201 		free(image);
   202 		return NULL;
   203 	}
   204 
   205 	s = razor_concat(razor_database_path, "/", system_repo_filename, NULL);
   206 	image->path = razor_path_add_root(s, root);
   207 	free(s);
   208 
   209 	if (razor_set_bind_sections(image->system, image->path,
   210 				    RAZOR_SET_PRIVATE, error)) {
   211 		free(image->path);
   212 		razor_set_unref(image->system);
   213 		free(image);
   214 		return NULL;
   215 	}
   216 
   217 	return image;
   218 }
   219 
   220 RAZOR_EXPORT struct razor_set *
   221 razor_root_open_read_only(const char *root, struct razor_error **error)
   222 {
   223 	char *s, *path;
   224 	struct razor_set *set;
   225 
   226 	assert (root != NULL);
   227 
   228 	razor_root_init();
   229 	set = razor_set_create_without_root();
   230 	if (set == NULL) {
   231 		razor_set_error(error, NULL, "Not enough memory");
   232 		return NULL;
   233 	}
   234 
   235 	s = razor_concat(razor_database_path, "/", system_lock_filename, NULL);
   236 	path = razor_path_add_root(s, root);
   237 	free(s);
   238 
   239 	if (razor_set_aquire_lock(set, path, 0) < 0) {
   240 		razor_set_error(error, NULL,
   241 				"Failed to aquire non-exclusive system lock");
   242 		free(path);
   243 		razor_set_unref(set);
   244 		return NULL;
   245 	}
   246 
   247 	free(path);
   248 
   249 	s = razor_concat(razor_database_path, "/", system_repo_filename, NULL);
   250 	path = razor_path_add_root(s, root);
   251 	free(s);
   252 
   253 	if (razor_set_bind_sections(set, path, 0, error)) {
   254 		razor_set_unref(set);
   255 		set = NULL;
   256 	}
   257 
   258 	free(path);
   259 
   260 	return set;
   261 }
   262 
   263 RAZOR_EXPORT struct razor_set *
   264 razor_root_get_system_set(struct razor_root *root)
   265 {
   266 	assert (root != NULL);
   267 
   268 	return root->system;
   269 }
   270 
   271 RAZOR_EXPORT int
   272 razor_root_close(struct razor_root *root)
   273 {
   274 	assert (root != NULL);
   275 
   276 	razor_set_unref(root->system);
   277 	free(root->path);
   278 	free(root);
   279 
   280 	return 0;
   281 }
   282 
   283 RAZOR_EXPORT int
   284 razor_root_update(struct razor_root *root, struct razor_set *next,
   285 		  struct razor_atomic *atomic)
   286 {
   287 	int handle, retval;
   288 
   289 	assert (root != NULL);
   290 	assert (next != NULL);
   291 
   292 	handle = razor_atomic_create_file(atomic, root->path,
   293 					  S_IRWXU | S_IRWXG | S_IRWXO);
   294 	if (handle < 0)
   295 		return handle;
   296 
   297 	razor_set_write_to_handle(next, atomic, handle, RAZOR_SECTION_ALL);
   298 
   299 	retval = razor_atomic_close(atomic, handle);
   300 
   301 	if (!retval) {
   302 		razor_set_unref(root->system);
   303 		root->system = razor_set_ref(next);
   304 	}
   305 
   306 	return retval;
   307 }