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