blob: 8ea6f686e3f8f12b40f41fa135b229ffacf92b4e [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001From e9ed26cde63f8ca7607a010a518329339f8c02d3 Mon Sep 17 00:00:00 2001
2From: =?UTF-8?q?Andr=C3=A9=20Draszik?= <git@andred.net>
3Date: Mon, 26 Sep 2016 12:12:41 +0100
4Subject: [PATCH] print progress in percent when downloading CVE db
5MIME-Version: 1.0
6Content-Type: text/plain; charset=UTF-8
7Content-Transfer-Encoding: 8bit
8
9Upstream-Status: Pending
10Signed-off-by: André Draszik <git@andred.net>
11---
12 src/library/fetch.c | 28 +++++++++++++++++++++++++++-
13 src/library/fetch.h | 3 ++-
14 src/update.c | 16 ++++++++++++----
15 3 files changed, 41 insertions(+), 6 deletions(-)
16
17diff --git a/src/library/fetch.c b/src/library/fetch.c
18index 06d4b30..0fe6d76 100644
19--- a/src/library/fetch.c
20+++ b/src/library/fetch.c
21@@ -37,13 +37,37 @@ static size_t write_func(void *ptr, size_t size, size_t nmemb, struct fetch_t *f
22 return fwrite(ptr, size, nmemb, f->f);
23 }
24
25-FetchStatus fetch_uri(const char *uri, const char *target, bool verbose)
26+struct percent_t {
27+ unsigned int start;
28+ unsigned int end;
29+};
30+
31+static int progress_callback_new(void *ptr, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
32+{
33+ (void) ultotal;
34+ (void) ulnow;
35+
36+ struct percent_t *percent = (struct percent_t *) ptr;
37+
38+ if (dltotal && percent && percent->end >= percent->start) {
39+ unsigned int diff = percent->end - percent->start;
40+ if (diff) {
Brad Bishopd7bf8c12018-02-25 22:55:05 -050041+ fprintf(stderr,"completed: %"CURL_FORMAT_CURL_OFF_T"%%\r", percent->start + (diff * dlnow / dltotal));
Patrick Williamsc0f7c042017-02-23 20:41:17 -060042+ }
43+ }
44+
45+ return 0;
46+}
47+
48+FetchStatus fetch_uri(const char *uri, const char *target, bool verbose,
49+ unsigned int start_percent, unsigned int end_percent)
50 {
51 FetchStatus ret = FETCH_STATUS_FAIL;
52 CURLcode res;
53 struct stat st;
54 CURL *curl = NULL;
55 struct fetch_t *f = NULL;
56+ struct percent_t percent = { .start = start_percent, .end = end_percent };
57
58 curl = curl_easy_init();
59 if (!curl) {
60@@ -67,6 +91,8 @@ FetchStatus fetch_uri(const char *uri, const char *target, bool verbose)
61 }
62 if (verbose) {
63 (void)curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
64+ (void)curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &percent);
65+ (void)curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback_new);
66 }
67 res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)write_func);
68 if (res != CURLE_OK) {
69diff --git a/src/library/fetch.h b/src/library/fetch.h
70index 70c3779..4cce5d1 100644
71--- a/src/library/fetch.h
72+++ b/src/library/fetch.h
73@@ -28,7 +28,8 @@ typedef enum {
74 * @param verbose Whether to be verbose
75 * @return A FetchStatus, indicating the operation taken
76 */
77-FetchStatus fetch_uri(const char *uri, const char *target, bool verbose);
78+FetchStatus fetch_uri(const char *uri, const char *target, bool verbose,
79+ unsigned int this_percent, unsigned int next_percent);
80
81 /**
82 * Attempt to extract the given gzipped file
83diff --git a/src/update.c b/src/update.c
84index 30fbe96..eaeeefd 100644
85--- a/src/update.c
86+++ b/src/update.c
87@@ -266,7 +266,8 @@ static inline void update_end(int fd, const char *update_fname, bool ok)
88 }
89
90 static int do_fetch_update(int year, const char *db_dir, CveDB *cve_db,
91- bool db_exist, bool verbose)
92+ bool db_exist, bool verbose,
93+ unsigned int this_percent, unsigned int next_percent)
94 {
95 const char nvd_uri[] = URI_PREFIX;
96 autofree(cve_string) *uri_meta = NULL;
97@@ -330,14 +331,14 @@ refetch:
98 }
99
100 /* Fetch NVD META file */
101- st = fetch_uri(uri_meta->str, nvdcve_meta->str, verbose);
102+ st = fetch_uri(uri_meta->str, nvdcve_meta->str, verbose, this_percent, this_percent);
103 if (st == FETCH_STATUS_FAIL) {
104 fprintf(stderr, "Failed to fetch %s\n", uri_meta->str);
105 return -1;
106 }
107
108 /* Fetch NVD XML file */
109- st = fetch_uri(uri_data_gz->str, nvdcve_data_gz->str, verbose);
110+ st = fetch_uri(uri_data_gz->str, nvdcve_data_gz->str, verbose, this_percent, next_percent);
111 switch (st) {
112 case FETCH_STATUS_FAIL:
113 fprintf(stderr, "Failed to fetch %s\n", uri_data_gz->str);
114@@ -459,10 +460,17 @@ bool update_db(bool quiet, const char *db_file)
115 for (int i = YEAR_START; i <= year+1; i++) {
116 int y = i > year ? -1 : i;
117 int rc;
118+ unsigned int start_percent = ((i+0 - YEAR_START) * 100) / (year+2 - YEAR_START);
119+ unsigned int end_percent = ((i+1 - YEAR_START) * 100) / (year+2 - YEAR_START);
120
121- rc = do_fetch_update(y, db_dir, cve_db, db_exist, !quiet);
122+ if (!quiet)
123+ fprintf(stderr, "completed: %u%%\r", start_percent);
124+ rc = do_fetch_update(y, db_dir, cve_db, db_exist, !quiet,
125+ start_percent, end_percent);
126 switch (rc) {
127 case 0:
128+ if (!quiet)
129+ fprintf(stderr,"completed: %u%%\r", end_percent);
130 continue;
131 case ENOMEM:
132 goto oom;
133--
1342.9.3
135