1 /* ipkg_download.c - the itsy package management system
5 Copyright (C) 2001 University of Southern California
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
19 #include "ipkg_download.h"
20 #include "ipkg_message.h"
22 #include "sprintf_alloc.h"
24 #include "file_util.h"
27 int ipkg_download(ipkg_conf_t *conf, const char *src, const char *dest_file_name)
31 char *src_basec = strdup(src);
32 char *src_base = basename(src_basec);
33 char *tmp_file_location;
36 ipkg_message(conf,IPKG_NOTICE,"Downloading %s\n", src);
40 if (str_starts_with(src, "file:")) {
42 const char *file_src = src + 5;
43 ipkg_message(conf,IPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
44 ret = file_copy(src + 5, dest_file_name);
45 ipkg_message(conf,IPKG_INFO,"Done\n");
49 sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
50 err = unlink(tmp_file_location);
51 if (err && errno != ENOENT) {
52 ipkg_message(conf,IPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
53 __FUNCTION__, tmp_file_location, strerror(errno));
54 free(tmp_file_location);
58 if (conf->http_proxy) {
59 ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
60 setenv("http_proxy", conf->http_proxy, 1);
62 if (conf->ftp_proxy) {
63 ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
64 setenv("ftp_proxy", conf->ftp_proxy, 1);
67 ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
68 setenv("no_proxy", conf->no_proxy, 1);
71 /* XXX: BUG rewrite to use execvp or else busybox's internal wget -Jamey 7/23/2002 */
72 sprintf_alloc(&cmd, "wget --passive-ftp %s %s%s %s%s %s -P %s %s",
73 (conf->http_proxy || conf->ftp_proxy) ? "--proxy=on" : "",
74 conf->proxy_user ? "--proxy-user=" : "",
75 conf->proxy_user ? conf->proxy_user : "",
76 conf->proxy_passwd ? "--proxy-passwd=" : "",
77 conf->proxy_passwd ? conf->proxy_passwd : "",
78 conf->verbose_wget ? "" : "-q",
84 ipkg_message(conf,IPKG_ERROR, "%s: ERROR: Command failed with return value %d: `%s'\n",
85 __FUNCTION__, err, cmd);
87 unlink(tmp_file_location);
88 free(tmp_file_location);
95 err = file_move(tmp_file_location, dest_file_name);
97 free(tmp_file_location);
107 int ipkg_download_pkg(ipkg_conf_t *conf, pkg_t *pkg, const char *dir)
112 if (pkg->src == NULL) {
113 ipkg_message(conf,IPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
114 pkg->name, pkg->parent->name);
118 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
120 /* XXX: BUG: The pkg->filename might be something like
121 "../../foo.ipk". While this is correct, and exactly what we
122 want to use to construct url above, here we actually need to
123 use just the filename part, without any directory. */
124 sprintf_alloc(&pkg->local_filename, "%s/%s", dir, pkg->filename);
126 err = ipkg_download(conf, url, pkg->local_filename);
133 * Downloads file from url, installs in package database, return package name.
135 int ipkg_prepare_url_for_install(ipkg_conf_t *conf, const char *url, char **namep)
143 if (str_starts_with(url, "http://")
144 || str_starts_with(url, "ftp://")) {
146 char *file_basec = strdup(url);
147 char *file_base = basename(file_basec);
149 sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
150 err = ipkg_download(conf, url, tmp_file);
154 err = pkg_init_from_file(pkg, tmp_file);
157 pkg->local_filename = strdup(tmp_file);
162 } else if (strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
163 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
165 err = pkg_init_from_file(pkg, url);
168 pkg->local_filename = strdup(url);
169 ipkg_message(conf, IPKG_DEBUG2, "Package %s provided by hand \(%s\).\n", pkg->name,pkg->local_filename);
170 pkg->provided_by_hand = 1;
178 if (!pkg->architecture) {
179 ipkg_message(conf, IPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
183 pkg->dest = conf->default_dest;
184 pkg->state_want = SW_INSTALL;
185 pkg->state_flag |= SF_PREFER;
186 pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);
188 fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
192 *namep = strdup(pkg->name);