convert razor_package_get_details() and razor_package_iterator_next() to varargs
The functions for getting package details about a package were limited to a few
things, when in the future we will want to support much more about a package.
The iterator was also limited to name,version,arch when most of the time we
didn't need all this data.
2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "razor-internal.h"
32 static const char system_repo_filename[] = "system.repo";
33 static const char system_repo_details_filename[] = "system-details.repo";
34 static const char system_repo_files_filename[] = "system-files.repo";
36 static const char next_repo_filename[] = "system-next.repo";
37 static const char razor_root_path[] = "/var/lib/razor";
40 struct razor_set *system;
41 struct razor_set *next;
44 char new_path[PATH_MAX];
48 razor_root_create(const char *root)
51 struct razor_set *set;
52 char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
54 assert (root != NULL);
56 if (stat(root, &buf) < 0) {
57 if (mkdir(root, 0777) < 0) {
59 "could not create install root \"%s\"\n",
63 fprintf(stderr, "created install root \"%s\"\n", root);
64 } else if (!S_ISDIR(buf.st_mode)) {
66 "install root \"%s\" exists, but is not a directory\n",
71 snprintf(path, sizeof path, "%s/%s",
72 razor_root_path, system_repo_filename);
73 if (razor_create_dir(root, path) < 0) {
74 fprintf(stderr, "could not create %s%s\n",
75 root, razor_root_path);
79 set = razor_set_create();
80 snprintf(path, sizeof path, "%s%s/%s",
81 root, razor_root_path, system_repo_filename);
82 snprintf(details_path, sizeof details_path, "%s%s/%s",
83 root, razor_root_path, system_repo_details_filename);
84 snprintf(files_path, sizeof files_path, "%s%s/%s",
85 root, razor_root_path, system_repo_files_filename);
86 if (stat(path, &buf) == 0) {
88 "a razor install root is already initialized\n");
91 if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
92 razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
93 razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
94 fprintf(stderr, "could not write initial package set\n");
97 razor_set_destroy(set);
102 RAZOR_EXPORT struct razor_root *
103 razor_root_open(const char *root)
105 struct razor_root *image;
107 assert (root != NULL);
109 image = malloc(sizeof *image);
113 /* Create the new next repo file up front to ensure exclusive
115 snprintf(image->new_path, sizeof image->new_path,
116 "%s%s/%s", root, razor_root_path, next_repo_filename);
117 image->fd = open(image->new_path,
118 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
120 fprintf(stderr, "failed to get lock file, "
121 "maybe previous operation crashed?\n");
123 /* FIXME: Use fcntl advisory locking on the system
124 * package set file to figure out whether previous
125 * operation crashed or is still in progress. */
131 snprintf(image->path, sizeof image->path,
132 "%s%s/%s", root, razor_root_path, system_repo_filename);
133 image->system = razor_set_open(image->path);
134 if (image->system == NULL) {
135 unlink(image->new_path);
144 RAZOR_EXPORT struct razor_set *
145 razor_root_open_read_only(const char *root)
149 assert (root != NULL);
151 snprintf(path, sizeof path, "%s%s/%s",
152 root, razor_root_path, system_repo_filename);
154 return razor_set_open(path);
157 RAZOR_EXPORT struct razor_set *
158 razor_root_get_system_set(struct razor_root *root)
160 assert (root != NULL);
166 razor_root_close(struct razor_root *root)
168 assert (root != NULL);
170 razor_set_destroy(root->system);
171 unlink(root->new_path);
179 razor_root_update(struct razor_root *root, struct razor_set *next)
181 assert (root != NULL);
182 assert (next != NULL);
184 razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
187 /* Sync the new repo file so the new package set is on disk
188 * before we start upgrading. */
190 printf("wrote %s\n", root->new_path);
194 razor_root_commit(struct razor_root *root)
196 assert (root != NULL);
199 rename(root->new_path, root->path);
200 printf("renamed %s to %s\n", root->new_path, root->path);
201 razor_set_destroy(root->system);