]> granicus.if.org Git - vnstat/commitdiff
drop support for old database formats
authorTeemu Toivola <git@humdi.net>
Tue, 13 Jan 2015 18:17:58 +0000 (20:17 +0200)
committerTeemu Toivola <git@humdi.net>
Tue, 13 Jan 2015 18:17:58 +0000 (20:17 +0200)
CHANGES
src/dbaccess.c
src/dbaccess.h

diff --git a/CHANGES b/CHANGES
index 5c8ad4d745a473c49b570b5da66f5bc140c624b7..3afdd6727273c4048a2463c09bd11a2bd30f34e9 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -13,6 +13,7 @@
    (pull requests by Jasper Lievisse Adriaanse)
  - Automatic interface bandwidth detection, BandwidthDetection in config
  - JSON output (--json)
+ - Drop support for over 10 year old database formats (versions 1.0 - 1.2)
 
 
 1.12 / 30-Aug-14
index dfbfc8fad009f6be58582edd641cb69d9a61e7e4..831044fbea4c31ae4ce028c302f3f261343ff86d 100644 (file)
@@ -44,7 +44,7 @@ int readdb(const char *iface, const char *dirname)
                        printe(PT_Info);
                }
 
-               if ((data.version==-1) || (!convertdb(db))) {
+               if ((data.version==-1) || (!convertdb())) {
 
                        /* close current db and try using backup if database conversion failed */
                        fclose(db);
@@ -81,7 +81,7 @@ int readdb(const char *iface, const char *dirname)
                        }
 
                        if (data.version!=DBVERSION) {
-                               if (!convertdb(db)) {
+                               if (!convertdb()) {
                                        snprintf(errorstring, 512, "Unable to use backup database.");
                                        printe(PT_Error);
                                        fclose(db);
@@ -291,253 +291,17 @@ int backupdb(const char *current, const char *backup)
        return 1;
 }
 
-int convertdb(FILE *db)
+int convertdb(void)
 {
-       int i, days, mod;
-       DATA10 data10;
-       DATA12 data12;
-       time_t current;
-       struct tm *d;
-       int month=0, day;
-       int tm_mday, tm_mon, tm_year;
-       int converted=0;
-
-       current=time(NULL);
-       d=localtime(&current);
-       days=d->tm_mday-1;
-
-       tm_mday=d->tm_mday;
-       tm_mon=d->tm_mon;
-       tm_year=d->tm_year;
-
-       /* version 1.0 database format */
-       if (data.version==1) {
-
-               snprintf(errorstring, 512, "Converting to db v2...\n");
-               printe(PT_Info);
-
-               rewind(db);
-               if (fread(&data10, sizeof(DATA10), 1, db)==0) {
-                       snprintf(errorstring, 512, "Unable to convert corrupted database.");
-                       printe(PT_Error);
-                       return 0;
-               }
-
-               /* set basic values */
-               data12.version=2;
-               strncpy_nt(data12.interface, data10.interface, 32);
-               strncpy_nt(data12.nick, data10.interface, 32);
-               data12.active=1;
-               data12.totalrx=data10.totalrx;
-               data12.totaltx=data10.totaltx;
-               data12.currx=data10.currx;
-               data12.curtx=data10.curtx;
-               data12.totalrxk=data10.totalrxk;
-               data12.totaltxk=data10.totaltxk;
-               data12.lastupdated=data10.lastupdated;
-               data12.created=data10.created;
-               data12.btime=data10.btime;
-
-               /* days */
-               for (i=0; i<=29; i++) {
-                       if (data10.day[i].rx+data10.day[i].tx>0) {
-                               data12.day[i].rx=data10.day[i].rx;
-                               data12.day[i].tx=data10.day[i].tx;
-                               data12.day[i].date=current-(i*86400);
-                               data12.day[i].used=1;
-                       } else {
-                               data12.day[i].rx=0;
-                               data12.day[i].tx=0;
-                               data12.day[i].used=0;
-                       }
-               }
-
-               /* months */
-               for (i=0; i<=11; i++) {
-                       if (data10.month[i].rx+data10.month[i].tx>0) {
-                               data12.month[i].rx=data10.month[i].rx;
-                               data12.month[i].tx=data10.month[i].tx;
-
-                               data12.month[i].month=current-(86400*days);
-
-                               /* we have to do this modulo thing to get the number of days right */
-                               mod=(d->tm_mon-i-1)%12;
-                               if (mod<0)
-                                       mod=12+mod;
-                               days+=dmonth(mod);
-                               data12.month[i].used=1;
-                       } else {
-                               data12.month[i].rx=0;
-                               data12.month[i].tx=0;
-                               data12.month[i].used=0;
-                       }
-               }
-
-               /* top10 */
-               for (i=0; i<=9; i++) {
-                       if (data10.top10[i].rx+data10.top10[i].tx>0) {
-                               data12.top10[i].rx=data10.top10[i].rx;
-                               data12.top10[i].tx=data10.top10[i].tx;
-
-                               /* get day */
-                               day=atoi(data10.top10[i].date+7);
-
-                               /* parse month */
-                               if (strncmp(data10.top10[i].date+4, "Jan", 3)==0) {
-                                       month=0;
-                               } else if (strncmp(data10.top10[i].date+4, "Feb", 3)==0) {
-                                       month=1;
-                               } else if (strncmp(data10.top10[i].date+4, "Mar", 3)==0) {
-                                       month=2;
-                               } else if (strncmp(data10.top10[i].date+4, "Apr", 3)==0) {
-                                       month=3;
-                               } else if (strncmp(data10.top10[i].date+4, "May", 3)==0) {
-                                       month=4;
-                               } else if (strncmp(data10.top10[i].date+4, "Jun", 3)==0) {
-                                       month=5;
-                               } else if (strncmp(data10.top10[i].date+4, "Jul", 3)==0) {
-                                       month=6;
-                               } else if (strncmp(data10.top10[i].date+4, "Aug", 3)==0) {
-                                       month=7;
-                               } else if (strncmp(data10.top10[i].date+4, "Sep", 3)==0) {
-                                       month=8;
-                               } else if (strncmp(data10.top10[i].date+4, "Oct", 3)==0) {
-                                       month=9;
-                               } else if (strncmp(data10.top10[i].date+4, "Nov", 3)==0) {
-                                       month=10;
-                               } else if (strncmp(data10.top10[i].date+4, "Dec", 3)==0) {
-                                       month=11;
-                               } else {
-                                       month=-1;
-                                       snprintf(errorstring, 512, "Convertion for date \"%s\" failed.", data10.top10[i].date);
-                                       printe(PT_Error);
-
-                               }
-
-                               if (month==-1)
-                                       break;
-
-                               /* guess year */
-                               d->tm_year=tm_year;
-                               if ((month>tm_mon) || ((month==tm_mon) && (day>tm_mday)))
-                                       d->tm_year--;
-
-                               d->tm_mday=day;
-                               d->tm_mon=month;
-                               data12.top10[i].date=mktime(d);
-                               data12.top10[i].used=1;
-
-                       } else {
-                               data12.top10[i].used=0;
-                               data12.top10[i].rx=0;
-                               data12.top10[i].tx=0;
-                       }
-
-
-
-               }
-
-               /* reset top10 if there was some problem */
-               if (month==-1) {
-                       for (i=0; i<=9; i++) {
-                               data12.top10[i].rx=data.top10[i].tx=0;
-                               data12.top10[i].used=0;
-                       }
-               }
-
-               converted=1;
-       } 
-
-       /* version 1.1-1.2 database format */
-       if (data.version==2 || converted==1) {
-
-               printf("Converting to db v3...\n");
-
-               /* don't read from file if already in memory */
-               if (converted==0) {
-                       rewind(db);
-                       if (fread(&data12, sizeof(DATA12), 1, db)==0) {
-                               snprintf(errorstring, 512, "Unable to convert corrupted database.");
-                               printe(PT_Error);
-                               return 0;
-                       }
-               }
-
-               /* set basic values */
-               data.version=3;
-               strncpy_nt(data.interface, data12.interface, 32);
-               strncpy_nt(data.nick, data12.nick, 32);
-               data.active=data12.active;
-               data.totalrx=data12.totalrx;
-               data.totaltx=data12.totaltx;
-               data.currx=data12.currx;
-               data.curtx=data12.curtx;
-               data.totalrxk=data12.totalrxk;
-               data.totaltxk=data12.totaltxk;
-               data.lastupdated=data12.lastupdated;
-               data.created=data12.created;
-               data.btime=data12.btime;
-
-               /* days */
-               for (i=0; i<=29; i++) {
-                       if (data12.day[i].used) {
-                               data.day[i].rx=data12.day[i].rx;
-                               data.day[i].tx=data12.day[i].tx;
-                               data.day[i].rxk=data.day[i].txk=0;
-                               data.day[i].date=data12.day[i].date;
-                               data.day[i].used=1;
-                       } else {
-                               data.day[i].rx=data.day[i].tx=0;
-                               data.day[i].rxk=data.day[i].txk=0;
-                               data.day[i].used=0;
-                       }
-               }
-
-               /* months */
-               for (i=0; i<=11; i++) {
-                       if (data12.month[i].used) {
-                               data.month[i].rx=data12.month[i].rx;
-                               data.month[i].tx=data12.month[i].tx;
-                               data.month[i].rxk=data.month[i].txk=0;
-                               data.month[i].month=data12.month[i].month;
-                               data.month[i].used=1;
-                       } else {
-                               data.month[i].rx=data.month[i].tx=0;
-                               data.month[i].rxk=data.month[i].txk=0;
-                               data.month[i].used=0;
-                       }
-               }
-
-               /* top10 */
-               for (i=0;i<=9;i++) {
-                       if (data12.top10[i].used) {
-                               data.top10[i].rx=data12.top10[i].rx;
-                               data.top10[i].tx=data12.top10[i].tx;
-                               data.top10[i].rxk=data.top10[i].txk=0;
-                               data.top10[i].date=data12.top10[i].date;
-                               data.top10[i].used=1;
-                       } else {
-                               data.top10[i].rx=data.top10[i].tx=0;
-                               data.top10[i].rxk=data.top10[i].txk=0;
-                               data.top10[i].date=0;
-                               data.top10[i].used=0;
-                       }
-               }
-
-               /* hours */
-               for (i=0;i<=23;i++) {
-                       data.hour[i].rx=0;
-                       data.hour[i].tx=0;
-                       data.hour[i].date=0;
-               }
-
-       }
-
        /* corrupted or unknown version handling */
        if (data.version==0) {
                snprintf(errorstring, 512, "Unable to convert corrupted database.");
                printe(PT_Error);
                return 0;
+       } else if (data.version<DBVERSION) {
+               snprintf(errorstring, 512, "Database upgrade from version \"%d\" is not supported.", data.version);
+               printe(PT_Error);
+               return 0;
        } else if (data.version>DBVERSION) {
                snprintf(errorstring, 512, "Unable to downgrade database from version \"%d\".", data.version);
                printe(PT_Error);
@@ -548,9 +312,6 @@ int convertdb(FILE *db)
                return 0;
        }
 
-       snprintf(errorstring, 512, "Conversion done.");
-       printe(PT_Info);
-
        return 1;
 }
 
index 0944ec3cea94e9a6c030d1a1d63e7ea30368cfa1..9345b3acc4e1ce166cbeba230359d97384f9ad72 100644 (file)
@@ -5,7 +5,7 @@ int readdb(const char *iface, const char *dirname);
 void initdb(void);
 int writedb(const char *iface, const char *dirname, int newdb);
 int backupdb(const char *current, const char *backup);
-int convertdb(FILE *db);
+int convertdb(void);
 int lockdb(int fd, int dbwrite);
 int checkdb(const char *iface, const char *dirname);
 int removedb(const char *iface, const char *dirname);
@@ -17,55 +17,4 @@ void rebuilddbtotal(const char *iface, const char *dirname);
 int validatedb(void);
 int importdb(const char *filename);
 
-/* version 1.0 database format aka db v1 */
-typedef struct {
-       char date[11];
-       uint64_t rx, tx;
-} DAY10;
-
-typedef struct {
-       char month[4];
-       uint64_t rx, tx;
-} MONTH10;
-
-typedef struct {
-       int version;
-       char interface[32];
-       uint64_t totalrx, totaltx, currx, curtx;
-       int totalrxk, totaltxk;
-       time_t lastupdated, created;
-       DAY10 day[30];
-       MONTH10 month[12];
-       DAY10 top10[10];
-       uint64_t btime;
-} DATA10;
-
-
-/* version 1.1-1.2 database format aka db v2 */
-typedef struct {
-       time_t date;
-       uint64_t rx, tx;
-       int used;
-} DAY12;
-
-typedef struct {
-       time_t month;
-       uint64_t rx, tx;
-       int used;
-} MONTH12;
-
-typedef struct {
-       int version;
-       char interface[32];
-       char nick[32];
-       int active;
-       uint64_t totalrx, totaltx, currx, curtx;
-       int totalrxk, totaltxk;
-       time_t lastupdated, created;
-       DAY12 day[30];
-       MONTH12 month[12];
-       DAY12 top10[10];
-       uint64_t btime;
-} DATA12;
-
 #endif