1 /* vi: set noexpandtab sw=4 sts=4: */
2 /* opkg_download.c - the opkg package management system
6 Copyright (C) 2001 University of Southern California
7 Copyright (C) 2008 OpenMoko Inc
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
25 #include "opkg_download.h"
26 #include "opkg_message.h"
28 #include "sprintf_alloc.h"
30 #include "file_util.h"
31 #include "opkg_defines.h"
32 #include "libbb/libbb.h"
34 static int str_starts_with(const char *str
, const char *prefix
)
36 return (strncmp(str
, prefix
, strlen(prefix
)) == 0);
40 opkg_download(const char *src
, const char *dest_file_name
,
41 const short hide_error
)
45 char *src_basec
= xstrdup(src
);
46 char *src_base
= basename(src_basec
);
47 char *tmp_file_location
;
49 opkg_msg(NOTICE
, "Downloading %s\n", src
);
51 if (str_starts_with(src
, "file:")) {
52 char *file_src
= urldecode_path(src
+ 5);
53 opkg_msg(INFO
, "Copying %s to %s...", file_src
, dest_file_name
);
54 err
= file_copy(file_src
, dest_file_name
);
55 opkg_msg(INFO
, "Done.\n");
61 sprintf_alloc(&tmp_file_location
, "%s/%s", conf
->tmp_dir
, src_base
);
63 err
= unlink(tmp_file_location
);
64 if (err
&& errno
!= ENOENT
) {
65 opkg_perror(ERROR
, "Failed to unlink %s", tmp_file_location
);
66 free(tmp_file_location
);
70 if (conf
->http_proxy
) {
72 "Setting environment variable: http_proxy = %s.\n",
74 setenv("http_proxy", conf
->http_proxy
, 1);
76 if (conf
->ftp_proxy
) {
78 "Setting environment variable: ftp_proxy = %s.\n",
80 setenv("ftp_proxy", conf
->ftp_proxy
, 1);
84 "Setting environment variable: no_proxy = %s.\n",
86 setenv("no_proxy", conf
->no_proxy
, 1);
96 if (conf
->no_check_certificate
) {
97 argv
[i
++] = "--no-check-certificate";
99 if (conf
->http_timeout
) {
100 argv
[i
++] = "--timeout";
101 argv
[i
++] = conf
->http_timeout
;
103 if (conf
->http_proxy
|| conf
->ftp_proxy
) {
108 argv
[i
++] = tmp_file_location
;
115 "Failed to download %s, wget returned %d.\n",
119 "Check your network settings and connectivity.\n\n");
120 free(tmp_file_location
);
125 err
= file_move(tmp_file_location
, dest_file_name
);
127 free(tmp_file_location
);
133 opkg_download_cache(const char *src
, const char *dest_file_name
)
135 char *cache_name
= xstrdup(src
);
136 char *cache_location
, *p
;
139 if (!conf
->cache
|| str_starts_with(src
, "file:")) {
140 err
= opkg_download(src
, dest_file_name
, 0);
144 if (!file_is_dir(conf
->cache
)) {
145 opkg_msg(ERROR
, "%s is not a directory.\n", conf
->cache
);
150 for (p
= cache_name
; *p
; p
++)
152 *p
= ','; /* looks nicer than | or # */
154 sprintf_alloc(&cache_location
, "%s/%s", conf
->cache
, cache_name
);
155 if (file_exists(cache_location
))
156 opkg_msg(NOTICE
, "Copying %s.\n", cache_location
);
158 /* cache file with funky name not found, try simple name */
160 char *filename
= strrchr(dest_file_name
, '/');
162 cache_name
= xstrdup(filename
+ 1); // strip leading '/'
164 cache_name
= xstrdup(dest_file_name
);
165 free(cache_location
);
166 sprintf_alloc(&cache_location
, "%s/%s", conf
->cache
,
168 if (file_exists(cache_location
))
169 opkg_msg(NOTICE
, "Copying %s.\n", cache_location
);
171 err
= opkg_download(src
, cache_location
, 0);
173 (void)unlink(cache_location
);
179 err
= file_copy(cache_location
, dest_file_name
);
182 free(cache_location
);
188 int opkg_download_pkg(pkg_t
* pkg
, const char *dir
)
192 char *local_filename
;
193 char *stripped_filename
;
194 char *urlencoded_path
;
197 if (pkg
->src
== NULL
) {
199 "Package %s is not available from any configured src.\n",
204 filename
= pkg_get_string(pkg
, PKG_FILENAME
);
206 if (filename
== NULL
) {
208 "Package %s does not have a valid filename field.\n",
213 urlencoded_path
= urlencode_path(filename
);
214 sprintf_alloc(&url
, "%s/%s", pkg
->src
->value
, urlencoded_path
);
215 free(urlencoded_path
);
217 /* The filename might be something like
218 "../../foo.opk". While this is correct, and exactly what we
219 want to use to construct url above, here we actually need to
220 use just the filename part, without any directory. */
222 stripped_filename
= strrchr(filename
, '/');
223 if (!stripped_filename
)
224 stripped_filename
= filename
;
226 sprintf_alloc(&local_filename
, "%s/%s", dir
, stripped_filename
);
227 pkg_set_string(pkg
, PKG_LOCAL_FILENAME
, local_filename
);
229 err
= opkg_download_cache(url
, local_filename
);
236 * Downloads file from url, installs in package database, return package name.
238 int opkg_prepare_url_for_install(const char *url
, char **namep
)
242 abstract_pkg_t
*ab_pkg
;
246 if (str_starts_with(url
, "http://")
247 || str_starts_with(url
, "ftp://")) {
249 char *file_basec
= xstrdup(url
);
250 char *file_base
= basename(file_basec
);
252 sprintf_alloc(&tmp_file
, "%s/%s", conf
->tmp_dir
, file_base
);
253 err
= opkg_download(url
, tmp_file
, 0);
257 err
= pkg_init_from_file(pkg
, tmp_file
);
264 } else if (strcmp(&url
[strlen(url
) - 4], OPKG_PKG_EXTENSION
) == 0
265 || strcmp(&url
[strlen(url
) - 4], IPKG_PKG_EXTENSION
) == 0
266 || strcmp(&url
[strlen(url
) - 4], DPKG_PKG_EXTENSION
) == 0) {
268 err
= pkg_init_from_file(pkg
, url
);
271 opkg_msg(DEBUG2
, "Package %s provided by hand (%s).\n",
272 pkg
->name
, pkg_get_string(pkg
, PKG_LOCAL_FILENAME
));
273 pkg
->provided_by_hand
= 1;
276 ab_pkg
= ensure_abstract_pkg_by_name(url
);
278 if (!(ab_pkg
->state_flag
& SF_NEED_DETAIL
)) {
279 opkg_msg(DEBUG
, "applying abpkg flag to %s\n", ab_pkg
->name
);
280 ab_pkg
->state_flag
|= SF_NEED_DETAIL
;
288 pkg
->dest
= conf
->default_dest
;
289 pkg
->state_want
= SW_INSTALL
;
290 pkg
->state_flag
|= SF_PREFER
;
291 hash_insert_pkg(pkg
, 1);
294 *namep
= xstrdup(pkg
->name
);
299 int opkg_verify_file(char *text_file
, char *sig_file
)
301 #if defined HAVE_USIGN
305 if (conf
->check_signature
== 0)
310 opkg_perror(ERROR
, "Failed to fork opkg-key process");
315 execl("/usr/sbin/opkg-key", "opkg-key", "verify", sig_file
,
320 waitpid(pid
, &status
, 0);
321 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
326 /* mute `unused variable' warnings. */