1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-11-11 20:05:07 -05:00

absorb some _alpm_download params into payload struct

Restore some sanity to the number of arguments passed to _alpm_download
and curl_download_internal.

Signed-off-by: Dave Reisner <dreisner@archlinux.org>
This commit is contained in:
Dave Reisner 2011-06-30 21:19:25 -04:00
parent 6dc71926f9
commit 3eec745910
4 changed files with 40 additions and 31 deletions

View File

@ -185,8 +185,10 @@ int SYMEXPORT alpm_db_update(int force, alpm_db_t *db)
len = strlen(server) + strlen(db->treename) + 9; len = strlen(server) + strlen(db->treename) + 9;
CALLOC(payload->fileurl, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, -1)); CALLOC(payload->fileurl, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
snprintf(payload->fileurl, len, "%s/%s.db", server, db->treename); snprintf(payload->fileurl, len, "%s/%s.db", server, db->treename);
payload->handle = handle;
payload->force = force;
ret = _alpm_download(handle, payload, syncpath, NULL, force, 0, 0); ret = _alpm_download(payload, syncpath, NULL);
if(ret == 0 && (level & ALPM_SIG_DATABASE)) { if(ret == 0 && (level & ALPM_SIG_DATABASE)) {
/* an existing sig file is no good at this point */ /* an existing sig file is no good at this point */
@ -198,16 +200,18 @@ int SYMEXPORT alpm_db_update(int force, alpm_db_t *db)
unlink(sigpath); unlink(sigpath);
free(sigpath); free(sigpath);
int errors_ok = (level & ALPM_SIG_DATABASE_OPTIONAL);
/* if we downloaded a DB, we want the .sig from the same server */ /* if we downloaded a DB, we want the .sig from the same server */
snprintf(payload->fileurl, len, "%s/%s.db.sig", server, db->treename); snprintf(payload->fileurl, len, "%s/%s.db.sig", server, db->treename);
payload->handle = handle;
payload->force = 1;
payload->errors_ok = (level & ALPM_SIG_DATABASE_OPTIONAL);
/* set hard upper limit of 16KiB */ /* set hard upper limit of 16KiB */
payload->max_size = 16 * 1024; payload->max_size = 16 * 1024;
sig_ret = _alpm_download(handle, payload, syncpath, NULL, 1, 0, errors_ok); sig_ret = _alpm_download(payload, syncpath, NULL);
/* errors_ok suppresses error messages, but not the return code */ /* errors_ok suppresses error messages, but not the return code */
sig_ret = errors_ok ? 0 : sig_ret; sig_ret = payload->errors_ok ? 0 : sig_ret;
} }
_alpm_dload_payload_free(payload); _alpm_dload_payload_free(payload);

View File

@ -177,9 +177,8 @@ static size_t parse_headers(void *ptr, size_t size, size_t nmemb, void *user)
return realsize; return realsize;
} }
static int curl_download_internal(alpm_handle_t *handle, struct dload_payload *payload, static int curl_download_internal(struct dload_payload *payload,
const char *localpath, char **final_file, int force, int allow_resume, const char *localpath, char **final_file)
int errors_ok)
{ {
int ret = -1, should_unlink = 0; int ret = -1, should_unlink = 0;
FILE *localf = NULL; FILE *localf = NULL;
@ -194,6 +193,8 @@ static int curl_download_internal(alpm_handle_t *handle, struct dload_payload *p
double remote_size, bytes_dl; double remote_size, bytes_dl;
struct sigaction sig_pipe[2], sig_int[2]; struct sigaction sig_pipe[2], sig_int[2];
struct fileinfo dlfile; struct fileinfo dlfile;
/* shortcut to our handle within the payload */
alpm_handle_t *handle = payload->handle;
dlfile.handle = handle; dlfile.handle = handle;
dlfile.initial_size = 0.0; dlfile.initial_size = 0.0;
@ -261,11 +262,11 @@ static int curl_download_internal(alpm_handle_t *handle, struct dload_payload *p
curl_easy_setopt(handle->curl, CURLOPT_USERAGENT, useragent); curl_easy_setopt(handle->curl, CURLOPT_USERAGENT, useragent);
} }
if(!allow_resume && !force && stat(destfile, &st) == 0) { if(!payload->allow_resume && !payload->force && stat(destfile, &st) == 0) {
/* start from scratch, but only download if our local is out of date. */ /* start from scratch, but only download if our local is out of date. */
curl_easy_setopt(handle->curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); curl_easy_setopt(handle->curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
curl_easy_setopt(handle->curl, CURLOPT_TIMEVALUE, (long)st.st_mtime); curl_easy_setopt(handle->curl, CURLOPT_TIMEVALUE, (long)st.st_mtime);
} else if(stat(tempfile, &st) == 0 && allow_resume) { } else if(stat(tempfile, &st) == 0 && payload->allow_resume) {
/* a previous partial download exists, resume from end of file. */ /* a previous partial download exists, resume from end of file. */
open_mode = "ab"; open_mode = "ab";
curl_easy_setopt(handle->curl, CURLOPT_RESUME_FROM, (long)st.st_size); curl_easy_setopt(handle->curl, CURLOPT_RESUME_FROM, (long)st.st_size);
@ -307,7 +308,7 @@ static int curl_download_internal(alpm_handle_t *handle, struct dload_payload *p
if(handle->curlerr == CURLE_ABORTED_BY_CALLBACK) { if(handle->curlerr == CURLE_ABORTED_BY_CALLBACK) {
goto cleanup; goto cleanup;
} else if(handle->curlerr != CURLE_OK) { } else if(handle->curlerr != CURLE_OK) {
if(!errors_ok) { if(!payload->errors_ok) {
handle->pm_errno = ALPM_ERR_LIBCURL; 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"),
dlfile.filename, hostname, error_buffer); dlfile.filename, hostname, error_buffer);
@ -402,29 +403,25 @@ cleanup:
/** Download a file given by a URL to a local directory. /** Download a file given by a URL to a local directory.
* Does not overwrite an existing file if the download fails. * Does not overwrite an existing file if the download fails.
* @param handle the context handle * @param payload the payload context
* @param url the file's URL
* @param localpath the directory to save the file in * @param localpath the directory to save the file in
* @param final_file the real name of the downloaded file (may be NULL) * @param final_file the real name of the downloaded file (may be NULL)
* @param force force download even if there is an up-to-date local copy
* @param allow_resume allow a partial download to be resumed
* @param errors_ok do not log errors (but still return them)
* @return 0 on success, -1 on error (pm_errno is set accordingly if errors_ok == 0) * @return 0 on success, -1 on error (pm_errno is set accordingly if errors_ok == 0)
*/ */
int _alpm_download(alpm_handle_t *handle, struct dload_payload *payload, int _alpm_download(struct dload_payload *payload, const char *localpath,
const char *localpath, char **final_file, int force, int allow_resume, char **final_file)
int errors_ok)
{ {
alpm_handle_t *handle = payload->handle;
if(handle->fetchcb == NULL) { if(handle->fetchcb == NULL) {
#ifdef HAVE_LIBCURL #ifdef HAVE_LIBCURL
return curl_download_internal(handle, payload, localpath, final_file, force, return curl_download_internal(payload, localpath, final_file);
allow_resume, errors_ok);
#else #else
RET_ERR(handle, ALPM_ERR_EXTERNAL_DOWNLOAD, -1); RET_ERR(handle, ALPM_ERR_EXTERNAL_DOWNLOAD, -1);
#endif #endif
} else { } else {
int ret = handle->fetchcb(payload->fileurl, localpath, force); int ret = handle->fetchcb(payload->fileurl, localpath, payload->force);
if(ret == -1 && !errors_ok) { if(ret == -1 && !payload->errors_ok) {
RET_ERR(handle, ALPM_ERR_EXTERNAL_DOWNLOAD, -1); RET_ERR(handle, ALPM_ERR_EXTERNAL_DOWNLOAD, -1);
} }
return ret; return ret;
@ -446,10 +443,12 @@ char SYMEXPORT *alpm_fetch_pkgurl(alpm_handle_t *handle, const char *url)
cachedir = _alpm_filecache_setup(handle); cachedir = _alpm_filecache_setup(handle);
CALLOC(payload, 1, sizeof(*payload), RET_ERR(handle, ALPM_ERR_MEMORY, NULL)); CALLOC(payload, 1, sizeof(*payload), RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
payload->handle = handle;
payload->fileurl = strdup(url); payload->fileurl = strdup(url);
payload->allow_resume = 1;
/* download the file */ /* download the file */
ret = _alpm_download(handle, payload, cachedir, &final_file, 0, 1, 0); ret = _alpm_download(payload, cachedir, &final_file);
if(ret == -1) { if(ret == -1) {
_alpm_log(handle, ALPM_LOG_WARNING, _("failed to download %s\n"), url); _alpm_log(handle, ALPM_LOG_WARNING, _("failed to download %s\n"), url);
return NULL; return NULL;
@ -460,17 +459,18 @@ char SYMEXPORT *alpm_fetch_pkgurl(alpm_handle_t *handle, const char *url)
if(ret == 0 && (handle->siglevel & ALPM_SIG_PACKAGE)) { if(ret == 0 && (handle->siglevel & ALPM_SIG_PACKAGE)) {
char *sig_final_file = NULL; char *sig_final_file = NULL;
size_t len; size_t len;
int errors_ok = (handle->siglevel & ALPM_SIG_PACKAGE_OPTIONAL);
struct dload_payload *sig_payload; struct dload_payload *sig_payload;
CALLOC(sig_payload, 1, sizeof(*sig_payload), RET_ERR(handle, ALPM_ERR_MEMORY, NULL)); CALLOC(sig_payload, 1, sizeof(*sig_payload), RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
len = strlen(url) + 5; len = strlen(url) + 5;
CALLOC(sig_payload->fileurl, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, NULL)); CALLOC(sig_payload->fileurl, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
snprintf(sig_payload->fileurl, len, "%s.sig", url); snprintf(sig_payload->fileurl, len, "%s.sig", url);
sig_payload->max_size = 1024 * 16; sig_payload->handle = handle;
sig_payload->force = 1;
sig_payload->errors_ok = (handle->siglevel & ALPM_SIG_PACKAGE_OPTIONAL);
ret = _alpm_download(handle, sig_payload, cachedir, &sig_final_file, 1, 0, errors_ok); ret = _alpm_download(sig_payload, cachedir, &sig_final_file);
if(ret == -1 && !errors_ok) { if(ret == -1 && !sig_payload->errors_ok) {
_alpm_log(handle, ALPM_LOG_WARNING, _("failed to download %s\n"), sig_payload->fileurl); _alpm_log(handle, ALPM_LOG_WARNING, _("failed to download %s\n"), sig_payload->fileurl);
/* Warn now, but don't return NULL. We will fail later during package /* Warn now, but don't return NULL. We will fail later during package
* load time. */ * load time. */

View File

@ -34,16 +34,19 @@ struct fileinfo {
}; };
struct dload_payload { struct dload_payload {
alpm_handle_t *handle;
char *filename; char *filename;
char *fileurl; char *fileurl;
long max_size; long max_size;
int force;
int allow_resume;
int errors_ok;
}; };
void _alpm_dload_payload_free(struct dload_payload *payload); void _alpm_dload_payload_free(struct dload_payload *payload);
int _alpm_download(alpm_handle_t *handle, struct dload_payload *payload, int _alpm_download(struct dload_payload *payload, const char *localpath,
const char *localpath, char **final_file, int force, int allow_resume, char **final_file);
int errors_ok);
#endif /* _ALPM_DLOAD_H */ #endif /* _ALPM_DLOAD_H */

View File

@ -812,8 +812,10 @@ static int download_files(alpm_handle_t *handle, alpm_list_t **deltas)
len = strlen(server_url) + strlen(payload->filename) + 2; len = strlen(server_url) + strlen(payload->filename) + 2;
CALLOC(payload->fileurl, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, -1)); CALLOC(payload->fileurl, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
snprintf(payload->fileurl, len, "%s/%s", server_url, payload->filename); snprintf(payload->fileurl, len, "%s/%s", server_url, payload->filename);
payload->handle = handle;
payload->allow_resume = 1;
ret = _alpm_download(handle, payload, cachedir, NULL, 0, 1, 0); ret = _alpm_download(payload, cachedir, NULL);
if(ret != -1) { if(ret != -1) {
break; break;
} }