librazor/root.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Jan 08 13:51:07 2009 +0000 (2009-01-08)
changeset 327 c85643dd7164
parent 317 019a53b65271
child 340 5962a461a5a3
permissions -rw-r--r--
Don't try and create symbolic links on platforms that don't support symlink()
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; either version 2 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along
    16  * with this program; if not, write to the Free Software Foundation, Inc.,
    17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    18  */
    19 
    20 #include <stdlib.h>
    21 #include <stdint.h>
    22 #include <stdio.h>
    23 #include <string.h>
    24 #include <sys/stat.h>
    25 #include <dirent.h>
    26 #include <unistd.h>
    27 #include <fcntl.h>
    28 #include <limits.h>
    29 #include <assert.h>
    30 
    31 #include "razor.h"
    32 #include "razor-internal.h"
    33 
    34 static const char system_repo_filename[] = "system.rzdb";
    35 static const char system_repo_details_filename[] = "system-details.rzdb";
    36 static const char system_repo_files_filename[] = "system-files.rzdb";
    37 
    38 static const char next_repo_filename[] = "system-next.rzdb";
    39 static const char razor_root_path[] = "/var/lib/razor";
    40 
    41 struct razor_root {
    42 	struct razor_set *system;
    43 	struct razor_set *next;
    44 	int fd;
    45 	char root[PATH_MAX];
    46 	char path[PATH_MAX];
    47 	char new_path[PATH_MAX];
    48 };
    49 
    50 RAZOR_EXPORT int
    51 razor_root_create(const char *root)
    52 {
    53 	struct stat buf;
    54 	struct razor_set *set;
    55 	char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
    56 
    57 	assert (root != NULL);
    58 
    59 	if (root[0] == '\0') {
    60 		/* root is file system root */
    61 	} else if (stat(root, &buf) < 0) {
    62 		if (mkdir(root, 0777) < 0) {
    63 			fprintf(stderr,
    64 				"could not create install root \"%s\"\n",
    65 				root);
    66 			return -1;
    67 		}
    68 		fprintf(stderr, "created install root \"%s\"\n", root);
    69 	} else if (!S_ISDIR(buf.st_mode)) {
    70 		fprintf(stderr,
    71 			"install root \"%s\" exists, but is not a directory\n",
    72 			root);
    73 		return -1;
    74 	}
    75 
    76 	snprintf(path, sizeof path, "%s/%s",
    77 		 razor_root_path, system_repo_filename);
    78 	if (razor_create_dir(root, path) < 0) {
    79 		fprintf(stderr, "could not create %s%s\n",
    80 			root, razor_root_path);
    81 		return -1;
    82 	}
    83 
    84 	set = razor_set_create();
    85 	snprintf(path, sizeof path, "%s%s/%s",
    86 		 root, razor_root_path, system_repo_filename);
    87 	snprintf(details_path, sizeof details_path, "%s%s/%s",
    88 		 root, razor_root_path, system_repo_details_filename);
    89 	snprintf(files_path, sizeof files_path, "%s%s/%s",
    90 		 root, razor_root_path, system_repo_files_filename);
    91 	if (stat(path, &buf) == 0) {
    92 		fprintf(stderr,
    93 			"a razor install root is already initialized\n");
    94 		return -1;
    95 	}
    96 	if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
    97 	    razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
    98 	    razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
    99 		fprintf(stderr, "could not write initial package set\n");
   100 		return -1;
   101 	}
   102 	razor_set_destroy(set);
   103 
   104 	return 0;
   105 }
   106 
   107 RAZOR_EXPORT struct razor_root *
   108 razor_root_open(const char *root)
   109 {
   110 	struct razor_root *image;
   111 	char details_path[PATH_MAX], files_path[PATH_MAX];
   112 
   113 	assert (root != NULL);
   114 
   115 	image = malloc(sizeof *image);
   116 	if (image == NULL)
   117 		return NULL;
   118 
   119 	/* Create the new next repo file up front to ensure exclusive
   120 	 * access. */
   121 	snprintf(image->new_path, sizeof image->new_path,
   122 		 "%s%s/%s", root, razor_root_path, next_repo_filename);
   123 	image->fd = open(image->new_path,
   124 			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
   125 	if (image->fd < 0) {
   126 		fprintf(stderr, "failed to get lock file, "
   127 			"maybe previous operation crashed?\n");
   128 
   129 		/* FIXME: Use fcntl advisory locking on the system
   130 		 * package set file to figure out whether previous
   131 		 * operation crashed or is still in progress. */
   132 
   133 		free(image);
   134 		return NULL;
   135 	}
   136 
   137 	snprintf(image->path, sizeof image->path,
   138 		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   139 	snprintf(details_path, sizeof details_path,
   140 		 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
   141 	snprintf(files_path, sizeof files_path,
   142 		 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
   143 
   144 	/* FIXME: We store the root path to make the hack in
   145 	 * razor_root_update() work.  Need to get rid of this. */
   146 	strcpy(image->root, root);
   147 
   148 	image->system = razor_set_open(image->path);
   149 	if (image->system == NULL ||
   150 	    razor_set_open_details(image->system, details_path) ||
   151 	    razor_set_open_files(image->system, files_path)) {
   152 		unlink(image->new_path);
   153 		close(image->fd);
   154 		free(image);
   155 		return NULL;
   156 	}
   157 
   158 	return image;
   159 }
   160 
   161 RAZOR_EXPORT struct razor_set *
   162 razor_root_open_read_only(const char *root)
   163 {
   164 	char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
   165 	struct razor_set *set;
   166 
   167 	assert (root != NULL);
   168 
   169 	snprintf(path, sizeof path, "%s%s/%s",
   170 		 root, razor_root_path, system_repo_filename);
   171 	snprintf(details_path, sizeof details_path,
   172 		 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
   173 	snprintf(files_path, sizeof files_path,
   174 		 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
   175 
   176 
   177 	set = razor_set_open(path);
   178 	if (set == NULL)
   179 		return NULL;
   180 
   181 	if (razor_set_open_details(set, details_path) ||
   182 	    razor_set_open_files(set, files_path)) {
   183 		razor_set_destroy(set);
   184 		return NULL;
   185 	}
   186 
   187 	return set;
   188 }
   189 
   190 RAZOR_EXPORT struct razor_set *
   191 razor_root_get_system_set(struct razor_root *root)
   192 {
   193 	assert (root != NULL);
   194 
   195 	return root->system;
   196 }
   197 
   198 RAZOR_EXPORT int
   199 razor_root_close(struct razor_root *root)
   200 {
   201 	assert (root != NULL);
   202 
   203 	razor_set_destroy(root->system);
   204 	unlink(root->new_path);
   205 	close(root->fd);
   206 	free(root);
   207 
   208 	return 0;
   209 }
   210 
   211 RAZOR_EXPORT void
   212 razor_root_update(struct razor_root *root, struct razor_set *next)
   213 {
   214 	char path[PATH_MAX];
   215 
   216 	assert (root != NULL);
   217 	assert (next != NULL);
   218 
   219 	razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
   220 	root->next = next;
   221 
   222 	/* FIXME: This is a pretty bad hack that just overwrites the
   223 	 * system details and files rzdb files before the transaction
   224 	 * succeeds.  We need to fix this by merging the separate
   225 	 * details and files rzdb files back into the main rzdb
   226 	 * file. */
   227 	snprintf(path, sizeof path,
   228 		 "%s%s/%s", root->root, razor_root_path, system_repo_details_filename);
   229 	razor_set_write(next, path, RAZOR_REPO_FILE_DETAILS);
   230 	snprintf(path, sizeof path,
   231 		 "%s%s/%s", root->root, razor_root_path, system_repo_files_filename);
   232 	razor_set_write(next, path, RAZOR_REPO_FILE_FILES);
   233 
   234 	/* Sync the new repo file so the new package set is on disk
   235 	 * before we start upgrading. */
   236 	fsync(root->fd);
   237 	printf("wrote %s\n", root->new_path);
   238 }
   239 
   240 RAZOR_EXPORT int
   241 razor_root_commit(struct razor_root *root)
   242 {
   243 	assert (root != NULL);
   244 
   245 	/* Make it so. */
   246 	rename(root->new_path, root->path);
   247 	printf("renamed %s to %s\n", root->new_path, root->path);
   248 	razor_set_destroy(root->system);
   249 	close(root->fd);
   250 	free(root);
   251 
   252 	return 0;
   253 }