1 /* vi: set noexpandtab sw=4 sts=4: */
2 /* ipkg_download.c - the itsy package management system
6 Copyright (C) 2001 University of Southern California
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
19 #include <curl/curl.h>
22 #include "ipkg_download.h"
23 #include "ipkg_message.h"
25 #include "sprintf_alloc.h"
27 #include "file_util.h"
30 int ipkg_download(ipkg_conf_t *conf, const char *src, const char *dest_file_name)
34 char *src_basec = strdup(src);
35 char *src_base = basename(src_basec);
36 char *tmp_file_location;
39 ipkg_message(conf,IPKG_NOTICE,"Downloading %s\n", src);
43 if (str_starts_with(src, "file:")) {
45 const char *file_src = src + 5;
46 ipkg_message(conf,IPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
47 ret = file_copy(src + 5, dest_file_name);
48 ipkg_message(conf,IPKG_INFO,"Done\n");
52 sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
53 err = unlink(tmp_file_location);
54 if (err && errno != ENOENT) {
55 ipkg_message(conf,IPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
56 __FUNCTION__, tmp_file_location, strerror(errno));
57 free(tmp_file_location);
61 if (conf->http_proxy) {
62 ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
63 setenv("http_proxy", conf->http_proxy, 1);
65 if (conf->ftp_proxy) {
66 ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
67 setenv("ftp_proxy", conf->ftp_proxy, 1);
70 ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
71 setenv("no_proxy", conf->no_proxy, 1);
74 /* XXX: BUG rewrite to use execvp or else busybox's internal wget -Jamey 7/23/2002 */
76 sprintf_alloc(&cmd, "wget --passive-ftp %s %s%s %s%s %s -P %s %s",
77 (conf->http_proxy || conf->ftp_proxy) ? "--proxy=on" : "",
78 conf->proxy_user ? "--proxy-user=" : "",
79 conf->proxy_user ? conf->proxy_user : "",
80 conf->proxy_passwd ? "--proxy-passwd=" : "",
81 conf->proxy_passwd ? conf->proxy_passwd : "",
82 conf->verbose_wget ? "" : "-q",
88 ipkg_message(conf,IPKG_ERROR, "%s: ERROR: Command failed with return value %d: `%s'\n",
89 __FUNCTION__, err, cmd);
91 unlink(tmp_file_location);
92 free(tmp_file_location);
101 FILE * file = fopen (tmp_file_location, "w");
103 curl = curl_easy_init ();
106 curl_easy_setopt (curl, CURLOPT_URL, src);
107 curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
108 res = curl_easy_perform (curl);
109 curl_easy_cleanup (curl);
116 err = file_move(tmp_file_location, dest_file_name);
118 free(tmp_file_location);
128 int ipkg_download_pkg(ipkg_conf_t *conf, pkg_t *pkg, const char *dir)
133 if (pkg->src == NULL) {
134 ipkg_message(conf,IPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
135 pkg->name, pkg->parent->name);
139 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
141 /* XXX: BUG: The pkg->filename might be something like
142 "../../foo.ipk". While this is correct, and exactly what we
143 want to use to construct url above, here we actually need to
144 use just the filename part, without any directory. */
145 sprintf_alloc(&pkg->local_filename, "%s/%s", dir, pkg->filename);
147 err = ipkg_download(conf, url, pkg->local_filename);
154 * Downloads file from url, installs in package database, return package name.
156 int ipkg_prepare_url_for_install(ipkg_conf_t *conf, const char *url, char **namep)
164 if (str_starts_with(url, "http://")
165 || str_starts_with(url, "ftp://")) {
167 char *file_basec = strdup(url);
168 char *file_base = basename(file_basec);
170 sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
171 err = ipkg_download(conf, url, tmp_file);
175 err = pkg_init_from_file(pkg, tmp_file);
178 pkg->local_filename = strdup(tmp_file);
183 } else if (strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
184 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
186 err = pkg_init_from_file(pkg, url);
189 pkg->local_filename = strdup(url);
190 ipkg_message(conf, IPKG_DEBUG2, "Package %s provided by hand \(%s\).\n", pkg->name,pkg->local_filename);
191 pkg->provided_by_hand = 1;
199 if (!pkg->architecture) {
200 ipkg_message(conf, IPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
204 pkg->dest = conf->default_dest;
205 pkg->state_want = SW_INSTALL;
206 pkg->state_flag |= SF_PREFER;
207 pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);
209 fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
213 *namep = strdup(pkg->name);