[pacman-dev] [PATCH 4/4] absorb fileinfo struct into dload_payload

Dave Reisner d at falconindy.com
Sun Jul 3 20:17:48 EDT 2011


On Sun, Jul 03, 2011 at 02:26:10PM -0500, Dan McGee wrote:
> On Fri, Jul 1, 2011 at 7:59 AM, Dave Reisner <d at falconindy.com> wrote:
> > This transitional struct becomes delicious noms for dload_payload.
> Ignore me on the previous patch; merging the two is better done in a
> separate patch, so keep it like this.
> 
> > Signed-off-by: Dave Reisner <dreisner at archlinux.org>
> > ---
> >  lib/libalpm/dload.c |   57 ++++++++++++++++++++++++--------------------------
> >  lib/libalpm/dload.h |   10 +-------
> >  2 files changed, 29 insertions(+), 38 deletions(-)
> >
> > diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c
> > index bac9356..f41e395 100644
> > --- a/lib/libalpm/dload.c
> > +++ b/lib/libalpm/dload.c
> > @@ -46,7 +46,7 @@
> >  static double prevprogress; /* last download amount */
> >  #endif
> >
> > -static const char *get_filename(const char *url)
> > +static char *get_filename(const char *url)
> explanation?
> 

Uhh, uhhhhh..... Got nothing, it's reverted/fixed.

