END_TEST START_TEST(rebuilddbtotal_rebuilds_total) { int i; initdb(); suppress_output(); disable_logprints(); cfg.flock = 1; strcpy(data.interface, "ethtest"); strcpy(data.nick, "ethtest"); data.totalrx = 1234; data.totaltx = 1234; for (i=0; i<12; i++) { data.month[i].rx = 1; data.month[i].tx = 2; data.month[i].used = 1; } ck_assert_int_eq(clean_testdbdir(), 1); ck_assert_int_eq(writedb("ethtest", TESTDBDIR, 1), 1); rebuilddbtotal("ethtest", TESTDBDIR); ck_assert_int_eq(check_dbfile_exists("ethtest", sizeof(DATA)), 1); ck_assert_int_eq(check_dbfile_exists(".ethtest", sizeof(DATA)), 1); ck_assert_int_eq(data.totalrx, 12); ck_assert_int_eq(data.totaltx, 24); }
int datalist_writedb(DSTATE *s) { if (!s->dodbsave) { return 1; } if (!checkdb(s->datalist->data.interface, s->dirname)) { return 0; } if (spacecheck(s->dirname)) { if (writedb(s->datalist->data.interface, s->dirname, 0)) { if (!s->dbsaved) { snprintf(errorstring, 512, "Database write possible again."); printe(PT_Info); s->dbsaved = 1; } } else { if (s->dbsaved) { snprintf(errorstring, 512, "Unable to write database, continuing with cached data."); printe(PT_Error); s->dbsaved = 0; } } } else { /* show freespace error only once */ if (s->dbsaved) { snprintf(errorstring, 512, "Free diskspace check failed, unable to write database, continuing with cached data."); printe(PT_Error); s->dbsaved = 0; } } return 1; }
static void cleanupdates(void) { struct dirent **cdlist; int cdn, i; parsedb(statusfile, pdb_weakclassification, NULL,NULL,NULL); *updatefnrest = '\0'; updateslength= -1; cdn= scandir(updatefnbuf, &cdlist, &ulist_select, alphasort); if (cdn == -1) ohshite(_("cannot scan updates directory `%.255s'"),updatefnbuf); if (cdn) { for (i=0; i<cdn; i++) { strcpy(updatefnrest, cdlist[i]->d_name); parsedb(updatefnbuf, pdb_weakclassification, NULL,NULL,NULL); if (cstatus < msdbrw_write) free(cdlist[i]); } if (cstatus >= msdbrw_write) { writedb(statusfile,0,1); for (i=0; i<cdn; i++) { strcpy(updatefnrest, cdlist[i]->d_name); if (unlink(updatefnbuf)) ohshite(_("failed to remove incorporated update file %.255s"),updatefnbuf); free(cdlist[i]); } } } free(cdlist); nextupdate= 0; }
END_TEST START_TEST(cleartop10_clears_top10) { int i; initdb(); suppress_output(); disable_logprints(); cfg.flock = 1; strcpy(data.interface, "ethtest"); strcpy(data.nick, "ethtest"); for (i=0; i<10; i++) { data.top10[i].rx = 1; data.top10[i].tx = 1; data.top10[i].used = 1; } ck_assert_int_eq(clean_testdbdir(), 1); ck_assert_int_eq(writedb("ethtest", TESTDBDIR, 1), 1); cleartop10("ethtest", TESTDBDIR); ck_assert_int_eq(check_dbfile_exists("ethtest", sizeof(DATA)), 1); ck_assert_int_eq(check_dbfile_exists(".ethtest", sizeof(DATA)), 1); for (i=0; i<10; i++) { ck_assert_int_eq(data.top10[i].rx, 0); ck_assert_int_eq(data.top10[i].tx, 0); ck_assert_int_eq(data.top10[i].rxk, 0); ck_assert_int_eq(data.top10[i].txk, 0); ck_assert_int_eq(data.top10[i].used, 0); } }
void modstatdb_checkpoint(void) { int i; assert(cstatus >= msdbrw_write); writedb(statusfile,0,1); for (i=0; i<nextupdate; i++) { sprintf(updatefnrest, IMPORTANTFMT, i); assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */ if (unlink(updatefnbuf)) ohshite(_("failed to remove my own update file %.255s"),updatefnbuf); } nextupdate= 0; }
END_TEST START_TEST(readdb_with_existing_dbfile_and_max_name_length) { initdb(); disable_logprints(); cfg.flock = 1; strcpy(data.interface, "1234567890123456789012345678901"); ck_assert_int_eq(clean_testdbdir(), 1); ck_assert_int_eq(writedb("1234567890123456789012345678901", TESTDBDIR, 1), 1); ck_assert_int_eq(check_dbfile_exists("1234567890123456789012345678901", sizeof(DATA)), 1); strcpy(data.interface, "none"); ck_assert_int_eq(readdb("1234567890123456789012345678901", TESTDBDIR), 0); ck_assert_str_eq(data.interface, "1234567890123456789012345678901"); }
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 modstatdb_checkpoint(void) { int i; assert(cstatus >= msdbrw_write); writedb(statusfile, wdb_must_sync); for (i=0; i<nextupdate; i++) { sprintf(updatefnrest, IMPORTANTFMT, i); /* Have we made a real mess? */ assert(strlen(updatefnrest) <= IMPORTANTMAXLEN); if (unlink(updatefnbuf)) ohshite(_("failed to remove my own update file %.255s"),updatefnbuf); } dir_sync_path(updatesdir); nextupdate= 0; }
/* flush cached data to disk and free all memory allocted for it */ void cacheflush(const char *dirname) { datanode *f, *p = dataptr; while (p != NULL) { f = p; p = p->next; /* write data to file if needed */ if (f->filled && dirname!=NULL) { memcpy(&data, &f->data, sizeof(data)); writedb(f->data.iface.interface, dirname, 0); } free(f); } dataptr = NULL; }
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); }
END_TEST START_TEST(readdb_with_existing_dbfile_with_rename) { initdb(); disable_logprints(); cfg.flock = 1; strcpy(data.interface, "ethtest"); strcpy(data.nick, "ethtest"); ck_assert_int_eq(clean_testdbdir(), 1); ck_assert_int_eq(writedb("ethtest2", TESTDBDIR, 1), 1); ck_assert_int_eq(check_dbfile_exists("ethtest2", sizeof(DATA)), 1); strcpy(data.interface, "none"); strcpy(data.nick, "none"); ck_assert_int_eq(readdb("ethtest2", TESTDBDIR), 0); ck_assert_str_eq(data.interface, "ethtest2"); ck_assert_str_eq(data.nick, "ethtest2"); }
void modstatdb_shutdown(void) { if (cflags >= msdbrw_available_write) writedb(availablefile, wdb_dump_available); switch (cstatus) { case msdbrw_write: modstatdb_checkpoint(); /* Tidy up a bit, but don't worry too much about failure. */ fclose(importanttmp); unlink(importanttmpfile); varbuf_destroy(&uvb); /* Fall through. */ case msdbrw_needsuperuserlockonly: modstatdb_unlock(); default: break; } modstatdb_done(); }
void modstatdb_shutdown(void) { const struct fni *fnip; switch (cstatus) { case msdbrw_write: modstatdb_checkpoint(); writedb(availablefile,1,0); /* tidy up a bit, but don't worry too much about failure */ fclose(importanttmp); unlink(importanttmpfile); varbuffree(&uvb); /* fall through */ case msdbrw_needsuperuserlockonly: unlockdatabase(); default: break; } for (fnip=fnis; fnip->suffix; fnip++) { free(*fnip->store); *fnip->store= NULL; } free(updatefnbuf); }
int addinterfaces(const char *dirname) { char *ifacelist, interface[32]; int index = 0, count = 0, bwlimit = 0; /* get list of currently visible interfaces */ if (getiflist(&ifacelist)==0) { free(ifacelist); return 0; } if (strlen(ifacelist)<2) { free(ifacelist); return 0; } if (debug) printf("Interface list: \"%s\"\n", ifacelist); while (sscanf(ifacelist+index, "%31s", interface)!=EOF) { if (debug) printf("Processing: \"%s\"\n", interface); index += strlen(interface)+1; /* skip local interfaces */ if ((strcmp(interface,"lo")==0) || (strcmp(interface,"lo0")==0) || (strcmp(interface,"sit0")==0)) { if (debug) printf("skip\n"); continue; } /* create database for interface */ initdb(); strncpy_nt(data.interface, interface, 32); strncpy_nt(data.nick, data.interface, 32); if (!getifinfo(interface)) { if (debug) printf("getifinfo failed, skip\n"); continue; } parseifinfo(1); if (!writedb(interface, dirname, 1)) { continue; } count++; bwlimit = ibwget(interface); if (bwlimit > 0) { printf("\"%s\" added with %d Mbit bandwidth limit.\n", interface, bwlimit); } else { printf("\"%s\" added. Warning: no bandwidth limit has been set.\n", interface); } } if (count==1) { printf("-> %d interface added.", count); } else { printf("-> %d interfaces added.", count); } if (count) { printf("\nLimits can be modified using the configuration file. See \"man vnstat.conf\".\n"); printf("Unwanted interfaces can be removed from monitoring with \"vnstat --delete\"."); } printf("\n"); free(ifacelist); return count; }
int updateavailable(const char *const *argv) { const char *sourcefile= argv[0]; char *availfile; int count= 0; modstatdb_init(); switch (cipaction->arg_int) { case act_avclear: if (sourcefile) badusage(_("--%s takes no arguments"),cipaction->olong); break; case act_avreplace: case act_avmerge: if (!sourcefile || argv[1]) badusage(_("--%s needs exactly one Packages-file argument"), cipaction->olong); break; default: internerr("unknown action '%d'", cipaction->arg_int); } if (!f_noact) { if (access(dpkg_db_get_dir(), W_OK)) { if (errno != EACCES) ohshite(_("unable to access dpkg status area for bulk available update")); else ohshit(_("bulk available update requires write access to dpkg status area")); } modstatdb_lock(); } switch (cipaction->arg_int) { case act_avreplace: printf(_("Replacing available packages info, using %s.\n"),sourcefile); break; case act_avmerge: printf(_("Updating available packages info, using %s.\n"),sourcefile); break; case act_avclear: break; default: internerr("unknown action '%d'", cipaction->arg_int); } availfile = dpkg_db_get_path(AVAILFILE); if (cipaction->arg_int == act_avmerge) parsedb(availfile, pdb_parse_available, NULL); if (cipaction->arg_int != act_avclear) count += parsedb(sourcefile, pdb_parse_available | pdb_ignoreolder, NULL); if (!f_noact) { writedb(availfile, wdb_dump_available); modstatdb_unlock(); } free(availfile); if (cipaction->arg_int != act_avclear) printf(P_("Information about %d package was updated.\n", "Information about %d packages was updated.\n", count), count); modstatdb_done(); return 0; }