7 #include <talloc/talloc.h>
8 #include <system/system.h>
14 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
20 static struct mount_map *mount_map;
21 static int mount_map_size;
23 static int is_prefix(const char *str, const char *prefix)
25 return !strncmp(str, prefix, strlen(prefix));
28 static int is_prefix_ignorecase(const char *str, const char *prefix)
30 return !strncasecmp(str, prefix, strlen(prefix));
33 const char *mount_base(void)
35 return DEVICE_MOUNT_BASE;
38 char *encode_label(void *alloc_ctx, const char *label)
43 /* the label can be expanded by up to four times */
44 str = talloc_size(alloc_ctx, strlen(label) * 4 + 1);
47 for (i = 0; i < strlen(label); i++) {
49 if (label[i] == '/' || label[i] == '\\') {
50 sprintf(c, "\\x%02x", label[i]);
63 char *parse_device_path(void *alloc_ctx,
64 const char *dev_str, const char __attribute__((unused)) *cur_dev)
68 if (is_prefix_ignorecase(dev_str, "uuid=")) {
69 dev = talloc_asprintf(alloc_ctx, "/dev/disk/by-uuid/%s",
70 dev_str + strlen("uuid="));
74 if (is_prefix_ignorecase(dev_str, "label=")) {
75 enc = encode_label(NULL, dev_str + strlen("label="));
76 dev = talloc_asprintf(alloc_ctx, "/dev/disk/by-label/%s", enc);
81 /* normalise '/dev/foo' to 'foo' for easy comparisons, we'll expand
82 * back before returning.
84 if (is_prefix(dev_str, "/dev/"))
85 dev_str += strlen("/dev/");
87 return join_paths(alloc_ctx, "/dev", dev_str);
90 const char *mountpoint_for_device(const char *dev)
94 if (is_prefix(dev, "/dev/"))
95 dev += strlen("/dev/");
97 /* check existing entries in the map */
98 for (i = 0; i < mount_map_size; i++)
99 if (!strcmp(mount_map[i].dev, dev))
100 return mount_map[i].mnt;
102 /* no existing entry, create a new one */
103 i = mount_map_size++;
104 mount_map = talloc_realloc(NULL, mount_map,
105 struct mount_map, mount_map_size);
107 mount_map[i].dev = talloc_strdup(mount_map, dev);
108 mount_map[i].mnt = join_paths(mount_map, DEVICE_MOUNT_BASE, dev);
109 return mount_map[i].mnt;
112 char *resolve_path(void *alloc_ctx, const char *path, const char *current_dev)
114 static const char s_file[] = "file://";
116 const char *devpath, *sep;
120 if (!strncasecmp(path, s_file, sizeof(s_file) - 1))
121 path += sizeof(s_file) - 1;
122 else if (strstr(path, "://"))
123 return talloc_strdup(alloc_ctx, path);
125 sep = strchr(path, ':');
127 devpath = mountpoint_for_device(current_dev);
128 ret = join_paths(alloc_ctx, devpath, path);
130 /* parse just the device name into dev */
132 tmp = talloc_strndup(NULL, path, sep - path);
133 dev = parse_device_path(NULL, tmp, current_dev);
135 devpath = mountpoint_for_device(dev);
136 ret = join_paths(alloc_ctx, devpath, sep + 1);
145 char *join_paths(void *alloc_ctx, const char *a, const char *b)
149 full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
151 strcpy(full_path, a);
152 if (b[0] != '/' && a[strlen(a) - 1] != '/')
153 strcat(full_path, "/");
154 strcat(full_path, b);
160 static char *local_name(void *ctx)
164 tmp = tempnam(NULL, "pb-");
169 ret = talloc_strdup(ctx, tmp);
176 * pb_load_nfs - Mounts the NFS export and returns the local file path.
178 * Returns the local file path in a talloc'ed character string on success,
181 static char *load_nfs(void *ctx, struct pb_url *url)
189 local = local_name(ctx);
194 result = pb_mkdir_recursive(local);
199 opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
202 opts = talloc_asprintf_append(opts, ",port=%s", url->port);
205 *p++ = pb_system_apps.mount; /* 1 */
207 *p++ = "nfs"; /* 3 */
209 *p++ = url->host; /* 5 */
210 *p++ = url->dir; /* 6 */
211 *p++ = local; /* 7 */
214 result = pb_run_cmd(argv, 1, 0);
221 local = talloc_asprintf_append(local, "/%s", url->path);
222 pb_log("%s: local '%s'\n", __func__, local);
227 pb_rmdir_recursive("/", local);
233 * pb_load_sftp - Loads a remote file via sftp and returns the local file path.
235 * Returns the local file path in a talloc'ed character string on success,
238 static char *load_sftp(void *ctx, struct pb_url *url)
245 local = local_name(ctx);
251 *p++ = pb_system_apps.sftp; /* 1 */
252 *p++ = talloc_asprintf(local, "%s:%s", url->host, url->path); /* 2 */
253 *p++ = local; /* 3 */
256 result = pb_run_cmd(argv, 1, 0);
269 * pb_load_tftp - Loads a remote file via tftp and returns the local file path.
271 * Returns the local file path in a talloc'ed character string on success,
275 static char *load_tftp(void *ctx, struct pb_url *url)
278 const char *argv[10];
282 local = local_name(ctx);
287 /* first try busybox tftp args */
290 *p++ = pb_system_apps.tftp; /* 1 */
293 *p++ = local; /* 4 */
295 *p++ = url->path; /* 6 */
296 *p++ = url->host; /* 7 */
298 *p++ = url->port; /* 8 */
301 result = pb_run_cmd(argv, 1, 0);
306 /* next try tftp-hpa args */
309 *p++ = pb_system_apps.tftp; /* 1 */
311 *p++ = "binary"; /* 3 */
312 *p++ = url->host; /* 4 */
314 *p++ = url->port; /* 5 */
316 *p++ = "get"; /* 7 */
317 *p++ = url->path; /* 8 */
318 *p++ = local; /* 9 */
319 *p++ = NULL; /* 10 */
321 result = pb_run_cmd(argv, 1, 0);
332 wget_no_check_certificate = 1,
336 * pb_load_wget - Loads a remote file via wget and returns the local file path.
338 * Returns the local file path in a talloc'ed character string on success,
342 static char *load_wget(void *ctx, struct pb_url *url, enum wget_flags flags)
349 local = local_name(ctx);
355 *p++ = pb_system_apps.wget; /* 1 */
357 *p++ = "--quiet"; /* 2 */
360 *p++ = local; /* 4 */
361 *p++ = url->full; /* 5 */
362 if (flags & wget_no_check_certificate)
363 *p++ = "--no-check-certificate"; /* 6 */
366 result = pb_run_cmd(argv, 1, 0);
379 * pb_load_file - Loads a (possibly) remote file and returns the local file
381 * @ctx: The talloc context to associate with the returned string.
382 * @remote: The remote file URL.
383 * @tempfile: An optional variable pointer to be set when a temporary local
386 * Returns the local file path in a talloc'ed character string on success,
390 char *load_file(void *ctx, const char *remote, unsigned int *tempfile)
392 struct pb_url *url = pb_url_parse(ctx, remote);
399 switch (url->scheme) {
402 local = load_wget(ctx, url, 0);
406 local = load_wget(ctx, url, wget_no_check_certificate);
410 local = load_nfs(ctx, url);
414 local = load_sftp(ctx, url);
418 local = load_tftp(ctx, url);
422 local = talloc_strdup(ctx, url->full);