From: Teemu Toivola Date: Sun, 2 Mar 2014 20:59:55 +0000 (+0200) Subject: extract parts of vnstat main to separate functions X-Git-Tag: v1.12~79 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dfb3cd0e75e5c8ff4dba7e74f20c23a2f8429d6d;p=vnstat extract parts of vnstat main to separate functions --- diff --git a/src/Makefile b/src/Makefile index 5fe15cb..60d9ee8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -24,7 +24,7 @@ ifinfo.o: ifinfo.c ifinfo.h common.h dbaccess.h misc.h cfg.h traffic.o: traffic.c traffic.h common.h ifinfo.h misc.h dbxml.o: dbxml.c dbxml.h common.h dbshow.o: dbshow.c dbshow.h misc.h common.h -dbaccess.o: dbaccess.c dbaccess.h common.h +dbaccess.o: dbaccess.c dbaccess.h misc.h common.h dbmerge.o: dbmerge.c dbmerge.h dbaccess.h common.h dbcache.o: dbcache.c dbcache.h dbaccess.h common.h ifinfo.h common.o: common.c common.h diff --git a/src/dbaccess.c b/src/dbaccess.c index a657630..1819e2f 100644 --- a/src/dbaccess.c +++ b/src/dbaccess.c @@ -1,5 +1,6 @@ #include "common.h" #include "dbaccess.h" +#include "misc.h" int readdb(const char *iface, const char *dirname) { @@ -762,3 +763,41 @@ void rotatemonths(void) printf("db: Months rotated. Current month: \"%d\".\n", d->tm_mon+1); } } + +void cleartop10(const char *iface, const char *dirname) +{ + int i; + + if (readdb(iface, dirname)!=0) { + exit(EXIT_FAILURE); + } + + for (i=0; i<=9; i++) { + data.top10[i].rx=data.top10[i].tx=0; + data.top10[i].rxk=data.top10[i].txk=0; + data.top10[i].used=0; + } + + writedb(iface, dirname, 0); + printf("Top10 cleared for interface \"%s\".\n", data.interface); +} + +void rebuilddbtotal(const char *iface, const char *dirname) +{ + int i; + + if (readdb(iface, dirname)!=0) { + exit(EXIT_FAILURE); + } + + data.totalrx=data.totaltx=data.totalrxk=data.totaltxk=0; + for (i=0; i<=11; i++) { + if (data.month[i].used) { + addtraffic(&data.totalrx, &data.totalrxk, data.month[i].rx, data.month[i].rxk); + addtraffic(&data.totaltx, &data.totaltxk, data.month[i].tx, data.month[i].txk); + } + } + + writedb(iface, dirname, 0); + printf("Total transfer rebuild completed for interface \"%s\".\n", data.interface); +} diff --git a/src/dbaccess.h b/src/dbaccess.h index 956b875..928bb00 100644 --- a/src/dbaccess.h +++ b/src/dbaccess.h @@ -12,6 +12,8 @@ int removedb(const char *iface, const char *dirname); void cleanhours(void); void rotatedays(void); void rotatemonths(void); +void cleartop10(const char *iface, const char *dirname); +void rebuilddbtotal(const char *iface, const char *dirname); /* version 1.0 database format aka db v1 */ diff --git a/src/vnstat.c b/src/vnstat.c index 15bc5f2..deb6d5c 100644 --- a/src/vnstat.c +++ b/src/vnstat.c @@ -457,16 +457,7 @@ int main(int argc, char *argv[]) { return 1; } if (force) { - readdb(interface, dirname); - - for (i=0; i<=9; i++) { - data.top10[i].rx=data.top10[i].tx=0; - data.top10[i].rxk=data.top10[i].txk=0; - data.top10[i].used=0; - } - - writedb(interface, dirname, 0); - printf("Top10 cleared for interface \"%s\".\n", data.interface); + cleartop10(interface, dirname); query=0; } else { printf("Warning:\nThe current option would clear the top10 for \"%s\".\n", interface); @@ -482,18 +473,7 @@ int main(int argc, char *argv[]) { return 1; } if (force) { - readdb(interface, dirname); - - data.totalrx=data.totaltx=data.totalrxk=data.totaltxk=0; - for (i=0; i<=11; i++) { - if (data.month[i].used) { - addtraffic(&data.totalrx, &data.totalrxk, data.month[i].rx, data.month[i].rxk); - addtraffic(&data.totaltx, &data.totaltxk, data.month[i].tx, data.month[i].txk); - } - } - - writedb(interface, dirname, 0); - printf("Total transfer rebuild completed for interface \"%s\".\n", data.interface); + rebuilddbtotal(interface, dirname); query=0; } else { printf("Warning:\nThe current option would rebuild total tranfers for \"%s\".\n", interface); diff --git a/tests/Makefile b/tests/Makefile index d4fd93a..477e5fe 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -28,7 +28,7 @@ misc_tests.o: misc_tests.c misc_tests.h vnstat_tests.h common.h misc.h common.o: common.c common.h ifinfo.o: ifinfo.c ifinfo.h common.h dbaccess.h misc.h cfg.h -dbaccess.o: dbaccess.c dbaccess.h common.h +dbaccess.o: dbaccess.c dbaccess.h misc.h common.h dbcache.o: dbcache.c dbcache.h dbaccess.h common.h ifinfo.h cfg.o: cfg.c cfg.h common.h misc.o: misc.c misc.h common.h