Move razor root code to its own file.
authorKristian Høgsberg <krh@redhat.com>
Mon, 9 Jun 2008 20:14:05 +0000 (16:14 -0400)
committerKristian Høgsberg <krh@redhat.com>
Mon, 9 Jun 2008 20:14:05 +0000 (16:14 -0400)
Makefile
main.c
razor-root.c [new file with mode: 0644]
razor.h

index 7ed1eb3..5e26373 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ LDLIBS = librazor.a -lexpat -lz -g -lrpm -lcurl
 
 all : razor test-driver rpm-razor
 
-librazor_objs = razor.o yum.o rpm.o types.o util.o
+librazor_objs = razor.o yum.o rpm.o types.o util.o razor-root.o
 librazor.a : $(librazor_objs)
        ar cr $@ $(librazor_objs)
 
diff --git a/main.c b/main.c
index 6f27f41..b261c10 100644 (file)
--- a/main.c
+++ b/main.c
 #include <fnmatch.h>
 #include <errno.h>
 #include "razor.h"
-#include "razor-internal.h"
 
 static const char system_repo_filename[] = "system.repo";
 static const char next_repo_filename[] = "system-next.repo";
 static const char rawhide_repo_filename[] = "rawhide.repo";
 static const char updated_repo_filename[] = "system-updated.repo";
-static const char razor_root_path[] = "/var/lib/razor";
-static const char root[] = "install";
+static const char install_root[] = "install";
 static const char *repo_filename = system_repo_filename;
 static const char *yum_url;
 
@@ -563,159 +561,6 @@ command_import_rpms(int argc, const char *argv[])
        return 0;
 }
 
-/* The image data struct encapsulates filesystem conventions and the
- * locking protocol.  FIXME: Should this be razor_root?*/
-struct razor_root {
-       struct razor_set *system;
-       int fd;
-       char path[PATH_MAX];
-       char new_path[PATH_MAX];
-};
-
-#define RAZOR_ROOT_OPEN_WRITE 0x01
-
-int
-razor_root_create(const char *root);
-struct razor_root *
-razor_root_open(const char *root, int flags);
-struct razor_transaction *
-razor_root_create_transaction(struct razor_root *image,
-                             struct razor_set *upstream);
-int
-razor_root_close(struct razor_root *image);
-void
-razor_root_update(struct razor_root *image, struct razor_set *next);
-int
-razor_root_commit(struct razor_root *image);
-
-int
-razor_root_create(const char *root)
-{      
-       struct stat buf;
-       struct razor_set *set;
-       char path[PATH_MAX];
-
-       if (stat(root, &buf) < 0) {
-               if (mkdir(root, 0777) < 0) {
-                       fprintf(stderr,
-                               "could not create install root \"%s\"\n",
-                               root);
-                       return -1;
-               }
-               fprintf(stderr, "created install root \"%s\"\n", root);
-       } else if (!S_ISDIR(buf.st_mode)) {
-               fprintf(stderr,
-                       "install root \"%s\" exists, but is not a directory\n",
-                       root);
-               return -1;
-       }
-
-       snprintf(path, sizeof path, "%s/%s",
-                razor_root_path, system_repo_filename);
-       if (razor_create_dir(root, path) < 0) {
-               fprintf(stderr, "could not create %s%s\n",
-                       root, razor_root_path);
-               return -1;
-       }
-
-       set = razor_set_create();
-       snprintf(path, sizeof path, "%s%s/%s",
-                root, razor_root_path, system_repo_filename);
-       if (stat(root, &buf) == 0) {
-               fprintf(stderr,
-                       "a razor install root is already initialized\n");
-               return -1;
-       }
-       if (razor_set_write(set, path) < 0) {
-               fprintf(stderr, "could not write initial package set\n");
-               return -1;
-       }
-       razor_set_destroy(set);
-
-       return 0;
-}
-
-struct razor_root *
-razor_root_open(const char *root, int flags)
-{
-       struct razor_root *image;
-
-       image = malloc(sizeof *image);
-       if (image == NULL)
-               return NULL;
-
-       /* Create the new next repo file up front to ensure exclusive
-        * access. */
-       snprintf(image->new_path, sizeof image->new_path,
-                "%s%s/%s", root, root, next_repo_filename);
-       image->fd = open(image->new_path,
-                        O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
-       if (image->fd < 0) {
-               fprintf(stderr, "failed to get lock file, "
-                       "maybe previous operation crashed?\n");
-
-               /* FIXME: Use fcntl advisory locking on the system
-                * package set file to figure out whether previous
-                * operation crashed or is still in progress. */
-
-               free(image);
-               return NULL;
-       }
-
-       snprintf(image->path, sizeof image->path,
-                "%s%s/%s", root, razor_root_path, system_repo_filename);
-       image->system = razor_set_open(image->path);
-       if (image->system == NULL) {
-               unlink(image->new_path);
-               close(image->fd);
-               free(image);
-               return NULL;
-       }
-
-       return image;
-}
-
-struct razor_transaction *
-razor_root_create_transaction(struct razor_root *image,
-                             struct razor_set *upstream)
-{
-       /* FIXME: This should take a number of upstream repos. */
-       return razor_transaction_create(image->system, upstream);
-}
-
-int
-razor_root_close(struct razor_root *image)
-{
-       unlink(image->new_path);
-       close(image->fd);
-       free(image);
-
-       return 0;
-}
-
-void
-razor_root_update(struct razor_root *image, struct razor_set *next)
-{
-       razor_set_write_to_fd(next, image->fd);
-
-       /* Sync the new repo file so the new package set is on disk
-        * before we start upgrading. */
-       fsync(image->fd);
-       printf("wrote %s\n", image->new_path);
-}
-
-int
-razor_root_commit(struct razor_root *image)
-{
-       /* Make it so. */
-       rename(image->new_path, image->path);
-       printf("renamed %s to %s\n", image->new_path, image->path);
-       close(image->fd);
-       free(image);
-
-       return 0;
-}
-
 static void
 download_package(const char *name,
                 const char *old_version,
@@ -797,7 +642,7 @@ command_install(int argc, const char *argv[])
                i++;
        }
 
-       root = razor_root_open(razor_root_path, RAZOR_ROOT_OPEN_WRITE);
+       root = razor_root_open(install_root, RAZOR_ROOT_OPEN_WRITE);
        upstream = razor_set_open(rawhide_repo_filename);
        trans = razor_root_create_transaction(root, upstream);
 
@@ -827,7 +672,7 @@ command_install(int argc, const char *argv[])
                return 1;
        }
 
