From e9ed26cde63f8ca7607a010a518329339f8c02d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Draszik?= <git@andred.net>
Date: Mon, 26 Sep 2016 12:12:41 +0100
Subject: [PATCH] print progress in percent when downloading CVE db
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Upstream-Status: Pending
Signed-off-by: André Draszik <git@andred.net>
---
 src/library/fetch.c | 28 +++++++++++++++++++++++++++-
 src/library/fetch.h |  3 ++-
 src/update.c        | 16 ++++++++++++----
 3 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/src/library/fetch.c b/src/library/fetch.c
index 06d4b30..0fe6d76 100644
--- a/src/library/fetch.c
+++ b/src/library/fetch.c
@@ -37,13 +37,37 @@ static size_t write_func(void *ptr, size_t size, size_t nmemb, struct fetch_t *f
         return fwrite(ptr, size, nmemb, f->f);
 }
 
-FetchStatus fetch_uri(const char *uri, const char *target, bool verbose)
+struct percent_t {
+        unsigned int start;
+        unsigned int end;
+};
+
+static int progress_callback_new(void *ptr, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
+{
+        (void) ultotal;
+        (void) ulnow;
+
+        struct percent_t *percent = (struct percent_t *) ptr;
+
+        if (dltotal && percent && percent->end >= percent->start) {
+                unsigned int diff = percent->end - percent->start;
+                if (diff) {
+                        fprintf(stderr,"completed: "CURL_FORMAT_OFF_T"%%\r", percent->start + (diff * dlnow / dltotal));
+                }
+        }
+
+        return 0;
+}
+
+FetchStatus fetch_uri(const char *uri, const char *target, bool verbose,
+                      unsigned int start_percent, unsigned int end_percent)
 {
         FetchStatus ret = FETCH_STATUS_FAIL;
         CURLcode res;
         struct stat st;
         CURL *curl = NULL;
         struct fetch_t *f = NULL;
+        struct percent_t percent = { .start = start_percent, .end = end_percent };
 
         curl = curl_easy_init();
         if (!curl) {
@@ -67,6 +91,8 @@ FetchStatus fetch_uri(const char *uri, const char *target, bool verbose)
         }
         if (verbose) {
                 (void)curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
+                (void)curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &percent);
+                (void)curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback_new);
         }
         res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)write_func);
         if (res != CURLE_OK) {
diff --git a/src/library/fetch.h b/src/library/fetch.h
index 70c3779..4cce5d1 100644
--- a/src/library/fetch.h
+++ b/src/library/fetch.h
@@ -28,7 +28,8 @@ typedef enum {
  * @param verbose Whether to be verbose
  * @return A FetchStatus, indicating the operation taken
  */
-FetchStatus fetch_uri(const char *uri, const char *target, bool verbose);
+FetchStatus fetch_uri(const char *uri, const char *target, bool verbose,
+                      unsigned int this_percent, unsigned int next_percent);
 
 /**
  * Attempt to extract the given gzipped file
diff --git a/src/update.c b/src/update.c
index 30fbe96..eaeeefd 100644
--- a/src/update.c
+++ b/src/update.c
@@ -266,7 +266,8 @@ static inline void update_end(int fd, const char *update_fname, bool ok)
 }
 
 static int do_fetch_update(int year, const char *db_dir, CveDB *cve_db,
-                           bool db_exist, bool verbose)
+                           bool db_exist, bool verbose,
+                           unsigned int this_percent, unsigned int next_percent)
 {
         const char nvd_uri[] = URI_PREFIX;
         autofree(cve_string) *uri_meta = NULL;
@@ -330,14 +331,14 @@ refetch:
         }
 
         /* Fetch NVD META file */
-        st = fetch_uri(uri_meta->str, nvdcve_meta->str, verbose);
+        st = fetch_uri(uri_meta->str, nvdcve_meta->str, verbose, this_percent, this_percent);
         if (st == FETCH_STATUS_FAIL) {
                 fprintf(stderr, "Failed to fetch %s\n", uri_meta->str);
                 return -1;
         }
 
         /* Fetch NVD XML file */
-        st = fetch_uri(uri_data_gz->str, nvdcve_data_gz->str, verbose);
+        st = fetch_uri(uri_data_gz->str, nvdcve_data_gz->str, verbose, this_percent, next_percent);
         switch (st) {
         case FETCH_STATUS_FAIL:
                 fprintf(stderr, "Failed to fetch %s\n", uri_data_gz->str);
@@ -459,10 +460,17 @@ bool update_db(bool quiet, const char *db_file)
         for (int i = YEAR_START; i <= year+1; i++) {
                 int y = i > year ? -1 : i;
                 int rc;
+                unsigned int start_percent = ((i+0 - YEAR_START) * 100) / (year+2 - YEAR_START);
+                unsigned int end_percent = ((i+1 - YEAR_START) * 100) / (year+2 - YEAR_START);
 
-                rc = do_fetch_update(y, db_dir, cve_db, db_exist, !quiet);
+                if (!quiet)
+                        fprintf(stderr, "completed: %u%%\r", start_percent);
+                rc = do_fetch_update(y, db_dir, cve_db, db_exist, !quiet,
+                                     start_percent, end_percent);
                 switch (rc) {
                 case 0:
+                        if (!quiet)
+                                fprintf(stderr,"completed: %u%%\r", end_percent);
                         continue;
                 case ENOMEM:
                         goto oom;
-- 
2.9.3

