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