7 #include <talloc/talloc.h>
8 #include <system/system.h>
14 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
16 const char *mount_base(void)
18 return DEVICE_MOUNT_BASE;
21 char *join_paths(void *alloc_ctx, const char *a, const char *b)
25 full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
28 if (b[0] != '/' && a[strlen(a) - 1] != '/')
29 strcat(full_path, "/");
36 static char *local_name(void *ctx)
40 tmp = tempnam(NULL, "pb-");
45 ret = talloc_strdup(ctx, tmp);
52 * pb_load_nfs - Mounts the NFS export and returns the local file path.
54 * Returns the local file path in a talloc'ed character string on success,
57 static char *load_nfs(void *ctx, struct pb_url *url)
65 local = local_name(ctx);
70 result = pb_mkdir_recursive(local);
75 opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
78 opts = talloc_asprintf_append(opts, ",port=%s", url->port);
81 *p++ = pb_system_apps.mount; /* 1 */
85 *p++ = url->host; /* 5 */
86 *p++ = url->dir; /* 6 */
90 result = pb_run_cmd(argv, 1, 0);
97 local = talloc_asprintf_append(local, "/%s", url->path);
98 pb_log("%s: local '%s'\n", __func__, local);
103 pb_rmdir_recursive("/", local);
109 * pb_load_sftp - Loads a remote file via sftp and returns the local file path.
111 * Returns the local file path in a talloc'ed character string on success,
114 static char *load_sftp(void *ctx, struct pb_url *url)
121 local = local_name(ctx);
127 *p++ = pb_system_apps.sftp; /* 1 */
128 *p++ = talloc_asprintf(local, "%s:%s", url->host, url->path); /* 2 */
129 *p++ = local; /* 3 */
132 result = pb_run_cmd(argv, 1, 0);
145 * pb_load_tftp - Loads a remote file via tftp and returns the local file path.
147 * Returns the local file path in a talloc'ed character string on success,
151 static char *load_tftp(void *ctx, struct pb_url *url)
154 const char *argv[10];
158 local = local_name(ctx);
163 /* first try busybox tftp args */
166 *p++ = pb_system_apps.tftp; /* 1 */
169 *p++ = local; /* 4 */
171 *p++ = url->path; /* 6 */
172 *p++ = url->host; /* 7 */
174 *p++ = url->port; /* 8 */
177 result = pb_run_cmd(argv, 1, 0);
182 /* next try tftp-hpa args */
185 *p++ = pb_system_apps.tftp; /* 1 */
187 *p++ = "binary"; /* 3 */
188 *p++ = url->host; /* 4 */
190 *p++ = url->port; /* 5 */
192 *p++ = "get"; /* 7 */
193 *p++ = url->path; /* 8 */
194 *p++ = local; /* 9 */
195 *p++ = NULL; /* 10 */
197 result = pb_run_cmd(argv, 1, 0);
208 wget_no_check_certificate = 1,
212 * pb_load_wget - Loads a remote file via wget and returns the local file path.
214 * Returns the local file path in a talloc'ed character string on success,
218 static char *load_wget(void *ctx, struct pb_url *url, enum wget_flags flags)
225 local = local_name(ctx);
231 *p++ = pb_system_apps.wget; /* 1 */
233 *p++ = "--quiet"; /* 2 */
236 *p++ = local; /* 4 */
237 *p++ = url->full; /* 5 */
238 if (flags & wget_no_check_certificate)
239 *p++ = "--no-check-certificate"; /* 6 */
242 result = pb_run_cmd(argv, 1, 0);
255 * load_url - Loads a (possibly) remote URL and returns the local file
257 * @ctx: The talloc context to associate with the returned string.
258 * @URL: The remote file URL.
259 * @tempfile: An optional variable pointer to be set when a temporary local
262 * Returns the local file path in a talloc'ed character string on success,
266 char *load_url(void *ctx, struct pb_url *url, unsigned int *tempfile)
274 switch (url->scheme) {
277 local = load_wget(ctx, url, 0);
281 local = load_wget(ctx, url, wget_no_check_certificate);
285 local = load_nfs(ctx, url);
289 local = load_sftp(ctx, url);
293 local = load_tftp(ctx, url);
297 local = talloc_strdup(ctx, url->full);