diff options
author | Jeremy Kerr <jk@ozlabs.org> | 2013-04-16 16:58:18 +0800 |
---|---|---|
committer | Jeremy Kerr <jk@ozlabs.org> | 2013-04-29 14:41:04 +1000 |
commit | 5be946cda7b8e2271ade6188ca3f5dc068826619 (patch) | |
tree | a62a64792bb61aa57c5d8dac36c7df9756d01ef8 /discover/paths.c | |
parent | 4e8b779626da98e2896efbb2df99b64f76e878f6 (diff) | |
download | talos-petitboot-5be946cda7b8e2271ade6188ca3f5dc068826619.tar.gz talos-petitboot-5be946cda7b8e2271ade6188ca3f5dc068826619.zip |
discover: Change parsers to emit resources rather than filenames
This change switches the parsers over to populate the resources in
discover_boot_option, rather than the string parameters in boot_option.
To do this, we need a few things:
* Add struct resources to discover_boot_option for the boot_image,
initrd and icon data.
* Have the parsers populate the resources, rather than the strings.
Currently, parsers can all use the devpath resource type.
* Add a resolve_resource callback to parsers; this is how the device
handler will attempt to resolve resources.
* Change load_file to load_url, as we should be only accessing
(resolved) resources by URLs.
This then allows us to remove the mount map, and associated lookup code,
as well as the UUID and label links to devices.
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Diffstat (limited to 'discover/paths.c')
-rw-r--r-- | discover/paths.c | 131 |
1 files changed, 3 insertions, 128 deletions
diff --git a/discover/paths.c b/discover/paths.c index ef8a45f..d95e359 100644 --- a/discover/paths.c +++ b/discover/paths.c @@ -13,135 +13,11 @@ #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt") -struct mount_map { - char *dev, *mnt; -}; - -static struct mount_map *mount_map; -static int mount_map_size; - -static int is_prefix(const char *str, const char *prefix) -{ - return !strncmp(str, prefix, strlen(prefix)); -} - -static int is_prefix_ignorecase(const char *str, const char *prefix) -{ - return !strncasecmp(str, prefix, strlen(prefix)); -} - const char *mount_base(void) { return DEVICE_MOUNT_BASE; } -char *encode_label(void *alloc_ctx, const char *label) -{ - char *str, *c; - unsigned int i; - - /* the label can be expanded by up to four times */ - str = talloc_size(alloc_ctx, strlen(label) * 4 + 1); - c = str; - - for (i = 0; i < strlen(label); i++) { - - if (label[i] == '/' || label[i] == '\\') { - sprintf(c, "\\x%02x", label[i]); - c += 4; - continue; - } - - *(c++) = label[i]; - } - - *c = '\0'; - - return str; -} - -char *parse_device_path(void *alloc_ctx, - const char *dev_str, const char __attribute__((unused)) *cur_dev) -{ - char *dev, *enc; - - if (is_prefix_ignorecase(dev_str, "uuid=")) { - dev = talloc_asprintf(alloc_ctx, "/dev/disk/by-uuid/%s", - dev_str + strlen("uuid=")); - return dev; - } - - if (is_prefix_ignorecase(dev_str, "label=")) { - enc = encode_label(NULL, dev_str + strlen("label=")); - dev = talloc_asprintf(alloc_ctx, "/dev/disk/by-label/%s", enc); - talloc_free(enc); - return dev; - } - - /* normalise '/dev/foo' to 'foo' for easy comparisons, we'll expand - * back before returning. - */ - if (is_prefix(dev_str, "/dev/")) - dev_str += strlen("/dev/"); - - return join_paths(alloc_ctx, "/dev", dev_str); -} - -const char *mountpoint_for_device(const char *dev) -{ - int i; - - if (is_prefix(dev, "/dev/")) - dev += strlen("/dev/"); - - /* check existing entries in the map */ - for (i = 0; i < mount_map_size; i++) - if (!strcmp(mount_map[i].dev, dev)) - return mount_map[i].mnt; - - /* no existing entry, create a new one */ - i = mount_map_size++; - mount_map = talloc_realloc(NULL, mount_map, - struct mount_map, mount_map_size); - - mount_map[i].dev = talloc_strdup(mount_map, dev); - mount_map[i].mnt = join_paths(mount_map, DEVICE_MOUNT_BASE, dev); - return mount_map[i].mnt; -} - -char *resolve_path(void *alloc_ctx, const char *path, const char *current_dev) -{ - static const char s_file[] = "file://"; - char *ret; - const char *devpath, *sep; - - /* test for urls */ - - if (!strncasecmp(path, s_file, sizeof(s_file) - 1)) - path += sizeof(s_file) - 1; - else if (strstr(path, "://")) - return talloc_strdup(alloc_ctx, path); - - sep = strchr(path, ':'); - if (!sep) { - devpath = mountpoint_for_device(current_dev); - ret = join_paths(alloc_ctx, devpath, path); - } else { - /* parse just the device name into dev */ - char *tmp, *dev; - tmp = talloc_strndup(NULL, path, sep - path); - dev = parse_device_path(NULL, tmp, current_dev); - - devpath = mountpoint_for_device(dev); - ret = join_paths(alloc_ctx, devpath, sep + 1); - - talloc_free(dev); - talloc_free(tmp); - } - - return ret; -} - char *join_paths(void *alloc_ctx, const char *a, const char *b) { char *full_path; @@ -376,10 +252,10 @@ fail: } /** - * pb_load_file - Loads a (possibly) remote file and returns the local file + * load_url - Loads a (possibly) remote URL and returns the local file * path. * @ctx: The talloc context to associate with the returned string. - * @remote: The remote file URL. + * @URL: The remote file URL. * @tempfile: An optional variable pointer to be set when a temporary local * file is created. * @@ -387,9 +263,8 @@ fail: * or NULL on error. */ -char *load_file(void *ctx, const char *remote, unsigned int *tempfile) +char *load_url(void *ctx, struct pb_url *url, unsigned int *tempfile) { - struct pb_url *url = pb_url_parse(ctx, remote); char *local; int tmp = 0; |