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);
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++ = url->host; /* 2 */
137 *p++ = url->path; /* 3 */
138 *p++ = local; /* 4 */
141 result = pb_run_cmd(argv, 1);
154 * pb_load_tftp - Loads a remote file via tftp and returns the local file path.
156 * Returns the local file path in a talloc'ed character string on success,
160 static char *pb_load_tftp(void *ctx, struct pb_url *url)
163 const char *argv[10];
167 local = pb_local_name(ctx);
172 /* first try busybox tftp args */
175 *p++ = pb_system_apps.tftp; /* 1 */
178 *p++ = local; /* 4 */
180 *p++ = url->path; /* 6 */
181 *p++ = url->host; /* 7 */
183 *p++ = url->port; /* 8 */
186 result = pb_run_cmd(argv, 1);
191 /* next try tftp-hpa args */
194 *p++ = pb_system_apps.tftp; /* 1 */
196 *p++ = "binary"; /* 3 */
197 *p++ = url->host; /* 4 */
199 *p++ = url->port; /* 5 */
201 *p++ = "get"; /* 7 */
202 *p++ = url->path; /* 8 */
203 *p++ = local; /* 9 */
204 *p++ = NULL; /* 10 */
206 result = pb_run_cmd(argv, 1);
217 wget_no_check_certificate = 1,
221 * pb_load_wget - Loads a remote file via wget and returns the local file path.
223 * Returns the local file path in a talloc'ed character string on success,
227 static char *pb_load_wget(void *ctx, struct pb_url *url, enum wget_flags flags)
234 local = pb_local_name(ctx);
240 *p++ = pb_system_apps.wget; /* 1 */
242 *p++ = "--quiet"; /* 2 */
245 *p++ = local; /* 4 */
246 *p++ = url->full; /* 5 */
247 if (flags & wget_no_check_certificate)
248 *p++ = "--no-check-certificate"; /* 6 */
251 result = pb_run_cmd(argv, 1);
264 * pb_load_file - Loads a remote file and returns the local file path.
265 * @ctx: The talloc context to associate with the returned string.
266 * @remote: The remote file URL.
267 * @tempfile: An optional variable pointer to be set when a temporary local
270 * Returns the local file path in a talloc'ed character string on success,
274 char *pb_load_file(void *ctx, const char *remote, unsigned int *tempfile)
277 struct pb_url *url = pb_url_parse(NULL, remote);
285 switch (url->scheme) {
288 local = pb_load_wget(ctx, url, 0);
289 if (tempfile && local)
293 local = pb_load_wget(ctx, url, wget_no_check_certificate);
294 if (tempfile && local)
298 local = pb_load_nfs(ctx, url);
299 if (tempfile && local)
303 local = pb_load_sftp(ctx, url);
304 if (tempfile && local)
308 local = pb_load_tftp(ctx, url);
309 if (tempfile && local)
313 local = talloc_strdup(ctx, url->full);