192c8b3271c5fe120f5405e9665a44dd52dd321b
[oweals/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
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.
12
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.
17 */
18
19 #include <curl/curl.h>
20
21 #include "ipkg.h"
22 #include "ipkg_download.h"
23 #include "ipkg_message.h"
24
25 #include "sprintf_alloc.h"
26 #include "xsystem.h"
27 #include "file_util.h"
28 #include "str_util.h"
29
30 int ipkg_download(ipkg_conf_t *conf, const char *src, const char *dest_file_name)
31 {
32     int err = 0;
33
34     char *src_basec = strdup(src);
35     char *src_base = basename(src_basec);
36     char *tmp_file_location;
37     char *cmd;
38
39     ipkg_message(conf,IPKG_NOTICE,"Downloading %s\n", src);
40         
41     fflush(stdout);
42     
43     if (str_starts_with(src, "file:")) {
44         int ret;
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");
49         return ret;
50     }
51
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);
58         return errno;
59     }
60
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);
64     }
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);
68     }
69     if (conf->no_proxy) {
70         ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
71         setenv("no_proxy", conf->no_proxy, 1);
72     }
73
74     /* XXX: BUG rewrite to use execvp or else busybox's internal wget -Jamey 7/23/2002 */ 
75 #if 0
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",
83                   conf->tmp_dir,
84                   src);
85     err = xsystem(cmd);
86     if (err) {
87         if (err != -1) {
88             ipkg_message(conf,IPKG_ERROR, "%s: ERROR: Command failed with return value %d: `%s'\n",
89                     __FUNCTION__, err, cmd);
90         } 
91         unlink(tmp_file_location);
92         free(tmp_file_location);
93         free(src_basec);
94         free(cmd);
95         return EINVAL;
96     }
97     free(cmd);
98 #endif
99     CURL *curl;
100     CURLcode res;
101     FILE * file = fopen (tmp_file_location, "w");
102     
103     curl = curl_easy_init ();
104     if (curl)
105     {
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);
110         fclose (file);
111
112     }
113     else
114         return -1;
115
116     err = file_move(tmp_file_location, dest_file_name);
117
118     free(tmp_file_location);
119     free(src_basec);
120
121     if (err) {
122         return err;
123     }
124
125     return 0;
126 }
127
128 int ipkg_download_pkg(ipkg_conf_t *conf, pkg_t *pkg, const char *dir)
129 {
130     int err;
131     char *url;
132
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);
136         return -1;
137     }
138
139     sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
140
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);
146
147     err = ipkg_download(conf, url, pkg->local_filename);
148     free(url);
149
150     return err;
151 }
152
153 /*
154  * Downloads file from url, installs in package database, return package name. 
155  */
156 int ipkg_prepare_url_for_install(ipkg_conf_t *conf, const char *url, char **namep)
157 {
158      int err = 0;
159      pkg_t *pkg;
160      pkg = pkg_new();
161      if (pkg == NULL)
162           return ENOMEM;
163
164      if (str_starts_with(url, "http://")
165          || str_starts_with(url, "ftp://")) {
166           char *tmp_file;
167           char *file_basec = strdup(url);
168           char *file_base = basename(file_basec);
169
170           sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
171           err = ipkg_download(conf, url, tmp_file);
172           if (err)
173                return err;
174
175           err = pkg_init_from_file(pkg, tmp_file);
176           if (err)
177                return err;
178           pkg->local_filename = strdup(tmp_file);
179
180           free(tmp_file);
181           free(file_basec);
182
183      } else if (strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
184                 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
185
186           err = pkg_init_from_file(pkg, url);
187           if (err)
188                return err;
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;
192
193      } else {
194        pkg_deinit(pkg);
195        free(pkg);
196        return 0;
197      }
198
199      if (!pkg->architecture) {
200           ipkg_message(conf, IPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
201           return -EINVAL;
202      }
203
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);  
208      if ( pkg == NULL ){
209         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
210         return 0;
211      }
212      if (namep) {
213           *namep = strdup(pkg->name);
214      }
215      return 0;
216 }