librazor/root.c
author Kristian H?gsberg <krh@redhat.com>
Tue Jul 08 22:02:58 2008 -0400 (2008-07-08)
changeset 316 5ebed314390c
parent 310 9a7691262ce6
child 317 019a53b65271
permissions -rw-r--r--
Change the install iterator API to what we want.
     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 <sys/stat.h>
    24 #include <dirent.h>
    25 #include <unistd.h>
    26 #include <fcntl.h>
    27 #include <assert.h>
    28 
    29 #include "razor.h"
    30 #include "razor-internal.h"
    31 
    32 static const char system_repo_filename[] = "system.rzdb";
    33 static const char system_repo_details_filename[] = "system-details.rzdb";
    34 static const char system_repo_files_filename[] = "system-files.rzdb";
    35 
    36 static const char next_repo_filename[] = "system-next.rzdb";
    37 static const char razor_root_path[] = "/var/lib/razor";
    38 
    39 struct razor_root {
    40 	struct razor_set *system;
    41 	struct razor_set *next;
    42 	int fd;
    43 	char path[PATH_MAX];
    44 	char new_path[PATH_MAX];
    45 };
    46 
    47 RAZOR_EXPORT int
    48 razor_root_create(const char *root)
    49 {
    50 	struct stat buf;
    51 	struct razor_set *set;
    52 	char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
    53 
    54 	assert (root != NULL);
    55 
    56 	if (stat(root, &buf) < 0) {
    57 		if (mkdir(root, 0777) < 0) {
    58 			fprintf(stderr,
    59 				"could not create install root \"%s\"\n",
    60 				root);
    61 			return -1;
    62 		}
    63 		fprintf(stderr, "created install root \"%s\"\n", root);
    64 	} else if (!S_ISDIR(buf.st_mode)) {
    65 		fprintf(stderr,
    66 			"install root \"%s\" exists, but is not a directory\n",
    67 			root);
    68 		return -1;
    69 	}
    70 
    71 	snprintf(path, sizeof path, "%s/%s",
    72 		 razor_root_path, system_repo_filename);
    73 	if (razor_create_dir(root, path) < 0) {
    74 		fprintf(stderr, "could not create %s%s\n",
    75 			root, razor_root_path);
    76 		return -1;
    77 	}
    78 
    79 	set = razor_set_create();
    80 	snprintf(path, sizeof path, "%s%s/%s",
    81 		 root, razor_root_path, system_repo_filename);
    82 	snprintf(details_path, sizeof details_path, "%s%s/%s",
    83 		 root, razor_root_path, system_repo_details_filename);
    84 	snprintf(files_path, sizeof files_path, "%s%s/%s",
    85 		 root, razor_root_path, system_repo_files_filename);
    86 	if (stat(path, &buf) == 0) {
    87 		fprintf(stderr,
    88 			"a razor install root is already initialized\n");
    89 		return -1;
    90 	}
    91 	if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
    92 	    razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
    93 	    razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
    94 		fprintf(stderr, "could not write initial package set\n");
    95 		return -1;
    96 	}
    97 	razor_set_destroy(set);
    98 
    99 	return 0;
   100 }
   101 
   102 RAZOR_EXPORT struct razor_root *
   103 razor_root_open(const char *root)
   104 {
   105 	struct razor_root *image;
   106 	char details_path[PATH_MAX], files_path[PATH_MAX];
   107 
   108 	assert (root != NULL);
   109 
   110 	image = malloc(sizeof *image);
   111 	if (image == NULL)
   112 		return NULL;
   113 
   114 	/* Create the new next repo file up front to ensure exclusive
   115 	 * access. */
   116 	snprintf(image->new_path, sizeof image->new_path,
   117 		 "%s%s/%s", root, razor_root_path, next_repo_filename);
   118 	image->fd = open(image->new_path,
   119 			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
   120 	if (image->fd < 0) {
   121 		fprintf(stderr, "failed to get lock file, "
   122 			"maybe previous operation crashed?\n");
   123 
   124 		/* FIXME: Use fcntl advisory locking on the system
   125 		 * package set file to figure out whether previous
   126 		 * operation crashed or is still in progress. */
   127 
   128 		free(image);
   129 		return NULL;
   130 	}
   131 
   132 	snprintf(image->path, sizeof image->path,
   133 		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   134 	snprintf(details_path, sizeof details_path,
   135 		 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
   136 	snprintf(files_path, sizeof files_path,
   137 		 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
   138 
   139 	image->system = razor_set_open(image->path);
   140 	if (image->system == NULL ||
   141 	    razor_set_open_details(image->system, details_path) ||
   142 	    razor_set_open_files(image->system, files_path)) {
   143 		unlink(image->new_path);
   144 		close(image->fd);
   145 		free(image);
   146 		return NULL;
   147 	}
   148 
   149 	return image;
   150 }
   151 
   152 RAZOR_EXPORT struct razor_set *
   153 razor_root_open_read_only(const char *root)
   154 {
   155 	char path[PATH_MAX];
   156 
   157 	assert (root != NULL);
   158 
   159 	snprintf(path, sizeof path, "%s%s/%s",
   160 		 root, razor_root_path, system_repo_filename);
   161 
   162 	return razor_set_open(path);
   163 }
   164 
   165 RAZOR_EXPORT struct razor_set *
   166 razor_root_get_system_set(struct razor_root *root)
   167 {
   168 	assert (root != NULL);
   169 
   170 	return root->system;
   171 }
   172 
   173 RAZOR_EXPORT int
   174 razor_root_close(struct razor_root *root)
   175 {
   176 	assert (root != NULL);
   177 
   178 	razor_set_destroy(root->system);
   179 	unlink(root->new_path);
   180 	close(root->fd);
   181 	free(root);
   182 
   183 	return 0;
   184 }
   185 
   186 RAZOR_EXPORT void
   187 razor_root_update(struct razor_root *root, struct razor_set *next)
   188 {
   189 	assert (root != NULL);
   190 	assert (next != NULL);
   191 
   192 	razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
   193 	root->next = next;
   194 
   195 	/* Sync the new repo file so the new package set is on disk
   196 	 * before we start upgrading. */
   197 	fsync(root->fd);
   198 	printf("wrote %s\n", root->new_path);
   199 }
   200 
   201 RAZOR_EXPORT int
   202 razor_root_commit(struct razor_root *root)
   203 {
   204 	assert (root != NULL);
   205 
   206 	/* Make it so. */
   207 	rename(root->new_path, root->path);
   208 	printf("renamed %s to %s\n", root->new_path, root->path);
   209 	razor_set_destroy(root->system);
   210 	close(root->fd);
   211 	free(root);
   212 
   213 	return 0;
   214 }