Always open all three rzdb files when we're merging package sets.
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.
30 #include "razor-internal.h"
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";
36 static const char next_repo_filename[] = "system-next.rzdb";
37 static const char razor_root_path[] = "/var/lib/razor";
40 struct razor_set *system;
41 struct razor_set *next;
44 char new_path[PATH_MAX];
48 razor_root_create(const char *root)
51 struct razor_set *set;
52 char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
54 assert (root != NULL);
56 if (stat(root, &buf) < 0) {
57 if (mkdir(root, 0777) < 0) {
59 "could not create install root \"%s\"\n",
63 fprintf(stderr, "created install root \"%s\"\n", root);
64 } else if (!S_ISDIR(buf.st_mode)) {
66 "install root \"%s\" exists, but is not a directory\n",
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);
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) {
88 "a razor install root is already initialized\n");
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");
97 razor_set_destroy(set);
102 RAZOR_EXPORT struct razor_root *
103 razor_root_open(const char *root)
105 struct razor_root *image;
106 char details_path[PATH_MAX], files_path[PATH_MAX];
108 assert (root != NULL);
110 image = malloc(sizeof *image);
114 /* Create the new next repo file up front to ensure exclusive
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);
121 fprintf(stderr, "failed to get lock file, "
122 "maybe previous operation crashed?\n");
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. */
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);
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);
152 RAZOR_EXPORT struct razor_set *
153 razor_root_open_read_only(const char *root)
157 assert (root != NULL);
159 snprintf(path, sizeof path, "%s%s/%s",
160 root, razor_root_path, system_repo_filename);
162 return razor_set_open(path);
165 RAZOR_EXPORT struct razor_set *
166 razor_root_get_system_set(struct razor_root *root)
168 assert (root != NULL);
174 razor_root_close(struct razor_root *root)
176 assert (root != NULL);
178 razor_set_destroy(root->system);
179 unlink(root->new_path);
187 razor_root_update(struct razor_root *root, struct razor_set *next)
189 assert (root != NULL);
190 assert (next != NULL);
192 razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
195 /* Sync the new repo file so the new package set is on disk
196 * before we start upgrading. */
198 printf("wrote %s\n", root->new_path);
202 razor_root_commit(struct razor_root *root)
204 assert (root != NULL);
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);