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.
24 #include "opkg_download.h"
25 #include "opkg_message.h"
27 #include "sprintf_alloc.h"
29 #include "file_util.h"
30 #include "opkg_defines.h"
31 #include "libbb/libbb.h"
33 static int str_starts_with(const char *str
, const char *prefix
)
35 return (strncmp(str
, prefix
, strlen(prefix
)) == 0);
38 int opkg_verify_integrity(pkg_t
*pkg
, const char *filename
)
41 char *file_md5
, *pkg_md5
;
42 char *file_sha256
, *pkg_sha256
;
44 long long int pkg_expected_size
;
47 err
= lstat(filename
, &pkg_stat
);
50 opkg_msg(ERROR
, "Failed to stat %s: %s\n",
51 filename
, strerror(errno
));
55 pkg_expected_size
= pkg_get_int(pkg
, PKG_SIZE
);
57 if (pkg_expected_size
> 0 && pkg_stat
.st_size
!= pkg_expected_size
) {
59 "Package size mismatch: %s is %lld bytes, expecting %lld bytes\n",
60 pkg
->name
, (long long int)pkg_stat
.st_size
, pkg_expected_size
);
65 /* Check for md5 values */
66 pkg_md5
= pkg_get_md5(pkg
);
68 file_md5
= file_md5sum_alloc(filename
);
69 if (file_md5
&& strcmp(file_md5
, pkg_md5
)) {
70 opkg_msg(INFO
, "Package %s md5sum mismatch.\n",
80 /* Check for sha256 value */
81 pkg_sha256
= pkg_get_sha256(pkg
);
83 file_sha256
= file_sha256sum_alloc(filename
);
84 if (file_sha256
&& strcmp(file_sha256
, pkg_sha256
)) {
85 opkg_msg(INFO
, "Package %s sha256sum mismatch.\n",
100 opkg_download(const char *src
, const char *dest_file_name
,
101 const short hide_error
)
105 char *src_basec
= xstrdup(src
);
106 char *src_base
= basename(src_basec
);
107 char *tmp_file_location
;
109 opkg_msg(NOTICE
, "Downloading %s\n", src
);
111 if (str_starts_with(src
, "file:")) {
112 char *file_src
= urldecode_path(src
+ 5);
113 opkg_msg(INFO
, "Copying %s to %s...", file_src
, dest_file_name
);
114 err
= file_copy(file_src
, dest_file_name
);
115 opkg_msg(INFO
, "Done.\n");
121 sprintf_alloc(&tmp_file_location
, "%s/%s", conf
->tmp_dir
, src_base
);
123 err
= unlink(tmp_file_location
);
124 if (err
&& errno
!= ENOENT
) {
125 opkg_perror(ERROR
, "Failed to unlink %s", tmp_file_location
);
126 free(tmp_file_location
);
130 if (conf
->http_proxy
) {
132 "Setting environment variable: http_proxy = %s.\n",
134 setenv("http_proxy", conf
->http_proxy
, 1);
136 if (conf
->ftp_proxy
) {
138 "Setting environment variable: ftp_proxy = %s.\n",
140 setenv("ftp_proxy", conf
->ftp_proxy
, 1);
142 if (conf
->no_proxy
) {
144 "Setting environment variable: no_proxy = %s.\n",
146 setenv("no_proxy", conf
->no_proxy
, 1);
151 const char *argv
[11];
156 if (conf
->no_check_certificate
) {
157 argv
[i
++] = "--no-check-certificate";
159 if (conf
->http_timeout
) {
160 argv
[i
++] = "--timeout";
161 argv
[i
++] = conf
->http_timeout
;
163 if (conf
->http_proxy
|| conf
->ftp_proxy
) {
168 argv
[i
++] = tmp_file_location
;
175 "Failed to download %s, wget returned %d.\n",
179 "Check your network settings and connectivity.\n\n");
180 free(tmp_file_location
);
185 err
= file_move(tmp_file_location
, dest_file_name
);
187 free(tmp_file_location
);
192 static char* get_cache_filename(const char *dest_file_name
)
195 char *filename
= strrchr(dest_file_name
, '/');
197 cache_name
= xstrdup(filename
+ 1); // strip leading '/'
199 cache_name
= xstrdup(dest_file_name
);
204 opkg_download_cache(const char *src
, const char *dest_file_name
)
206 char *cache_name
, *cache_location
;
209 if (!conf
->cache
|| str_starts_with(src
, "file:")) {
210 err
= opkg_download(src
, dest_file_name
, 0);
214 if (!file_is_dir(conf
->cache
)) {
215 opkg_msg(ERROR
, "%s is not a directory.\n", conf
->cache
);
220 cache_name
= get_cache_filename(dest_file_name
);
221 sprintf_alloc(&cache_location
, "%s/%s", conf
->cache
, cache_name
);
222 if (file_exists(cache_location
))
223 opkg_msg(NOTICE
, "Copying %s.\n", cache_location
);
225 err
= opkg_download(src
, cache_location
, 0);
227 (void)unlink(cache_location
);
232 err
= file_copy(cache_location
, dest_file_name
);
235 free(cache_location
);
241 int opkg_download_pkg(pkg_t
* pkg
, const char *dir
)
245 char *local_filename
;
246 char *stripped_filename
;
247 char *urlencoded_path
;
250 char *cache_location
;
252 if (pkg
->src
== NULL
) {
254 "Package %s is not available from any configured src.\n",
259 filename
= pkg_get_string(pkg
, PKG_FILENAME
);
261 if (filename
== NULL
) {
263 "Package %s does not have a valid filename field.\n",
268 urlencoded_path
= urlencode_path(filename
);
269 sprintf_alloc(&url
, "%s/%s", pkg
->src
->value
, urlencoded_path
);
270 free(urlencoded_path
);
272 /* The filename might be something like
273 "../../foo.opk". While this is correct, and exactly what we
274 want to use to construct url above, here we actually need to
275 use just the filename part, without any directory. */
277 stripped_filename
= strrchr(filename
, '/');
278 if (!stripped_filename
)
279 stripped_filename
= filename
;
281 sprintf_alloc(&local_filename
, "%s/%s", dir
, stripped_filename
);
282 pkg_set_string(pkg
, PKG_LOCAL_FILENAME
, local_filename
);
284 /* Invalidate/remove cached package if it has an incorrect checksum. */
286 cache_name
= get_cache_filename(local_filename
);
287 sprintf_alloc(&cache_location
, "%s/%s", conf
->cache
, cache_name
);
289 if (file_exists(cache_location
)) {
290 err
= opkg_verify_integrity(pkg
, cache_location
);
293 "Removing %s from cache because it has incorrect checksum.\n",
295 unlink(cache_location
);
298 free(cache_location
);
301 err
= opkg_download_cache(url
, local_filename
);
308 * Downloads file from url, installs in package database, return package name.
310 int opkg_prepare_url_for_install(const char *url
, char **namep
)
314 abstract_pkg_t
*ab_pkg
;
318 if (str_starts_with(url
, "http://")
319 || str_starts_with(url
, "ftp://")) {
321 char *file_basec
= xstrdup(url
);
322 char *file_base
= basename(file_basec
);
324 sprintf_alloc(&tmp_file
, "%s/%s", conf
->tmp_dir
, file_base
);
325 err
= opkg_download(url
, tmp_file
, 0);
329 err
= pkg_init_from_file(pkg
, tmp_file
);
336 } else if (strcmp(&url
[strlen(url
) - 4], OPKG_PKG_EXTENSION
) == 0
337 || strcmp(&url
[strlen(url
) - 4], IPKG_PKG_EXTENSION
) == 0
338 || strcmp(&url
[strlen(url
) - 4], DPKG_PKG_EXTENSION
) == 0) {
340 err
= pkg_init_from_file(pkg
, url
);
343 opkg_msg(DEBUG2
, "Package %s provided by hand (%s).\n",
344 pkg
->name
, pkg_get_string(pkg
, PKG_LOCAL_FILENAME
));
345 pkg
->provided_by_hand
= 1;
348 ab_pkg
= ensure_abstract_pkg_by_name(url
);
350 if (!(ab_pkg
->state_flag
& SF_NEED_DETAIL
)) {
351 opkg_msg(DEBUG
, "applying abpkg flag to %s\n", ab_pkg
->name
);
352 ab_pkg
->state_flag
|= SF_NEED_DETAIL
;
360 pkg
->dest
= conf
->default_dest
;
361 pkg
->state_want
= SW_INSTALL
;
362 pkg
->state_flag
|= SF_PREFER
;
363 hash_insert_pkg(pkg
, 1);
366 *namep
= xstrdup(pkg
->name
);
371 int opkg_verify_file(char *text_file
, char *sig_file
)
373 #if defined HAVE_USIGN
374 const char *argv
[] = { conf
->verify_program
, "verify", sig_file
,
377 return xsystem(argv
) ? -1 : 0;
379 /* mute `unused variable' warnings. */