Only include sys/wait.h on platforms that have it.
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.
32 #include "razor-internal.h"
34 static const char system_repo_filename[] = "system.rzdb";
35 static const char system_repo_details_filename[] = "system-details.rzdb";
36 static const char system_repo_files_filename[] = "system-files.rzdb";
38 static const char next_repo_filename[] = "system-next.rzdb";
39 static const char razor_root_path[] = "/var/lib/razor";
42 struct razor_set *system;
43 struct razor_set *next;
47 char new_path[PATH_MAX];
51 razor_root_create(const char *root)
54 struct razor_set *set;
55 char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
57 assert (root != NULL);
59 if (root[0] == '\0') {
60 /* root is file system root */
61 } else if (stat(root, &buf) < 0) {
62 if (mkdir(root, 0777) < 0) {
64 "could not create install root \"%s\"\n",
68 fprintf(stderr, "created install root \"%s\"\n", root);
69 } else if (!S_ISDIR(buf.st_mode)) {
71 "install root \"%s\" exists, but is not a directory\n",
76 snprintf(path, sizeof path, "%s/%s",
77 razor_root_path, system_repo_filename);
78 if (razor_create_dir(root, path) < 0) {
79 fprintf(stderr, "could not create %s%s\n",
80 root, razor_root_path);
84 set = razor_set_create();
85 snprintf(path, sizeof path, "%s%s/%s",
86 root, razor_root_path, system_repo_filename);
87 snprintf(details_path, sizeof details_path, "%s%s/%s",
88 root, razor_root_path, system_repo_details_filename);
89 snprintf(files_path, sizeof files_path, "%s%s/%s",
90 root, razor_root_path, system_repo_files_filename);
91 if (stat(path, &buf) == 0) {
93 "a razor install root is already initialized\n");
96 if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
97 razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
98 razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
99 fprintf(stderr, "could not write initial package set\n");
102 razor_set_destroy(set);
107 RAZOR_EXPORT struct razor_root *
108 razor_root_open(const char *root)
110 struct razor_root *image;
111 char details_path[PATH_MAX], files_path[PATH_MAX];
113 assert (root != NULL);
115 image = malloc(sizeof *image);
119 /* Create the new next repo file up front to ensure exclusive
121 snprintf(image->new_path, sizeof image->new_path,
122 "%s%s/%s", root, razor_root_path, next_repo_filename);
123 image->fd = open(image->new_path,
124 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
126 fprintf(stderr, "failed to get lock file, "
127 "maybe previous operation crashed?\n");
129 /* FIXME: Use fcntl advisory locking on the system
130 * package set file to figure out whether previous
131 * operation crashed or is still in progress. */
137 snprintf(image->path, sizeof image->path,
138 "%s%s/%s", root, razor_root_path, system_repo_filename);
139 snprintf(details_path, sizeof details_path,
140 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
141 snprintf(files_path, sizeof files_path,
142 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
144 /* FIXME: We store the root path to make the hack in
145 * razor_root_update() work. Need to get rid of this. */
146 strcpy(image->root, root);
148 image->system = razor_set_open(image->path);
149 if (image->system == NULL ||
150 razor_set_open_details(image->system, details_path) ||
151 razor_set_open_files(image->system, files_path)) {
152 unlink(image->new_path);
161 RAZOR_EXPORT struct razor_set *
162 razor_root_open_read_only(const char *root)
164 char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
165 struct razor_set *set;
167 assert (root != NULL);
169 snprintf(path, sizeof path, "%s%s/%s",
170 root, razor_root_path, system_repo_filename);
171 snprintf(details_path, sizeof details_path,
172 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
173 snprintf(files_path, sizeof files_path,
174 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
177 set = razor_set_open(path);
181 if (razor_set_open_details(set, details_path) ||
182 razor_set_open_files(set, files_path)) {
183 razor_set_destroy(set);
190 RAZOR_EXPORT struct razor_set *
191 razor_root_get_system_set(struct razor_root *root)
193 assert (root != NULL);
199 razor_root_close(struct razor_root *root)
201 assert (root != NULL);
203 razor_set_destroy(root->system);
204 unlink(root->new_path);
212 razor_root_update(struct razor_root *root, struct razor_set *next)
216 assert (root != NULL);
217 assert (next != NULL);
219 razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
222 /* FIXME: This is a pretty bad hack that just overwrites the
223 * system details and files rzdb files before the transaction
224 * succeeds. We need to fix this by merging the separate
225 * details and files rzdb files back into the main rzdb
227 snprintf(path, sizeof path,
228 "%s%s/%s", root->root, razor_root_path, system_repo_details_filename);
229 razor_set_write(next, path, RAZOR_REPO_FILE_DETAILS);
230 snprintf(path, sizeof path,
231 "%s%s/%s", root->root, razor_root_path, system_repo_files_filename);
232 razor_set_write(next, path, RAZOR_REPO_FILE_FILES);
234 /* Sync the new repo file so the new package set is on disk
235 * before we start upgrading. */
237 printf("wrote %s\n", root->new_path);
241 razor_root_commit(struct razor_root *root)
243 assert (root != NULL);
246 rename(root->new_path, root->path);
247 printf("renamed %s to %s\n", root->new_path, root->path);
248 razor_set_destroy(root->system);