Don't explode when listing files.
committer: Kristian H?gsberg <krh@redhat.com>
9 #include "razor-internal.h"
11 static const char system_repo_filename[] = "system.repo";
12 static const char system_repo_details_filename[] = "system-details.repo";
13 static const char system_repo_files_filename[] = "system-files.repo";
15 static const char next_repo_filename[] = "system-next.repo";
16 static const char razor_root_path[] = "/var/lib/razor";
19 struct razor_set *system;
20 struct razor_set *next;
23 char new_path[PATH_MAX];
27 razor_root_create(const char *root)
30 struct razor_set *set;
31 char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
33 if (stat(root, &buf) < 0) {
34 if (mkdir(root, 0777) < 0) {
36 "could not create install root \"%s\"\n",
40 fprintf(stderr, "created install root \"%s\"\n", root);
41 } else if (!S_ISDIR(buf.st_mode)) {
43 "install root \"%s\" exists, but is not a directory\n",
48 snprintf(path, sizeof path, "%s/%s",
49 razor_root_path, system_repo_filename);
50 if (razor_create_dir(root, path) < 0) {
51 fprintf(stderr, "could not create %s%s\n",
52 root, razor_root_path);
56 set = razor_set_create();
57 snprintf(path, sizeof path, "%s%s/%s",
58 root, razor_root_path, system_repo_filename);
59 snprintf(details_path, sizeof details_path, "%s%s/%s",
60 root, razor_root_path, system_repo_details_filename);
61 snprintf(files_path, sizeof files_path, "%s%s/%s",
62 root, razor_root_path, system_repo_files_filename);
63 if (stat(path, &buf) == 0) {
65 "a razor install root is already initialized\n");
68 if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
69 razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
70 razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
71 fprintf(stderr, "could not write initial package set\n");
74 razor_set_destroy(set);
79 RAZOR_EXPORT struct razor_root *
80 razor_root_open(const char *root)
82 struct razor_root *image;
84 image = malloc(sizeof *image);
88 /* Create the new next repo file up front to ensure exclusive
90 snprintf(image->new_path, sizeof image->new_path,
91 "%s%s/%s", root, razor_root_path, next_repo_filename);
92 image->fd = open(image->new_path,
93 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
95 fprintf(stderr, "failed to get lock file, "
96 "maybe previous operation crashed?\n");
98 /* FIXME: Use fcntl advisory locking on the system
99 * package set file to figure out whether previous
100 * operation crashed or is still in progress. */
106 snprintf(image->path, sizeof image->path,
107 "%s%s/%s", root, razor_root_path, system_repo_filename);
108 image->system = razor_set_open(image->path);
109 if (image->system == NULL) {
110 unlink(image->new_path);
119 RAZOR_EXPORT struct razor_set *
120 razor_root_open_read_only(const char *root)
124 snprintf(path, sizeof path, "%s%s/%s",
125 root, razor_root_path, system_repo_filename);
127 return razor_set_open(path);
130 RAZOR_EXPORT struct razor_set *
131 razor_root_get_system_set(struct razor_root *root)
137 razor_root_close(struct razor_root *root)
139 razor_set_destroy(root->system);
140 unlink(root->new_path);
148 razor_root_update(struct razor_root *root, struct razor_set *next)
150 razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
153 /* Sync the new repo file so the new package set is on disk
154 * before we start upgrading. */
156 printf("wrote %s\n", root->new_path);
160 razor_root_commit(struct razor_root *root)
163 rename(root->new_path, root->path);
164 printf("renamed %s to %s\n", root->new_path, root->path);
165 razor_set_destroy(root->system);