opkg: improve download callback handling and integrate into opkg_update_package_lists
authorticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Mon, 15 Dec 2008 05:12:27 +0000 (05:12 +0000)
committerticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Mon, 15 Dec 2008 05:12:27 +0000 (05:12 +0000)
git-svn-id: http://opkg.googlecode.com/svn/trunk@86 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358

libopkg/opkg.c
libopkg/opkg_cmd.c
libopkg/opkg_download.c
libopkg/opkg_download.h

index 0e63180fa6d4638713d97c7430c991af11821368..fc7bd3f2ff84019a43de7324ef111d76a105f1f3 100644 (file)
@@ -87,6 +87,39 @@ opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
   return err;
 }
 
   return err;
 }
 
+struct _curl_cb_data
+{
+  opkg_progress_callback_t cb;
+  opkg_t *opkg;
+  void *user_data;
+  int start_range;
+  int finish_range;
+};
+
+int
+curl_progress_cb (struct _curl_cb_data *cb_data,
+                   double t, /* dltotal */
+                   double d, /* dlnow */
+                   double ultotal,
+                   double ulnow)
+{
+  int p = (t) ? d*100/t : 0;
+  static int prev = -1;
+
+  /* prevent the same value being sent twice (can occur due to rounding) */
+  if (p == prev)
+    return 0;
+  prev = p;
+
+  if (t < 1)
+    return 0;
+
+  (cb_data->cb) (cb_data->opkg,
+      cb_data->start_range + (d/t * ((cb_data->finish_range - cb_data->start_range))),
+      cb_data->user_data);
+
+  return 0;
+}
 
 
 /*** Public API ***/
 
 
 /*** Public API ***/
@@ -456,11 +489,19 @@ opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callb
     {
       char *tmp_file_name;
       FILE *in, *out;
     {
       char *tmp_file_name;
       FILE *in, *out;
+      struct _curl_cb_data cb_data;
 
       sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
 
       /* XXX: Note: downloading url */
 
       sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
 
       /* XXX: Note: downloading url */
-      err = opkg_download (opkg->conf, url, tmp_file_name);
+
+      cb_data.cb = progress_callback;
+      cb_data.opkg = opkg;
+      cb_data.user_data = user_data;
+      cb_data.start_range = 100 * sources_done / sources_list_count;
+      cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
+
+      err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
 
       if (err == 0)
       {
 
       if (err == 0)
       {
@@ -479,7 +520,7 @@ opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callb
       }
     }
     else
       }
     }
     else
