download: remove compatibility with old cache naming scheme
[project/opkg-lede.git] / libopkg / opkg_download.c
1 /* vi: set noexpandtab sw=4 sts=4: */
2 /* opkg_download.c - the opkg package management system
3
4 Carl D. Worth
5
6 Copyright (C) 2001 University of Southern California
7 Copyright (C) 2008 OpenMoko Inc
8
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.
13
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.
18 */
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <libgen.h>
23
24 #include "opkg_download.h"
25 #include "opkg_message.h"
26
27 #include "sprintf_alloc.h"
28 #include "xsystem.h"
29 #include "file_util.h"
30 #include "opkg_defines.h"
31 #include "libbb/libbb.h"
32
33 static int str_starts_with(const char *str, const char *prefix)
34 {
35 return (strncmp(str, prefix, strlen(prefix)) == 0);
36 }
37
38 int
39 opkg_download(const char *src, const char *dest_file_name,
40 const short hide_error)
41 {
42 int err = 0;
43
44 char *src_basec = xstrdup(src);
45 char *src_base = basename(src_basec);
46 char *tmp_file_location;
47
48 opkg_msg(NOTICE, "Downloading %s\n", src);
49
50 if (str_starts_with(src, "file:")) {
51 char *file_src = urldecode_path(src + 5);
52 opkg_msg(INFO, "Copying %s to %s...", file_src, dest_file_name);
53 err = file_copy(file_src, dest_file_name);
54 opkg_msg(INFO, "Done.\n");
55 free(src_basec);
56 free(file_src);
57 return err;
58 }
59
60 sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
61 free(src_basec);
62 err = unlink(tmp_file_location);
63 if (err && errno != ENOENT) {
64 opkg_perror(ERROR, "Failed to unlink %s", tmp_file_location);
65 free(tmp_file_location);
66 return -1;
67 }
68
69 if (conf->http_proxy) {
70 opkg_msg(DEBUG,
71 "Setting environment variable: http_proxy = %s.\n",
72 conf->http_proxy);
73 setenv("http_proxy", conf->http_proxy, 1);
74 }
75 if (conf->ftp_proxy) {
76 opkg_msg(DEBUG,
77 "Setting environment variable: ftp_proxy = %s.\n",
78 conf->ftp_proxy);
79 setenv("ftp_proxy", conf->ftp_proxy, 1);
80 }
81 if (conf->no_proxy) {
82 opkg_msg(DEBUG,
83 "Setting environment variable: no_proxy = %s.\n",
84 conf->no_proxy);
85 setenv("no_proxy", conf->no_proxy, 1);
86 }
87
88 {
89 int res;
90 const char *argv[11];
91 int i = 0;
92
93 argv[i++] = "wget";
94 argv[i++] = "-q";
95 if (conf->no_check_certificate) {
96 argv[i++] = "--no-check-certificate";
97 }
98 if (conf->http_timeout) {
99 argv[i++] = "--timeout";
100 argv[i++] = conf->http_timeout;
101 }
102 if (conf->http_proxy || conf->ftp_proxy) {
103 argv[i++] = "-Y";
104 argv[i++] = "on";
105 }
106 argv[i++] = "-O";
107 argv[i++] = tmp_file_location;
108 argv[i++] = src;
109 argv[i++] = NULL;
110 res = xsystem(argv);
111
112 if (res) {
113 opkg_msg(ERROR,
114 "Failed to download %s, wget returned %d.\n",
115 src, res);
116 if (res == 4)
117 opkg_msg(ERROR,
118 "Check your network settings and connectivity.\n\n");
119 free(tmp_file_location);
120 return -1;
121 }
122 }
123
124 err = file_move(tmp_file_location, dest_file_name);
125
126 free(tmp_file_location);
127
128 return err;
129 }
130
131 static int
132 opkg_download_cache(const char *src, const char *dest_file_name)
133 {
134 char *cache_name, *cache_location;
135 int err = 0;
136
137 if (!conf->cache || str_starts_with(src, "file:")) {
138 err = opkg_download(src, dest_file_name, 0);
139 goto out1;
140 }
141
142 if (!file_is_dir(conf->cache)) {
143 opkg_msg(ERROR, "%s is not a directory.\n", conf->cache);
144 err = 1;
145 goto out1;
146 }
147
148 char *filename = strrchr(dest_file_name, '/');
149 if (filename)
150 cache_name = xstrdup(filename + 1); // strip leading '/'
151 else
152 cache_name = xstrdup(dest_file_name);
153 sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
154 if (file_exists(cache_location))
155 opkg_msg(NOTICE, "Copying %s.\n", cache_location);
156 else {
157 err = opkg_download(src, cache_location, 0);
158 if (err) {
159 (void)unlink(cache_location);
160 goto out2;
161 }
162 }
163
164 err = file_copy(cache_location, dest_file_name);
165
166 out2:
167 free(cache_location);
168 free(cache_name);
169 out1:
170 return err;
171 }
172
173 int opkg_download_pkg(pkg_t * pkg, const char *dir)
174 {
175 int err;
176 char *url;
177 char *local_filename;
178 char *stripped_filename;
179 char *urlencoded_path;
180 char *filename;
181
182 if (pkg->src == NULL) {
183 opkg_msg(ERROR,
184 "Package %s is not available from any configured src.\n",
185 pkg->name);
186 return -1;
187 }
188
189 filename = pkg_get_string(pkg, PKG_FILENAME);
190
191 if (filename == NULL) {
192 opkg_msg(ERROR,
193 "Package %s does not have a valid filename field.\n",
194 pkg->name);
195 return -1;
196 }
197
198 urlencoded_path = urlencode_path(filename);
199 sprintf_alloc(&url, "%s/%s", pkg->src->value, urlencoded_path);
200 free(urlencoded_path);
201
202 /* The filename might be something like
203 "../../foo.opk". While this is correct, and exactly what we
204 want to use to construct url above, here we actually need to
205 use just the filename part, without any directory. */
206
207 stripped_filename = strrchr(filename, '/');
208 if (!stripped_filename)
209 stripped_filename = filename;
210
211 sprintf_alloc(&local_filename, "%s/%s", dir, stripped_filename);
212 pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
213
214 err = opkg_download_cache(url, local_filename);
215 free(url);
216
217 return err;
218 }
219
220 /*
221 * Downloads file from url, installs in package database, return package name.
222 */
223 int opkg_prepare_url_for_install(const char *url, char **namep)
224 {
225 int err = 0;
226 pkg_t *pkg;
227 abstract_pkg_t *ab_pkg;
228
229 pkg = pkg_new();
230
231 if (str_starts_with(url, "http://")
232 || str_starts_with(url, "ftp://")) {
233 char *tmp_file;
234 char *file_basec = xstrdup(url);
235 char *file_base = basename(file_basec);
236
237 sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
238 err = opkg_download(url, tmp_file, 0);
239 if (err)
240 return err;
241
242 err = pkg_init_from_file(pkg, tmp_file);
243 if (err)
244 return err;
245
246 free(tmp_file);
247 free(file_basec);
248
249 } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
250 || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
251 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
252
253 err = pkg_init_from_file(pkg, url);
254 if (err)
255 return err;
256 opkg_msg(DEBUG2, "Package %s provided by hand (%s).\n",
257 pkg->name, pkg_get_string(pkg, PKG_LOCAL_FILENAME));
258 pkg->provided_by_hand = 1;
259
260 } else {
261 ab_pkg = ensure_abstract_pkg_by_name(url);
262
263 if (!(ab_pkg->state_flag & SF_NEED_DETAIL)) {
264 opkg_msg(DEBUG, "applying abpkg flag to %s\n", ab_pkg->name);
265 ab_pkg->state_flag |= SF_NEED_DETAIL;
266 }
267
268 pkg_deinit(pkg);
269 free(pkg);
270 return 0;
271 }
272
273 pkg->dest = conf->default_dest;
274 pkg->state_want = SW_INSTALL;
275 pkg->state_flag |= SF_PREFER;
276 hash_insert_pkg(pkg, 1);
277
278 if (namep) {
279 *namep = xstrdup(pkg->name);
280 }
281 return 0;
282 }
283
284 int opkg_verify_file(char *text_file, char *sig_file)
285 {
286 #if defined HAVE_USIGN
287 const char *argv[] = { conf->verify_program, "verify", sig_file,
288 text_file, NULL };
289
290 return xsystem(argv) ? -1 : 0;
291 #else
292 /* mute `unused variable' warnings. */
293 (void)sig_file;
294 (void)text_file;
295 (void)conf;
296 return 0;
297 #endif
298 }