ipkg: remove auto-generated files
[oweals/opkg-lede.git] / ipkg_download.c
1 /* ipkg_download.c - the itsy package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
6
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.
11
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.
16 */
17
18 #include "ipkg.h"
19 #include "ipkg_download.h"
20 #include "ipkg_message.h"
21
22 #include "sprintf_alloc.h"
23 #include "xsystem.h"
24 #include "file_util.h"
25 #include "str_util.h"
26
27 int ipkg_download(ipkg_conf_t *conf, const char *src, const char *dest_file_name)
28 {
29     int err = 0;
30
31     char *src_basec = strdup(src);
32     char *src_base = basename(src_basec);
33     char *tmp_file_location;
34     char *cmd;
35
36     ipkg_message(conf,IPKG_NOTICE,"Downloading %s\n", src);
37         
38     fflush(stdout);
39     
40     if (str_starts_with(src, "file:")) {
41         int ret;
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");
46         return ret;
47     }
48
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);
55         return errno;
56     }
57
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);
61     }
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);
65     }
66     if (conf->no_proxy) {
67         ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
68         setenv("no_proxy", conf->no_proxy, 1);
69     }
70
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",
79                   conf->tmp_dir,
80                   src);
81     err = xsystem(cmd);
82     if (err) {
83         if (err != -1) {
84             ipkg_message(conf,IPKG_ERROR, "%s: ERROR: Command failed with return value %d: `%s'\n",
85                     __FUNCTION__, err, cmd);
86         } 
87         unlink(tmp_file_location);
88         free(tmp_file_location);
89         free(src_basec);
90         free(cmd);
91         return EINVAL;
92     }
93     free(cmd);
94
95     err = file_move(tmp_file_location, dest_file_name);
96
97     free(tmp_file_location);
98     free(src_basec);
99
100     if (err) {
101         return err;
102     }
103
104     return 0;
105 }
106
107 int ipkg_download_pkg(ipkg_conf_t *conf, pkg_t *pkg, const char *dir)
108 {
109     int err;
110     char *url;
111
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);
115         return -1;
116     }
117
118     sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
119
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);
125
126     err = ipkg_download(conf, url, pkg->local_filename);
127     free(url);
128
129     return err;
130 }
131
132 /*
133  * Downloads file from url, installs in package database, return package name. 
134  */
135 int ipkg_prepare_url_for_install(ipkg_conf_t *conf, const char *url, char **namep)
136 {
137      int err = 0;
138      pkg_t *pkg;
139      pkg = pkg_new();
140      if (pkg == NULL)
141           return ENOMEM;
142
143      if (str_starts_with(url, "http://")
144          || str_starts_with(url, "ftp://")) {
145           char *tmp_file;
146           char *file_basec = strdup(url);
147           char *file_base = basename(file_basec);
148
149           sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
150           err = ipkg_download(conf, url, tmp_file);
151           if (err)
152                return err;
153
154           err = pkg_init_from_file(pkg, tmp_file);
155           if (err)
156                return err;
157           pkg->local_filename = strdup(tmp_file);
158
159           free(tmp_file);
160           free(file_basec);
161
162      } else if (strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
163                 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
164
165           err = pkg_init_from_file(pkg, url);
166           if (err)
167                return err;
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;
171
172      } else {
173        pkg_deinit(pkg);
174        free(pkg);
175        return 0;
176      }
177
178      if (!pkg->architecture) {
179           ipkg_message(conf, IPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
180           return -EINVAL;
181      }
182
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);  
187      if ( pkg == NULL ){
188         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
189         return 0;
190      }
191      if (namep) {
192           *namep = strdup(pkg->name);
193      }
194      return 0;
195 }