Style cleanups in clean cache code
[pacman-ng.git] / lib / libalpm / dload.c
blobcb6d000c455089413527b13e0cd073198087e72b
1 /*
2 * download.c
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <signal.h>
32 /* the following two are needed for FreeBSD's libfetch */
33 #include <limits.h> /* PATH_MAX */
34 #if defined(HAVE_SYS_PARAM_H)
35 #include <sys/param.h> /* MAXHOSTNAMELEN */
36 #endif
38 #ifdef HAVE_LIBFETCH
39 #include <fetch.h>
40 #endif
42 /* libalpm */
43 #include "dload.h"
44 #include "alpm_list.h"
45 #include "alpm.h"
46 #include "log.h"
47 #include "util.h"
48 #include "handle.h"
50 static char *get_filename(const char *url) {
51 char *filename = strrchr(url, '/');
52 if(filename != NULL) {
53 filename++;
55 return(filename);
58 #ifdef HAVE_LIBFETCH
59 static char *get_destfile(const char *path, const char *filename) {
60 char *destfile;
61 /* len = localpath len + filename len + null */
62 size_t len = strlen(path) + strlen(filename) + 1;
63 CALLOC(destfile, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, NULL));
64 snprintf(destfile, len, "%s%s", path, filename);
66 return(destfile);
69 static char *get_tempfile(const char *path, const char *filename) {
70 char *tempfile;
71 /* len = localpath len + filename len + '.part' len + null */
72 size_t len = strlen(path) + strlen(filename) + 6;
73 CALLOC(tempfile, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, NULL));
74 snprintf(tempfile, len, "%s%s.part", path, filename);
76 return(tempfile);
79 static const char *gethost(struct url *fileurl)
81 const char *host = _("disk");
82 if(strcmp(SCHEME_FILE, fileurl->scheme) != 0) {
83 host = fileurl->host;
85 return(host);
88 int dload_interrupted;
89 static void inthandler(int signum)
91 dload_interrupted = 1;
94 #define check_stop() if(dload_interrupted) { ret = -1; goto cleanup; }
95 enum sighandlers { OLD = 0, NEW = 1 };
97 static int download_internal(const char *url, const char *localpath,
98 int force) {
99 FILE *localf = NULL;
100 struct stat st;
101 int ret = 0;
102 off_t dl_thisfile = 0;
103 ssize_t nread = 0;
104 char *tempfile, *destfile, *filename;
105 struct sigaction sig_pipe[2], sig_int[2];
107 off_t local_size = 0;
108 time_t local_time = 0;
110 struct url *fileurl;
111 struct url_stat ust;
112 fetchIO *dlf = NULL;
114 char buffer[PM_DLBUF_LEN];
116 filename = get_filename(url);
117 if(!filename) {
118 _alpm_log(PM_LOG_ERROR, _("url '%s' is invalid\n"), url);
119 RET_ERR(PM_ERR_SERVER_BAD_URL, -1);
122 fileurl = fetchParseURL(url);
123 if(!fileurl) {
124 _alpm_log(PM_LOG_ERROR, _("url '%s' is invalid\n"), url);
125 RET_ERR(PM_ERR_LIBFETCH, -1);
128 destfile = get_destfile(localpath, filename);
129 tempfile = get_tempfile(localpath, filename);
131 if(stat(tempfile, &st) == 0 && st.st_size > 0) {
132 _alpm_log(PM_LOG_DEBUG, "tempfile found, attempting continuation\n");
133 local_time = fileurl->last_modified = st.st_mtime;
134 local_size = fileurl->offset = (off_t)st.st_size;
135 dl_thisfile = st.st_size;
136 localf = fopen(tempfile, "ab");
137 } else if(!force && stat(destfile, &st) == 0 && st.st_size > 0) {
138 _alpm_log(PM_LOG_DEBUG, "destfile found, using mtime only\n");
139 local_time = fileurl->last_modified = st.st_mtime;
140 local_size = /* no fu->off here */ (off_t)st.st_size;
141 } else {
142 _alpm_log(PM_LOG_DEBUG, "no file found matching criteria, starting from scratch\n");
145 /* pass the raw filename for passing to the callback function */
146 _alpm_log(PM_LOG_DEBUG, "using '%s' for download progress\n", filename);
148 /* print proxy info for debug purposes */
149 _alpm_log(PM_LOG_DEBUG, "HTTP_PROXY: %s\n", getenv("HTTP_PROXY"));
150 _alpm_log(PM_LOG_DEBUG, "http_proxy: %s\n", getenv("http_proxy"));
151 _alpm_log(PM_LOG_DEBUG, "FTP_PROXY: %s\n", getenv("FTP_PROXY"));
152 _alpm_log(PM_LOG_DEBUG, "ftp_proxy: %s\n", getenv("ftp_proxy"));
154 /* 10s timeout */
155 fetchTimeout = 10;
157 /* ignore any SIGPIPE signals- these may occur if our FTP socket dies or
158 * something along those lines. Store the old signal handler first. */
159 sig_pipe[NEW].sa_handler = SIG_IGN;
160 sigemptyset(&sig_pipe[NEW].sa_mask);
161 sig_pipe[NEW].sa_flags = 0;
162 sigaction(SIGPIPE, NULL, &sig_pipe[OLD]);
163 sigaction(SIGPIPE, &sig_pipe[NEW], NULL);
165 dload_interrupted = 0;
166 sig_int[NEW].sa_handler = &inthandler;
167 sigemptyset(&sig_int[NEW].sa_mask);
168 sig_int[NEW].sa_flags = 0;
169 sigaction(SIGINT, NULL, &sig_int[OLD]);
170 sigaction(SIGINT, &sig_int[NEW], NULL);
172 /* NOTE: libfetch does not reset the error code, be sure to do it before
173 * calls into the library */
175 /* find out the remote size *and* mtime in one go. there is a lot of
176 * trouble in trying to do both size and "if-modified-since" logic in a
177 * non-stat request, so avoid it. */
178 fetchLastErrCode = 0;
179 if(fetchStat(fileurl, &ust, "") == -1) {
180 pm_errno = PM_ERR_LIBFETCH;
181 _alpm_log(PM_LOG_ERROR, _("failed retrieving file '%s' from %s : %s\n"),
182 filename, gethost(fileurl), fetchLastErrString);
183 ret = -1;
184 goto cleanup;
186 check_stop();
188 _alpm_log(PM_LOG_DEBUG, "ust.mtime: %ld local_time: %ld compare: %ld\n",
189 ust.mtime, local_time, local_time - ust.mtime);
190 _alpm_log(PM_LOG_DEBUG, "ust.size: %jd local_size: %jd compare: %jd\n",
191 (intmax_t)ust.size, (intmax_t)local_size, (intmax_t)(local_size - ust.size));
192 if(!force && ust.mtime && ust.mtime == local_time
193 && ust.size && ust.size == local_size) {
194 /* the remote time and size values agreed with what we have, so move on
195 * because there is nothing more to do. */
196 _alpm_log(PM_LOG_DEBUG, "files are identical, skipping %s\n", filename);
197 ret = 1;
198 goto cleanup;
200 if(!ust.mtime || ust.mtime != local_time) {
201 _alpm_log(PM_LOG_DEBUG, "mtimes were different or unavailable, downloading %s from beginning\n", filename);
202 fileurl->offset = 0;
205 fetchLastErrCode = 0;
206 dlf = fetchGet(fileurl, "");
207 check_stop();
209 if(fetchLastErrCode != 0 || dlf == NULL) {
210 pm_errno = PM_ERR_LIBFETCH;
211 _alpm_log(PM_LOG_ERROR, _("failed retrieving file '%s' from %s : %s\n"),
212 filename, gethost(fileurl), fetchLastErrString);
213 ret = -1;
214 goto cleanup;
215 } else {
216 _alpm_log(PM_LOG_DEBUG, "connected to %s successfully\n", fileurl->host);
219 if(localf && fileurl->offset == 0) {
220 _alpm_log(PM_LOG_WARNING, _("resuming download of %s not possible; starting over\n"), filename);
221 fclose(localf);
222 localf = NULL;
223 } else if(fileurl->offset) {
224 _alpm_log(PM_LOG_DEBUG, "resuming download at position %jd\n", (intmax_t)fileurl->offset);
228 if(localf == NULL) {
229 _alpm_rmrf(tempfile);
230 fileurl->offset = (off_t)0;
231 dl_thisfile = 0;
232 localf = fopen(tempfile, "wb");
233 if(localf == NULL) { /* still null? */
234 pm_errno = PM_ERR_RETRIEVE;
235 _alpm_log(PM_LOG_ERROR, _("error writing to file '%s': %s\n"),
236 tempfile, strerror(errno));
237 ret = -1;
238 goto cleanup;
242 /* Progress 0 - initialize */
243 if(handle->dlcb) {
244 handle->dlcb(filename, 0, ust.size);
247 while((nread = fetchIO_read(dlf, buffer, PM_DLBUF_LEN)) > 0) {
248 check_stop();
249 size_t nwritten = 0;
250 nwritten = fwrite(buffer, 1, (size_t)nread, localf);
251 if((nwritten != (size_t)nread) || ferror(localf)) {
252 pm_errno = PM_ERR_RETRIEVE;
253 _alpm_log(PM_LOG_ERROR, _("error writing to file '%s': %s\n"),
254 tempfile, strerror(errno));
255 ret = -1;
256 goto cleanup;
258 dl_thisfile += nread;
260 if(handle->dlcb) {
261 handle->dlcb(filename, dl_thisfile, ust.size);
265 /* did the transfer complete normally? */
266 if (nread == -1) {
267 /* not PM_ERR_LIBFETCH here because libfetch error string might be empty */
268 pm_errno = PM_ERR_RETRIEVE;
269 _alpm_log(PM_LOG_ERROR, _("failed retrieving file '%s' from %s\n"),
270 filename, gethost(fileurl));
271 ret = -1;
272 goto cleanup;
275 if (ust.size != -1 && dl_thisfile < ust.size) {
276 pm_errno = PM_ERR_RETRIEVE;
277 _alpm_log(PM_LOG_ERROR, _("%s appears to be truncated: %jd/%jd bytes\n"),
278 filename, (intmax_t)dl_thisfile, (intmax_t)ust.size);
279 ret = -1;
280 goto cleanup;
283 /* probably safer to close the file descriptors now before renaming the file,
284 * for example to make sure the buffers are flushed.
286 fclose(localf);
287 localf = NULL;
288 fetchIO_close(dlf);
289 dlf = NULL;
291 /* set the times on the file to the same as that of the remote file */
292 if(ust.mtime) {
293 struct timeval tv[2];
294 memset(&tv, 0, sizeof(tv));
295 tv[0].tv_sec = ust.atime;
296 tv[1].tv_sec = ust.mtime;
297 utimes(tempfile, tv);
299 rename(tempfile, destfile);
300 ret = 0;
302 cleanup:
303 FREE(tempfile);
304 FREE(destfile);
305 if(localf != NULL) {
306 /* if we still had a local file open, we got interrupted. set the mtimes on
307 * the file accordingly. */
308 fflush(localf);
309 if(ust.mtime) {
310 struct timeval tv[2];
311 memset(&tv, 0, sizeof(tv));
312 tv[0].tv_sec = ust.atime;
313 tv[1].tv_sec = ust.mtime;
314 futimes(fileno(localf), tv);
316 fclose(localf);
318 if(dlf != NULL) {
319 fetchIO_close(dlf);
321 fetchFreeURL(fileurl);
323 /* restore the old signal handlers */
324 sigaction(SIGINT, &sig_int[OLD], NULL);
325 sigaction(SIGPIPE, &sig_pipe[OLD], NULL);
326 /* if we were interrupted, trip the old handler */
327 if(dload_interrupted) {
328 raise(SIGINT);
331 return(ret);
333 #endif
335 static int download(const char *url, const char *localpath,
336 int force) {
337 if(handle->fetchcb == NULL) {
338 #ifdef HAVE_LIBFETCH
339 return(download_internal(url, localpath, force));
340 #else
341 RET_ERR(PM_ERR_EXTERNAL_DOWNLOAD, -1);
342 #endif
343 } else {
344 int ret = handle->fetchcb(url, localpath, force);
345 if(ret == -1) {
346 RET_ERR(PM_ERR_EXTERNAL_DOWNLOAD, -1);
348 return(ret);
353 * Download a single file
354 * - servers must be a list of urls WITHOUT trailing slashes.
356 * RETURN: 0 for successful download
357 * 1 if the files are identical
358 * -1 on error
360 int _alpm_download_single_file(const char *filename,
361 alpm_list_t *servers, const char *localpath,
362 int force)
364 alpm_list_t *i;
365 int ret = -1;
367 ASSERT(servers != NULL, RET_ERR(PM_ERR_SERVER_NONE, -1));
369 for(i = servers; i; i = i->next) {
370 const char *server = i->data;
371 char *fileurl = NULL;
372 size_t len;
374 /* print server + filename into a buffer */
375 len = strlen(server) + strlen(filename) + 2;
376 CALLOC(fileurl, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, -1));
377 snprintf(fileurl, len, "%s/%s", server, filename);
379 ret = download(fileurl, localpath, force);
380 FREE(fileurl);
381 if(ret != -1) {
382 break;
386 return(ret);
389 int _alpm_download_files(alpm_list_t *files,
390 alpm_list_t *servers, const char *localpath)
392 int ret = 0;
393 alpm_list_t *lp;
395 for(lp = files; lp; lp = lp->next) {
396 char *filename = lp->data;
397 if(_alpm_download_single_file(filename, servers,
398 localpath, 0) == -1) {
399 ret++;
403 return(ret);
406 /** Fetch a remote pkg.
407 * @param url URL of the package to download
408 * @return the downloaded filepath on success, NULL on error
409 * @addtogroup alpm_misc
411 char SYMEXPORT *alpm_fetch_pkgurl(const char *url)
413 char *filename, *filepath;
414 const char *cachedir;
415 int ret;
417 ALPM_LOG_FUNC;
419 filename = get_filename(url);
421 /* find a valid cache dir to download to */
422 cachedir = _alpm_filecache_setup();
424 /* download the file */
425 ret = download(url, cachedir, 0);
426 if(ret == -1) {
427 _alpm_log(PM_LOG_WARNING, _("failed to download %s\n"), url);
428 return(NULL);
430 _alpm_log(PM_LOG_DEBUG, "successfully downloaded %s\n", url);
432 /* we should be able to find the file the second time around */
433 filepath = _alpm_filecache_find(filename);
434 return(filepath);
437 /* vim: set ts=2 sw=2 noet: */