From f68496e26d9ef9ee4d4c54d2b95400918786dc36 Mon Sep 17 00:00:00 2001 From: Teemu Toivola Date: Sun, 10 Aug 2014 00:59:28 +0300 Subject: [PATCH] reduce indentation --- src/cfg.c | 15 +- src/daemon.c | 69 +++++----- src/dbaccess.c | 304 ++++++++++++++++++++--------------------- src/dbcache.c | 155 +++++++++++---------- src/dbshow.c | 70 +++++----- tests/Makefile | 2 +- tests/database_tests.c | 19 +++ 7 files changed, 325 insertions(+), 309 deletions(-) diff --git a/src/cfg.c b/src/cfg.c index 07b47a0..c520d45 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -599,15 +599,14 @@ void ibwlist(void) if (p == NULL) { printf("ibw list is empty.\n"); + return; + } - } else { - - printf("ibw:\n"); - while (p != NULL) { - printf(" %2d: \"%s\" \"%d\"\n", i, p->interface, p->limit); - p = p->next; - i++; - } + printf("ibw:\n"); + while (p != NULL) { + printf(" %2d: \"%s\" \"%d\"\n", i, p->interface, p->limit); + p = p->next; + i++; } } diff --git a/src/daemon.c b/src/daemon.c index 8e62808..880ab29 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -521,23 +521,26 @@ int datalist_cacheget(DSTATE *s) void datalist_getifinfo(DSTATE *s) { - if (data.active) { - if (getifinfo(data.interface)) { - if (s->datalist->sync) { /* if --sync was used during startup */ - data.currx = ifinfo.rx; - data.curtx = ifinfo.tx; - s->datalist->sync = 0; - } else { - parseifinfo(0); - } - } else { - /* disable interface since we can't access its data */ - data.active = 0; - snprintf(errorstring, 512, "Interface \"%s\" not available, disabling.", data.interface); - printe(PT_Info); - } - } else if (debug) { - printf("d: interface is disabled\n"); + if (!data.active) { + if (debug) + printf("d: interface is disabled\n"); + return; + } + + if (!getifinfo(data.interface)) { + /* disable interface since we can't access its data */ + data.active = 0; + snprintf(errorstring, 512, "Interface \"%s\" not available, disabling.", data.interface); + printe(PT_Info); + return; + } + + if (s->datalist->sync) { /* if --sync was used during startup */ + data.currx = ifinfo.rx; + data.curtx = ifinfo.tx; + s->datalist->sync = 0; + } else { + parseifinfo(0); } } @@ -546,25 +549,25 @@ int datalist_timevalidation(DSTATE *s) if (s->current >= data.lastupdated) { data.lastupdated = s->current; cacheupdate(); - } else { - /* skip update if previous update is less than a day in the future */ - /* otherwise exit with error message since the clock is problably messed */ - if (data.lastupdated > (s->current+86400)) { - snprintf(errorstring, 512, "Interface \"%s\" has previous update date too much in the future, exiting. (%d / %d)", data.interface, (unsigned int)data.lastupdated, (unsigned int)s->current); - printe(PT_Error); + return 1; + } - /* clean daemon stuff before exit */ - if (s->rundaemon && !debug) { - close(pidfile); - unlink(cfg.pidfile); - } - ibwflush(); - exit(EXIT_FAILURE); - } else { - return 0; + /* skip update if previous update is less than a day in the future */ + /* otherwise exit with error message since the clock is problably messed */ + if (data.lastupdated > (s->current+86400)) { + snprintf(errorstring, 512, "Interface \"%s\" has previous update date too much in the future, exiting. (%d / %d)", data.interface, (unsigned int)data.lastupdated, (unsigned int)s->current); + printe(PT_Error); + + /* clean daemon stuff before exit */ + if (s->rundaemon && !debug) { + close(pidfile); + unlink(cfg.pidfile); } + ibwflush(); + exit(EXIT_FAILURE); } - return 1; + + return 0; } int datalist_writedb(DSTATE *s) diff --git a/src/dbaccess.c b/src/dbaccess.c index b883b60..bbf29b4 100644 --- a/src/dbaccess.c +++ b/src/dbaccess.c @@ -5,83 +5,87 @@ int readdb(const char *iface, const char *dirname) { FILE *db; char file[512], backup[512]; - int newdb=0; snprintf(file, 512, "%s/%s", dirname, iface); snprintf(backup, 512, "%s/.%s", dirname, iface); - if ((db=fopen(file,"r"))!=NULL) { + if ((db=fopen(file,"r"))==NULL) { + snprintf(errorstring, 512, "Unable to read database \"%s\": %s", file, strerror(errno)); + printe(PT_Error); - /* lock file */ - if (!lockdb(fileno(db), 0)) { - fclose(db); - return -1; + /* create new database template */ + initdb(); + strncpy_nt(data.interface, iface, 32); + strncpy_nt(data.nick, data.interface, 32); + return 1; + } + + /* lock file */ + if (!lockdb(fileno(db), 0)) { + fclose(db); + return -1; + } + + if (fread(&data,sizeof(DATA),1,db)==0) { + data.version=-1; + if (debug) { + printf("db: Database read failed for file \"%s\".\n", file); + } + } else { + if (debug) { + printf("db: Database loaded for interface \"%s\"...\n", data.interface); } + } - if (fread(&data,sizeof(DATA),1,db)==0) { - data.version=-1; - if (debug) { - printf("db: Database read failed for file \"%s\".\n", file); - } - } else { - if (debug) { - printf("db: Database loaded for interface \"%s\"...\n", data.interface); - } + /* convert old database to new format if necessary */ + if (data.versionDBVERSION) { - snprintf(errorstring, 512, "Downgrading database \"%s\" (v%d) is not supported.", file, data.version); - printe(PT_Error); - fclose(db); - - if (noexit) { - return -1; - } else { - exit(EXIT_FAILURE); - } + snprintf(errorstring, 512, "Database possibly corrupted, using backup instead."); + printe(PT_Info); } + } else if (data.version>DBVERSION) { + snprintf(errorstring, 512, "Downgrading database \"%s\" (v%d) is not supported.", file, data.version); + printe(PT_Error); fclose(db); - if (strcmp(data.interface,iface)) { - snprintf(errorstring, 512, "Warning:\nThe previous interface for this file was \"%s\".",data.interface); - printe(PT_Multiline); - snprintf(errorstring, 512, "It has now been replaced with \"%s\".",iface); - printe(PT_Multiline); - snprintf(errorstring, 512, "You can ignore this message if you renamed the filename."); - printe(PT_Multiline); - snprintf(errorstring, 512, "Interface name mismatch, renamed \"%s\" -> \"%s\"", data.interface, iface); - printe(PT_ShortMultiline); - if (strcmp(data.interface, data.nick)==0) { - strncpy_nt(data.nick, iface, 32); - } - strncpy_nt(data.interface, iface, 32); + if (noexit) { + return -1; + } else { + exit(EXIT_FAILURE); } - } else { - snprintf(errorstring, 512, "Unable to read database \"%s\": %s", file, strerror(errno)); - printe(PT_Error); + } - newdb=1; - initdb(); + fclose(db); + + if (strcmp(data.interface,iface)) { + snprintf(errorstring, 512, "Warning:\nThe previous interface for this file was \"%s\".",data.interface); + printe(PT_Multiline); + snprintf(errorstring, 512, "It has now been replaced with \"%s\".",iface); + printe(PT_Multiline); + snprintf(errorstring, 512, "You can ignore this message if you renamed the filename."); + printe(PT_Multiline); + snprintf(errorstring, 512, "Interface name mismatch, renamed \"%s\" -> \"%s\"", data.interface, iface); + printe(PT_ShortMultiline); + if (strcmp(data.interface, data.nick)==0) { + strncpy_nt(data.nick, iface, 32); + } strncpy_nt(data.interface, iface, 32); - strncpy_nt(data.nick, data.interface, 32); } - return newdb; + + return 0; } void initdb(void) @@ -223,38 +222,37 @@ int writedb(const char *iface, const char *dirname, int newdb) /* make sure version stays correct */ data.version=DBVERSION; - if ((db=fopen(file,"w"))!=NULL) { + if ((db=fopen(file,"w"))==NULL) { + snprintf(errorstring, 512, "Unable to open database \"%s\" for writing: %s", file, strerror(errno)); + printe(PT_Error); + return 0; + } - /* lock file */ - if (!lockdb(fileno(db), 1)) { - fclose(db); - return 0; - } + /* lock file */ + if (!lockdb(fileno(db), 1)) { + fclose(db); + return 0; + } - /* update timestamp when not merging */ - if (newdb!=2) { - data.lastupdated=time(NULL); - } + /* update timestamp when not merging */ + if (newdb!=2) { + data.lastupdated=time(NULL); + } - if (fwrite(&data,sizeof(DATA),1,db)==0) { - snprintf(errorstring, 512, "Unable to write database \"%s\": %s", file, strerror(errno)); - printe(PT_Error); - fclose(db); - return 0; - } else { - if (debug) { - printf("db: Database \"%s\" saved.\n", file); - } - fclose(db); - if ((newdb) && (noexit==0)) { - snprintf(errorstring, 512, "-> A new database has been created."); - printe(PT_Info); - } - } - } else { - snprintf(errorstring, 512, "Unable to open database \"%s\" for writing: %s", file, strerror(errno)); + if (fwrite(&data,sizeof(DATA),1,db)==0) { + snprintf(errorstring, 512, "Unable to write database \"%s\": %s", file, strerror(errno)); printe(PT_Error); + fclose(db); return 0; + } else { + if (debug) { + printf("db: Database \"%s\" saved.\n", file); + } + fclose(db); + if ((newdb) && (noexit==0)) { + snprintf(errorstring, 512, "-> A new database has been created."); + printe(PT_Info); + } } return 1; @@ -561,50 +559,52 @@ int lockdb(int fd, int dbwrite) int operation, locktry=1; /* lock only if configured to do so */ - if (cfg.flock) { + if (!cfg.flock) { + return 1; + } - if (dbwrite) { - operation = LOCK_EX|LOCK_NB; - } else { - operation = LOCK_SH|LOCK_NB; - } + if (dbwrite) { + operation = LOCK_EX|LOCK_NB; + } else { + operation = LOCK_SH|LOCK_NB; + } - /* try locking file */ - while (flock(fd, operation)!=0) { + /* try locking file */ + while (flock(fd, operation)!=0) { - if (debug) { - printf("db: Database access locked (%d, %d)\n", dbwrite, locktry); - } + if (debug) { + printf("db: Database access locked (%d, %d)\n", dbwrite, locktry); + } - /* give up if lock can't be obtained */ - if (locktry>=LOCKTRYLIMIT) { - if (dbwrite) { - snprintf(errorstring, 512, "Locking database file for write failed for %d tries:\n%s (%d)", locktry, strerror(errno), errno); - } else { - snprintf(errorstring, 512, "Locking database file for read failed for %d tries:\n%s (%d)", locktry, strerror(errno), errno); - } - printe(PT_Error); - return 0; + /* give up if lock can't be obtained */ + if (locktry>=LOCKTRYLIMIT) { + if (dbwrite) { + snprintf(errorstring, 512, "Locking database file for write failed for %d tries:\n%s (%d)", locktry, strerror(errno), errno); + } else { + snprintf(errorstring, 512, "Locking database file for read failed for %d tries:\n%s (%d)", locktry, strerror(errno), errno); } + printe(PT_Error); + return 0; + } - /* someone else has the lock */ - if (errno==EWOULDBLOCK) { - sleep(1); + /* someone else has the lock */ + if (errno==EWOULDBLOCK) { + sleep(1); - /* real error */ + /* real error */ + } else { + if (dbwrite) { + snprintf(errorstring, 512, "Locking database file for write failed:\n%s (%d)", strerror(errno), errno); } else { - if (dbwrite) { - snprintf(errorstring, 512, "Locking database file for write failed:\n%s (%d)", strerror(errno), errno); - } else { - snprintf(errorstring, 512, "Locking database file for read failed:\n%s (%d)", strerror(errno), errno); - } - printe(PT_Error); - return 0; + snprintf(errorstring, 512, "Locking database file for read failed:\n%s (%d)", strerror(errno), errno); } - - locktry++; + printe(PT_Error); + return 0; } + + locktry++; } + return 1; } diff --git a/src/dbcache.c b/src/dbcache.c index dce8a5b..9464d2c 100644 --- a/src/dbcache.c +++ b/src/dbcache.c @@ -51,36 +51,35 @@ datanode *cacheremove(const char *iface) if (p == NULL) { return NULL; - } else { + } + + /* handle list head remove */ + if (strcmp(p->data.interface, iface)==0) { + dataptr = p->next; + if (debug) { + printf("cache: h %s removed\n", iface); + } + free(p); + return dataptr; + } + + o = p; + p = p->next; + + /* handle other locations */ + while (p != NULL) { - /* handle list head remove */ if (strcmp(p->data.interface, iface)==0) { - dataptr = p->next; + o->next = p->next; if (debug) { - printf("cache: h %s removed\n", iface); + printf("cache: %s removed\n", iface); } free(p); - return dataptr; + return o->next; } o = p; p = p->next; - - /* handle other locations */ - while (p != NULL) { - - if (strcmp(p->data.interface, iface)==0) { - o->next = p->next; - if (debug) { - printf("cache: %s removed\n", iface); - } - free(p); - return o->next; - } - - o = p; - p = p->next; - } } return NULL; @@ -137,17 +136,16 @@ void cacheshow(void) if (p == NULL) { printf("cache: empty.\n"); + return; + } - } else { - - printf("cache:"); - while (p != NULL) { - printf(" %d. \"%s\" ", i, p->data.interface); - p = p->next; - i++; - } - printf("\n"); + printf("cache:"); + while (p != NULL) { + printf(" %d. \"%s\" ", i, p->data.interface); + p = p->next; + i++; } + printf("\n"); } void cachestatus(void) @@ -158,26 +156,23 @@ void cachestatus(void) snprintf(buffer, b, "Monitoring: "); - if (p != NULL) { - - while (p != NULL) { - if ((b+strlen(p->data.interface)+16) < 508) { - bwlimit = ibwget(p->data.interface); - if (bwlimit < 0) { - snprintf(bwtemp, 16, " (no limit) "); - } else { - snprintf(bwtemp, 16, " (%d Mbit) ", bwlimit); - } - strncat(buffer, p->data.interface, strlen(p->data.interface)); - strncat(buffer, bwtemp, strlen(bwtemp)); - b += strlen(p->data.interface) + strlen(bwtemp); + while (p != NULL) { + if ((b+strlen(p->data.interface)+16) < 508) { + bwlimit = ibwget(p->data.interface); + if (bwlimit < 0) { + snprintf(bwtemp, 16, " (no limit) "); } else { - strcat(buffer, "..."); - break; + snprintf(bwtemp, 16, " (%d Mbit) ", bwlimit); } - count++; - p = p->next; + strncat(buffer, p->data.interface, strlen(p->data.interface)); + strncat(buffer, bwtemp, strlen(bwtemp)); + b += strlen(p->data.interface) + strlen(bwtemp); + } else { + strcat(buffer, "..."); + break; } + count++; + p = p->next; } if (count) { @@ -284,47 +279,49 @@ uint32_t dbcheck(uint32_t dbhash, int *forcesave) newhash = simplehash(ifacelist, (int)strlen(ifacelist)); - /* search for changes if hash doesn't match */ - if (newhash!=dbhash) { + if (newhash == dbhash) { + free(ifacelist); + return newhash; + } - if (debug) { - printf("ifacelist changed: '%s' %u <> %u\n", ifacelist, dbhash, newhash); - } + /* search for changes if hash doesn't match */ + if (debug) { + printf("ifacelist changed: '%s' %u <> %u\n", ifacelist, dbhash, newhash); + } - while (p != NULL) { + while (p != NULL) { - if (p->filled) { - found = offset = 0; + if (p->filled) { + found = offset = 0; - while (offset <= (int)strlen(ifacelist)) { - sscanf(ifacelist+offset, "%31s", interface); - if (strcmp(p->data.interface, interface)==0) { - found = 1; - break; - } - offset += (int)strlen(interface)+1; + while (offset <= (int)strlen(ifacelist)) { + sscanf(ifacelist+offset, "%31s", interface); + if (strcmp(p->data.interface, interface)==0) { + found = 1; + break; } + offset += (int)strlen(interface)+1; + } - if (p->data.active==1 && found==0) { - p->data.active = 0; - p->data.currx = p->data.curtx = 0; - if (cfg.savestatus) { - *forcesave = 1; - } - snprintf(errorstring, 512, "Interface \"%s\" disabled.", p->data.interface); - printe(PT_Info); - } else if (p->data.active==0 && found==1) { - p->data.active = 1; - p->data.currx = p->data.curtx = 0; - if (cfg.savestatus) { - *forcesave = 1; - } - snprintf(errorstring, 512, "Interface \"%s\" enabled.", p->data.interface); - printe(PT_Info); + if (p->data.active==1 && found==0) { + p->data.active = 0; + p->data.currx = p->data.curtx = 0; + if (cfg.savestatus) { + *forcesave = 1; } + snprintf(errorstring, 512, "Interface \"%s\" disabled.", p->data.interface); + printe(PT_Info); + } else if (p->data.active==0 && found==1) { + p->data.active = 1; + p->data.currx = p->data.curtx = 0; + if (cfg.savestatus) { + *forcesave = 1; + } + snprintf(errorstring, 512, "Interface \"%s\" enabled.", p->data.interface); + printe(PT_Info); } - p = p->next; } + p = p->next; } free(ifacelist); diff --git a/src/dbshow.c b/src/dbshow.c index ce43e74..d3398bf 100644 --- a/src/dbshow.c +++ b/src/dbshow.c @@ -5,43 +5,41 @@ void showdb(int qmode) { if (data.totalrx+data.totaltx==0 && data.totalrxk+data.totaltxk==0 && qmode!=4) { - printf(" %s: Not enough data available yet.\n", data.interface); - - } else { - - switch(qmode) { - case 0: - showsummary(); - break; - case 1: - showdays(); - break; - case 2: - showmonths(); - break; - case 3: - showtop(); - break; - case 4: - dumpdb(); - break; - case 5: - showshort(); - break; - case 6: - showweeks(); - break; - case 7: - showhours(); - break; - case 9: - showoneline(); - break; - default: - printf("Error: Not such query mode: %d\n", qmode); - break; - } + return; + } + + switch(qmode) { + case 0: + showsummary(); + break; + case 1: + showdays(); + break; + case 2: + showmonths(); + break; + case 3: + showtop(); + break; + case 4: + dumpdb(); + break; + case 5: + showshort(); + break; + case 6: + showweeks(); + break; + case 7: + showhours(); + break; + case 9: + showoneline(); + break; + default: + printf("Error: Not such query mode: %d\n", qmode); + break; } } diff --git a/tests/Makefile b/tests/Makefile index d5b60e8..9368b81 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -27,7 +27,7 @@ vnstat_tests: $(OBJS) vnstat_tests.o: vnstat_tests.c vnstat_tests.h common_tests.h database_tests.h common.h common_tests.o: common_tests.c common_tests.h vnstat_tests.h common.h dbaccess.h cfg.h -database_tests.o: database_tests.c database_tests.h vnstat_tests.h dbaccess.h dbcache.h cfg.h +database_tests.o: database_tests.c database_tests.h vnstat_tests.h common.h ifinfo.h dbaccess.h dbcache.h cfg.h config_tests.o: config_tests.c config_tests.h vnstat_tests.h common.h cfg.h ifinfo_tests.o: ifinfo_tests.c ifinfo_tests.h vnstat_tests.h common.h ifinfo.h dbaccess.h misc.h cfg.h misc_tests.o: misc_tests.c misc_tests.h vnstat_tests.h common.h misc.h diff --git a/tests/database_tests.c b/tests/database_tests.c index fcc5b95..cd82a75 100644 --- a/tests/database_tests.c +++ b/tests/database_tests.c @@ -1,6 +1,7 @@ #include "vnstat_tests.h" #include "database_tests.h" #include "common.h" +#include "ifinfo.h" #include "dbaccess.h" #include "dbcache.h" #include "cfg.h" @@ -715,6 +716,23 @@ START_TEST(dbcheck_with_empty_cache) } END_TEST +START_TEST(dbcheck_with_no_changes_in_iflist) +{ + int forcesave = 0; + uint32_t ifhash; + char *ifacelist; + + ck_assert_int_eq(remove_directory(TESTDIR), 1); + fake_proc_net_dev("w", "ethsomething", 1, 2, 3, 4); + fake_proc_net_dev("a", "ethelse", 5, 6, 7, 8); + ck_assert_int_ne(getiflist(&ifacelist), 0); + ifhash = simplehash(ifacelist, (int)strlen(ifacelist)); + + ck_assert_int_eq(dbcheck(ifhash, &forcesave), ifhash); + ck_assert_int_eq(forcesave, 0); +} +END_TEST + START_TEST(dbcheck_with_filled_cache) { int forcesave = 0; @@ -790,6 +808,7 @@ void add_database_tests(Suite *s) tcase_add_test(tc_db, validatedb_with_top10_use); tcase_add_test(tc_db, dbcheck_with_no_interfaces); tcase_add_test(tc_db, dbcheck_with_empty_cache); + tcase_add_test(tc_db, dbcheck_with_no_changes_in_iflist); tcase_add_test(tc_db, dbcheck_with_filled_cache); suite_add_tcase(s, tc_db); } -- 2.40.0