]> granicus.if.org Git - vnstat/commitdiff
implement first layer of interface merge support
authorTeemu Toivola <git@humdi.net>
Sun, 10 Mar 2019 20:12:02 +0000 (22:12 +0200)
committerTeemu Toivola <git@humdi.net>
Sun, 10 Mar 2019 20:12:02 +0000 (22:12 +0200)
src/dbsql.c
src/dbsql.h

index 89bf09ff6fc18ad9f46776d442101fa9bf75ca9c..ef60c64eb47cb299c4af5108cc511eeec8f1c590 100644 (file)
@@ -342,16 +342,31 @@ uint64_t db_getinterfacecount(void)
 
 uint64_t db_getinterfacecountbyname(const char *iface)
 {
-       int rc;
+       int rc, i, search_count = 1;
        uint64_t result = 0;
-       char sql[512];
+       char sql[512], *inquery = NULL;
        sqlite3_stmt *sqlstmt;
 
-       if (strlen(iface) > 0) {
-               sqlite3_snprintf(512, sql, "select count(*) from interface where name='%q'", iface);
+       if (strchr(iface, '+') == NULL) {
+               if (strlen(iface) > 0) {
+                       sqlite3_snprintf(512, sql, "select count(*) from interface where name='%q'", iface);
+               } else {
+                       sqlite3_snprintf(512, sql, "select count(*) from interface");
+               }
        } else {
-               sqlite3_snprintf(512, sql, "select count(*) from interface");
+               for (i = 0; i < (int)strlen(iface); i++) {
+                       if (iface[i] == '+') {
+                               search_count++;
+                       }
+               }
+               inquery = getifaceinquery(iface, (unsigned int)search_count);
+               if (inquery == NULL) {
+                       return 0;
+               }
+               sqlite3_snprintf(512, sql, "select count(*) from interface where name in (%q)", inquery);
+               free(inquery);
        }
+
        rc = sqlite3_prepare_v2(db, sql, -1, &sqlstmt, NULL);
        if (rc != SQLITE_OK) {
                db_errcode = rc;
@@ -1188,3 +1203,33 @@ void dbdatalistfree(dbdatalist **dbdata)
        }
 
 }
+
+char *getifaceinquery(const char *input, const unsigned int ifacecount)
+{
+       unsigned int i, j;
+       char *result;
+
+       if (input[strlen(input)-1] == '+') {
+               return NULL;
+       }
+
+       /* each interface requires two quotes and comma or \0 so 3 extra chars */
+       j = (unsigned int)strlen(input) + ifacecount * 3;
+       result = malloc(sizeof(char) * j);
+       memset(result, '\0', j);
+
+       result[0] = '"';
+       j = 1;
+       for (i = 0; i < (unsigned int)strlen(input); i++) {
+               if (input[i] == '+') {
+                       strcat(result, "\",\"");
+                       j += 3;
+               } else {
+                       result[j] = input[i];
+                       j++;
+               }
+       }
+       result[j] = '"';
+
+       return result;
+}
index fbaf309b20e9f9cd28668a8ced8927a9807ac75d..c68ca1629ea46aba2a84a2cdcd19b804ab9e4896 100644 (file)
@@ -77,6 +77,8 @@ void updatelistinfo(dbdatalistinfo *listinfo, const uint64_t rx, const uint64_t
 int dbdatalistadd(dbdatalist **dbdata, const uint64_t rx, const uint64_t tx, const time_t timestamp, const int64_t rowid);
 void dbdatalistfree(dbdatalist **dbdata);
 
+char *getifaceinquery(const char *input, const unsigned int ifacecount);
+
 /* global db */
 extern sqlite3 *db;
 extern int db_errcode;