Add an error object.
This is intended to dis-entangle the two roles that the atomic
object has evolved into so that atomic need only be used where
atomic actions are actually being undertaken.
2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2009, 2011 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;
63 struct razor_atomic *atomic;
65 char *path, *new_path;
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)
88 struct razor_set *set;
89 struct razor_atomic *atomic;
92 assert (root != NULL);
95 if (root[0] == '\0') {
96 /* root is file system root */
97 } else if (stat(root, &buf) < 0) {
98 if (mkdir(root, 0777) < 0) {
100 "could not create install root \"%s\"\n",
104 fprintf(stderr, "created install root \"%s\"\n", root);
105 } else if (!S_ISDIR(buf.st_mode)) {
107 "install root \"%s\" exists, but is not a directory\n",
112 file = razor_concat(razor_root_path, "/", system_repo_filename, NULL);
113 path = razor_concat(root, file, NULL);
114 retval = !stat(path, &buf);
117 "a razor install root is already initialized\n");
123 atomic = razor_atomic_open("Create initial package set");
124 razor_atomic_make_dirs(atomic, root, file);
125 set = razor_set_create();
126 razor_set_write(set, atomic, path, RAZOR_SECTION_ALL);
129 retval = razor_atomic_commit(atomic);
131 fprintf(stderr, "could not write initial package set\n");
132 razor_set_unref(set);
133 razor_atomic_destroy(atomic);
138 RAZOR_EXPORT struct razor_root *
139 razor_root_open(const char *root, struct razor_atomic *atomic)
141 struct razor_root *image;
145 assert (root != NULL);
148 image = malloc(sizeof *image);
150 razor_atomic_abort(atomic, "Not enough memory");
154 image->atomic = atomic;
156 image->system = razor_set_create_without_root();
157 if (image->system == NULL) {
159 razor_atomic_abort(atomic, "Not enough memory");
163 lock_path = razor_concat(root, razor_root_path, "/",
164 system_lock_filename, NULL);
166 r = razor_set_aquire_lock(image->system, lock_path, 1);
171 razor_atomic_abort(atomic,
172 "Failed to aquire exclusive system lock");
173 razor_set_unref(image->system);
178 image->new_path = razor_concat(root, razor_root_path, "/",
179 system_tmp_filename, NULL);
180 image->handle = razor_atomic_create_file(atomic, image->new_path,
181 S_IRWXU | S_IRWXG | S_IRWXO);
182 if (image->handle < 0) {
183 free(image->new_path);
184 razor_set_unref(image->system);
189 image->path = razor_concat(root, razor_root_path, "/",
190 system_repo_filename, NULL);
192 if (razor_set_bind_sections(image->system, atomic, image->path)) {
193 unlink(image->new_path);
194 free(image->new_path);
196 razor_set_unref(image->system);
204 RAZOR_EXPORT struct razor_set *
205 razor_root_open_read_only(const char *root, struct razor_atomic *atomic)
208 struct razor_set *set;
210 assert (root != NULL);
213 set = razor_set_create_without_root();
215 razor_atomic_abort(atomic, "Not enough memory");
219 path = razor_concat(root, razor_root_path, "/", system_lock_filename,
221 if (razor_set_aquire_lock(set, path, 0) < 0) {
222 razor_atomic_abort(atomic, "Failed to aquire non-exclusive "
225 razor_set_unref(set);
230 path = razor_concat(root, razor_root_path, "/", system_repo_filename,
233 if (razor_set_bind_sections(set, atomic, path)) {
234 razor_set_unref(set);
243 RAZOR_EXPORT struct razor_set *
244 razor_root_get_system_set(struct razor_root *root)
246 assert (root != NULL);
252 razor_root_close(struct razor_root *root)
254 assert (root != NULL);
256 razor_set_unref(root->system);
257 razor_atomic_close(root->atomic, root->handle);
258 razor_atomic_remove(root->atomic, root->new_path);
260 free(root->new_path);
267 razor_root_update(struct razor_root *root, struct razor_set *next)
269 assert (root != NULL);
270 assert (next != NULL);
273 razor_set_write_to_handle(next, root->atomic, root->handle,
277 /* Sync the new repo file so the new package set is on disk
278 * before we start upgrading. */
279 razor_atomic_sync(root->atomic, root->handle);
283 razor_root_commit(struct razor_root *root)
286 assert (root != NULL);
288 razor_atomic_close(root->atomic, root->handle);
289 retval = razor_atomic_rename_file(root->atomic, root->new_path,
291 razor_set_unref(root->system);
293 free(root->new_path);