opkg: apply "1-pkg-parse--Optimize-inefficient-parsing.patch" from OpenEmbedded
[oweals/opkg-lede.git] / 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
20 #include <curl/curl.h>
21
22 #include "opkg.h"
23 #include "opkg_download.h"
24 #include "opkg_message.h"
25
26 #include "sprintf_alloc.h"
27 #include "xsystem.h"
28 #include "file_util.h"
29 #include "str_util.h"
30
31 #ifdef OPKG_LIB
32 #include "libopkg.h"
33 opkg_download_progress_callback opkg_cb_download_progress = NULL;
34 #endif
35
36 int
37 curl_progress_func (char* url,
38                     double t, /* dltotal */
39                     double d, /* dlnow */
40                     double ultotal,
41                     double ulnow)
42 {
43     int i;
44     int p = d*100/t;
45
46 #ifdef OPKG_LIB
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 #endif
61
62     printf ("\r%3d%% |", p);
63     for (i = 1; i < 73; i++)
64     {
65         if (i <= p * 0.73)
66             printf ("=");
67         else
68             printf ("-");
69     }
70     printf ("|");
71     fflush(stdout);
72     return 0;
73 }
74
75 int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name)
76 {
77     int err = 0;
78
79     char *src_basec = strdup(src);
80     char *src_base = basename(src_basec);
81     char *tmp_file_location;
82
83     opkg_message(conf,OPKG_NOTICE,"Downloading %s\n", src);
84         
85     fflush(stdout);
86     
87     if (str_starts_with(src, "file:")) {
88         int ret;
89         const char *file_src = src + 5;
90         opkg_message(conf,OPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
91         ret = file_copy(src + 5, dest_file_name);
92         opkg_message(conf,OPKG_INFO,"Done\n");
93         return ret;
94     }
95
96     sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
97     err = unlink(tmp_file_location);
98     if (err && errno != ENOENT) {
99         opkg_message(conf,OPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
100                 __FUNCTION__, tmp_file_location, strerror(errno));
101         free(tmp_file_location);
102         return errno;
103     }
104
105     if (conf->http_proxy) {
106         opkg_message(conf,OPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
107         setenv("http_proxy", conf->http_proxy, 1);
108     }
109     if (conf->ftp_proxy) {
110         opkg_message(conf,OPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
111         setenv("ftp_proxy", conf->ftp_proxy, 1);
112     }
113     if (conf->no_proxy) {
114         opkg_message(conf,OPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
115         setenv("no_proxy", conf->no_proxy, 1);
116     }
117
118     /* XXX: BUG rewrite to use execvp or else busybox's internal wget -Jamey 7/23/2002 */ 
119 #if 0
120     sprintf_alloc(&cmd, "wget --passive-ftp %s %s%s %s%s %s -P %s %s",
121                   (conf->http_proxy || conf->ftp_proxy) ? "--proxy=on" : "",
122                   conf->proxy_user ? "--proxy-user=" : "",
123                   conf->proxy_user ? conf->proxy_user : "",
124                   conf->proxy_passwd ? "--proxy-passwd=" : "",
125                   conf->proxy_passwd ? conf->proxy_passwd : "",
126                   conf->verbose_wget ? "" : "-q",
127                   conf->tmp_dir,
128                   src);
129     err = xsystem(cmd);
130     if (err) {
131         if (err != -1) {
132             opkg_message(conf,OPKG_ERROR, "%s: ERROR: Command failed with return value %d: `%s'\n",
133                     __FUNCTION__, err, cmd);
134         } 
135         unlink(tmp_file_location);
136         free(tmp_file_location);
137         free(src_basec);
138         free(cmd);
139         return EINVAL;
140     }
141     free(cmd);
142 #endif
143     CURL *curl;
144     CURLcode res;
145     FILE * file = fopen (tmp_file_location, "w");
146
147     curl = curl_easy_init ();
148     if (curl)
149     {
150         curl_easy_setopt (curl, CURLOPT_URL, src);
151         curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
152         curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0);
153         curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, src);
154         curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
155         if (conf->http_proxy || conf->ftp_proxy)
156         {
157             char *userpwd;
158             sprintf_alloc (&userpwd, "%s:%s", conf->proxy_user,
159                     conf->proxy_passwd);
160             curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
161             free (userpwd);
162         }
163         res = curl_easy_perform (curl);
164         curl_easy_cleanup (curl);
165         fclose (file);
166
167     }
168     else
169         return -1;
170
171     printf ("\n");
172
173     err = file_move(tmp_file_location, dest_file_name);
174
175     free(tmp_file_location);
176     free(src_basec);
177
178     if (err) {
179         return err;
180     }
181
182     return 0;
183 }
184
185 int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
186 {
187     int err;
188     char *url;
189
190     if (pkg->src == NULL) {
191         opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
192                 pkg->name, pkg->parent->name);
193         return -1;
194     }
195
196     sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
197
198     /* XXX: BUG: The pkg->filename might be something like
199        "../../foo.ipk". While this is correct, and exactly what we
200        want to use to construct url above, here we actually need to
201        use just the filename part, without any directory. */
202     sprintf_alloc(&pkg->local_filename, "%s/%s", dir, pkg->filename);
203
204     err = opkg_download(conf, url, pkg->local_filename);
205     free(url);
206
207     return err;
208 }
209
210 /*
211  * Downloads file from url, installs in package database, return package name. 
212  */
213 int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **namep)
214 {
215      int err = 0;
216      pkg_t *pkg;
217      pkg = pkg_new();
218      if (pkg == NULL)
219           return ENOMEM;
220
221      if (str_starts_with(url, "http://")
222          || str_starts_with(url, "ftp://")) {
223           char *tmp_file;
224           char *file_basec = strdup(url);
225           char *file_base = basename(file_basec);
226
227           sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
228           err = opkg_download(conf, url, tmp_file);
229           if (err)
230                return err;
231
232           err = pkg_init_from_file(pkg, tmp_file);
233           if (err)
234                return err;
235           pkg->local_filename = strdup(tmp_file);
236
237           free(tmp_file);
238           free(file_basec);
239
240      } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
241                 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
242
243           err = pkg_init_from_file(pkg, url);
244           if (err)
245                return err;
246           pkg->local_filename = strdup(url);
247           opkg_message(conf, OPKG_DEBUG2, "Package %s provided by hand (%s).\n", pkg->name,pkg->local_filename);
248           pkg->provided_by_hand = 1;
249
250      } else {
251        pkg_deinit(pkg);
252        free(pkg);
253        return 0;
254      }
255
256      if (!pkg->architecture) {
257           opkg_message(conf, OPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
258           return -EINVAL;
259      }
260
261      pkg->dest = conf->default_dest;
262      pkg->state_want = SW_INSTALL;
263      pkg->state_flag |= SF_PREFER;
264      pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);  
265      if ( pkg == NULL ){
266         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
267         return 0;
268      }
269      if (namep) {
270           *namep = strdup(pkg->name);
271      }
272      return 0;
273 }