opkg: remove opkg.h in preperation for new API
[project/opkg-lede.git] / libopkg / opkg_download.c
1 /* vi: set noexpandtab sw=4 sts=4: */
2 /* opkg_download.c - the itsy 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 #include "config.h"
20 #include <curl/curl.h>
21 #ifdef HAVE_GPGME
22 #include <gpgme.h>
23 #endif
24
25 #include "includes.h"
26 #include "opkg_download.h"
27 #include "opkg_message.h"
28 #include "opkg_state.h"
29
30 #include "sprintf_alloc.h"
31 #include "xsystem.h"
32 #include "file_util.h"
33 #include "str_util.h"
34 #include "opkg_defines.h"
35
36 opkg_download_progress_callback opkg_cb_download_progress = NULL;
37
38 int
39 curl_progress_func (char* url,
40 double t, /* dltotal */
41 double d, /* dlnow */
42 double ultotal,
43 double ulnow)
44 {
45 int i;
46 int p = (t) ? d*100/t : 0;
47
48 if (opkg_cb_download_progress)
49 {
50 static int prev = -1;
51
52 /* don't report the same percentage multiple times
53 * (this can occur due to rounding) */
54 if (prev == p)
55 return 0;
56 prev = p;
57
58 opkg_cb_download_progress (p, url);
59 return 0;
60 }
61
62 /* skip progress bar if we haven't done started yet
63 * this prevents drawing the progress bar if we receive an error such as
64 * file not found */
65 if (t == 0)
66 return 0;
67
68 printf ("\r%3d%% |", p);
69 for (i = 1; i < 73; i++)
70 {
71 if (i <= p * 0.73)
72 printf ("=");
73 else
74 printf ("-");
75 }
76 printf ("|");
77 fflush(stdout);
78 return 0;
79 }
80
81 int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name)
82 {
83 int err = 0;
84
85 char *src_basec = strdup(src);
86 char *src_base = basename(src_basec);
87 char *tmp_file_location;
88
89 opkg_message(conf,OPKG_NOTICE,"Downloading %s\n", src);
90
91 fflush(stdout);
92
93 if (str_starts_with(src, "file:")) {
94 int ret;
95 const char *file_src = src + 5;
96 opkg_message(conf,OPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
97 ret = file_copy(src + 5, dest_file_name);
98 opkg_message(conf,OPKG_INFO,"Done\n");
99 return ret;
100 }
101
102 sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
103 err = unlink(tmp_file_location);
104 if (err && errno != ENOENT) {
105 opkg_message(conf,OPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
106 __FUNCTION__, tmp_file_location, strerror(errno));
107 free(tmp_file_location);
108 return errno;
109 }
110
111 if (conf->http_proxy) {
112 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
113 setenv("http_proxy", conf->http_proxy, 1);
114 }
115 if (conf->ftp_proxy) {
116 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
117 setenv("ftp_proxy", conf->ftp_proxy, 1);
118 }
119 if (conf->no_proxy) {
120 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
121 setenv("no_proxy", conf->no_proxy, 1);
122 }
123
124 CURL *curl;
125 CURLcode res;
126 FILE * file = fopen (tmp_file_location, "w");
127
128 curl = curl_easy_init ();
129 if (curl)
130 {
131 curl_easy_setopt (curl, CURLOPT_URL, src);
132 curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
133 curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0);
134 curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, src);
135 curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
136 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
137 if (conf->http_proxy || conf->ftp_proxy)
138 {
139 char *userpwd;
140 sprintf_alloc (&userpwd, "%s:%s", conf->proxy_user,
141 conf->proxy_passwd);
142 curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
143 free (userpwd);
144 }
145 res = curl_easy_perform (curl);
146 curl_easy_cleanup (curl);
147 fclose (file);
148 if (res)
149 {
150 long error_code;
151 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &error_code);
152 opkg_message(conf, OPKG_ERROR, "Failed to download %s, error %d\n", src, error_code);
153 return res;
154 }
155
156 }
157 else
158 return -1;
159
160 /* if no custom progress handler was set, we need to clear the default progress bar */
161 if (!opkg_cb_download_progress)
162 printf ("\n");
163
164 err = file_move(tmp_file_location, dest_file_name);
165
166 free(tmp_file_location);
167 free(src_basec);
168
169 if (err) {
170 return err;
171 }
172
173 return 0;
174 }
175
176 int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
177 {
178 int err;
179 char *url;
180 char *pkgid;
181
182 if (pkg->src == NULL) {
183 opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
184 pkg->name, pkg->parent->name);
185 return -1;
186 }
187
188 sprintf_alloc (&pkgid, "%s;%s;%s;", pkg->name, pkg->version, pkg->architecture);
189 opkg_set_current_state (conf, OPKG_STATE_DOWNLOADING_PKG, pkgid);
190 free (pkgid);
191
192 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
193
194 /* XXX: BUG: The pkg->filename might be something like
195 "../../foo.ipk". While this is correct, and exactly what we
196 want to use to construct url above, here we actually need to
197 use just the filename part, without any directory. */
198 sprintf_alloc(&pkg->local_filename, "%s/%s", dir, pkg->filename);
199
200 err = opkg_download(conf, url, pkg->local_filename);
201 free(url);
202
203 opkg_set_current_state (conf, OPKG_STATE_NONE, NULL);
204 return err;
205 }
206
207 /*
208 * Downloads file from url, installs in package database, return package name.
209 */
210 int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **namep)
211 {
212 int err = 0;
213 pkg_t *pkg;
214 pkg = pkg_new();
215 if (pkg == NULL)
216 return ENOMEM;
217
218 if (str_starts_with(url, "http://")
219 || str_starts_with(url, "ftp://")) {
220 char *tmp_file;
221 char *file_basec = strdup(url);
222 char *file_base = basename(file_basec);
223
224 sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
225 err = opkg_download(conf, url, tmp_file);
226 if (err)
227 return err;
228
229 err = pkg_init_from_file(pkg, tmp_file);
230 if (err)
231 return err;
232 pkg->local_filename = strdup(tmp_file);
233
234 free(tmp_file);
235 free(file_basec);
236
237 } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
238 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
239
240 err = pkg_init_from_file(pkg, url);
241 if (err)
242 return err;
243 pkg->local_filename = strdup(url);
244 opkg_message(conf, OPKG_DEBUG2, "Package %s provided by hand (%s).\n", pkg->name,pkg->local_filename);
245 pkg->provided_by_hand = 1;
246
247 } else {
248 pkg_deinit(pkg);
249 free(pkg);
250 return 0;
251 }
252
253 if (!pkg->architecture) {
254 opkg_message(conf, OPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
255 return -EINVAL;
256 }
257
258 pkg->dest = conf->default_dest;
259 pkg->state_want = SW_INSTALL;
260 pkg->state_flag |= SF_PREFER;
261 pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);
262 if ( pkg == NULL ){
263 fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
264 return 0;
265 }
266 if (namep) {
267 *namep = strdup(pkg->name);
268 }
269 return 0;
270 }
271
272 int
273 opkg_verify_file (opkg_conf_t *conf, char *text_file, char *sig_file)
274 {
275 #ifdef HAVE_GPGME
276 int status = -1;
277 gpgme_ctx_t ctx;
278 gpgme_data_t sig, text;
279 gpgme_error_t err = -1;
280 gpgme_verify_result_t result;
281 gpgme_signature_t s;
282
283 err = gpgme_new (&ctx);
284
285 if (err)
286 return -1;
287
288 err = gpgme_data_new_from_file (&sig, sig_file, 1);
289 if (err)
290 return -1;
291
292 err = gpgme_data_new_from_file (&text, text_file, 1);
293 if (err)
294 return -1;
295
296 err = gpgme_op_verify (ctx, sig, text, NULL);
297
298 result = gpgme_op_verify_result (ctx);
299
300 /* see if any of the signitures matched */
301 s = result->signatures;
302 while (s)
303 {
304 status = gpg_err_code (s->status);
305 if (status == GPG_ERR_NO_ERROR)
306 break;
307 s = s->next;
308 }
309
310 gpgme_data_release (sig);
311 gpgme_data_release (text);
312 gpgme_release (ctx);
313
314 return status;
315 #else
316 opkg_message (conf, OPKG_NOTICE, "Signature check for %s was skipped because GPG support was not enabled in this build\n");
317 return 0;
318 #endif
319 }