]> granicus.if.org Git - vnstat/commitdiff
improve database import
authorTeemu Toivola <git@humdi.net>
Tue, 12 Aug 2014 20:23:57 +0000 (23:23 +0300)
committerTeemu Toivola <git@humdi.net>
Tue, 12 Aug 2014 20:23:57 +0000 (23:23 +0300)
src/dbaccess.c
tests/database_tests.c

index cbc039c6f471bef8f78d85aef7dc633ede1510b8..dfbfc8fad009f6be58582edd641cb69d9a61e7e4 100644 (file)
@@ -907,7 +907,7 @@ int importdb(const char *filename)
 {
        FILE *input;
        char line[512];
-       int i, count;
+       int i, count, linecount, scancount;
        uint64_t tempint;
        DAY day;
        MONTH month;
@@ -918,6 +918,7 @@ int importdb(const char *filename)
                return 0;
        }
 
+       linecount = 0;
        while (fgets(line, sizeof(line), input) != NULL) {
                if (debug) {
                        printf("parsing %s", line);
@@ -927,23 +928,26 @@ int importdb(const char *filename)
                        continue;
                }
 
-               sscanf(line, "version;%2d", &data.version);
-               sscanf(line, "active;%2d", &data.active);
-               sscanf(line, "interface;%31s", data.interface);
-               sscanf(line, "nick;%31s", data.nick);
+               scancount = 0;
+               scancount += sscanf(line, "version;%2d", &data.version);
+               scancount += sscanf(line, "active;%2d", &data.active);
+               scancount += sscanf(line, "interface;%31s", data.interface);
+               scancount += sscanf(line, "nick;%31s", data.nick);
                if (sscanf(line, "created;%20"PRIu64, &tempint)) {
                        data.created = (time_t)tempint;
+                       scancount++;
                }
                if (sscanf(line, "updated;%20"PRIu64, &tempint)) {
                        data.lastupdated = (time_t)tempint;
+                       scancount++;
                }
-               sscanf(line, "totalrx;%20"PRIu64, &data.totalrx);
-               sscanf(line, "totaltx;%20"PRIu64, &data.totaltx);
-               sscanf(line, "currx;%20"PRIu64, &data.currx);
-               sscanf(line, "curtx;%20"PRIu64, &data.curtx);
-               sscanf(line, "totalrxk;%10d", &data.totalrxk);
-               sscanf(line, "totaltxk;%10d", &data.totaltxk);
-               sscanf(line, "btime;%20"PRIu64, &data.btime);
+               scancount += sscanf(line, "totalrx;%20"PRIu64, &data.totalrx);
+               scancount += sscanf(line, "totaltx;%20"PRIu64, &data.totaltx);
+               scancount += sscanf(line, "currx;%20"PRIu64, &data.currx);
+               scancount += sscanf(line, "curtx;%20"PRIu64, &data.curtx);
+               scancount += sscanf(line, "totalrxk;%10d", &data.totalrxk);
+               scancount += sscanf(line, "totaltxk;%10d", &data.totaltxk);
+               scancount += sscanf(line, "btime;%20"PRIu64, &data.btime);
 
                count = sscanf(line, "d;%2d;%20"PRIu64";%20"PRIu64";%20"PRIu64";%10d;%10d;%2d",
                                &i, &tempint, &day.rx, &day.tx, &day.rxk, &day.txk, &day.used);
@@ -951,6 +955,7 @@ int importdb(const char *filename)
                        if (i >= 0 && i < (int)sizeof(data.day) / (int)sizeof(DAY)) {
                                day.date = (time_t)tempint;
                                data.day[i] = day;
+                               scancount++;
                        }
                }
 
@@ -960,6 +965,7 @@ int importdb(const char *filename)
                        if ( i >= 0 && i < (int)sizeof(data.month) / (int)sizeof(MONTH) ) {
                                month.month = (time_t)tempint;
                                data.month[i] = month;
+                               scancount++;
                        }
                }
 
@@ -969,6 +975,7 @@ int importdb(const char *filename)
                        if ( i >= 0 && i < (int)sizeof(data.top10) / (int)sizeof(DAY) ) {
                                day.date = (time_t)tempint;
                                data.top10[i] = day;
+                               scancount++;
                        }
                }
 
@@ -978,11 +985,16 @@ int importdb(const char *filename)
                        if ( i >= 0 && i < (int)sizeof(data.hour) / (int)sizeof(HOUR) ) {
                                hour.date = (time_t)tempint;
                                data.hour[i] = hour;
+                               scancount++;
                        }
                }
+
+               if (scancount) {
+                       linecount++;
+               }
        }
 
        fclose(input);
 
-       return 1;
+       return linecount;
 }
index 2e11d9cd578efb9dbff8c2c8dfca2bdd48edb075..0ac040fb0c2ac0aa4b7d307489cdcfb0f98b61bc 100644 (file)
@@ -818,7 +818,7 @@ START_TEST(importdb_can_parse_exported_database)
        dumpdb();
        memset(&data, '\0', sizeof(DATA));
 
-       ck_assert_int_eq(importdb(buffer), 1);
+       ck_assert_int_gt(importdb(buffer), 0);
 
        ck_assert_str_eq(data.interface, filleddb.interface);
        ck_assert_str_eq(data.nick, filleddb.nick);