2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
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.
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.
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.
28 #include "razor-internal.h"
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";
34 static const char next_repo_filename[] = "system-next.repo";
35 static const char razor_root_path[] = "/var/lib/razor";
38 struct razor_set *system;
39 struct razor_set *next;
42 char new_path[PATH_MAX];
46 razor_root_create(const char *root)
49 struct razor_set *set;
50 char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
52 if (stat(root, &buf) < 0) {
53 if (mkdir(root, 0777) < 0) {
55 "could not create install root \"%s\"\n",
59 fprintf(stderr, "created install root \"%s\"\n", root);
60 } else if (!S_ISDIR(buf.st_mode)) {
62 "install root \"%s\" exists, but is not a directory\n",
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);
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) {
84 "a razor install root is already initialized\n");
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");
93 razor_set_destroy(set);
98 RAZOR_EXPORT struct razor_root *
99 razor_root_open(const char *root)
101 struct razor_root *image;
103 image = malloc(sizeof *image);
107 /* Create the new next repo file up front to ensure exclusive
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);
114 fprintf(stderr, "failed to get lock file, "
115 "maybe previous operation crashed?\n");
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. */
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);
138 RAZOR_EXPORT struct razor_set *
139 razor_root_open_read_only(const char *root)
143 snprintf(path, sizeof path, "%s%s/%s",
144 root, razor_root_path, system_repo_filename);
146 return razor_set_open(path);
149 RAZOR_EXPORT struct razor_set *
150 razor_root_get_system_set(struct razor_root *root)
156 razor_root_close(struct razor_root *root)
158 razor_set_destroy(root->system);
159 unlink(root->new_path);
167 razor_root_update(struct razor_root *root, struct razor_set *next)
169 razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
172 /* Sync the new repo file so the new package set is on disk
173 * before we start upgrading. */
175 printf("wrote %s\n", root->new_path);
179 razor_root_commit(struct razor_root *root)
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);