]> git.ozlabs.org Git - petitboot/blobdiff - discover/paths.c
discover/grub2: Implement parser
[petitboot] / discover / paths.c
index 2373c28f560555025d7b522733551b5cb3dffe22..ce90caee3775d8b9714fb865d4ef6dad1c98ce5d 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "paths.h"
+#include <talloc/talloc.h>
+#include <system/system.h>
+#include <process/process.h>
+#include <url/url.h>
+#include <log/log.h>
 
-static char *mount_base;
+#include "paths.h"
 
-struct device_map {
-       char *dev, *mnt;
-};
+#define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
 
-#define DEVICE_MAP_SIZE 32
-static struct device_map device_map[DEVICE_MAP_SIZE];
+const char *mount_base(void)
+{
+       return DEVICE_MOUNT_BASE;
+}
 
-char *encode_label(const char *label)
+char *join_paths(void *alloc_ctx, const char *a, const char *b)
 {
-       char *str, *c;
-       int i;
+       char *full_path;
+
+       full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
+
+       strcpy(full_path, a);
+       if (b[0] != '/' && a[strlen(a) - 1] != '/')
+               strcat(full_path, "/");
+       strcat(full_path, b);
 
-       /* the label can be expanded by up to four times */
-       str = malloc(strlen(label) * 4 + 1);
-       c = str;
+       return full_path;
+}
 
-       for (i = 0; i < strlen(label); i++) {
 
-               if (label[i] == '/' || label[i] == '\\') {
-                       sprintf(c, "\\x%02x", label[i]);
-                       c += 4;
-                       continue;
-               }
+static char *local_name(void *ctx)
+{
+       char *tmp, *ret;
 
-               *(c++) = label[i];
-       }
+       tmp = tempnam(NULL, "pb-");
 
-       *c = '\0';
+       if (!tmp)
+               return NULL;
+
+       ret = talloc_strdup(ctx, tmp);
+       free(tmp);
 
-       return str;
+       return ret;
 }
 
-char *parse_device_path(const char *dev_str, const char *cur_dev)
+/**
+ * pb_load_nfs - Mounts the NFS export and returns the local file path.
+ *
+ * Returns the local file path in a talloc'ed character string on success,
+ * or NULL on error.
+ */
+static char *load_nfs(void *ctx, struct pb_url *url)
 {
-       char *dev, tmp[256], *enc;
+       char *local, *opts;
+       int result;
 
-       if (!strncasecmp(dev_str, "uuid=", 5)) {
-               asprintf(&dev, "/dev/disk/by-uuid/%s", dev_str + 5);
-               return dev;
-       }
+       local = local_name(ctx);
 
-       if (!strncasecmp(dev_str, "label=", 6)) {
-               enc = encode_label(dev_str + 6);
-               asprintf(&dev, "/dev/disk/by-label/%s", enc);
-               free(enc);
-               return dev;
-       }
+       if (!local)
+               return NULL;
 
-       /* normalise '/dev/foo' to 'foo' for easy comparisons, we'll expand
-        * back before returning.
-        */
-       if (!strncmp(dev_str, "/dev/", 5))
-               dev_str += 5;
-
-       /* PS3 hack: if we're reading from a ps3dx device, and we refer to
-        * a sdx device, remap to ps3dx */
-       if (cur_dev && !strncmp(cur_dev, "/dev/ps3d", 9)
-                       && !strncmp(dev_str, "sd", 2)) {
-               snprintf(tmp, 255, "ps3d%s", dev_str + 2);
-               dev_str = tmp;
-       }
+       result = pb_mkdir_recursive(local);
+
+       if (result)
+               goto fail;
 
-       return join_paths("/dev", dev_str);
+       opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
+
+       if (url->port)
+               opts = talloc_asprintf_append(opts, ",port=%s", url->port);
+
+       result = process_run_simple(ctx, pb_system_apps.mount, "-t", "nfs",
+                       opts, url->host, url->dir, local, NULL);
+
+       talloc_free(opts);
+
+       if (result)
+               goto fail;
+
+       local = talloc_asprintf_append(local,  "/%s", url->path);
+       pb_log("%s: local '%s'\n", __func__, local);
+
+       return local;
+
+fail:
+       pb_rmdir_recursive("/", local);
+       talloc_free(local);
+       return NULL;
 }
 
-const char *mountpoint_for_device(const char *dev)
+/**
+ * pb_load_sftp - Loads a remote file via sftp and returns the local file path.
+ *
+ * Returns the local file path in a talloc'ed character string on success,
+ * or NULL on error.
+ */
+static char *load_sftp(void *ctx, struct pb_url *url)
 {
-       int i;
-
-       if (!strncmp(dev, "/dev/", 5))
-               dev += 5;
+       char *host_path, *local;
+       int result;
 
-       /* check existing entries in the map */
-       for (i = 0; (i < DEVICE_MAP_SIZE) && device_map[i].dev; i++)
-               if (!strcmp(device_map[i].dev, dev))
-                       return device_map[i].mnt;
+       local = local_name(ctx);
 
-       if (i == DEVICE_MAP_SIZE)
+       if (!local)
                return NULL;
 
-       device_map[i].dev = strdup(dev);
-       device_map[i].mnt = join_paths(mount_base, dev);
-       return device_map[i].mnt;
+       host_path = talloc_asprintf(local, "%s:%s", url->host, url->path);
+
+       result = process_run_simple(ctx, pb_system_apps.sftp, host_path,
+                       local, NULL);
+
+       if (result)
+               goto fail;
+
+       return local;
+
+fail:
+       talloc_free(local);
+       return NULL;
 }
 
