librazor/root.c
author Kristian H?gsberg <krh@redhat.com>
Tue Jul 01 10:49:48 2008 -0400 (2008-07-01)
changeset 309 a69289c9080c
parent 300 455eaa569767
child 310 9a7691262ce6
permissions -rw-r--r--
Document the razor_importer API.
     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.repo";
    33 static const char system_repo_details_filename[] = "system-details.repo";
    34 static const char system_repo_files_filename[] = "system-files.repo";
    35 
    36 static const char next_repo_filename[] = "system-next.repo";
    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 
   107 	assert (root != NULL);
   108 
   109 	image = malloc(sizeof *image);
   110 	if (image == NULL)
   111 		return NULL;
   112 
   113 	/* Create the new next repo file up front to ensure exclusive
   114 	 * access. */
   115 	snprintf(image->new_path, sizeof image->new_path,
   116 		 "%s%s/%s", root, razor_root_path, next_repo_filename);
   117 	image->fd = open(image->new_path,
   118 			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
   119 	if (image->fd < 0) {
   120 		fprintf(stderr, "failed to get lock file, "
   121 			"maybe previous operation crashed?\n");
   122 
   123 		/* FIXME: Use fcntl advisory locking on the system
   124 		 * package set file to figure out whether previous
   125 		 * operation crashed or is still in progress. */
   126 
   127 		free(image);
   128 		return NULL;
   129 	}
   130 
   131 	snprintf(image->path, sizeof image->path,
   132 		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   133 	image->system = razor_set_open(image->path);
   134 	if (image->system == NULL) {
   135 		unlink(image->new_path);
   136 		close(image->fd);
   137 		free(image);
   138 		return NULL;
   139 	}
   140 
   141 	return image;
   142 }
   143 
   144 RAZOR_EXPORT struct razor_set *
   145 razor_root_open_read_only(const char *root)
   146 {
   147 	char path[PATH_MAX];
   148 
   149 	assert (root != NULL);
   150 
   151 	snprintf(path, sizeof path, "%s%s/%s",
   152 		 root, razor_root_path, system_repo_filename);
   153 
   154 	return razor_set_open(path);
   155 }
   156 
   157 RAZOR_EXPORT struct razor_set *
   158 razor_root_get_system_set(struct razor_root *root)
   159 {
   160 	assert (root != NULL);
   161 
   162 	return root->system;
   163 }
   164 
   165 RAZOR_EXPORT int
   166 razor_root_close(struct razor_root *root)
   167 {
   168 	assert (root != NULL);
   169 
   170 	razor_set_destroy(root->system);
   171 	unlink(root->new_path);
   172 	close(root->fd);
   173 	free(root);
   174 
   175 	return 0;
   176 }
   177 
   178 RAZOR_EXPORT void
   179 razor_root_update(struct razor_root *root, struct razor_set *next)
   180 {
   181 	assert (root != NULL);
   182 	assert (next != NULL);
   183 
   184 	razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
   185 	root->next = next;
   186 
   187 	/* Sync the new repo file so the new package set is on disk
   188 	 * before we start upgrading. */
   189 	fsync(root->fd);
   190 	printf("wrote %s\n", root->new_path);
   191 }
   192 
   193 RAZOR_EXPORT int
   194 razor_root_commit(struct razor_root *root)
   195 {
   196 	assert (root != NULL);
   197 
   198 	/* Make it so. */
   199 	rename(root->new_path, root->path);
   200 	printf("renamed %s to %s\n", root->new_path, root->path);
   201 	razor_set_destroy(root->system);
   202 	close(root->fd);
   203 	free(root);
   204 
   205 	return 0;
   206 }