Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
update: Abstract NVD fetching and update into separate function
Browse files Browse the repository at this point in the history
To reduce one level of indendation and
make update_db() code more readable.

Signed-off-by: Sergey Popovich <[email protected]>
  • Loading branch information
Sergey Popovich authored and Serhii Popovych committed Jan 5, 2016
1 parent 1b1bee3 commit 5c6cf61
Showing 1 changed file with 54 additions and 44 deletions.
98 changes: 54 additions & 44 deletions src/update.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,59 @@ static inline void update_end(int fd, const char *update_fname, bool ok)
unlink(update_fname);
}

int update_required(const gchar *db_file)
static int do_fetch_update(int year, const gchar *db_dir, CveDB *cve_db,
bool db_exist, bool verbose)
{
return __update_required(db_file);
const gchar *nvd_uri = URI_PREFIX;
autofree(gchar) *uri = NULL;
autofree(gchar) *target = NULL;
autofree(gchar) *nvd_xml_gz = NULL;
FetchStatus st;
bool update = false;

target = __nvdcve_get_pname(year, db_dir, "xml.gz", &nvd_xml_gz);
if (!target)
return ENOMEM;

uri = g_strdup_printf("%s/%s", nvd_uri, nvd_xml_gz);
if (!uri)
return ENOMEM;

st = fetch_uri(uri, target, verbose);
switch (st) {
case FETCH_STATUS_FAIL:
fprintf(stderr, "Failed to fetch %s\n", uri);
return -1;
case FETCH_STATUS_UPDATE:
update = true;
break;
default:
if (verbose) {
fprintf(stderr, "Skipping: %s\n", nvd_xml_gz);
}
break;
}

if (update || !db_exist) {
/* Only load on updates */
if (!gunzip_file(target)) {
fprintf(stderr, "Unable to extract %s\n", target);
return -1;
}
if (!cve_db_load(cve_db, target)) {
fprintf(stderr, "\nUnable to find: %s\n", target);
return -1;
}
if (verbose) {
fprintf(stderr, "Loaded: %s\n", nvd_xml_gz);
}
}

return 0;
}

bool update_db(bool quiet, const gchar *db_file)
{
const gchar *nvd_uri = URI_PREFIX;
autofree(gchar) *db_dir = NULL;
autofree(CveDB) *cve_db = NULL;
autofree(GDateTime) *date = NULL;
Expand Down Expand Up @@ -212,49 +257,14 @@ bool update_db(bool quiet, const gchar *db_file)
year = g_date_time_get_year(date);

for (int i = YEAR_START; i <= year+1; i++) {
autofree(gchar) *uri = NULL;
autofree(gchar) *target = NULL;
autofree(gchar) *nvd_xml_gz = NULL;
FetchStatus st;
bool update = false;

target = __nvdcve_get_pname(i > year ? -1 : i, db_dir,
"xml.gz", &nvd_xml_gz);
if (!target)
goto oom;
int y = i > year ? -1 : i;
int rc;

uri = g_strdup_printf("%s/%s", nvd_uri, nvd_xml_gz);
if (!uri)
rc = do_fetch_update(y, db_dir, cve_db, db_exist, !quiet);
if (rc == ENOMEM)
goto oom;

st = fetch_uri(uri, target, !quiet);
switch (st) {
case FETCH_STATUS_FAIL:
fprintf(stderr, "Failed to fetch %s\n", uri);
goto end;
case FETCH_STATUS_UPDATE:
update = true;
break;
default:
if (!quiet) {
fprintf(stderr, "Skipping: %s\n", nvd_xml_gz);
}
break;
}
if (update || !db_exist) {
/* Only load on updates */
if (!gunzip_file(target)) {
fprintf(stderr, "Unable to extract %s\n", target);
goto end;
}
if (!cve_db_load(cve_db, target)) {
fprintf(stderr, "\nUnable to find: %s\n", target);
goto end;
}
if (!quiet) {
fprintf(stderr, "Loaded: %s\n", nvd_xml_gz);
}
}
if (rc)
goto end;
}

/* Make sure we always update access and modify time on
Expand Down

0 comments on commit 5c6cf61

Please sign in to comment.