mirror of
https://github.com/clearlinux/cve-check-tool.git
synced 2026-09-05 21:21:48 +00:00
update: Abstract NVD fetching and update into separate function
To reduce one level of indendation and make update_db() code more readable. Signed-off-by: Sergey Popovich <popovich_sergei@mail.ua>
This commit is contained in:
+67
-46
@@ -144,6 +144,64 @@ 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)
|
||||
{
|
||||
const char nvd_uri[] = URI_PREFIX;
|
||||
autofree(cve_string) *uri = NULL;
|
||||
autofree(cve_string) *target = NULL;
|
||||
autofree(cve_string) *nvd_xml_gz = NULL;
|
||||
FetchStatus st;
|
||||
bool update = false;
|
||||
|
||||
nvd_xml_gz = nvdcve_make_fname(year, "xml.gz");
|
||||
if (!nvd_xml_gz) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
target = cve_string_dup_printf("%s/%s", db_dir, nvd_xml_gz->str);
|
||||
if (!target) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
uri = cve_string_dup_printf("%s/%s", nvd_uri, nvd_xml_gz->str);
|
||||
if (!uri) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
st = fetch_uri(uri->str, target->str, verbose);
|
||||
switch (st) {
|
||||
case FETCH_STATUS_FAIL:
|
||||
fprintf(stderr, "Failed to fetch %s\n", uri->str);
|
||||
return -1;
|
||||
case FETCH_STATUS_UPDATE:
|
||||
update = true;
|
||||
break;
|
||||
default:
|
||||
if (verbose) {
|
||||
fprintf(stderr, "Skipping: %s\n", nvd_xml_gz->str);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (update || !db_exist) {
|
||||
/* Only load on updates */
|
||||
if (!gunzip_file(target->str)) {
|
||||
fprintf(stderr, "Unable to extract %s\n", target->str);
|
||||
return -1;
|
||||
}
|
||||
if (!cve_db_load(cve_db, target->str)) {
|
||||
fprintf(stderr, "\nUnable to find: %s\n", target->str);
|
||||
return -1;
|
||||
}
|
||||
if (verbose) {
|
||||
fprintf(stderr, "Loaded: %s\n", nvd_xml_gz->str);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool update_db(bool quiet, const char *db_file)
|
||||
{
|
||||
autofree(gchar) *db_dir = NULL;
|
||||
@@ -196,54 +254,17 @@ bool update_db(bool quiet, const char *db_file)
|
||||
year = g_date_time_get_year(date);
|
||||
|
||||
for (int i = YEAR_START; i <= year+1; i++) {
|
||||
autofree(cve_string) *uri = NULL;
|
||||
autofree(cve_string) *target = NULL;
|
||||
autofree(cve_string) *nvd_xml_gz = NULL;
|
||||
FetchStatus st;
|
||||
bool update = false;
|
||||
int y = i > year ? -1 : i;
|
||||
int rc;
|
||||
|
||||
nvd_xml_gz = nvdcve_make_fname(i > year ? -1 : i, "xml.gz");
|
||||
if (!nvd_xml_gz) {
|
||||
rc = do_fetch_update(y, db_dir, cve_db, db_exist, !quiet);
|
||||
switch (rc) {
|
||||
case 0:
|
||||
continue;
|
||||
case ENOMEM:
|
||||
goto oom;
|
||||
}
|
||||
|
||||
target = cve_string_dup_printf("%s/%s", db_dir, nvd_xml_gz->str);
|
||||
if (!target) {
|
||||
goto oom;
|
||||
}
|
||||
|
||||
uri = cve_string_dup_printf("%s/%s", nvd_uri, nvd_xml_gz->str);
|
||||
if (!uri) {
|
||||
goto oom;
|
||||
}
|
||||
|
||||
st = fetch_uri(uri->str, target->str, !quiet);
|
||||
switch (st) {
|
||||
case FETCH_STATUS_FAIL:
|
||||
fprintf(stderr, "Failed to fetch %s\n", uri->str);
|
||||
goto end;
|
||||
case FETCH_STATUS_UPDATE:
|
||||
update = true;
|
||||
break;
|
||||
default:
|
||||
if (!quiet) {
|
||||
fprintf(stderr, "Skipping: %s\n", nvd_xml_gz->str);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (update || !db_exist) {
|
||||
/* Only load on updates */
|
||||
if (!gunzip_file(target->str)) {
|
||||
fprintf(stderr, "Unable to extract %s\n", target->str);
|
||||
goto end;
|
||||
}
|
||||
if (!cve_db_load(cve_db, target->str)) {
|
||||
fprintf(stderr, "\nUnable to find: %s\n", target->str);
|
||||
goto end;
|
||||
}
|
||||
if (!quiet) {
|
||||
fprintf(stderr, "Loaded: %s\n", nvd_xml_gz->str);
|
||||
}
|
||||
default:
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user