From 4e60b9646d69227c6c9f226bf03a12158feaaee7 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Sat, 14 Jan 2012 15:27:48 -0500 Subject: fetch_url: look for files in cache before downloading We lost this logic somewhere between the libfetch and libcurl transition, as it existed in the internal downloader, but was pulled back only into the sync workflow. Add a helper function that will let us check for existance in the filecache prior to calling the downloader. Signed-off-by: Dave Reisner Signed-off-by: Dan McGee --- lib/libalpm/dload.c | 92 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 32 deletions(-) (limited to 'lib/libalpm/dload.c') diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c index 29285903..928324e8 100644 --- a/lib/libalpm/dload.c +++ b/lib/libalpm/dload.c @@ -562,6 +562,22 @@ int _alpm_download(struct dload_payload *payload, const char *localpath, } } +static char *filecache_find_url(alpm_handle_t *handle, const char *url) +{ + const char *basename = strrchr(url, '/'); + + if(basename == NULL) { + return NULL; + } + + basename++; + if(basename == '\0') { + return NULL; + } + + return _alpm_filecache_find(handle, basename); +} + /** Fetch a remote pkg. */ char SYMEXPORT *alpm_fetch_pkgurl(alpm_handle_t *handle, const char *url) { @@ -569,7 +585,7 @@ char SYMEXPORT *alpm_fetch_pkgurl(alpm_handle_t *handle, const char *url) const char *cachedir; char *final_file = NULL; struct dload_payload payload; - int ret; + int ret = 0; CHECK_HANDLE(handle, return NULL); ASSERT(url, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL)); @@ -578,51 +594,63 @@ char SYMEXPORT *alpm_fetch_pkgurl(alpm_handle_t *handle, const char *url) cachedir = _alpm_filecache_setup(handle); memset(&payload, 0, sizeof(struct dload_payload)); - payload.handle = handle; - STRDUP(payload.fileurl, url, RET_ERR(handle, ALPM_ERR_MEMORY, NULL)); - payload.allow_resume = 1; - - /* download the file */ - ret = _alpm_download(&payload, cachedir, &final_file); - _alpm_dload_payload_reset(&payload); - if(ret == -1) { - _alpm_log(handle, ALPM_LOG_WARNING, _("failed to download %s\n"), url); - free(final_file); - return NULL; + + /* attempt to find the file in our pkgcache */ + filepath = filecache_find_url(handle, url); + if(filepath == NULL) { + STRDUP(payload.fileurl, url, RET_ERR(handle, ALPM_ERR_MEMORY, NULL)); + payload.allow_resume = 1; + payload.handle = handle; + + /* download the file */ + ret = _alpm_download(&payload, cachedir, &final_file); + _alpm_dload_payload_reset(&payload); + if(ret == -1) { + _alpm_log(handle, ALPM_LOG_WARNING, _("failed to download %s\n"), url); + free(final_file); + return NULL; + } + _alpm_log(handle, ALPM_LOG_DEBUG, "successfully downloaded %s\n", url); } - _alpm_log(handle, ALPM_LOG_DEBUG, "successfully downloaded %s\n", url); /* attempt to download the signature */ if(ret == 0 && (handle->siglevel & ALPM_SIG_PACKAGE)) { - char *sig_final_file = NULL; + char *sig_filepath, *sig_final_file = NULL; size_t len; len = strlen(url) + 5; MALLOC(payload.fileurl, len, RET_ERR(handle, ALPM_ERR_MEMORY, NULL)); snprintf(payload.fileurl, len, "%s.sig", url); - payload.handle = handle; - payload.force = 1; - payload.errors_ok = (handle->siglevel & ALPM_SIG_PACKAGE_OPTIONAL); - - /* set hard upper limit of 16KiB */ - payload.max_size = 16 * 1024; - - ret = _alpm_download(&payload, cachedir, &sig_final_file); - if(ret == -1 && !payload.errors_ok) { - _alpm_log(handle, ALPM_LOG_WARNING, - _("failed to download %s\n"), payload.fileurl); - /* Warn now, but don't return NULL. We will fail later during package - * load time. */ - } else if(ret == 0) { - _alpm_log(handle, ALPM_LOG_DEBUG, - "successfully downloaded %s\n", payload.fileurl); + + sig_filepath = filecache_find_url(handle, payload.fileurl); + if(sig_filepath == NULL) { + payload.handle = handle; + payload.force = 1; + payload.errors_ok = (handle->siglevel & ALPM_SIG_PACKAGE_OPTIONAL); + + /* set hard upper limit of 16KiB */ + payload.max_size = 16 * 1024; + + ret = _alpm_download(&payload, cachedir, &sig_final_file); + if(ret == -1 && !payload.errors_ok) { + _alpm_log(handle, ALPM_LOG_WARNING, + _("failed to download %s\n"), payload.fileurl); + /* Warn now, but don't return NULL. We will fail later during package + * load time. */ + } else if(ret == 0) { + _alpm_log(handle, ALPM_LOG_DEBUG, + "successfully downloaded %s\n", payload.fileurl); + } + FREE(sig_final_file); } - FREE(sig_final_file); + free(sig_filepath); _alpm_dload_payload_reset(&payload); } /* we should be able to find the file the second time around */ - filepath = _alpm_filecache_find(handle, final_file); + if(filepath == NULL) { + filepath = _alpm_filecache_find(handle, final_file); + } free(final_file); return filepath; -- cgit v1.2.3-54-g00ecf From 562109c0e8717eaac3b9078271c4ca4f82abfecd Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Wed, 18 Jan 2012 22:25:27 -0600 Subject: Update copyright on changed files since beginning of year Signed-off-by: Dan McGee --- lib/libalpm/dload.c | 2 +- lib/libalpm/remove.c | 2 +- lib/libalpm/signing.c | 2 +- lib/libalpm/util.c | 2 +- scripts/makepkg.sh.in | 4 ++-- scripts/pacman-key.sh.in | 4 ++-- scripts/repo-add.sh.in | 4 ++-- src/pacman/pacman.c | 2 +- src/pacman/sync.c | 2 +- src/pacman/util.c | 2 +- src/pacman/util.h | 2 +- 11 files changed, 14 insertions(+), 14 deletions(-) (limited to 'lib/libalpm/dload.c') diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c index 928324e8..6fe4a323 100644 --- a/lib/libalpm/dload.c +++ b/lib/libalpm/dload.c @@ -1,7 +1,7 @@ /* * download.c * - * Copyright (c) 2006-2011 Pacman Development Team + * Copyright (c) 2006-2012 Pacman Development Team * Copyright (c) 2002-2006 by Judd Vinet * * This program is free software; you can redistribute it and/or modify diff --git a/lib/libalpm/remove.c b/lib/libalpm/remove.c index 41cee514..cc9e289d 100644 --- a/lib/libalpm/remove.c +++ b/lib/libalpm/remove.c @@ -1,7 +1,7 @@ /* * remove.c * - * Copyright (c) 2006-2011 Pacman Development Team + * Copyright (c) 2006-2012 Pacman Development Team * Copyright (c) 2002-2006 by Judd Vinet * Copyright (c) 2005 by Aurelien Foret * Copyright (c) 2005 by Christian Hamar diff --git a/lib/libalpm/signing.c b/lib/libalpm/signing.c index 1a53deaa..3ec957de 100644 --- a/lib/libalpm/signing.c +++ b/lib/libalpm/signing.c @@ -1,7 +1,7 @@ /* * signing.c * - * Copyright (c) 2008-2011 Pacman Development Team + * Copyright (c) 2008-2012 Pacman Development Team * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 6834751f..0e5e8a00 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -1,7 +1,7 @@ /* * util.c * - * Copyright (c) 2006-2011 Pacman Development Team + * Copyright (c) 2006-2012 Pacman Development Team * Copyright (c) 2002-2006 by Judd Vinet * Copyright (c) 2005 by Aurelien Foret * Copyright (c) 2005 by Christian Hamar diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 797b8d78..169162ce 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -3,7 +3,7 @@ # makepkg - make packages compatible for use with pacman # @configure_input@ # -# Copyright (c) 2006-2011 Pacman Development Team +# Copyright (c) 2006-2012 Pacman Development Team # Copyright (c) 2002-2006 by Judd Vinet # Copyright (c) 2005 by Aurelien Foret # Copyright (c) 2006 by Miklos Vajna @@ -1866,7 +1866,7 @@ usage() { version() { printf "makepkg (pacman) %s\n" "$myver" printf "$(gettext "\ -Copyright (c) 2006-2011 Pacman Development Team .\n\ +Copyright (c) 2006-2012 Pacman Development Team .\n\ Copyright (C) 2002-2006 Judd Vinet .\n\n\ This is free software; see the source for copying conditions.\n\ There is NO WARRANTY, to the extent permitted by law.\n")" diff --git a/scripts/pacman-key.sh.in b/scripts/pacman-key.sh.in index b7c77d82..2159fdf1 100644 --- a/scripts/pacman-key.sh.in +++ b/scripts/pacman-key.sh.in @@ -4,7 +4,7 @@ # Based on apt-key, from Debian # @configure_input@ # -# Copyright (c) 2010-2011 Pacman Development Team +# Copyright (c) 2010-2012 Pacman Development Team # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -88,7 +88,7 @@ usage() { version() { printf "pacman-key (pacman) %s\n" "${myver}" printf "$(gettext "\ -Copyright (c) 2010-2011 Pacman Development Team .\n\ +Copyright (c) 2010-2012 Pacman Development Team .\n\ This is free software; see the source for copying conditions.\n\ There is NO WARRANTY, to the extent permitted by law.\n")" } diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in index 6a10a684..8c1d53da 100644 --- a/scripts/repo-add.sh.in +++ b/scripts/repo-add.sh.in @@ -4,7 +4,7 @@ # repo-remove - remove a package entry from a given repo database file # @configure_input@ # -# Copyright (c) 2006-2011 Pacman Development Team +# Copyright (c) 2006-2012 Pacman Development Team # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -87,7 +87,7 @@ version() { cmd=${0##*/} printf "%s (pacman) %s\n\n" "$cmd" "$myver" printf "$(gettext "\ -Copyright (c) 2006-2011 Pacman Development Team \n\n\ +Copyright (c) 2006-2012 Pacman Development Team \n\n\ This is free software; see the source for copying conditions.\n\ There is NO WARRANTY, to the extent permitted by law.\n")" } diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index bce73d21..4c0d69ed 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -223,7 +223,7 @@ static void version(void) { printf("\n"); printf(" .--. Pacman v%s - libalpm v%s\n", PACKAGE_VERSION, alpm_version()); - printf("/ _.-' .-. .-. .-. Copyright (C) 2006-2011 Pacman Development Team\n"); + printf("/ _.-' .-. .-. .-. Copyright (C) 2006-2012 Pacman Development Team\n"); printf("\\ '-. '-' '-' '-' Copyright (C) 2002-2006 Judd Vinet\n"); printf(" '--'\n"); printf(_(" This program may be freely redistributed under\n" diff --git a/src/pacman/sync.c b/src/pacman/sync.c index 2c64f090..4f10e6b9 100644 --- a/src/pacman/sync.c +++ b/src/pacman/sync.c @@ -1,7 +1,7 @@ /* * sync.c * - * Copyright (c) 2006-2011 Pacman Development Team + * Copyright (c) 2006-2012 Pacman Development Team * Copyright (c) 2002-2006 by Judd Vinet * * This program is free software; you can redistribute it and/or modify diff --git a/src/pacman/util.c b/src/pacman/util.c index a8f19682..facdc1c9 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -1,7 +1,7 @@ /* * util.c * - * Copyright (c) 2006-2011 Pacman Development Team + * Copyright (c) 2006-2012 Pacman Development Team * Copyright (c) 2002-2006 by Judd Vinet * * This program is free software; you can redistribute it and/or modify diff --git a/src/pacman/util.h b/src/pacman/util.h index 49cf1dbd..1b5a3daf 100644 --- a/src/pacman/util.h +++ b/src/pacman/util.h @@ -1,7 +1,7 @@ /* * util.h * - * Copyright (c) 2006-2011 Pacman Development Team + * Copyright (c) 2006-2012 Pacman Development Team * Copyright (c) 2002-2006 by Judd Vinet * * This program is free software; you can redistribute it and/or modify -- cgit v1.2.3-54-g00ecf From 44f146f232be5203fb01ad35fdf73122838df97c Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Sat, 21 Jan 2012 21:31:39 -0500 Subject: lib/dload: enforce usage of TCP keepalives This is particularly important in the case of FTP control connections, which may be closed by rogue NAT/firewall devices detecting idle connections on larger transfers which may take 5-10+ minutes. Signed-off-by: Dave Reisner Signed-off-by: Dan McGee --- configure.ac | 3 ++- lib/libalpm/dload.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) (limited to 'lib/libalpm/dload.c') diff --git a/configure.ac b/configure.ac index a8b142ea..0bf11e99 100644 --- a/configure.ac +++ b/configure.ac @@ -173,7 +173,8 @@ AM_CONDITIONAL([HAVE_LIBGPGME], [test "x$with_gpgme" = "xyes"]) # Checks for header files. AC_CHECK_HEADERS([fcntl.h float.h glob.h libintl.h limits.h locale.h \ - mntent.h stddef.h string.h sys/ioctl.h \ + mntent.h netinet/in.h netinet/tcp.h \ + stddef.h string.h sys/ioctl.h \ sys/mnttab.h sys/mount.h \ sys/param.h sys/statvfs.h sys/time.h sys/types.h \ sys/ucred.h syslog.h termios.h wchar.h]) diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c index 6fe4a323..f5af7c64 100644 --- a/lib/libalpm/dload.c +++ b/lib/libalpm/dload.c @@ -25,11 +25,19 @@ #include #include #include +#include /* setsockopt, SO_KEEPALIVE */ #include #include #include #include +#ifdef HAVE_NETINET_IN_H +#include /* IPPROTO_TCP */ +#endif +#ifdef HAVE_NETINET_TCP_H +#include /* TCP_KEEPINTVL, TCP_KEEPIDLE */ +#endif + #ifdef HAVE_LIBCURL #include #endif @@ -217,6 +225,47 @@ static size_t parse_headers(void *ptr, size_t size, size_t nmemb, void *user) return realsize; } +static int dload_sockopt_cb(void *userdata, curl_socket_t curlfd, + curlsocktype purpose) +{ + alpm_handle_t *handle = userdata; + int optval = 1; + + /* this whole method is to prevent FTP control connections from going sour + * during a long data transfer; crappy firewalls love to drop otherwise idle + * connections if there is no traffic. */ + if(purpose != CURLSOCKTYPE_IPCXN) { + return 0; + } + + /* don't abort operation if any setsockopt fails, just log to debug */ + if(setsockopt(curlfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&optval, + sizeof(optval)) < 0) { + _alpm_log(handle, ALPM_LOG_DEBUG, + "Failed to set SO_KEEPALIVE on fd %d\n", curlfd); + } + else { +#ifdef TCP_KEEPIDLE + optval = 60; + if(setsockopt(curlfd, IPPROTO_TCP, TCP_KEEPIDLE, (void *)&optval, + sizeof(optval)) < 0) { + _alpm_log(handle, ALPM_LOG_DEBUG, + "Failed to set TCP_KEEPIDLE on fd %d\n", curlfd); + } +#endif +#ifdef TCP_KEEPINTVL + optval = 60; + if(setsockopt(curlfd, IPPROTO_TCP, TCP_KEEPINTVL, (void *)&optval, + sizeof(optval)) < 0) { + _alpm_log(handle, ALPM_LOG_DEBUG, + "Failed to set TCP_KEEPINTVL on fd %d\n", curlfd); + } +#endif + } + + return 0; +} + static void curl_set_handle_opts(struct dload_payload *payload, CURL *curl, char *error_buffer) { @@ -241,6 +290,8 @@ static void curl_set_handle_opts(struct dload_payload *payload, curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, parse_headers); curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void *)payload); curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); + curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, dload_sockopt_cb); + curl_easy_setopt(curl, CURLOPT_SOCKOPTDATA, (void *)handle); _alpm_log(handle, ALPM_LOG_DEBUG, "url: %s\n", payload->fileurl); @@ -392,6 +443,8 @@ static int curl_download_internal(struct dload_payload *payload, /* perform transfer */ payload->curlerr = curl_easy_perform(curl); + _alpm_log(handle, ALPM_LOG_DEBUG, "curl returned error %d from transfer\n", + payload->curlerr); /* disconnect relationships from the curl handle for things that might go out * of scope, but could still be touched on connection teardown. This really -- cgit v1.2.3-54-g00ecf From 825b4ff35aa676b139dc24bc651724b092f2fded Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Sun, 22 Jan 2012 18:46:23 -0500 Subject: lib/dload: give uniform naming to curl CB functions Signed-off-by: Dave Reisner Signed-off-by: Dan McGee --- lib/libalpm/dload.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/libalpm/dload.c') diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c index f5af7c64..414d5d76 100644 --- a/lib/libalpm/dload.c +++ b/lib/libalpm/dload.c @@ -92,7 +92,7 @@ static void inthandler(int UNUSED signum) dload_interrupted = ABORT_SIGINT; } -static int curl_progress(void *file, double dltotal, double dlnow, +static int dload_progress_cb(void *file, double dltotal, double dlnow, double UNUSED ultotal, double UNUSED ulnow) { struct dload_payload *payload = (struct dload_payload *)file; @@ -194,7 +194,7 @@ static mode_t _getumask(void) return mask; } -static size_t parse_headers(void *ptr, size_t size, size_t nmemb, void *user) +static size_t dload_parseheader_cb(void *ptr, size_t size, size_t nmemb, void *user) { size_t realsize = size * nmemb; const char *fptr, *endptr = NULL; @@ -283,11 +283,11 @@ static void curl_set_handle_opts(struct dload_payload *payload, curl_easy_setopt(curl, CURLOPT_FILETIME, 1L); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curl_progress); + curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, dload_progress_cb); curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, (void *)payload); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1024L); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 10L); - curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, parse_headers); + curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, dload_parseheader_cb); curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void *)payload); curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, dload_sockopt_cb); -- cgit v1.2.3-54-g00ecf