librazor/root.c
author J. Ali Harlow <ali@juiblex.co.uk>
Wed Aug 24 15:28:52 2011 +0100 (2011-08-24)
changeset 399 98aade0d875b
parent 373 fda83d91e600
child 403 e63951c1d0f8
permissions -rw-r--r--
Update gnulib
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  * Copyright (C) 2009  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 	int fd;
    64 	char path[PATH_MAX];
    65 	char new_path[PATH_MAX];
    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 	struct stat buf;
    87 	struct razor_set *set;
    88 	char path[PATH_MAX];
    89 
    90 	assert (root != NULL);
    91 
    92 	razor_root_init();
    93 	if (root[0] == '\0') {
    94 		/* root is file system root */
    95 	} else if (stat(root, &buf) < 0) {
    96 		if (mkdir(root, 0777) < 0) {
    97 			fprintf(stderr,
    98 				"could not create install root \"%s\"\n",
    99 				root);
   100 			return -1;
   101 		}
   102 		fprintf(stderr, "created install root \"%s\"\n", root);
   103 	} else if (!S_ISDIR(buf.st_mode)) {
   104 		fprintf(stderr,
   105 			"install root \"%s\" exists, but is not a directory\n",
   106 			root);
   107 		return -1;
   108 	}
   109 
   110 	snprintf(path, sizeof path, "%s/%s",
   111 		 razor_root_path, system_repo_filename);
   112 	if (razor_create_dir(root, path) < 0) {
   113 		fprintf(stderr, "could not create %s%s\n",
   114 			root, razor_root_path);
   115 		return -1;
   116 	}
   117 
   118 	set = razor_set_create();
   119 	snprintf(path, sizeof path, "%s%s/%s",
   120 		 root, razor_root_path, system_repo_filename);
   121 	if (stat(path, &buf) == 0) {
   122 		fprintf(stderr,
   123 			"a razor install root is already initialized\n");
   124 		return -1;
   125 	}
   126 	if (razor_set_write(set, path, RAZOR_SECTION_ALL) < 0) {
   127 		fprintf(stderr, "could not write initial package set\n");
   128 		return -1;
   129 	}
   130 	razor_set_destroy(set);
   131 
   132 	return 0;
   133 }
   134 
   135 RAZOR_EXPORT struct razor_root *
   136 razor_root_open(const char *root)
   137 {
   138 	struct razor_root *image;
   139 	char lock_path[PATH_MAX];
   140 
   141 	assert (root != NULL);
   142 
   143 	razor_root_init();
   144 	image = malloc(sizeof *image);
   145 	if (image == NULL)
   146 		return NULL;
   147 
   148 	image->system = razor_set_create_without_root();
   149 	if (image->system == NULL) {
   150 		free(image);
   151 		return NULL;
   152 	}
   153 
   154 	snprintf(lock_path, sizeof lock_path,
   155 		 "%s%s/%s", root, razor_root_path, system_lock_filename);
   156 
   157 	if (razor_set_aquire_lock(image->system, lock_path, 1) < 0) {
   158 		razor_set_destroy(image->system);
   159 		free(image);
   160 		return NULL;
   161 	}
   162 
   163 	snprintf(image->new_path, sizeof image->new_path,
   164 		 "%s%s/%s", root, razor_root_path, system_tmp_filename);
   165 	image->fd = open(image->new_path,
   166 			 O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,
   167 			 0666);
   168 	if (image->fd < 0) {
   169 		razor_set_destroy(image->system);
   170 		free(image);
   171 		return NULL;
   172 	}
   173 
   174 	snprintf(image->path, sizeof image->path,
   175 		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   176 
   177 	if (razor_set_bind_sections(image->system, image->path)) {
   178 		close(image->fd);
   179 		unlink(image->new_path);
   180 		razor_set_destroy(image->system);
   181 		free(image);
   182 		return NULL;
   183 	}
   184 
   185 	return image;
   186 }
   187 
   188 RAZOR_EXPORT struct razor_set *
   189 razor_root_open_read_only(const char *root)
   190 {
   191 	char path[PATH_MAX];
   192 	struct razor_set *set;
   193 
   194 	assert (root != NULL);
   195 
   196 	razor_root_init();
   197 	set = razor_set_create_without_root();
   198 	if (set == NULL)
   199 		return NULL;
   200 
   201 	snprintf(path, sizeof path,
   202 		 "%s%s/%s", root, razor_root_path, system_lock_filename);
   203 	if (razor_set_aquire_lock(set, path, 0) < 0) {
   204 		razor_set_destroy(set);
   205 		return NULL;
   206 	}
   207 
   208 	snprintf(path, sizeof path, "%s%s/%s",
   209 		 root, razor_root_path, system_repo_filename);
   210 
   211 	if (razor_set_bind_sections(set, path)) {
   212 		razor_set_destroy(set);
   213 		return NULL;
   214 	}
   215 
   216 	return set;
   217 }
   218 
   219 RAZOR_EXPORT struct razor_set *
   220 razor_root_get_system_set(struct razor_root *root)
   221 {
   222 	assert (root != NULL);
   223 
   224 	return root->system;
   225 }
   226 
   227 RAZOR_EXPORT int
   228 razor_root_close(struct razor_root *root)
   229 {
   230 	assert (root != NULL);
   231 
   232 	razor_set_destroy(root->system);
   233 	close(root->fd);
   234 	unlink(root->new_path);
   235 	free(root);
   236 
   237 	return 0;
   238 }
   239 
   240 RAZOR_EXPORT void
   241 razor_root_update(struct razor_root *root, struct razor_set *next)
   242 {
   243 	assert (root != NULL);
   244 	assert (next != NULL);
   245 
   246 	razor_root_init();
   247 	razor_set_write_to_fd(next, root->fd, RAZOR_SECTION_ALL);
   248 	root->next = next;
   249 
   250 	/* Sync the new repo file so the new package set is on disk
   251 	 * before we start upgrading. */
   252 	fsync(root->fd);
   253 	printf("wrote %s\n", root->new_path);
   254 }
   255 
   256 RAZOR_EXPORT int
   257 razor_root_commit(struct razor_root *root)
   258 {
   259 	int retval;
   260 	assert (root != NULL);
   261 
   262 	/* Make it so. */
   263 	close(root->fd);
   264 #ifdef MSWIN_API
   265 	/* Rename is not atomic under MS-Windows */
   266 	remove(root->path);
   267 #endif
   268 	retval = rename(root->new_path, root->path);
   269 	if (retval)
   270 		perror(root->path);
   271 	else
   272 		printf("renamed %s to %s\n", root->new_path, root->path);
   273 	razor_set_destroy(root->system);
   274 	free(root);
   275 
   276 	return retval;
   277 }