Use Windows KTM (atomic transactions) where supported.
Increment current header version to 2
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 path = razor_concat(root, razor_root_path, "/", system_repo_filename,
114 retval = !stat(path, &buf);
118 "a razor install root is already initialized\n");
122 atomic = razor_atomic_open("Create initial package set");
123 path = razor_concat(razor_root_path, "/", system_repo_filename, NULL);
124 razor_atomic_make_dirs(atomic, root, path);
125 set = razor_set_create();
126 razor_set_write(set, atomic, path, RAZOR_SECTION_ALL);
128 retval = razor_atomic_commit(atomic);
130 fprintf(stderr, "could not write initial package set\n");
131 razor_set_unref(set);
132 razor_atomic_destroy(atomic);
137 RAZOR_EXPORT struct razor_root *
138 razor_root_open(const char *root, struct razor_atomic *atomic)
140 struct razor_root *image;
144 assert (root != NULL);
147 image = malloc(sizeof *image);
149 razor_atomic_abort(atomic, "Not enough memory");
153 image->atomic = atomic;
155 image->system = razor_set_create_without_root();
156 if (image->system == NULL) {
158 razor_atomic_abort(atomic, "Not enough memory");
162 lock_path = razor_concat(root, razor_root_path, "/",
163 system_lock_filename, NULL);
165 r = razor_set_aquire_lock(image->system, lock_path, 1);
170 razor_atomic_abort(atomic,
171 "Failed to aquire exclusive system lock");
172 razor_set_unref(image->system);
177 image->new_path = razor_concat(root, razor_root_path, "/",
178 system_tmp_filename, NULL);
179 image->handle = razor_atomic_create_file(atomic, image->new_path,
180 S_IRWXU | S_IRWXG | S_IRWXO);
181 if (image->handle < 0) {
182 free(image->new_path);
183 razor_set_unref(image->system);
188 image->path = razor_concat(root, razor_root_path, "/",
189 system_repo_filename, NULL);
191 if (razor_set_bind_sections(image->system, atomic, image->path)) {
192 unlink(image->new_path);
193 free(image->new_path);
195 razor_set_unref(image->system);
203 RAZOR_EXPORT struct razor_set *
204 razor_root_open_read_only(const char *root, struct razor_atomic *atomic)
207 struct razor_set *set;
209 assert (root != NULL);
212 set = razor_set_create_without_root();
214 razor_atomic_abort(atomic, "Not enough memory");
218 path = razor_concat(root, razor_root_path, "/", system_lock_filename,
220 if (razor_set_aquire_lock(set, path, 0) < 0) {
221 razor_atomic_abort(atomic, "Failed to aquire non-exclusive "
224 razor_set_unref(set);
229 path = razor_concat(root, razor_root_path, "/", system_repo_filename,
232 if (razor_set_bind_sections(set, atomic, path)) {
233 razor_set_unref(set);
242 RAZOR_EXPORT struct razor_set *
243 razor_root_get_system_set(struct razor_root *root)
245 assert (root != NULL);
251 razor_root_close(struct razor_root *root)
253 assert (root != NULL);
255 razor_set_unref(root->system);
256 razor_atomic_close(root->atomic, root->handle);
257 razor_atomic_remove(root->atomic, root->new_path);
259 free(root->new_path);
266 razor_root_update(struct razor_root *root, struct razor_set *next)
268 assert (root != NULL);
269 assert (next != NULL);
272 razor_set_write_to_handle(next, root->atomic, root->handle,
276 /* Sync the new repo file so the new package set is on disk
277 * before we start upgrading. */
278 razor_atomic_sync(root->atomic, root->handle);
282 razor_root_commit(struct razor_root *root)
285 assert (root != NULL);
287 razor_atomic_close(root->atomic, root->handle);
288 retval = razor_atomic_rename_file(root->atomic, root->new_path,
290 razor_set_unref(root->system);
292 free(root->new_path);