2 * Copyright (C) 2009 Sony Computer Entertainment Inc.
3 * Copyright 2009 Sony Corp.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #if defined(HAVE_CONFIG_H)
28 #include <system/system.h>
29 #include "talloc/talloc.h"
35 * pb_local_name - Helper to create a unique local path name.
36 * @ctx: A talloc context.
38 * Returns the local file path in a talloc'ed character string on success,
42 static char *pb_local_name(void *ctx)
46 tmp = tempnam(NULL, "pb-");
51 ret = talloc_strdup(ctx, tmp);
58 * pb_load_nfs - Mounts the NFS export and returns the local file path.
60 * Returns the local file path in a talloc'ed character string on success,
64 static char *pb_load_nfs(void *ctx, struct pb_url *url)
72 local = pb_local_name(ctx);
77 result = pb_mkdir_recursive(local);
82 opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
85 opts = talloc_asprintf_append(opts, ",port=%s", url->port);
88 *p++ = pb_system_apps.mount; /* 1 */
92 *p++ = url->host; /* 5 */
93 *p++ = url->dir; /* 6 */
97 result = pb_run_cmd(argv, 1, 0);
104 local = talloc_asprintf_append(local, "/%s", url->path);
105 pb_log("%s: local '%s'\n", __func__, local);
110 pb_rmdir_recursive("/", local);
116 * pb_load_sftp - Loads a remote file via sftp and returns the local file path.
118 * Returns the local file path in a talloc'ed character string on success,
122 static char *pb_load_sftp(void *ctx, struct pb_url __attribute__((unused)) *url)
129 local = pb_local_name(ctx);
135 *p++ = pb_system_apps.sftp; /* 1 */
136 *p++ = talloc_asprintf(local, "%s:%s", url->host, url->path); /* 2 */
137 *p++ = local; /* 3 */
140 result = pb_run_cmd(argv, 1, 0);
153 * pb_load_tftp - Loads a remote file via tftp and returns the local file path.
155 * Returns the local file path in a talloc'ed character string on success,
159 static char *pb_load_tftp(void *ctx, struct pb_url *url)
162 const char *argv[10];
166 local = pb_local_name(ctx);
171 /* first try busybox tftp args */
174 *p++ = pb_system_apps.tftp; /* 1 */
177 *p++ = local; /* 4 */
179 *p++ = url->path; /* 6 */
180 *p++ = url->host; /* 7 */
182 *p++ = url->port; /* 8 */
185 result = pb_run_cmd(argv, 1, 0);
190 /* next try tftp-hpa args */
193 *p++ = pb_system_apps.tftp; /* 1 */
195 *p++ = "binary"; /* 3 */
196 *p++ = url->host; /* 4 */
198 *p++ = url->port; /* 5 */
200 *p++ = "get"; /* 7 */
201 *p++ = url->path; /* 8 */
202 *p++ = local; /* 9 */
203 *p++ = NULL; /* 10 */
205 result = pb_run_cmd(argv, 1, 0);
216 wget_no_check_certificate = 1,
220 * pb_load_wget - Loads a remote file via wget and returns the local file path.
222 * Returns the local file path in a talloc'ed character string on success,
226 static char *pb_load_wget(void *ctx, struct pb_url *url, enum wget_flags flags)
233 local = pb_local_name(ctx);
239 *p++ = pb_system_apps.wget; /* 1 */
241 *p++ = "--quiet"; /* 2 */
244 *p++ = local; /* 4 */
245 *p++ = url->full; /* 5 */
246 if (flags & wget_no_check_certificate)
247 *p++ = "--no-check-certificate"; /* 6 */
250 result = pb_run_cmd(argv, 1, 0);
263 * pb_load_file - Loads a remote file and returns the local file path.
264 * @ctx: The talloc context to associate with the returned string.
265 * @remote: The remote file URL.
266 * @tempfile: An optional variable pointer to be set when a temporary local
269 * Returns the local file path in a talloc'ed character string on success,
273 char *pb_load_file(void *ctx, const char *remote, unsigned int *tempfile)
276 struct pb_url *url = pb_url_parse(NULL, remote);
284 switch (url->scheme) {
287 local = pb_load_wget(ctx, url, 0);
288 if (tempfile && local)
292 local = pb_load_wget(ctx, url, wget_no_check_certificate);
293 if (tempfile && local)
297 local = pb_load_nfs(ctx, url);
298 if (tempfile && local)
302 local = pb_load_sftp(ctx, url);
303 if (tempfile && local)
307 local = pb_load_tftp(ctx, url);
308 if (tempfile && local)
312 local = talloc_strdup(ctx, url->full);