2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2009, 2011, 2012, 2014 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"
52 static const char system_repo_filename[] = "system.rzdb";
54 * system_lock_filename is chosen to be the same as the pre v0.3
55 * next_repo_filename. This means that once a system has been
56 * updated by a v0.3+ copy of razor all pre v0.3 versions of razor
57 * will see the system as permenantly locked.
59 static const char system_lock_filename[] = "system-next.rzdb";
61 #define RAZOR_DATABASE_PATH NULL
63 #define RAZOR_DATABASE_PATH "/var/lib/razor"
65 static char *razor_database_path = RAZOR_DATABASE_PATH;
66 static int razor_database_path_alloced = FALSE;
69 struct razor_set *system;
77 static char database_path[MAX_PATH];
78 if (!razor_database_path) {
80 CSIDL_COMMON_APPDATA | CSIDL_FLAG_DONT_VERIFY, NULL, 0,
82 strcat(database_path, "\\Razor");
83 razor_database_path = database_path;
84 razor_database_path_alloced = FALSE;
89 RAZOR_EXPORT const char *
90 razor_get_database_path()
94 return razor_database_path;
98 razor_set_database_path(const char *database_path)
100 if (razor_database_path_alloced)
101 free(razor_database_path);
104 razor_database_path = strdup(database_path);
105 razor_database_path_alloced = TRUE;
107 razor_database_path = RAZOR_DATABASE_PATH;
108 razor_database_path_alloced = FALSE;
113 razor_root_create(const char *root, struct razor_error **error)
117 struct razor_set *set;
118 struct razor_atomic *atomic;
121 assert (root != NULL);
124 if (root[0] == '\0') {
125 /* root is file system root */
126 } else if (stat(root, &buf) < 0) {
127 if (mkdir(root, 0777) < 0) {
128 razor_set_error(error, root,
129 "Could not create install root");
132 } else if (!S_ISDIR(buf.st_mode)) {
133 razor_set_error(error, root, "Not a directory");
137 file = razor_concat(razor_database_path, "/", system_repo_filename,
139 path = razor_path_add_root(file, root);
140 retval = !stat(path, &buf);
142 razor_set_error(error, NULL,
143 "A razor install root is already initialized");
149 atomic = razor_atomic_open("Create initial package set");
150 razor_atomic_make_dirs(atomic, root, file);
151 set = razor_set_create();
152 razor_set_write(set, atomic, path, RAZOR_SECTION_ALL);
155 retval = razor_atomic_commit(atomic);
157 razor_propagate_error(error,
158 razor_atomic_get_error(atomic),
159 "Could not write initial package set");
160 razor_set_unref(set);
161 razor_atomic_destroy(atomic);
166 RAZOR_EXPORT struct razor_root *
167 razor_root_open(const char *root, struct razor_error **error)
169 struct razor_root *image;
173 assert (root != NULL);
176 image = malloc(sizeof *image);
178 razor_set_error(error, NULL, "Not enough memory");
182 image->system = razor_set_create_without_root();
183 if (image->system == NULL) {
185 razor_set_error(error, NULL, "Not enough memory");
189 s = razor_concat(razor_database_path, "/", system_lock_filename, NULL);
190 lock_path = razor_path_add_root(s, root);
193 r = razor_set_aquire_lock(image->system, lock_path, 1);
198 razor_set_error(error, NULL,
199 "Failed to aquire exclusive system lock");
200 razor_set_unref(image->system);
205 s = razor_concat(razor_database_path, "/", system_repo_filename, NULL);
206 image->path = razor_path_add_root(s, root);
209 if (razor_set_bind_sections(image->system, image->path,
210 RAZOR_SET_PRIVATE, error)) {
212 razor_set_unref(image->system);
220 RAZOR_EXPORT struct razor_set *
221 razor_root_open_read_only(const char *root, struct razor_error **error)
224 struct razor_set *set;
226 assert (root != NULL);
229 set = razor_set_create_without_root();
231 razor_set_error(error, NULL, "Not enough memory");
235 s = razor_concat(razor_database_path, "/", system_lock_filename, NULL);
236 path = razor_path_add_root(s, root);
239 if (razor_set_aquire_lock(set, path, 0) < 0) {
240 razor_set_error(error, NULL,
241 "Failed to aquire non-exclusive system lock");
243 razor_set_unref(set);
249 s = razor_concat(razor_database_path, "/", system_repo_filename, NULL);
250 path = razor_path_add_root(s, root);
253 if (razor_set_bind_sections(set, path, 0, error)) {
254 razor_set_unref(set);
263 RAZOR_EXPORT struct razor_set *
264 razor_root_get_system_set(struct razor_root *root)
266 assert (root != NULL);
272 razor_root_close(struct razor_root *root)
274 assert (root != NULL);
276 razor_set_unref(root->system);
284 razor_root_update(struct razor_root *root, struct razor_set *next,
285 struct razor_atomic *atomic)
289 assert (root != NULL);
290 assert (next != NULL);
292 handle = razor_atomic_create_file(atomic, root->path,
293 S_IRWXU | S_IRWXG | S_IRWXO);
297 razor_set_write_to_handle(next, atomic, handle, RAZOR_SECTION_ALL);
299 retval = razor_atomic_close(atomic, handle);
302 razor_set_unref(root->system);
303 root->system = razor_set_ref(next);