librazor/root.c
author J. Ali Harlow <ali@juiblex.co.uk>
Sat Aug 23 17:24:34 2014 +0100 (2014-08-23)
changeset 443 b2d6a8273459
parent 439 f28bb31024b4
child 445 aada48958b92
permissions -rw-r--r--
Update libtool versioning in preparation for release
     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_path_add_root(file, root);
   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_propagate_error(error,
   124 				      razor_atomic_get_error(atomic),
   125 				      "Could not write initial package set");
   126 	razor_set_unref(set);
   127 	razor_atomic_destroy(atomic);
   128 
   129 	return retval;
   130 }
   131 
   132 RAZOR_EXPORT struct razor_root *
   133 razor_root_open(const char *root, struct razor_error **error)
   134 {
   135 	struct razor_root *image;
   136 	char *s, *lock_path;
   137 	int r;
   138 
   139 	assert (root != NULL);
   140 
   141 	razor_root_init();
   142 	image = malloc(sizeof *image);
   143 	if (image == NULL) {
   144 		razor_set_error(error, NULL, "Not enough memory");
   145 		return NULL;
   146 	}
   147 
   148 	image->system = razor_set_create_without_root();
   149 	if (image->system == NULL) {
   150 		free(image);
   151 		razor_set_error(error, NULL, "Not enough memory");
   152 		return NULL;
   153 	}
   154 
   155 	s = razor_concat(razor_root_path, "/", system_lock_filename, NULL);
   156 	lock_path = razor_path_add_root(s, root);
   157 	free(s);
   158 
   159 	r = razor_set_aquire_lock(image->system, lock_path, 1);
   160 
   161 	free(lock_path);
   162 
   163 	if (r < 0) {
   164 		razor_set_error(error, NULL,
   165 				"Failed to aquire exclusive system lock");
   166 		razor_set_unref(image->system);
   167 		free(image);
   168 		return NULL;
   169 	}
   170 
   171 	s = razor_concat(razor_root_path, "/", system_repo_filename, NULL);
   172 	image->path = razor_path_add_root(s, root);
   173 	free(s);
   174 
   175 	if (razor_set_bind_sections(image->system, image->path,
   176 				    RAZOR_SET_PRIVATE, error)) {
   177 		free(image->path);
   178 		razor_set_unref(image->system);
   179 		free(image);
   180 		return NULL;
   181 	}
   182 
   183 	return image;
   184 }
   185 
   186 RAZOR_EXPORT struct razor_set *
   187 razor_root_open_read_only(const char *root, struct razor_error **error)
   188 {
   189 	char *s, *path;
   190 	struct razor_set *set;
   191 
   192 	assert (root != NULL);
   193 
   194 	razor_root_init();
   195 	set = razor_set_create_without_root();
   196 	if (set == NULL) {
   197 		razor_set_error(error, NULL, "Not enough memory");
   198 		return NULL;
   199 	}
   200 
   201 	s = razor_concat(razor_root_path, "/", system_lock_filename, NULL);
   202 	path = razor_path_add_root(s, root);
   203 	free(s);
   204 
   205 	if (razor_set_aquire_lock(set, path, 0) < 0) {
   206 		razor_set_error(error, NULL,
   207 				"Failed to aquire non-exclusive system lock");
   208 		free(path);
   209 		razor_set_unref(set);
   210 		return NULL;
   211 	}
   212 
   213 	free(path);
   214 
   215 	s = razor_concat(razor_root_path, "/", system_repo_filename, NULL);
   216 	path = razor_path_add_root(s, root);
   217 	free(s);
   218 
   219 	if (razor_set_bind_sections(set, path, 0, error)) {
   220 		razor_set_unref(set);
   221 		set = NULL;
   222 	}
   223 
   224 	free(path);
   225 
   226 	return set;
   227 }
   228 
   229 RAZOR_EXPORT struct razor_set *
   230 razor_root_get_system_set(struct razor_root *root)
   231 {
   232 	assert (root != NULL);
   233 
   234 	return root->system;
   235 }
   236 
   237 RAZOR_EXPORT int
   238 razor_root_close(struct razor_root *root)
   239 {
   240 	assert (root != NULL);
   241 
   242 	razor_set_unref(root->system);
   243 	free(root->path);
   244 	free(root);
   245 
   246 	return 0;
   247 }
   248 
   249 RAZOR_EXPORT int
   250 razor_root_update(struct razor_root *root, struct razor_set *next,
   251 		  struct razor_atomic *atomic)
   252 {
   253 	int handle, retval;
   254 
   255 	assert (root != NULL);
   256 	assert (next != NULL);
   257 
   258 	handle = razor_atomic_create_file(atomic, root->path,
   259 					  S_IRWXU | S_IRWXG | S_IRWXO);
   260 	if (handle < 0)
   261 		return handle;
   262 
   263 	razor_set_write_to_handle(next, atomic, handle, RAZOR_SECTION_ALL);
   264 
   265 	retval = razor_atomic_close(atomic, handle);
   266 
   267 	if (!retval) {
   268 		razor_set_unref(root->system);
   269 		root->system = razor_set_ref(next);
   270 	}
   271 
   272 	return retval;
   273 }