libopkg: remove default progress bar
[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 #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 return 0;
62 }
63
64 int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name)
65 {
66 int err = 0;
67
68 char *src_basec = strdup(src);
69 char *src_base = basename(src_basec);
70 char *tmp_file_location;
71
72 opkg_message(conf,OPKG_NOTICE,"Downloading %s\n", src);
73
74 if (str_starts_with(src, "file:")) {
75 int ret;
76 const char *file_src = src + 5;
77 opkg_message(conf,OPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
78 ret = file_copy(src + 5, dest_file_name);
79 opkg_message(conf,OPKG_INFO,"Done\n");
80 return ret;
81 }
82
83 sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
84 err = unlink(tmp_file_location);
85 if (err && errno != ENOENT) {
86 opkg_message(conf,OPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
87 __FUNCTION__, tmp_file_location, strerror(errno));
88 free(tmp_file_location);
89 return errno;
90 }
91
92 if (conf->http_proxy) {
93 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
94 setenv("http_proxy", conf->http_proxy, 1);
95 }
96 if (conf->ftp_proxy) {
97 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
98 setenv("ftp_proxy", conf->ftp_proxy, 1);
99 }
100 if (conf->no_proxy) {
101 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
102 setenv("no_proxy", conf->no_proxy, 1);
103 }
104
105 CURL *curl;
106 CURLcode res;
107 FILE * file = fopen (tmp_file_location, "w");
108
109 curl = curl_easy_init ();
110 if (curl)
111 {
112 curl_easy_setopt (curl, CURLOPT_URL, src);
113 curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
114 curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0);
115 curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, src);
116 curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
117 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
118 if (conf->http_proxy || conf->ftp_proxy)
119 {
120 char *userpwd;
121 sprintf_alloc (&userpwd, "%s:%s", conf->proxy_user,
122 conf->proxy_passwd);
123 curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
124 free (userpwd);
125 }
126 res = curl_easy_perform (curl);
127 curl_easy_cleanup (curl);
128 fclose (file);
129 if (res)
130 {
131 long error_code;
132 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &error_code);
133 opkg_message(conf, OPKG_ERROR, "Failed to download %s, error %d\n", src, error_code);
134 return res;
135 }
136
137 }
138 else
139 return -1;
140
141 err = file_move(tmp_file_location, dest_file_name);
142
143 free(tmp_file_location);
144 free(src_basec);
145
146 if (err) {
147 return err;
148 }
149
150 return 0;
151 }
152
153 int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
154 {
155 int err;
156 char *url;
157 char *pkgid;
158 char *stripped_filename;
159
160 if (pkg->src == NULL) {
161 opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
162 pkg->name, pkg->parent->name);
163 return -1;
164 }
165
166 sprintf_alloc (&pkgid, "%s;%s;%s;", pkg->name, pkg->version, pkg->architecture);
167 opkg_set_current_state (conf, OPKG_STATE_DOWNLOADING_PKG, pkgid);
168 free (pkgid);
169
170 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
171
172 /* XXX: BUG: The pkg->filename might be something like
173 "../../foo.ipk". While this is correct, and exactly what we
174 want to use to construct url above, here we actually need to
175 use just the filename part, without any directory. */
176
177 stripped_filename = strrchr(pkg->filename, '/');
178 if ( ! stripped_filename )
179 stripped_filename = pkg->filename;
180
181 sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename);
182
183 err = opkg_download(conf, url, pkg->local_filename);
184 free(url);
185
186 opkg_set_current_state (conf, OPKG_STATE_NONE, NULL);
187 return err;
188 }
189
190 /*
191 * Downloads file from url, installs in package database, return package name.
192 */
193 int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **namep)
194 {
195 int err = 0;
196 pkg_t *pkg;
197 pkg = pkg_new();
198 if (pkg == NULL)
199 return ENOMEM;
200
201 if (str_starts_with(url, "http://")
202 || str_starts_with(url, "ftp://")) {
203 char *tmp_file;
204 char *file_basec = strdup(url);
205 char *file_base = basename(file_basec);
206
207 sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
208 err = opkg_download(conf, url, tmp_file);
209 if (err)
210 return err;
211
212 err = pkg_init_from_file(pkg, tmp_file);
213 if (err)
214 return err;
215 pkg->local_filename = strdup(tmp_file);
216
217 free(tmp_file);
218 free(file_basec);
219
220 } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
221 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
222
223 err = pkg_init_from_file(pkg, url);
224 if (err)
225 return err;
226 pkg->local_filename = strdup(url);
227 opkg_message(conf, OPKG_DEBUG2, "Package %s provided by hand (%s).\n", pkg->name,pkg->local_filename);
228 pkg->provided_by_hand = 1;
229
230 } else {
231 pkg_deinit(pkg);
232 free(pkg);
233 return 0;
234 }
235
236 if (!pkg->architecture) {
237 opkg_message(conf, OPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
238 return -EINVAL;
239 }
240
241 pkg->dest = conf->default_dest;
242 pkg->state_want = SW_INSTALL;
243 pkg->state_flag |= SF_PREFER;
244 pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);
245 if ( pkg == NULL ){
246 fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
247 return 0;
248 }
249 if (namep) {
250 *namep = strdup(pkg->name);
251 }
252 return 0;
253 }
254
255 int
256 opkg_verify_file (opkg_conf_t *conf, char *text_file, char *sig_file)
257 {
258 #ifdef HAVE_GPGME
259 int status = -1;
260 gpgme_ctx_t ctx;
261 gpgme_data_t sig, text;
262 gpgme_error_t err = -1;
263 gpgme_verify_result_t result;
264 gpgme_signature_t s;
265
266 err = gpgme_new (&ctx);
267
268 if (err)
269 return -1;
270
271 err = gpgme_data_new_from_file (&sig, sig_file, 1);
272 if (err)
273 return -1;
274
275 err = gpgme_data_new_from_file (&text, text_file, 1);
276 if (err)
277 return -1;
278
279 err = gpgme_op_verify (ctx, sig, text, NULL);
280
281 result = gpgme_op_verify_result (ctx);
282
283 /* see if any of the signitures matched */
284 s = result->signatures;
285 while (s)
286 {
287 status = gpg_err_code (s->status);
288 if (status == GPG_ERR_NO_ERROR)
289 break;
290 s = s->next;
291 }
292
293 gpgme_data_release (sig);
294 gpgme_data_release (text);
295 gpgme_release (ctx);
296
297 return status;
298 #else
299 opkg_message (conf, OPKG_NOTICE, "Signature check for %s was skipped because GPG support was not enabled in this build\n");
300 return 0;
301 #endif
302 }