1 /* vi: set noexpandtab sw=4 sts=4: */
2 /* opkg_download.c - the itsy package management system
6 Copyright (C) 2001 University of Southern California
7 Copyright (C) 2008 OpenMoko Inc
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.
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.
20 #include <curl/curl.h>
23 #include "opkg_download.h"
24 #include "opkg_message.h"
26 #include "sprintf_alloc.h"
28 #include "file_util.h"
33 opkg_download_progress_callback opkg_cb_download_progress = NULL;
37 curl_progress_func (char* url,
38 double t, /* dltotal */
47 if (opkg_cb_download_progress)
51 /* don't report the same percentage multiple times
52 * (this can occur due to rounding) */
57 opkg_cb_download_progress (p, url);
62 printf ("\r%3d%% |", p);
63 for (i = 1; i < 73; i++)
75 int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name)
79 char *src_basec = strdup(src);
80 char *src_base = basename(src_basec);
81 char *tmp_file_location;
84 opkg_message(conf,OPKG_NOTICE,"Downloading %s\n", src);
88 if (str_starts_with(src, "file:")) {
90 const char *file_src = src + 5;
91 opkg_message(conf,OPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
92 ret = file_copy(src + 5, dest_file_name);
93 opkg_message(conf,OPKG_INFO,"Done\n");
97 sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
98 err = unlink(tmp_file_location);
99 if (err && errno != ENOENT) {
100 opkg_message(conf,OPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
101 __FUNCTION__, tmp_file_location, strerror(errno));
102 free(tmp_file_location);
106 if (conf->http_proxy) {
107 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
108 setenv("http_proxy", conf->http_proxy, 1);
110 if (conf->ftp_proxy) {
111 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
112 setenv("ftp_proxy", conf->ftp_proxy, 1);
114 if (conf->no_proxy) {
115 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
116 setenv("no_proxy", conf->no_proxy, 1);
119 /* XXX: BUG rewrite to use execvp or else busybox's internal wget -Jamey 7/23/2002 */
121 sprintf_alloc(&cmd, "wget --passive-ftp %s %s%s %s%s %s -P %s %s",
122 (conf->http_proxy || conf->ftp_proxy) ? "--proxy=on" : "",
123 conf->proxy_user ? "--proxy-user=" : "",
124 conf->proxy_user ? conf->proxy_user : "",
125 conf->proxy_passwd ? "--proxy-passwd=" : "",
126 conf->proxy_passwd ? conf->proxy_passwd : "",
127 conf->verbose_wget ? "" : "-q",
133 opkg_message(conf,OPKG_ERROR, "%s: ERROR: Command failed with return value %d: `%s'\n",
134 __FUNCTION__, err, cmd);
136 unlink(tmp_file_location);
137 free(tmp_file_location);
146 FILE * file = fopen (tmp_file_location, "w");
148 curl = curl_easy_init ();
151 curl_easy_setopt (curl, CURLOPT_URL, src);
152 curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
153 curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0);
154 curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, src);
155 curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
156 if (conf->http_proxy || conf->ftp_proxy)
159 sprintf_alloc (&userpwd, "%s:%s", conf->proxy_user,
161 curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
164 res = curl_easy_perform (curl);
165 curl_easy_cleanup (curl);
174 err = file_move(tmp_file_location, dest_file_name);
176 free(tmp_file_location);
186 int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
191 if (pkg->src == NULL) {
192 opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
193 pkg->name, pkg->parent->name);
197 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
199 /* XXX: BUG: The pkg->filename might be something like
200 "../../foo.ipk". While this is correct, and exactly what we
201 want to use to construct url above, here we actually need to
202 use just the filename part, without any directory. */
203 sprintf_alloc(&pkg->local_filename, "%s/%s", dir, pkg->filename);
205 err = opkg_download(conf, url, pkg->local_filename);
212 * Downloads file from url, installs in package database, return package name.
214 int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **namep)
222 if (str_starts_with(url, "http://")
223 || str_starts_with(url, "ftp://")) {
225 char *file_basec = strdup(url);
226 char *file_base = basename(file_basec);
228 sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
229 err = opkg_download(conf, url, tmp_file);
233 err = pkg_init_from_file(pkg, tmp_file);
236 pkg->local_filename = strdup(tmp_file);
241 } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
242 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
244 err = pkg_init_from_file(pkg, url);
247 pkg->local_filename = strdup(url);
248 opkg_message(conf, OPKG_DEBUG2, "Package %s provided by hand \(%s\).\n", pkg->name,pkg->local_filename);
249 pkg->provided_by_hand = 1;
257 if (!pkg->architecture) {
258 opkg_message(conf, OPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
262 pkg->dest = conf->default_dest;
263 pkg->state_want = SW_INSTALL;
264 pkg->state_flag |= SF_PREFER;
265 pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);
267 fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
271 *namep = strdup(pkg->name);