> >  {
> >        char *filename = strrchr(url, '/');
> >        if(filename != NULL) {
> > @@ -80,7 +80,7 @@ static void inthandler(int UNUSED signum)
> >  static int curl_progress(void *file, double dltotal, double dlnow,
> >                double UNUSED ultotal, double UNUSED ulnow)
> >  {
> > -       struct fileinfo *dlfile = (struct fileinfo *)file;
> > +       struct dload_payload *payload = (struct dload_payload *)file;
> >        double current_size, total_size;
> >
> >        /* SIGINT sent, abort by alerting curl */
> > @@ -89,12 +89,12 @@ static int curl_progress(void *file, double dltotal, double dlnow,
> >        }
> >
> >        /* none of what follows matters if the front end has no callback */
> > -       if(dlfile->handle->dlcb == NULL) {
> > +       if(payload->handle->dlcb == NULL) {
> >                return 0;
> >        }
> >
> > -       current_size = dlfile->initial_size + dlnow;
> > -       total_size = dlfile->initial_size + dltotal;
> > +       current_size = payload->initial_size + dlnow;
> > +       total_size = payload->initial_size + dltotal;
> >
> >        if(DOUBLE_EQ(dltotal, 0) || DOUBLE_EQ(prevprogress, total_size)) {
> >                return 0;
> > @@ -103,10 +103,10 @@ static int curl_progress(void *file, double dltotal, double dlnow,
> >        /* 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)) {
> > -               dlfile->handle->dlcb(dlfile->filename, 0, (long)dltotal);
> > +               payload->handle->dlcb(payload->filename, 0, (long)dltotal);
> >        }
> >
> > -       dlfile->handle->dlcb(dlfile->filename, (long)current_size, (long)total_size);
> > +       payload->handle->dlcb(payload->filename, (long)current_size, (long)total_size);
> >
> >        prevprogress = current_size;
> >
> > @@ -154,7 +154,7 @@ static size_t parse_headers(void *ptr, size_t size, size_t nmemb, void *user)
> >        const char *fptr, *endptr = NULL;
> >        const char * const cd_header = "Content-Disposition:";
> >        const char * const fn_key = "filename=";
> > -       struct fileinfo **dlfile = (struct fileinfo**)user;
> > +       struct dload_payload *payload = (struct dload_payload *)user;
> >
> >        if(strncasecmp(cd_header, ptr, strlen(cd_header)) == 0) {
> >                if((fptr = strstr(ptr, fn_key))) {
> > @@ -171,8 +171,8 @@ static size_t parse_headers(void *ptr, size_t size, size_t nmemb, void *user)
> >                                endptr--;
> >                        }
> >
> > -                       STRNDUP((*dlfile)->cd_filename, fptr, endptr - fptr + 1,
> > -                                       RET_ERR((*dlfile)->handle, PM_ERR_MEMORY, realsize));
> > +                       STRNDUP(payload->cd_filename, fptr, endptr - fptr + 1,
> > +                                       RET_ERR(payload->handle, PM_ERR_MEMORY, realsize));
> >                }
> >        }
> >
> > @@ -194,20 +194,18 @@ static int curl_download_internal(struct dload_payload *payload,
> >        long timecond, remote_time = -1;
> >        double remote_size, bytes_dl;
> >        struct sigaction sig_pipe[2], sig_int[2];
> > -       struct fileinfo dlfile;
> >
> > -       dlfile.handle = payload->handle;
> > -       dlfile.initial_size = 0.0;
> > -       dlfile.filename = get_filename(payload->fileurl);
> > -       dlfile.cd_filename = NULL;
> > -       if(!dlfile.filename || curl_gethost(payload->fileurl, hostname) != 0) {
> > +       if(!payload->filename) {
> > +               payload->filename = get_filename(payload->fileurl);
> > +       }
> > +       if(!payload->filename || curl_gethost(payload->fileurl, hostname) != 0) {
> >                _alpm_log(payload->handle, PM_LOG_ERROR, _("url '%s' is invalid\n"), payload->fileurl);
> >                RET_ERR(payload->handle, PM_ERR_SERVER_BAD_URL, -1);
> >        }
> >
> > -       if(strlen(dlfile.filename) > 0 && strcmp(dlfile.filename, ".sig") != 0) {
> > -               destfile = get_fullpath(localpath, dlfile.filename, "");
> > -               tempfile = get_fullpath(localpath, dlfile.filename, ".part");
> > +       if(strlen(payload->filename) > 0 && strcmp(payload->filename, ".sig") != 0) {
> > +               destfile = get_fullpath(localpath, payload->filename, "");
> > +               tempfile = get_fullpath(localpath, payload->filename, ".part");
> >                if(!destfile || !tempfile) {
> >                        goto cleanup;
> >                }
> > @@ -231,7 +229,7 @@ static int curl_download_internal(struct dload_payload *payload,
> >                }
> >                /* localf now points to our alpmtmp.XXXXXX */
> >                STRDUP(tempfile, randpath, RET_ERR(payload->handle, PM_ERR_MEMORY, -1));
> > -               dlfile.filename = strrchr(randpath, '/') + 1;
> > +               payload->filename = strrchr(randpath, '/') + 1;
> >        }
> >
> >        error_buffer[0] = '\0';
> > @@ -247,11 +245,11 @@ static int curl_download_internal(struct dload_payload *payload,
> >        curl_easy_setopt(payload->handle->curl, CURLOPT_NOPROGRESS, 0L);
> >        curl_easy_setopt(payload->handle->curl, CURLOPT_FOLLOWLOCATION, 1L);
> >        curl_easy_setopt(payload->handle->curl, CURLOPT_PROGRESSFUNCTION, curl_progress);
> > -       curl_easy_setopt(payload->handle->curl, CURLOPT_PROGRESSDATA, (void *)&dlfile);
> > +       curl_easy_setopt(payload->handle->curl, CURLOPT_PROGRESSDATA, (void *)payload);
> >        curl_easy_setopt(payload->handle->curl, CURLOPT_LOW_SPEED_LIMIT, 1024L);
> >        curl_easy_setopt(payload->handle->curl, CURLOPT_LOW_SPEED_TIME, 10L);
> >        curl_easy_setopt(payload->handle->curl, CURLOPT_HEADERFUNCTION, parse_headers);
> > -       curl_easy_setopt(payload->handle->curl, CURLOPT_WRITEHEADER, &dlfile);
> > +       curl_easy_setopt(payload->handle->curl, CURLOPT_WRITEHEADER, (void *)payload);
> >
> >        if(payload->max_size) {
> >                curl_easy_setopt(payload->handle->curl, CURLOPT_MAXFILESIZE, payload->max_size);
> > @@ -271,7 +269,7 @@ static int curl_download_internal(struct dload_payload *payload,
> >                open_mode = "ab";
> >                curl_easy_setopt(payload->handle->curl, CURLOPT_RESUME_FROM, (long)st.st_size);
> >                _alpm_log(payload->handle, PM_LOG_DEBUG, "tempfile found, attempting continuation");
> > -               dlfile.initial_size = (double)st.st_size;
> > +               payload->initial_size = (double)st.st_size;
> >        }
> >
> >        if(localf == NULL) {
> > @@ -311,10 +309,10 @@ static int curl_download_internal(struct dload_payload *payload,
> >                if(!payload->errors_ok) {
> >                        payload->handle->pm_errno = PM_ERR_LIBCURL;
> >                        _alpm_log(payload->handle, PM_LOG_ERROR, _("failed retrieving file '%s' from %s : %s\n"),
> > -                                       dlfile.filename, hostname, error_buffer);
> > +                                       payload->filename, hostname, error_buffer);
> >                } else {
> >                        _alpm_log(payload->handle, PM_LOG_DEBUG, "failed retrieving file '%s' from %s : %s\n",
> > -                                       dlfile.filename, hostname, error_buffer);
> > +                                       payload->filename, hostname, error_buffer);
> >                }
> >                unlink(tempfile);
> >                goto cleanup;
> > @@ -342,14 +340,14 @@ static int curl_download_internal(struct dload_payload *payload,
> >                        !DOUBLE_EQ(bytes_dl, remote_size)) {
> >                payload->handle->pm_errno = PM_ERR_RETRIEVE;
> >                _alpm_log(payload->handle, PM_LOG_ERROR, _("%s appears to be truncated: %jd/%jd bytes\n"),
> > -                               dlfile.filename, (intmax_t)bytes_dl, (intmax_t)remote_size);
> > +                               payload->filename, (intmax_t)bytes_dl, (intmax_t)remote_size);
> >                goto cleanup;
> >        }
> >
> > -       if(dlfile.cd_filename) {
> > +       if(payload->cd_filename) {
> >                /* content-disposition header has a better name for our file */
> >                free(destfile);
> > -               destfile = get_fullpath(localpath, dlfile.cd_filename, "");
> > +               destfile = get_fullpath(localpath, payload->cd_filename, "");
> >        } else {
> >                const char *effective_filename = strrchr(effective_url, '/');
> >                if(effective_filename) {
> > @@ -387,7 +385,6 @@ cleanup:
> >
> >        FREE(tempfile);
> >        FREE(destfile);
> > -       FREE(dlfile.cd_filename);
> >
> >        /* restore the old signal handlers */
> >        sigaction(SIGINT, &sig_int[OLD], NULL);
> > @@ -494,8 +491,8 @@ void _alpm_dload_payload_free(void *payload) {
> I missed this in an earlier patch, but I'd rather this match the sig
> of the rest of our free functions and take a typed pointer- that way,
> when used in a non list_free context, errors get caught. Instead, cast
> the function in list_free.
> 

Makes sense. Will fix.

> >
> >        ASSERT(load, return);
> >
> > -       FREE(load->filename);
> >        FREE(load->fileurl);
> > +       FREE(load->cd_filename);
> >        FREE(load);
> >  }
> >
> > diff --git a/lib/libalpm/dload.h b/lib/libalpm/dload.h
> > index 19bd499..db558be 100644
> > --- a/lib/libalpm/dload.h
> > +++ b/lib/libalpm/dload.h
> > @@ -25,18 +25,12 @@
> >
> >  #include <time.h>
> >
> > -/* internal structure for communicating with curl progress callback */
> > -struct fileinfo {
> > -       alpm_handle_t *handle;
> > -       const char *filename;
> > -       char *cd_filename;
> > -       double initial_size;
> > -};
> > -
> >  struct dload_payload {
> >        alpm_handle_t *handle;
> >        char *filename;
> > +       char *cd_filename;
> >        char *fileurl;
> > +       double initial_size;
> >        long max_size;
> >        int force;
> >        int allow_resume;
> > --
> > 1.7.6
> >
> >
> >
> 


More information about the pacman-dev mailing list