Implement relocatations when installing rpms.
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";
45 static const char system_repo_details_filename[] = "system-details.rzdb";
46 static const char system_repo_files_filename[] = "system-files.rzdb";
48 static const char next_repo_filename[] = "system-next.rzdb";
50 #define RAZOR_ROOT_PATH NULL
52 #define RAZOR_ROOT_PATH "/var/lib/razor"
54 static const char *razor_root_path = RAZOR_ROOT_PATH;
57 struct razor_set *system;
58 struct razor_set *next;
62 char new_path[PATH_MAX];
69 static char root_path[MAX_PATH];
70 if (!razor_root_path) {
72 CSIDL_COMMON_APPDATA | CSIDL_FLAG_DONT_VERIFY, NULL, 0,
74 strcat(root_path, "\\Razor");
75 razor_root_path = root_path;
81 razor_root_create(const char *root)
84 struct razor_set *set;
85 char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
87 assert (root != NULL);
90 if (root[0] == '\0') {
91 /* root is file system root */
92 } else if (stat(root, &buf) < 0) {
93 if (mkdir(root, 0777) < 0) {
95 "could not create install root \"%s\"\n",
99 fprintf(stderr, "created install root \"%s\"\n", root);
100 } else if (!S_ISDIR(buf.st_mode)) {
102 "install root \"%s\" exists, but is not a directory\n",
107 snprintf(path, sizeof path, "%s/%s",
108 razor_root_path, system_repo_filename);
109 if (razor_create_dir(root, path) < 0) {
110 fprintf(stderr, "could not create %s%s\n",
111 root, razor_root_path);
115 set = razor_set_create();
116 snprintf(path, sizeof path, "%s%s/%s",
117 root, razor_root_path, system_repo_filename);
118 snprintf(details_path, sizeof details_path, "%s%s/%s",
119 root, razor_root_path, system_repo_details_filename);
120 snprintf(files_path, sizeof files_path, "%s%s/%s",
121 root, razor_root_path, system_repo_files_filename);
122 if (stat(path, &buf) == 0) {
124 "a razor install root is already initialized\n");
127 if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
128 razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
129 razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
130 fprintf(stderr, "could not write initial package set\n");
133 razor_set_destroy(set);
138 RAZOR_EXPORT struct razor_root *
139 razor_root_open(const char *root)
141 struct razor_root *image;
142 char details_path[PATH_MAX], files_path[PATH_MAX];
144 assert (root != NULL);
147 image = malloc(sizeof *image);
151 /* Create the new next repo file up front to ensure exclusive
153 snprintf(image->new_path, sizeof image->new_path,
154 "%s%s/%s", root, razor_root_path, next_repo_filename);
155 image->fd = open(image->new_path,
156 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL | O_BINARY,
159 fprintf(stderr, "failed to get lock file, "
160 "maybe previous operation crashed?\n");
162 /* FIXME: Use fcntl advisory locking on the system
163 * package set file to figure out whether previous
164 * operation crashed or is still in progress. */
170 snprintf(image->path, sizeof image->path,
171 "%s%s/%s", root, razor_root_path, system_repo_filename);
172 snprintf(details_path, sizeof details_path,
173 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
174 snprintf(files_path, sizeof files_path,
175 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
177 /* FIXME: We store the root path to make the hack in
178 * razor_root_update() work. Need to get rid of this. */
179 strcpy(image->root, root);
181 image->system = razor_set_open(image->path);
182 if (image->system == NULL ||
183 razor_set_open_details(image->system, details_path) ||
184 razor_set_open_files(image->system, files_path)) {
185 unlink(image->new_path);
194 RAZOR_EXPORT struct razor_set *
195 razor_root_open_read_only(const char *root)
197 char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
198 struct razor_set *set;
200 assert (root != NULL);
203 snprintf(path, sizeof path, "%s%s/%s",
204 root, razor_root_path, system_repo_filename);
205 snprintf(details_path, sizeof details_path,
206 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
207 snprintf(files_path, sizeof files_path,
208 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
211 set = razor_set_open(path);
215 if (razor_set_open_details(set, details_path) ||
216 razor_set_open_files(set, files_path)) {
217 razor_set_destroy(set);
224 RAZOR_EXPORT struct razor_set *
225 razor_root_get_system_set(struct razor_root *root)
227 assert (root != NULL);
233 razor_root_close(struct razor_root *root)
235 assert (root != NULL);
237 razor_set_destroy(root->system);
239 unlink(root->new_path);
246 razor_root_update(struct razor_root *root, struct razor_set *next)
250 assert (root != NULL);
251 assert (next != NULL);
254 razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
257 /* FIXME: This is a pretty bad hack that just overwrites the
258 * system details and files rzdb files before the transaction
259 * succeeds. We need to fix this by merging the separate
260 * details and files rzdb files back into the main rzdb
262 snprintf(path, sizeof path,
263 "%s%s/%s", root->root, razor_root_path, system_repo_details_filename);
264 razor_set_write(next, path, RAZOR_REPO_FILE_DETAILS);
265 snprintf(path, sizeof path,
266 "%s%s/%s", root->root, razor_root_path, system_repo_files_filename);
267 razor_set_write(next, path, RAZOR_REPO_FILE_FILES);
269 /* Sync the new repo file so the new package set is on disk
270 * before we start upgrading. */
272 printf("wrote %s\n", root->new_path);
276 razor_root_commit(struct razor_root *root)
279 assert (root != NULL);
284 /* Rename is not atomic under MS-Windows */
287 retval = rename(root->new_path, root->path);
291 printf("renamed %s to %s\n", root->new_path, root->path);
292 razor_set_destroy(root->system);