-      err = opkg_download (opkg->conf, url, list_file_name);
+      err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
 
     if (err)
     {
 
     if (err)
     {
@@ -501,7 +542,7 @@ opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callb
 
     sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
 
 
     sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
 
-    err = opkg_download (opkg->conf, url, tmp_file_name);
+    err = opkg_download (opkg->conf, url, tmp_file_name, NULL, NULL);
     if (err)
     {
       /* XXX: Warning: Download failed */
     if (err)
     {
       /* XXX: Warning: Download failed */
index 42443a50d6748b9b9a9161bf1d5c1d5c86a40985..a7f9b1afdfdcdbee4e2dd5fb9f017c97fb75724a 100644 (file)
@@ -226,7 +226,7 @@ static int opkg_update_cmd(opkg_conf_t *conf, int argc, char **argv)
              FILE *in, *out;
              
              sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
              FILE *in, *out;
              
              sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
-             err = opkg_download(conf, url, tmp_file_name);
+             err = opkg_download(conf, url, tmp_file_name, NULL, NULL);
              if (err == 0) {
                   opkg_message (conf, OPKG_NOTICE, "Inflating %s\n", url);
                   in = fopen (tmp_file_name, "r");
              if (err == 0) {
                   opkg_message (conf, OPKG_NOTICE, "Inflating %s\n", url);
                   in = fopen (tmp_file_name, "r");
@@ -242,7 +242,7 @@ static int opkg_update_cmd(opkg_conf_t *conf, int argc, char **argv)
                   unlink (tmp_file_name);
              }
          } else
                   unlink (tmp_file_name);
              }
          } else
-             err = opkg_download(conf, url, list_file_name);
+             err = opkg_download(conf, url, list_file_name, NULL, NULL);
          if (err) {
               failures++;
          } else {
          if (err) {
               failures++;
          } else {
@@ -266,7 +266,7 @@ static int opkg_update_cmd(opkg_conf_t *conf, int argc, char **argv)
 
          sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
 
 
          sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
 
-         err = opkg_download(conf, url, tmp_file_name);
+         err = opkg_download(conf, url, tmp_file_name, NULL, NULL);
          if (err) {
            failures++;
                opkg_message (conf, OPKG_NOTICE, "Signature check failed\n");
          if (err) {
            failures++;
                opkg_message (conf, OPKG_NOTICE, "Signature check failed\n");
index d994670dbd66912e81e9194511a74d88721b1546..428cad5ff82c54caab4c9a808dfe9f5f4c4c054a 100644 (file)
 #include "str_util.h"
 #include "opkg_defines.h"
 
 #include "str_util.h"
 #include "opkg_defines.h"
 
-opkg_download_progress_callback opkg_cb_download_progress = NULL;
-
-int
-curl_progress_func (char* url,
-                   double t, /* dltotal */
-                   double d, /* dlnow */
-                   double ultotal,
-                   double ulnow)
-{
-    int p = (t) ? d*100/t : 0;
-
-    if (opkg_cb_download_progress)
-    {
-       static int prev = -1;
-
-       /* don't report the same percentage multiple times
-        * (this can occur due to rounding) */
-       if (prev == p)
-           return 0;
-       prev = p;
-
-       opkg_cb_download_progress (p, url);
-       return 0;
-    }
-    return 0;
-}
-
-int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name)
+int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name, curl_progress_func cb, void *data)
 {
     int err = 0;
 
 {
     int err = 0;
 
@@ -110,9 +83,12 @@ int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name
     {
        curl_easy_setopt (curl, CURLOPT_URL, src);
        curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
     {
        curl_easy_setopt (curl, CURLOPT_URL, src);
        curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
-       curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0);
-       curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, src);
-       curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
+       curl_easy_setopt (curl, CURLOPT_NOPROGRESS, (cb == NULL));
+       if (cb)
+       {
+               curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, data);
+               curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, cb);
+       }
        curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
        if (conf->http_proxy || conf->ftp_proxy)
        {
        curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
        if (conf->http_proxy || conf->ftp_proxy)
        {
@@ -179,7 +155,7 @@ int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
 
     sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename);
 
 
     sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename);
 
-    err = opkg_download(conf, url, pkg->local_filename);
+    err = opkg_download(conf, url, pkg->local_filename, NULL, NULL);
     free(url);
 
     opkg_set_current_state (conf, OPKG_STATE_NONE, NULL);
     free(url);
 
     opkg_set_current_state (conf, OPKG_STATE_NONE, NULL);
@@ -204,7 +180,7 @@ int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **name
          char *file_base = basename(file_basec);
 
          sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
          char *file_base = basename(file_basec);
 
          sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
-         err = opkg_download(conf, url, tmp_file);
+         err = opkg_download(conf, url, tmp_file, NULL, NULL);
          if (err)
               return err;
 
          if (err)
               return err;
 
index 8b940e7253bef4677c51fadd4881a5a5b2a63d7f..39cd9a687321c6464975524c5a2b8a6831603e59 100644 (file)
 #include "opkg_conf.h"
 
 typedef void (*opkg_download_progress_callback)(int percent, char *url);
 #include "opkg_conf.h"
 
 typedef void (*opkg_download_progress_callback)(int percent, char *url);
+typedef int (*curl_progress_func)(void *data, double t, double d, double ultotal, double ulnow);
 
 
-int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name);
+
+int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name, curl_progress_func cb, void *data);
 int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir);
 /*
  * Downloads file from url, installs in package database, return package name. 
 int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir);
 /*
  * Downloads file from url, installs in package database, return package name.