Allow multiple atomic transactions to be used with one root object.
This allows transactions that include barriers to be performed
while holding an exclusive system lock.
2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2009, 2011, 2012 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";
53 #define RAZOR_ROOT_PATH NULL
55 #define RAZOR_ROOT_PATH "/var/lib/razor"
57 static const char *razor_root_path = RAZOR_ROOT_PATH;
60 struct razor_set *system;
68 static char root_path[MAX_PATH];
69 if (!razor_root_path) {
71 CSIDL_COMMON_APPDATA | CSIDL_FLAG_DONT_VERIFY, NULL, 0,
73 strcat(root_path, "\\Razor");
74 razor_root_path = root_path;
80 razor_root_create(const char *root)
84 struct razor_set *set;
85 struct razor_atomic *atomic;
88 assert (root != NULL);
91 if (root[0] == '\0') {
92 /* root is file system root */
93 } else if (stat(root, &buf) < 0) {
94 if (mkdir(root, 0777) < 0) {
96 "could not create install root \"%s\"\n",
100 fprintf(stderr, "created install root \"%s\"\n", root);
101 } else if (!S_ISDIR(buf.st_mode)) {
103 "install root \"%s\" exists, but is not a directory\n",
108 file = razor_concat(razor_root_path, "/", system_repo_filename, NULL);
109 path = razor_concat(root, file, NULL);
110 retval = !stat(path, &buf);
113 "a razor install root is already initialized\n");
119 atomic = razor_atomic_open("Create initial package set");
120 razor_atomic_make_dirs(atomic, root, file);
121 set = razor_set_create();
122 razor_set_write(set, atomic, path, RAZOR_SECTION_ALL);
125 retval = razor_atomic_commit(atomic);
127 fprintf(stderr, "could not write initial package set\n");
128 razor_set_unref(set);
129 razor_atomic_destroy(atomic);
134 RAZOR_EXPORT struct razor_root *
135 razor_root_open(const char *root, struct razor_error **error)
137 struct razor_root *image;
141 assert (root != NULL);
144 image = malloc(sizeof *image);
146 razor_set_error(error, NULL, "Not enough memory");
150 image->system = razor_set_create_without_root();
151 if (image->system == NULL) {
153 razor_set_error(error, NULL, "Not enough memory");
157 lock_path = razor_concat(root, razor_root_path, "/",
158 system_lock_filename, NULL);
160 r = razor_set_aquire_lock(image->system, lock_path, 1);
165 razor_set_error(error, NULL,
166 "Failed to aquire exclusive system lock");
167 razor_set_unref(image->system);
172 image->path = razor_concat(root, razor_root_path, "/",
173 system_repo_filename, NULL);
175 if (razor_set_bind_sections(image->system, image->path,
176 RAZOR_SET_PRIVATE, error)) {
178 razor_set_unref(image->system);
186 RAZOR_EXPORT struct razor_set *
187 razor_root_open_read_only(const char *root, struct razor_error **error)
190 struct razor_set *set;
192 assert (root != NULL);
195 set = razor_set_create_without_root();
197 razor_set_error(error, NULL, "Not enough memory");
201 path = razor_concat(root, razor_root_path, "/", system_lock_filename,
203 if (razor_set_aquire_lock(set, path, 0) < 0) {
204 razor_set_error(error, NULL,
205 "Failed to aquire non-exclusive system lock");
207 razor_set_unref(set);
212 path = razor_concat(root, razor_root_path, "/", system_repo_filename,
215 if (razor_set_bind_sections(set, path, 0, error)) {
216 razor_set_unref(set);
225 RAZOR_EXPORT struct razor_set *
226 razor_root_get_system_set(struct razor_root *root)
228 assert (root != NULL);
234 razor_root_close(struct razor_root *root)
236 assert (root != NULL);
238 razor_set_unref(root->system);
246 razor_root_update(struct razor_root *root, struct razor_set *next,
247 struct razor_atomic *atomic)
251 assert (root != NULL);
252 assert (next != NULL);
254 handle = razor_atomic_create_file(atomic, root->path,
255 S_IRWXU | S_IRWXG | S_IRWXO);
259 razor_set_write_to_handle(next, atomic, handle, RAZOR_SECTION_ALL);
261 retval = razor_atomic_close(atomic, handle);
264 razor_set_unref(root->system);
265 root->system = razor_set_ref(next);