-char *resolve_path(const char *path, const char *current_dev)
+/**
+ * pb_load_tftp - Loads a remote file via tftp and returns the local file path.
+ *
+ * Returns the local file path in a talloc'ed character string on success,
+ * or NULL on error.
+ */
+
+static char *load_tftp(void *ctx, struct pb_url *url)
 {
-       char *ret;
-       const char *devpath, *sep;
-
-       sep = strchr(path, ':');
-       if (!sep) {
-               devpath = mountpoint_for_device(current_dev);
-               ret = join_paths(devpath, path);
-       } else {
-               /* parse just the device name into dev */
-               char *tmp, *dev;
-               tmp = strndup(path, sep - path);
-               dev = parse_device_path(tmp, current_dev);
-
-               devpath = mountpoint_for_device(dev);
-               ret = join_paths(devpath, sep + 1);
-
-               free(dev);
-               free(tmp);
-       }
+       int result;
+       const char *argv[10];
+       const char **p;
+       char *local;
 
-       return ret;
+       local = local_name(ctx);
+
+       if (!local)
+               return NULL;
+
+       /* first try busybox tftp args */
+
+       p = argv;
+       *p++ = pb_system_apps.tftp;     /* 1 */
+       *p++ = "-g";                    /* 2 */
+       *p++ = "-l";                    /* 3 */
+       *p++ = local;                   /* 4 */
+       *p++ = "-r";                    /* 5 */
+       *p++ = url->path;               /* 6 */
+       *p++ = url->host;               /* 7 */
+       if (url->port)
+               *p++ = url->port;       /* 8 */
+       *p++ = NULL;                    /* 9 */
+
+       result = process_run_simple_argv(ctx, argv);
+
+       if (!result)
+               return local;
+
+       /* next try tftp-hpa args */
+
+       p = argv;
+       *p++ = pb_system_apps.tftp;     /* 1 */
+       *p++ = "-m";                    /* 2 */
+       *p++ = "binary";                /* 3 */
+       *p++ = url->host;               /* 4 */
+       if (url->port)
+               *p++ = url->port;       /* 5 */
+       *p++ = "-c";                    /* 6 */
+       *p++ = "get";                   /* 7 */
+       *p++ = url->path;               /* 8 */
+       *p++ = local;                   /* 9 */
+       *p++ = NULL;                    /* 10 */
+
+       result = process_run_simple_argv(ctx, argv);
+
+       if (!result)
+               return local;
+
+       talloc_free(local);
+       return NULL;
 }
 
-void set_mount_base(const char *path)
+enum wget_flags {
+       wget_empty = 0,
+       wget_no_check_certificate = 1,
+};
+
+/**
+ * pb_load_wget - Loads a remote file via wget and returns the local file path.
+ *
+ * Returns the local file path in a talloc'ed character string on success,
+ * or NULL on error.
+ */
+
+static char *load_wget(void *ctx, struct pb_url *url, enum wget_flags flags)
 {
-       if (mount_base)
-               free(mount_base);
-       mount_base = strdup(path);
+       int result;
+       const char *argv[7];
+       const char **p;
+       char *local;
+
+       local = local_name(ctx);
+
+       if (!local)
+               return NULL;
+
+       p = argv;
+       *p++ = pb_system_apps.wget;                     /* 1 */
+#if !defined(DEBUG)
+       *p++ = "--quiet";                               /* 2 */
+#endif
+       *p++ = "-O";                                    /* 3 */
+       *p++ = local;                                   /* 4 */
+       *p++ = url->full;                               /* 5 */
+       if (flags & wget_no_check_certificate)
+               *p++ = "--no-check-certificate";        /* 6 */
+       *p++ = NULL;                                    /* 7 */
+
+       result = process_run_simple_argv(ctx, argv);
+
+       if (result)
+               goto fail;
+
+       return local;
+
+fail:
+       talloc_free(local);
+       return NULL;
 }
 
-char *join_paths(const char *a, const char *b)
+/**
+ * load_url - Loads a (possibly) remote URL and returns the local file
+ * path.
+ * @ctx: The talloc context to associate with the returned string.
+ * @URL: The remote file URL.
+ * @tempfile: An optional variable pointer to be set when a temporary local
+ *  file is created.
+ *
+ * Returns the local file path in a talloc'ed character string on success,
+ * or NULL on error.
+ */
+
+char *load_url(void *ctx, struct pb_url *url, unsigned int *tempfile)
 {
-       char *full_path;
+       char *local;
+       int tmp = 0;
 
-       full_path = malloc(strlen(a) + strlen(b) + 2);
+       if (!url)
+               return NULL;
 
-       strcpy(full_path, a);
-       if (b[0] != '/' && a[strlen(a) - 1] != '/')
-               strcat(full_path, "/");
-       strcat(full_path, b);
+       switch (url->scheme) {
+       case pb_url_ftp:
+       case pb_url_http:
+               local = load_wget(ctx, url, 0);
+               tmp = !!local;
+               break;
+       case pb_url_https:
+               local = load_wget(ctx, url, wget_no_check_certificate);
+               tmp = !!local;
+               break;
+       case pb_url_nfs:
+               local = load_nfs(ctx, url);
+               tmp = !!local;
+               break;
+       case pb_url_sftp:
+               local = load_sftp(ctx, url);
+               tmp = !!local;
+               break;
+       case pb_url_tftp:
+               local = load_tftp(ctx, url);
+               tmp = !!local;
+               break;
+       default:
+               local = talloc_strdup(ctx, url->full);
+               tmp = 0;
+               break;
+       }
 
-       return full_path;
-}
+       if (tempfile)
+               *tempfile = tmp;
 
+       return local;
+}