]> git.ozlabs.org Git - petitboot/commitdiff
discover: Add device lookup functions
authorJeremy Kerr <jk@ozlabs.org>
Thu, 28 Feb 2013 09:16:57 +0000 (17:16 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 29 Apr 2013 04:31:20 +0000 (14:31 +1000)
Add a few functions to find devices by various attributes:

 * device_lookup_by_name
 * device_lookup_by_path
 * device_lookup_by_uuid
 * device_lookup_by_label
 * device_lookup_by_id

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/device-handler.c
discover/device-handler.h

index 7533cfa1d514b0b6f80048ad7362b5b932b3f94f..89aa67d5e342b66dafdea013b7534e50eed6b089 100644 (file)
@@ -464,6 +464,93 @@ void device_handler_destroy(struct device_handler *handler)
        talloc_free(handler);
 }
 
+static int device_match_path(struct discover_device *dev, const char *path)
+{
+       return !strcmp(dev->device_path, path);
+}
+
+static int device_match_uuid(struct discover_device *dev, const char *uuid)
+{
+       return dev->uuid && !strcmp(dev->uuid, uuid);
+}
+
+static int device_match_label(struct discover_device *dev, const char *label)
+{
+       return dev->label && !strcmp(dev->label, label);
+}
+
+static int device_match_id(struct discover_device *dev, const char *id)
+{
+       return !strcmp(dev->device->id, id);
+}
+
+static struct discover_device *device_lookup(
+               struct device_handler *device_handler,
+               int (match_fn)(struct discover_device *, const char *),
+               const char *str)
+{
+       struct discover_device *dev;
+       unsigned int i;
+
+       if (!str)
+               return NULL;
+
+       for (i = 0; i < device_handler->n_devices; i++) {
+               dev = device_handler->devices[i];
+
+               if (match_fn(dev, str))
+                       return dev;
+       }
+
+       return NULL;
+}
+
+struct discover_device *device_lookup_by_name(struct device_handler *handler,
+               const char *name)
+{
+       struct discover_device *dev;
+       char *path;
+
+       if (strncmp(name, "/dev/", strlen("/dev/")))
+               path = talloc_asprintf(NULL, "/dev/%s", name);
+       else
+               path = talloc_strdup(NULL, name);
+
+       dev = device_lookup_by_path(handler, path);
+
+       talloc_free(path);
+
+       return dev;
+}
+
+struct discover_device *device_lookup_by_path(
+               struct device_handler *device_handler,
+               const char *path)
+{
+       return device_lookup(device_handler, device_match_path, path);
+}
+
+struct discover_device *device_lookup_by_uuid(
+               struct device_handler *device_handler,
+               const char *uuid)
+{
+       return device_lookup(device_handler, device_match_uuid, uuid);
+}
+
+struct discover_device *device_lookup_by_label(
+               struct device_handler *device_handler,
+               const char *label)
+{
+       return device_lookup(device_handler, device_match_label, label);
+}
+
+struct discover_device *device_lookup_by_id(
+               struct device_handler *device_handler,
+               const char *id)
+{
+       return device_lookup(device_handler, device_match_id, id);
+}
+
 static struct boot_option *find_boot_option_by_id(
                struct device_handler *handler, const char *id)
 {
index e298fedc9da7edd83a3daa2f6ee136b3f4d95448..9a7cf6d07603435508b3b4b46a39818a219d9f94 100644 (file)
@@ -45,6 +45,17 @@ void discover_context_add_boot_option(struct discover_context *ctx,
 
 int device_handler_event(struct device_handler *handler, struct event *event);
 
+struct discover_device *device_lookup_by_name(struct device_handler *handler,
+               const char *name);
+struct discover_device *device_lookup_by_path(struct device_handler *handler,
+               const char *path);
+struct discover_device *device_lookup_by_uuid(struct device_handler *handler,
+               const char *uuid);
+struct discover_device *device_lookup_by_label(struct device_handler *handler,
+               const char *label);
+struct discover_device *device_lookup_by_id(struct device_handler *handler,
+               const char *id);
+
 void device_handler_boot(struct device_handler *handler,
                struct boot_command *cmd);