Include test.xml in the distribution since it is needed for make check.
2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2009 J. Ali Harlow <ali@juiblex.co.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
38 #include "razor-internal.h"
40 static const char system_repo_filename[] = "system.rzdb";
41 static const char system_repo_details_filename[] = "system-details.rzdb";
42 static const char system_repo_files_filename[] = "system-files.rzdb";
44 static const char next_repo_filename[] = "system-next.rzdb";
46 #define RAZOR_ROOT_PATH NULL
48 #define RAZOR_ROOT_PATH "/var/lib/razor"
50 static const char *razor_root_path = RAZOR_ROOT_PATH;
53 struct razor_set *system;
54 struct razor_set *next;
58 char new_path[PATH_MAX];
65 static char root_path[MAX_PATH];
66 if (!razor_root_path) {
68 CSIDL_COMMON_APPDATA | CSIDL_FLAG_DONT_VERIFY, NULL, 0,
70 strcat(root_path, "\\Razor");
71 razor_root_path = root_path;
77 razor_root_create(const char *root)
80 struct razor_set *set;
81 char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
83 assert (root != NULL);
86 if (root[0] == '\0') {
87 /* root is file system root */
88 } else if (stat(root, &buf) < 0) {
89 if (mkdir(root, 0777) < 0) {
91 "could not create install root \"%s\"\n",
95 fprintf(stderr, "created install root \"%s\"\n", root);
96 } else if (!S_ISDIR(buf.st_mode)) {
98 "install root \"%s\" exists, but is not a directory\n",
103 snprintf(path, sizeof path, "%s/%s",
104 razor_root_path, system_repo_filename);
105 if (razor_create_dir(root, path) < 0) {
106 fprintf(stderr, "could not create %s%s\n",
107 root, razor_root_path);
111 set = razor_set_create();
112 snprintf(path, sizeof path, "%s%s/%s",
113 root, razor_root_path, system_repo_filename);
114 snprintf(details_path, sizeof details_path, "%s%s/%s",
115 root, razor_root_path, system_repo_details_filename);
116 snprintf(files_path, sizeof files_path, "%s%s/%s",
117 root, razor_root_path, system_repo_files_filename);
118 if (stat(path, &buf) == 0) {
120 "a razor install root is already initialized\n");
123 if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
124 razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
125 razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
126 fprintf(stderr, "could not write initial package set\n");
129 razor_set_destroy(set);
134 RAZOR_EXPORT struct razor_root *
135 razor_root_open(const char *root)
137 struct razor_root *image;
138 char details_path[PATH_MAX], files_path[PATH_MAX];
140 assert (root != NULL);
143 image = malloc(sizeof *image);
147 /* Create the new next repo file up front to ensure exclusive
149 snprintf(image->new_path, sizeof image->new_path,
150 "%s%s/%s", root, razor_root_path, next_repo_filename);
151 image->fd = open(image->new_path,
152 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
154 fprintf(stderr, "failed to get lock file, "
155 "maybe previous operation crashed?\n");
157 /* FIXME: Use fcntl advisory locking on the system
158 * package set file to figure out whether previous
159 * operation crashed or is still in progress. */
165 snprintf(image->path, sizeof image->path,
166 "%s%s/%s", root, razor_root_path, system_repo_filename);
167 snprintf(details_path, sizeof details_path,
168 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
169 snprintf(files_path, sizeof files_path,
170 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
172 /* FIXME: We store the root path to make the hack in
173 * razor_root_update() work. Need to get rid of this. */
174 strcpy(image->root, root);
176 image->system = razor_set_open(image->path);
177 if (image->system == NULL ||
178 razor_set_open_details(image->system, details_path) ||
179 razor_set_open_files(image->system, files_path)) {
180 unlink(image->new_path);
189 RAZOR_EXPORT struct razor_set *
190 razor_root_open_read_only(const char *root)
192 char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
193 struct razor_set *set;
195 assert (root != NULL);
198 snprintf(path, sizeof path, "%s%s/%s",
199 root, razor_root_path, system_repo_filename);
200 snprintf(details_path, sizeof details_path,
201 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
202 snprintf(files_path, sizeof files_path,
203 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
206 set = razor_set_open(path);
210 if (razor_set_open_details(set, details_path) ||
211 razor_set_open_files(set, files_path)) {
212 razor_set_destroy(set);
219 RAZOR_EXPORT struct razor_set *
220 razor_root_get_system_set(struct razor_root *root)
222 assert (root != NULL);
228 razor_root_close(struct razor_root *root)
230 assert (root != NULL);
232 razor_set_destroy(root->system);
234 unlink(root->new_path);
241 razor_root_update(struct razor_root *root, struct razor_set *next)
245 assert (root != NULL);
246 assert (next != NULL);
249 razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
252 /* FIXME: This is a pretty bad hack that just overwrites the
253 * system details and files rzdb files before the transaction
254 * succeeds. We need to fix this by merging the separate
255 * details and files rzdb files back into the main rzdb
257 snprintf(path, sizeof path,
258 "%s%s/%s", root->root, razor_root_path, system_repo_details_filename);
259 razor_set_write(next, path, RAZOR_REPO_FILE_DETAILS);
260 snprintf(path, sizeof path,
261 "%s%s/%s", root->root, razor_root_path, system_repo_files_filename);
262 razor_set_write(next, path, RAZOR_REPO_FILE_FILES);
264 /* Sync the new repo file so the new package set is on disk
265 * before we start upgrading. */
267 printf("wrote %s\n", root->new_path);
271 razor_root_commit(struct razor_root *root)
273 assert (root != NULL);
276 rename(root->new_path, root->path);
277 printf("renamed %s to %s\n", root->new_path, root->path);
278 razor_set_destroy(root->system);