7 #include <talloc/talloc.h>
8 #include <system/system.h>
9 #include <process/process.h>
15 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
17 const char *mount_base(void)
19 return DEVICE_MOUNT_BASE;
22 char *join_paths(void *alloc_ctx, const char *a, const char *b)
26 full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
29 if (b[0] != '/' && a[strlen(a) - 1] != '/')
30 strcat(full_path, "/");
37 static char *local_name(void *ctx)
41 tmp = tempnam(NULL, "pb-");
46 ret = talloc_strdup(ctx, tmp);
53 * pb_load_nfs - Mounts the NFS export and returns the local file path.
55 * Returns the local file path in a talloc'ed character string on success,
58 static char *load_nfs(void *ctx, struct pb_url *url)
63 local = local_name(ctx);
68 result = pb_mkdir_recursive(local);
73 opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
76 opts = talloc_asprintf_append(opts, ",port=%s", url->port);
78 result = process_run_simple(ctx, pb_system_apps.mount, "-t", "nfs",
79 opts, url->host, url->dir, local, NULL);
86 local = talloc_asprintf_append(local, "/%s", url->path);
87 pb_log("%s: local '%s'\n", __func__, local);
92 pb_rmdir_recursive("/", local);
98 * pb_load_sftp - Loads a remote file via sftp and returns the local file path.
100 * Returns the local file path in a talloc'ed character string on success,
103 static char *load_sftp(void *ctx, struct pb_url *url)
105 char *host_path, *local;
108 local = local_name(ctx);
113 host_path = talloc_asprintf(local, "%s:%s", url->host, url->path);
115 result = process_run_simple(ctx, pb_system_apps.sftp, host_path,
129 * pb_load_tftp - Loads a remote file via tftp and returns the local file path.
131 * Returns the local file path in a talloc'ed character string on success,
135 static char *load_tftp(void *ctx, struct pb_url *url)
138 const char *argv[10];
142 local = local_name(ctx);
147 /* first try busybox tftp args */
150 *p++ = pb_system_apps.tftp; /* 1 */
153 *p++ = local; /* 4 */
155 *p++ = url->path; /* 6 */
156 *p++ = url->host; /* 7 */
158 *p++ = url->port; /* 8 */
161 result = process_run_simple_argv(ctx, argv);
166 /* next try tftp-hpa args */
169 *p++ = pb_system_apps.tftp; /* 1 */
171 *p++ = "binary"; /* 3 */
172 *p++ = url->host; /* 4 */
174 *p++ = url->port; /* 5 */
176 *p++ = "get"; /* 7 */
177 *p++ = url->path; /* 8 */
178 *p++ = local; /* 9 */
179 *p++ = NULL; /* 10 */
181 result = process_run_simple_argv(ctx, argv);
192 wget_no_check_certificate = 1,
196 * pb_load_wget - Loads a remote file via wget and returns the local file path.
198 * Returns the local file path in a talloc'ed character string on success,
202 static char *load_wget(void *ctx, struct pb_url *url, enum wget_flags flags)
209 local = local_name(ctx);
215 *p++ = pb_system_apps.wget; /* 1 */
217 *p++ = "--quiet"; /* 2 */
220 *p++ = local; /* 4 */
221 *p++ = url->full; /* 5 */
222 if (flags & wget_no_check_certificate)
223 *p++ = "--no-check-certificate"; /* 6 */
226 result = process_run_simple_argv(ctx, argv);
239 * load_url - Loads a (possibly) remote URL and returns the local file
241 * @ctx: The talloc context to associate with the returned string.
242 * @URL: The remote file URL.
243 * @tempfile: An optional variable pointer to be set when a temporary local
246 * Returns the local file path in a talloc'ed character string on success,
250 char *load_url(void *ctx, struct pb_url *url, unsigned int *tempfile)
258 switch (url->scheme) {
261 local = load_wget(ctx, url, 0);
265 local = load_wget(ctx, url, wget_no_check_certificate);
269 local = load_nfs(ctx, url);
273 local = load_sftp(ctx, url);
277 local = load_tftp(ctx, url);
281 local = talloc_strdup(ctx, url->full);