librazor/root.c
author Richard Hughes <richard@hughsie.com>
Sun Jun 29 10:55:17 2008 +0100 (2008-06-29)
changeset 300 455eaa569767
parent 273 3e37dc49d6b7
child 301 4124c37fd953
permissions -rw-r--r--
add missing licencing information at the top of each source file

The licence text is needed when razor is packaged for Debian. :-)
     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 "razor.h"
    28 #include "razor-internal.h"
    29 
    30 static const char system_repo_filename[] = "system.repo";
    31 static const char system_repo_details_filename[] = "system-details.repo";
    32 static const char system_repo_files_filename[] = "system-files.repo";
    33 
    34 static const char next_repo_filename[] = "system-next.repo";
    35 static const char razor_root_path[] = "/var/lib/razor";
    36 
    37 struct razor_root {
    38 	struct razor_set *system;
    39 	struct razor_set *next;
    40 	int fd;
    41 	char path[PATH_MAX];
    42 	char new_path[PATH_MAX];
    43 };
    44 
    45 RAZOR_EXPORT int
    46 razor_root_create(const char *root)
    47 {
    48 	struct stat buf;
    49 	struct razor_set *set;
    50 	char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
    51 
    52 	if (stat(root, &buf) < 0) {
    53 		if (mkdir(root, 0777) < 0) {
    54 			fprintf(stderr,
    55 				"could not create install root \"%s\"\n",
    56 				root);
    57 			return -1;
    58 		}
    59 		fprintf(stderr, "created install root \"%s\"\n", root);
    60 	} else if (!S_ISDIR(buf.st_mode)) {
    61 		fprintf(stderr,
    62 			"install root \"%s\" exists, but is not a directory\n",
    63 			root);
    64 		return -1;
    65 	}
    66 
    67 	snprintf(path, sizeof path, "%s/%s",
    68 		 razor_root_path, system_repo_filename);
    69 	if (razor_create_dir(root, path) < 0) {
    70 		fprintf(stderr, "could not create %s%s\n",
    71 			root, razor_root_path);
    72 		return -1;
    73 	}
    74 
    75 	set = razor_set_create();
    76 	snprintf(path, sizeof path, "%s%s/%s",
    77 		 root, razor_root_path, system_repo_filename);
    78 	snprintf(details_path, sizeof details_path, "%s%s/%s",
    79 		 root, razor_root_path, system_repo_details_filename);
    80 	snprintf(files_path, sizeof files_path, "%s%s/%s",
    81 		 root, razor_root_path, system_repo_files_filename);
    82 	if (stat(path, &buf) == 0) {
    83 		fprintf(stderr,
    84 			"a razor install root is already initialized\n");
    85 		return -1;
    86 	}
    87 	if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
    88 	    razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
    89 	    razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
    90 		fprintf(stderr, "could not write initial package set\n");
    91 		return -1;
    92 	}
    93 	razor_set_destroy(set);
    94 
    95 	return 0;
    96 }
    97 
    98 RAZOR_EXPORT struct razor_root *
    99 razor_root_open(const char *root)
   100 {
   101 	struct razor_root *image;
   102 
   103 	image = malloc(sizeof *image);
   104 	if (image == NULL)
   105 		return NULL;
   106 
   107 	/* Create the new next repo file up front to ensure exclusive
   108 	 * access. */
   109 	snprintf(image->new_path, sizeof image->new_path,
   110 		 "%s%s/%s", root, razor_root_path, next_repo_filename);
   111 	image->fd = open(image->new_path,
   112 			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
   113 	if (image->fd < 0) {
   114 		fprintf(stderr, "failed to get lock file, "
   115 			"maybe previous operation crashed?\n");
   116 
   117 		/* FIXME: Use fcntl advisory locking on the system
   118 		 * package set file to figure out whether previous
   119 		 * operation crashed or is still in progress. */
   120 
   121 		free(image);
   122 		return NULL;
   123 	}
   124 
   125 	snprintf(image->path, sizeof image->path,
   126 		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   127 	image->system = razor_set_open(image->path);
   128 	if (image->system == NULL) {
   129 		unlink(image->new_path);
   130 		close(image->fd);
   131 		free(image);
   132 		return NULL;
   133 	}
   134 
   135 	return image;
   136 }
   137 
   138 RAZOR_EXPORT struct razor_set *
   139 razor_root_open_read_only(const char *root)
   140 {
   141 	char path[PATH_MAX];
   142 
   143 	snprintf(path, sizeof path, "%s%s/%s",
   144 		 root, razor_root_path, system_repo_filename);
   145 
   146 	return razor_set_open(path);
   147 }
   148 
   149 RAZOR_EXPORT struct razor_set *
   150 razor_root_get_system_set(struct razor_root *root)
   151 {
   152 	return root->system;
   153 }
   154 
   155 RAZOR_EXPORT int
   156 razor_root_close(struct razor_root *root)
   157 {
   158 	razor_set_destroy(root->system);
   159 	unlink(root->new_path);
   160 	close(root->fd);
   161 	free(root);
   162 
   163 	return 0;
   164 }
   165 
   166 RAZOR_EXPORT void
   167 razor_root_update(struct razor_root *root, struct razor_set *next)
   168 {
   169 	razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
   170 	root->next = next;
   171 
   172 	/* Sync the new repo file so the new package set is on disk
   173 	 * before we start upgrading. */
   174 	fsync(root->fd);
   175 	printf("wrote %s\n", root->new_path);
   176 }
   177 
   178 RAZOR_EXPORT int
   179 razor_root_commit(struct razor_root *root)
   180 {
   181 	/* Make it so. */
   182 	rename(root->new_path, root->path);
   183 	printf("renamed %s to %s\n", root->new_path, root->path);
   184 	razor_set_destroy(root->system);
   185 	close(root->fd);
   186 	free(root);
   187 
   188 	return 0;
   189 }