Changeset 4394


Ignore:
Timestamp:
04/23/08 17:40:38 (5 years ago)
Author:
thomas
Message:

opkg: improve download callback handling and integrate into opkg_update_package_lists

Location:
trunk/src/target/opkg/libopkg
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/target/opkg/libopkg/opkg.c

    r4391 r4394  
    8888} 
    8989 
     90struct _curl_cb_data 
     91{ 
     92  opkg_progress_callback_t cb; 
     93  opkg_t *opkg; 
     94  void *user_data; 
     95  int start_range; 
     96  int finish_range; 
     97}; 
     98 
     99int 
     100curl_progress_cb (struct _curl_cb_data *cb_data, 
     101                    double t, /* dltotal */ 
     102                    double d, /* dlnow */ 
     103                    double ultotal, 
     104                    double ulnow) 
     105{ 
     106  int p = (t) ? d*100/t : 0; 
     107  static int prev = -1; 
     108 
     109  /* prevent the same value being sent twice (can occur due to rounding) */ 
     110  if (p == prev) 
     111    return 0; 
     112  prev = p; 
     113 
     114  if (t < 1) 
     115    return 0; 
     116 
     117  (cb_data->cb) (cb_data->opkg, 
     118      cb_data->start_range + (d/t * ((cb_data->finish_range - cb_data->start_range))), 
     119      cb_data->user_data); 
     120 
     121  return 0; 
     122} 
    90123 
    91124 
     
    457490      char *tmp_file_name; 
    458491      FILE *in, *out; 
     492      struct _curl_cb_data cb_data; 
    459493 
    460494      sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name); 
    461495 
    462496      /* XXX: Note: downloading url */ 
    463       err = opkg_download (opkg->conf, url, tmp_file_name); 
     497 
     498      cb_data.cb = progress_callback; 
     499      cb_data.opkg = opkg; 
     500      cb_data.user_data = user_data; 
     501      cb_data.start_range = 100 * sources_done / sources_list_count; 
     502      cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count; 
     503 
     504      err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data); 
    464505 
    465506      if (err == 0) 
     
    480521    } 
    481522    else 
    482       err = opkg_download (opkg->conf, url, list_file_name); 
     523      err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL); 
    483524 
    484525    if (err) 
     
    502543    sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig"); 
    503544 
    504     err = opkg_download (opkg->conf, url, tmp_file_name); 
     545    err = opkg_download (opkg->conf, url, tmp_file_name, NULL, NULL); 
    505546    if (err) 
    506547    { 
  • trunk/src/target/opkg/libopkg/opkg_cmd.c

    r4357 r4394  
    227227               
    228228              sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name); 
    229               err = opkg_download(conf, url, tmp_file_name); 
     229              err = opkg_download(conf, url, tmp_file_name, NULL, NULL); 
    230230              if (err == 0) { 
    231231                   opkg_message (conf, OPKG_NOTICE, "Inflating %s\n", url); 
     
    243243              } 
    244244          } else 
    245               err = opkg_download(conf, url, list_file_name); 
     245              err = opkg_download(conf, url, list_file_name, NULL, NULL); 
    246246          if (err) { 
    247247               failures++; 
     
    267267          sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig"); 
    268268 
    269           err = opkg_download(conf, url, tmp_file_name); 
     269          err = opkg_download(conf, url, tmp_file_name, NULL, NULL); 
    270270          if (err) { 
    271271            failures++; 
  • trunk/src/target/opkg/libopkg/opkg_download.c

    r4389 r4394  
    3434#include "opkg_defines.h" 
    3535 
    36 opkg_download_progress_callback opkg_cb_download_progress = NULL; 
    37  
    38 int 
    39 curl_progress_func (char* url, 
    40                     double t, /* dltotal */ 
    41                     double d, /* dlnow */ 
    42                     double ultotal, 
    43                     double ulnow) 
    44 { 
    45     int p = (t) ? d*100/t : 0; 
    46  
    47     if (opkg_cb_download_progress) 
    48     { 
    49         static int prev = -1; 
    50  
    51         /* don't report the same percentage multiple times 
    52          * (this can occur due to rounding) */ 
    53         if (prev == p) 
    54             return 0; 
    55         prev = p; 
    56  
    57         opkg_cb_download_progress (p, url); 
    58         return 0; 
    59     } 
    60     return 0; 
    61 } 
    62  
    63 int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name) 
     36int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name, curl_progress_func cb, void *data) 
    6437{ 
    6538    int err = 0; 
     
    11184        curl_easy_setopt (curl, CURLOPT_URL, src); 
    11285        curl_easy_setopt (curl, CURLOPT_WRITEDATA, file); 
    113         curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0); 
    114         curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, src); 
    115         curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func); 
     86        curl_easy_setopt (curl, CURLOPT_NOPROGRESS, (cb == NULL)); 
     87        if (cb) 
     88        { 
     89                curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, data); 
     90                curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, cb); 
     91        } 
    11692        curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1); 
    11793        if (conf->http_proxy || conf->ftp_proxy) 
     
    180156    sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename); 
    181157 
    182     err = opkg_download(conf, url, pkg->local_filename); 
     158    err = opkg_download(conf, url, pkg->local_filename, NULL, NULL); 
    183159    free(url); 
    184160 
     
    205181 
    206182          sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base); 
    207           err = opkg_download(conf, url, tmp_file); 
     183          err = opkg_download(conf, url, tmp_file, NULL, NULL); 
    208184          if (err) 
    209185               return err; 
  • trunk/src/target/opkg/libopkg/opkg_download.h

    r4357 r4394  
    2222 
    2323typedef void (*opkg_download_progress_callback)(int percent, char *url); 
     24typedef int (*curl_progress_func)(void *data, double t, double d, double ultotal, double ulnow); 
    2425 
    25 int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name); 
     26 
     27int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name, curl_progress_func cb, void *data); 
    2628int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir); 
    2729/* 
Note: See TracChangeset for help on using the changeset viewer.