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"
44 static const char system_repo_filename[] = "system.rzdb";
46 * system_lock_filename is chosen to be the same as the pre v0.3
47 * next_repo_filename. This means that once a system has been
48 * updated by a v0.3+ copy of razor all pre v0.3 versions of razor
49 * will see the system as permenantly locked.
51 static const char system_lock_filename[] = "system-next.rzdb";
52 static const char system_tmp_filename[] = "system.tmp";
54 #define RAZOR_ROOT_PATH NULL
56 #define RAZOR_ROOT_PATH "/var/lib/razor"
58 static const char *razor_root_path = RAZOR_ROOT_PATH;
61 struct razor_set *system;
62 struct razor_set *next;
65 char new_path[PATH_MAX];
72 static char root_path[MAX_PATH];
73 if (!razor_root_path) {
75 CSIDL_COMMON_APPDATA | CSIDL_FLAG_DONT_VERIFY, NULL, 0,
77 strcat(root_path, "\\Razor");
78 razor_root_path = root_path;
84 razor_root_create(const char *root)
87 struct razor_set *set;
90 assert (root != NULL);
93 if (root[0] == '\0') {
94 /* root is file system root */
95 } else if (stat(root, &buf) < 0) {
96 if (mkdir(root, 0777) < 0) {
98 "could not create install root \"%s\"\n",
102 fprintf(stderr, "created install root \"%s\"\n", root);
103 } else if (!S_ISDIR(buf.st_mode)) {
105 "install root \"%s\" exists, but is not a directory\n",
110 snprintf(path, sizeof path, "%s/%s",
111 razor_root_path, system_repo_filename);
112 if (razor_create_dir(root, path) < 0) {
113 fprintf(stderr, "could not create %s%s\n",
114 root, razor_root_path);
118 set = razor_set_create();
119 snprintf(path, sizeof path, "%s%s/%s",
120 root, razor_root_path, system_repo_filename);
121 if (stat(path, &buf) == 0) {
123 "a razor install root is already initialized\n");
126 if (razor_set_write(set, path, RAZOR_SECTION_ALL) < 0) {
127 fprintf(stderr, "could not write initial package set\n");
130 razor_set_destroy(set);
135 RAZOR_EXPORT struct razor_root *
136 razor_root_open(const char *root)
138 struct razor_root *image;
139 char lock_path[PATH_MAX];
141 assert (root != NULL);
144 image = malloc(sizeof *image);
148 image->system = razor_set_create_without_root();
149 if (image->system == NULL) {
154 snprintf(lock_path, sizeof lock_path,
155 "%s%s/%s", root, razor_root_path, system_lock_filename);
157 if (razor_set_aquire_lock(image->system, lock_path, 1) < 0) {
158 razor_set_destroy(image->system);
163 snprintf(image->new_path, sizeof image->new_path,
164 "%s%s/%s", root, razor_root_path, system_tmp_filename);
165 image->fd = open(image->new_path,
166 O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,
169 razor_set_destroy(image->system);
174 snprintf(image->path, sizeof image->path,
175 "%s%s/%s", root, razor_root_path, system_repo_filename);
177 if (razor_set_bind_sections(image->system, image->path)) {
179 unlink(image->new_path);
180 razor_set_destroy(image->system);
188 RAZOR_EXPORT struct razor_set *
189 razor_root_open_read_only(const char *root)
192 struct razor_set *set;
194 assert (root != NULL);
197 set = razor_set_create_without_root();
201 snprintf(path, sizeof path,
202 "%s%s/%s", root, razor_root_path, system_lock_filename);
203 if (razor_set_aquire_lock(set, path, 0) < 0) {
204 razor_set_destroy(set);
208 snprintf(path, sizeof path, "%s%s/%s",
209 root, razor_root_path, system_repo_filename);
211 if (razor_set_bind_sections(set, 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)
243 assert (root != NULL);
244 assert (next != NULL);
247 razor_set_write_to_fd(next, root->fd, RAZOR_SECTION_ALL);
250 /* Sync the new repo file so the new package set is on disk
251 * before we start upgrading. */
253 printf("wrote %s\n", root->new_path);
257 razor_root_commit(struct razor_root *root)
260 assert (root != NULL);
265 /* Rename is not atomic under MS-Windows */
268 retval = rename(root->new_path, root->path);
272 printf("renamed %s to %s\n", root->new_path, root->path);
273 razor_set_destroy(root->system);