]> git.ozlabs.org Git - petitboot/blobdiff - discover/device-handler.c
discover: Finalise boot options during commit
[petitboot] / discover / device-handler.c
index 2b16b2ea2f7a9c937b864b66d28996cca9bae0cf..c0f60669d287857e7c9520399756ad564f98fc44 100644 (file)
 #include <log/log.h>
 #include <types/types.h>
 #include <system/system.h>
+#include <url/url.h>
 
 #include "device-handler.h"
 #include "discover-server.h"
 #include "event.h"
 #include "parser.h"
+#include "resource.h"
 #include "udev.h"
 #include "paths.h"
 #include "boot.h"
@@ -29,6 +31,38 @@ struct device_handler {
        unsigned int            n_devices;
 };
 
+static bool resource_is_resolved(struct resource *res)
+{
+       return !res || res->resolved;
+}
+
+/* We only use this in an assert, which will disappear if we're compiling
+ * with NDEBUG, so we need the 'used' attribute for these builds */
+static bool __attribute__((used)) boot_option_is_resolved(
+               struct discover_boot_option *opt)
+{
+       return resource_is_resolved(opt->boot_image) &&
+               resource_is_resolved(opt->initrd) &&
+               resource_is_resolved(opt->icon);
+}
+
+static void boot_option_finalise(struct discover_boot_option *opt)
+{
+       assert(boot_option_is_resolved(opt));
+
+       /* check that the parsers haven't set any of the final data */
+       assert(!opt->option->boot_image_file);
+       assert(!opt->option->initrd_file);
+       assert(!opt->option->icon_file);
+
+       if (opt->boot_image)
+               opt->option->boot_image_file = opt->boot_image->url->full;
+       if (opt->initrd)
+               opt->option->initrd_file = opt->initrd->url->full;
+       if (opt->icon)
+               opt->option->icon_file = opt->icon->url->full;
+}
+
 /**
  * context_commit - Commit a temporary discovery context to the handler,
  * and notify the clients about any new options / devices
@@ -38,7 +72,7 @@ static void context_commit(struct device_handler *handler,
 {
        struct discover_device *dev = ctx->device;
        struct discover_boot_option *opt, *tmp;
-       unsigned int i, existing_device;
+       unsigned int i, existing_device = 0;
 
        /* do we already have this device? */
        for (i = 0; i < handler->n_devices; i++) {
@@ -65,6 +99,7 @@ static void context_commit(struct device_handler *handler,
                list_remove(&opt->list);
                list_add(&dev->boot_options, &opt->list);
                talloc_steal(dev, opt);
+               boot_option_finalise(opt);
                discover_server_notify_boot_option_add(handler->server,
                                                        opt->option);
        }
@@ -130,73 +165,13 @@ const struct discover_device *device_handler_get_device(
        return handler->devices[index];
 }
 
-static void setup_device_links(struct discover_device *dev)
-{
-       struct link {
-               const char *dir, *val;
-       } *link, links[] = {
-               {
-                       .dir = "disk/by-uuid",
-                       .val = dev->uuid,
-               },
-               {
-                       .dir = "disk/by-label",
-                       .val = dev->label,
-               },
-               {
-                       .dir = NULL
-               }
-       };
-
-       for (link = links; link->dir; link++) {
-               char *enc, *dir, *path;
-
-               if (!link->val || !*link->val)
-                       continue;
-
-               enc = encode_label(dev, link->val);
-               dir = join_paths(dev, mount_base(), link->dir);
-               path = join_paths(dev, dir, enc);
-
-               if (!pb_mkdir_recursive(dir)) {
-                       unlink(path);
-                       if (symlink(dev->mount_path, path)) {
-                               pb_log("symlink(%s,%s): %s\n",
-                                               dev->mount_path, path,
-                                               strerror(errno));
-                               talloc_free(path);
-                       } else {
-                               int i = dev->n_links++;
-                               dev->links = talloc_realloc(dev,
-                                               dev->links, char *,
-                                               dev->n_links);
-                               dev->links[i] = path;
-                       }
-
-               }
-
-               talloc_free(dir);
-               talloc_free(enc);
-       }
-}
-
-static void remove_device_links(struct discover_device *dev)
-{
-       int i;
-
-       for (i = 0; i < dev->n_links; i++)
-               unlink(dev->links[i]);
-}
-
 static int mount_device(struct discover_device *dev)
 {
-       const char *mountpoint;
        const char *argv[6];
 
-       if (!dev->mount_path) {
-               mountpoint = mountpoint_for_device(dev->device_path);
-               dev->mount_path = talloc_strdup(dev, mountpoint);
-       }
+       if (!dev->mount_path)
+               dev->mount_path = join_paths(dev, mount_base(),
+                                               dev->device_path);
 
        if (pb_mkdir_recursive(dev->mount_path))
                pb_log("couldn't create mount directory %s: %s\n",
@@ -222,7 +197,6 @@ static int mount_device(struct discover_device *dev)
                        goto out_rmdir;
        }
 
-       setup_device_links(dev);
        return 0;
 
 out_rmdir:
@@ -235,8 +209,6 @@ static int umount_device(struct discover_device *dev)
        int status;
        pid_t pid;
 
-       remove_device_links(dev);
-
        if (!dev->mount_path)
                return 0;
 
@@ -590,5 +562,5 @@ void device_handler_boot(struct device_handler *handler,
 
        opt = find_boot_option_by_id(handler, cmd->option_id);
 
-       boot(handler, opt->option, cmd, handler->dry_run);
+       boot(handler, opt, cmd, handler->dry_run);
 }