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