[pacman-dev] [PATCH 1/2] Use long rather than double where possible
Beautiful of libcurl to use floating point types for what are never fractional values. We can do better, and we usually want these values in their integer form anyway. Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/dload.c | 16 ++++++++-------- lib/libalpm/dload.h | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c index 8387ae6..bd5ccce 100644 --- a/lib/libalpm/dload.c +++ b/lib/libalpm/dload.c @@ -43,7 +43,7 @@ #include "handle.h" #ifdef HAVE_LIBCURL -static double prevprogress; /* last download amount */ +static long prevprogress; /* last download amount */ static const char *get_filename(const char *url) { @@ -76,7 +76,7 @@ static int curl_progress(void *file, double dltotal, double dlnow, double UNUSED ultotal, double UNUSED ulnow) { struct dload_payload *payload = (struct dload_payload *)file; - double current_size, total_size; + long current_size, total_size; /* SIGINT sent, abort by alerting curl */ if(dload_interrupted) { @@ -88,20 +88,20 @@ static int curl_progress(void *file, double dltotal, double dlnow, return 0; } - current_size = payload->initial_size + dlnow; - total_size = payload->initial_size + dltotal; + current_size = payload->initial_size + (long)dlnow; + total_size = payload->initial_size + (long)dltotal; - if(DOUBLE_EQ(dltotal, 0) || DOUBLE_EQ(prevprogress, total_size)) { + if(DOUBLE_EQ(dltotal, 0.0) || prevprogress == total_size) { return 0; } /* initialize the progress bar here to avoid displaying it when * a repo is up to date and nothing gets downloaded */ - if(DOUBLE_EQ(prevprogress, 0)) { + if(prevprogress == 0) { payload->handle->dlcb(payload->remote_name, 0, (long)dltotal); } - payload->handle->dlcb(payload->remote_name, (long)current_size, (long)total_size); + payload->handle->dlcb(payload->remote_name, current_size, total_size); prevprogress = current_size; @@ -216,7 +216,7 @@ static void curl_set_handle_opts(struct dload_payload *payload, payload->tempfile_openmode = "ab"; curl_easy_setopt(handle->curl, CURLOPT_RESUME_FROM, (long)st.st_size); _alpm_log(handle, ALPM_LOG_DEBUG, "tempfile found, attempting continuation\n"); - payload->initial_size = (double)st.st_size; + payload->initial_size = st.st_size; } } diff --git a/lib/libalpm/dload.h b/lib/libalpm/dload.h index 13034f7..f973f2d 100644 --- a/lib/libalpm/dload.h +++ b/lib/libalpm/dload.h @@ -33,7 +33,7 @@ struct dload_payload { char *destfile_name; char *content_disp_name; char *fileurl; - double initial_size; + long initial_size; long max_size; int force; int allow_resume; -- 1.7.6
This handles the no Content-Length header problem as stated in the comments of FS#23413. We add a quick check to the callback that will force an abort if the downloaded data exceeds the payload size, and then check for this error in the post-download cleanup code. Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/dload.c | 23 ++++++++++++++++++++--- 1 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c index bd5ccce..0c411e9 100644 --- a/lib/libalpm/dload.c +++ b/lib/libalpm/dload.c @@ -83,12 +83,18 @@ static int curl_progress(void *file, double dltotal, double dlnow, return 1; } + current_size = payload->initial_size + (long)dlnow; + + /* is our filesize still under any set limit? */ + if(payload->max_size && current_size > payload->max_size) { + return 1; + } + /* none of what follows matters if the front end has no callback */ if(payload->handle->dlcb == NULL) { return 0; } - current_size = payload->initial_size + (long)dlnow; total_size = payload->initial_size + (long)dltotal; if(DOUBLE_EQ(dltotal, 0.0) || prevprogress == total_size) { @@ -341,6 +347,15 @@ static int curl_download_internal(struct dload_payload *payload, } break; case CURLE_ABORTED_BY_CALLBACK: + /* two cases here- interrupted by user, or we exceeded max file size. */ + if(!dload_interrupted) { + handle->curlerr = CURLE_FILESIZE_EXCEEDED; + handle->pm_errno = ALPM_ERR_LIBCURL; + /* the hardcoded 'size exceeded' message is same as libcurl's normal */ + _alpm_log(handle, ALPM_LOG_ERROR, + _("failed retrieving file '%s' from %s : %s\n"), + payload->remote_name, hostname, "Maximum file size exceeded"); + } goto cleanup; default: /* delete zero length downloads */ @@ -349,10 +364,12 @@ static int curl_download_internal(struct dload_payload *payload, } if(!payload->errors_ok) { handle->pm_errno = ALPM_ERR_LIBCURL; - _alpm_log(handle, ALPM_LOG_ERROR, _("failed retrieving file '%s' from %s : %s\n"), + _alpm_log(handle, ALPM_LOG_ERROR, + _("failed retrieving file '%s' from %s : %s\n"), payload->remote_name, hostname, error_buffer); } else { - _alpm_log(handle, ALPM_LOG_DEBUG, "failed retrieving file '%s' from %s : %s\n", + _alpm_log(handle, ALPM_LOG_DEBUG, + "failed retrieving file '%s' from %s : %s\n", payload->remote_name, hostname, error_buffer); } goto cleanup; -- 1.7.6
participants (1)
-
Dan McGee