librazor/root.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Jan 09 16:35:09 2009 +0000 (2009-01-09)
changeset 341 0a11e755ccda
parent 340 5962a461a5a3
child 345 edd9b0fa63ca
permissions -rw-r--r--
Close files before deleting them for compatibility with MS-Windows
     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 static const char system_repo_filename[] = "system.rzdb";
    41 static const char system_repo_details_filename[] = "system-details.rzdb";
    42 static const char system_repo_files_filename[] = "system-files.rzdb";
    43 
    44 static const char next_repo_filename[] = "system-next.rzdb";
    45 #ifdef MSWIN_API
    46 #define RAZOR_ROOT_PATH	NULL
    47 #else
    48 #define RAZOR_ROOT_PATH	"/var/lib/razor"
    49 #endif
    50 static const char *razor_root_path = RAZOR_ROOT_PATH;
    51 
    52 struct razor_root {
    53 	struct razor_set *system;
    54 	struct razor_set *next;
    55 	int fd;
    56 	char root[PATH_MAX];
    57 	char path[PATH_MAX];
    58 	char new_path[PATH_MAX];
    59 };
    60 
    61 static void
    62 razor_root_init(void)
    63 {
    64 #ifdef MSWIN_API
    65 	static char root_path[MAX_PATH];
    66 	if (!razor_root_path) {
    67 		SHGetFolderPath(NULL,
    68 			CSIDL_COMMON_APPDATA | CSIDL_FLAG_DONT_VERIFY, NULL, 0,
    69 			root_path);
    70 		strcat(root_path, "\\Razor");
    71 		razor_root_path = root_path;
    72 	}
    73 #endif
    74 }
    75 
    76 RAZOR_EXPORT int
    77 razor_root_create(const char *root)
    78 {
    79 	struct stat buf;
    80 	struct razor_set *set;
    81 	char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
    82 
    83 	assert (root != NULL);
    84 
    85 	razor_root_init();
    86 	if (root[0] == '\0') {
    87 		/* root is file system root */
    88 	} else if (stat(root, &buf) < 0) {
    89 		if (mkdir(root, 0777) < 0) {
    90 			fprintf(stderr,
    91 				"could not create install root \"%s\"\n",
    92 				root);
    93 			return -1;
    94 		}
    95 		fprintf(stderr, "created install root \"%s\"\n", root);
    96 	} else if (!S_ISDIR(buf.st_mode)) {
    97 		fprintf(stderr,
    98 			"install root \"%s\" exists, but is not a directory\n",
    99 			root);
   100 		return -1;
   101 	}
   102 
   103 	snprintf(path, sizeof path, "%s/%s",
   104 		 razor_root_path, system_repo_filename);
   105 	if (razor_create_dir(root, path) < 0) {
   106 		fprintf(stderr, "could not create %s%s\n",
   107 			root, razor_root_path);
   108 		return -1;
   109 	}
   110 
   111 	set = razor_set_create();
   112 	snprintf(path, sizeof path, "%s%s/%s",
   113 		 root, razor_root_path, system_repo_filename);
   114 	snprintf(details_path, sizeof details_path, "%s%s/%s",
   115 		 root, razor_root_path, system_repo_details_filename);
   116 	snprintf(files_path, sizeof files_path, "%s%s/%s",
   117 		 root, razor_root_path, system_repo_files_filename);
   118 	if (stat(path, &buf) == 0) {
   119 		fprintf(stderr,
   120 			"a razor install root is already initialized\n");
   121 		return -1;
   122 	}
   123 	if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
   124 	    razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
   125 	    razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
   126 		fprintf(stderr, "could not write initial package set\n");
   127 		return -1;
   128 	}
   129 	razor_set_destroy(set);
   130 
   131 	return 0;
   132 }
   133 
   134 RAZOR_EXPORT struct razor_root *
   135 razor_root_open(const char *root)
   136 {
   137 	struct razor_root *image;
   138 	char details_path[PATH_MAX], files_path[PATH_MAX];
   139 
   140 	assert (root != NULL);
   141 
   142 	razor_root_init();
   143 	image = malloc(sizeof *image);
   144 	if (image == NULL)
   145 		return NULL;
   146 
   147 	/* Create the new next repo file up front to ensure exclusive
   148 	 * access. */
   149 	snprintf(image->new_path, sizeof image->new_path,
   150 		 "%s%s/%s", root, razor_root_path, next_repo_filename);
   151 	image->fd = open(image->new_path,
   152 			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
   153 	if (image->fd < 0) {
   154 		fprintf(stderr, "failed to get lock file, "
   155 			"maybe previous operation crashed?\n");
   156 
   157 		/* FIXME: Use fcntl advisory locking on the system
   158 		 * package set file to figure out whether previous
   159 		 * operation crashed or is still in progress. */
   160 
   161 		free(image);
   162 		return NULL;
   163 	}
   164 
   165 	snprintf(image->path, sizeof image->path,
   166 		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   167 	snprintf(details_path, sizeof details_path,
   168 		 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
   169 	snprintf(files_path, sizeof files_path,
   170 		 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
   171 
   172 	/* FIXME: We store the root path to make the hack in
   173 	 * razor_root_update() work.  Need to get rid of this. */
   174 	strcpy(image->root, root);
   175 
   176 	image->system = razor_set_open(image->path);
   177 	if (image->system == NULL ||
   178 	    razor_set_open_details(image->system, details_path) ||
   179 	    razor_set_open_files(image->system, files_path)) {
   180 		unlink(image->new_path);
   181 		close(image->fd);
   182 		free(image);
   183 		return NULL;
   184 	}
   185 
   186 	return image;
   187 }
   188 
   189 RAZOR_EXPORT struct razor_set *
   190 razor_root_open_read_only(const char *root)
   191 {
   192 	char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
   193 	struct razor_set *set;
   194 
   195 	assert (root != NULL);
   196 
   197 	razor_root_init();
   198 	snprintf(path, sizeof path, "%s%s/%s",
   199 		 root, razor_root_path, system_repo_filename);
   200 	snprintf(details_path, sizeof details_path,
   201 		 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
   202 	snprintf(files_path, sizeof files_path,
   203 		 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
   204 
   205 
   206 	set = razor_set_open(path);
   207 	if (set == NULL)
   208 		return NULL;
   209 
   210 	if (razor_set_open_details(set, details_path) ||
   211 	    razor_set_open_files(set, files_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 	char path[PATH_MAX];
   244 
   245 	assert (root != NULL);
   246 	assert (next != NULL);
   247 
   248 	razor_root_init();
   249 	razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
   250 	root->next = next;
   251 
   252 	/* FIXME: This is a pretty bad hack that just overwrites the
   253 	 * system details and files rzdb files before the transaction
   254 	 * succeeds.  We need to fix this by merging the separate
   255 	 * details and files rzdb files back into the main rzdb
   256 	 * file. */
   257 	snprintf(path, sizeof path,
   258 		 "%s%s/%s", root->root, razor_root_path, system_repo_details_filename);
   259 	razor_set_write(next, path, RAZOR_REPO_FILE_DETAILS);
   260 	snprintf(path, sizeof path,
   261 		 "%s%s/%s", root->root, razor_root_path, system_repo_files_filename);
   262 	razor_set_write(next, path, RAZOR_REPO_FILE_FILES);
   263 
   264 	/* Sync the new repo file so the new package set is on disk
   265 	 * before we start upgrading. */
   266 	fsync(root->fd);
   267 	printf("wrote %s\n", root->new_path);
   268 }
   269 
   270 RAZOR_EXPORT int
   271 razor_root_commit(struct razor_root *root)
   272 {
   273 	assert (root != NULL);
   274 
   275 	/* Make it so. */
   276 	rename(root->new_path, root->path);
   277 	printf("renamed %s to %s\n", root->new_path, root->path);
   278 	razor_set_destroy(root->system);
   279 	close(root->fd);
   280 	free(root);
   281 
   282 	return 0;
   283 }