-       razor_set_diff(root->system, next, download_package, &errors);
+       razor_root_diff(root, download_package, &errors);
        if (errors > 0) {
                fprintf(stderr, "failed to download %d packages\n", errors);
                razor_root_close(root);
@@ -836,7 +681,7 @@ command_install(int argc, const char *argv[])
 
        /* FIXME: We need to figure out the right install order here,
         * so the post and pre scripts can run. */
-       razor_set_diff(root->system, next, install_package, (void *) root);
+       razor_root_diff(root, install_package, (void *) root);
 
        razor_set_destroy(next);
        razor_set_destroy(upstream);
@@ -847,7 +692,7 @@ command_install(int argc, const char *argv[])
 static int
 command_init(int argc, const char *argv[])
 {
-       return razor_root_create(root);
+       return razor_root_create(install_root);
 }
 
 static int
diff --git a/razor-root.c b/razor-root.c
new file mode 100644 (file)
index 0000000..5330868
--- /dev/null
@@ -0,0 +1,157 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "razor.h"
+#include "razor-internal.h"
+
+static const char system_repo_filename[] = "system.repo";
+static const char next_repo_filename[] = "system-next.repo";
+static const char razor_root_path[] = "/var/lib/razor";
+
+struct razor_root {
+       struct razor_set *system;
+       struct razor_set *next;
+       int fd;
+       char path[PATH_MAX];
+       char new_path[PATH_MAX];
+};
+
+int
+razor_root_create(const char *root)
+{      
+       struct stat buf;
+       struct razor_set *set;
+       char path[PATH_MAX];
+
+       if (stat(root, &buf) < 0) {
+               if (mkdir(root, 0777) < 0) {
+                       fprintf(stderr,
+                               "could not create install root \"%s\"\n",
+                               root);
+                       return -1;
+               }
+               fprintf(stderr, "created install root \"%s\"\n", root);
+       } else if (!S_ISDIR(buf.st_mode)) {
+               fprintf(stderr,
+                       "install root \"%s\" exists, but is not a directory\n",
+                       root);
+               return -1;
+       }
+
+       snprintf(path, sizeof path, "%s/%s",
+                razor_root_path, system_repo_filename);
+       if (razor_create_dir(root, path) < 0) {
+               fprintf(stderr, "could not create %s%s\n",
+                       root, razor_root_path);
+               return -1;
+       }
+
+       set = razor_set_create();
+       snprintf(path, sizeof path, "%s%s/%s",
+                root, razor_root_path, system_repo_filename);
+       if (stat(root, &buf) == 0) {
+               fprintf(stderr,
+                       "a razor install root is already initialized\n");
+               return -1;
+       }
+       if (razor_set_write(set, path) < 0) {
+               fprintf(stderr, "could not write initial package set\n");
+               return -1;
+       }
+       razor_set_destroy(set);
+
+       return 0;
+}
+
+struct razor_root *
+razor_root_open(const char *root, int flags)
+{
+       struct razor_root *image;
+
+       image = malloc(sizeof *image);
+       if (image == NULL)
+               return NULL;
+
+       /* Create the new next repo file up front to ensure exclusive
+        * access. */
+       snprintf(image->new_path, sizeof image->new_path,
+                "%s%s/%s", root, root, next_repo_filename);
+       image->fd = open(image->new_path,
+                        O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
+       if (image->fd < 0) {
+               fprintf(stderr, "failed to get lock file, "
+                       "maybe previous operation crashed?\n");
+
+               /* FIXME: Use fcntl advisory locking on the system
+                * package set file to figure out whether previous
+                * operation crashed or is still in progress. */
+
+               free(image);
+               return NULL;
+       }
+
+       snprintf(image->path, sizeof image->path,
+                "%s%s/%s", root, razor_root_path, system_repo_filename);
+       image->system = razor_set_open(image->path);
+       if (image->system == NULL) {
+               unlink(image->new_path);
+               close(image->fd);
+               free(image);
+               return NULL;
+       }
+
+       return image;
+}
+
+struct razor_transaction *
+razor_root_create_transaction(struct razor_root *image,
+                             struct razor_set *upstream)
+{
+       /* FIXME: This should take a number of upstream repos. */
+       return razor_transaction_create(image->system, upstream);
+}
+
+int
+razor_root_close(struct razor_root *image)
+{
+       unlink(image->new_path);
+       close(image->fd);
+       free(image);
+
+       return 0;
+}
+
+void
+razor_root_update(struct razor_root *root, struct razor_set *next)
+{
+       razor_set_write_to_fd(next, root->fd);
+       root->next = next;
+
+       /* Sync the new repo file so the new package set is on disk
+        * before we start upgrading. */
+       fsync(root->fd);
+       printf("wrote %s\n", root->new_path);
+}
+
+int
+razor_root_commit(struct razor_root *image)
+{
+       /* Make it so. */
+       rename(image->new_path, image->path);
+       printf("renamed %s to %s\n", image->new_path, image->path);
+       close(image->fd);
+       free(image);
+
+       return 0;
+}
+
+void
+razor_root_diff(struct razor_root *root,
+               razor_package_callback_t callback, void *data)
+{
+       return razor_set_diff(root->system, root->next, callback, data);
+}
diff --git a/razor.h b/razor.h
index b49a0d9..3177fb4 100644 (file)
--- a/razor.h
+++ b/razor.h
@@ -166,4 +166,22 @@ struct razor_rpm *razor_rpm_open(const char *filename);
 int razor_rpm_install(struct razor_rpm *rpm, const char *root);
 int razor_rpm_close(struct razor_rpm *rpm);
 
+
+/* Razor root functions. The root data struct encapsulates filesystem
+ * conventions and the locking protocol. */
+
+struct razor_root;
+#define RAZOR_ROOT_OPEN_WRITE 0x01
+
+int razor_root_create(const char *root);
+struct razor_root *razor_root_open(const char *root, int flags);
+struct razor_transaction *
+razor_root_create_transaction(struct razor_root *image,
+                             struct razor_set *upstream);
+int razor_root_close(struct razor_root *image);
+void razor_root_update(struct razor_root *image, struct razor_set *next);
+int razor_root_commit(struct razor_root *image);
+void razor_root_diff(struct razor_root *root,
+                    razor_package_callback_t callback, void *data);
+
 #endif /* _RAZOR_H_ */