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