]> jfr.im git - irc/quakenet/newserv.git/commitdiff
Refactor data storage into data.c
authorChris Porter <redacted>
Wed, 1 Oct 2008 18:02:26 +0000 (19:02 +0100)
committerChris Porter <redacted>
Wed, 1 Oct 2008 18:02:26 +0000 (19:02 +0100)
trusts/Makefile.in
trusts/data.c [new file with mode: 0644]
trusts/db.c
trusts/trusts.c
trusts/trusts.h

index 0f34ef1b895997464455d69cbee54d610141e9df..a9a4369904f1045431f6d8c9a0c4afb4c2ef84f7 100644 (file)
@@ -3,6 +3,6 @@
 .PHONY: all
 all: trusts.so trusts_commands.so
 
-trusts.so: trusts.o migration.o db.o db-migration.o
+trusts.so: trusts.o migration.o db.o db-migration.o data.o
 
 trusts_commands.so: trusts_commands.o
diff --git a/trusts/data.c b/trusts/data.c
new file mode 100644 (file)
index 0000000..92514ed
--- /dev/null
@@ -0,0 +1,96 @@
+#include <stdlib.h>
+
+#include "../lib/sstring.h"
+#include "../nick/nick.h" /* NICKLEN */
+#include "trusts.h"
+
+trustgroup *tglist;
+
+void trusts_freeall(void) {
+  trustgroup *tg, *ntg;
+  trusthost *th, *nth;
+
+  for(tg=tglist;tg;tg=ntg) {
+    ntg = tg->next;
+    for(th=tg->hosts;th;th=nth) {
+      nth = th->next;
+
+      th_free(th);
+    }
+
+    tg_free(tg);
+  }
+
+  tglist = NULL;
+}
+
+trustgroup *tg_getbyid(unsigned int id) {
+  trustgroup *tg;
+
+  for(tg=tglist;tg;tg=tg->next)
+    if(tg->id == id)
+      return tg;
+
+  return NULL;
+}
+
+void th_free(trusthost *th) {
+  freesstring(th->host);
+  free(th);
+}
+
+int th_add(trustgroup *tg, char *host, unsigned int maxseen, time_t lastseen) {
+  trusthost *th = malloc(sizeof(trusthost));
+  if(!th)
+    return 0;
+
+  th->host = getsstring(host, TRUSTHOSTLEN);
+  if(!th->host) {
+    th_free(th);
+    return 0;
+  }
+  th->maxseen = maxseen;
+  th->lastseen = lastseen;
+
+  th->next = tg->hosts;
+  tg->hosts = th;
+
+  return 1;
+}
+
+void tg_free(trustgroup *tg) {
+  freesstring(tg->name);
+  freesstring(tg->createdby);
+  freesstring(tg->contact);
+  freesstring(tg->comment);
+  free(tg);
+}
+
+int tg_add(unsigned int id, char *name, unsigned int trustedfor, int mode, unsigned int maxperident, unsigned int maxseen, time_t expires, time_t lastseen, time_t lastmaxuserreset, char *createdby, char *contact, char *comment) {
+  trustgroup *tg = malloc(sizeof(trustgroup));
+  if(!tg)
+    return 0;
+
+  tg->name = getsstring(name, TRUSTNAMELEN);
+  tg->createdby = getsstring(createdby, NICKLEN);
+  tg->contact = getsstring(contact, CONTACTLEN);
+  tg->comment = getsstring(comment, COMMENTLEN);
+  if(!tg->name || !tg->createdby || !tg->contact || !tg->comment) {
+    tg_free(tg);
+    return 0;
+  }
+
+  tg->id = id;
+  tg->trustedfor = trustedfor;
+  tg->mode = mode;
+  tg->maxperident = maxperident;
+  tg->maxseen = maxseen;
+  tg->expires = expires;
+  tg->lastseen = lastseen;
+  tg->lastmaxuserreset = lastmaxuserreset;
+
+  tg->next = tglist;
+  tglist = tg;
+
+  return 1;
+}
index 46cddae25a9f5c326bce9e6678037b9be0522408..4d24819614a5dd0b7de238e593a8260ef6a99260 100644 (file)
@@ -44,40 +44,12 @@ void createtrusttables(int migration) {
 }
 
 static void trusts_freedb(void) {
-  trustgroup *tg, *ntg;
-  trusthost *th, *nth;
-
-  for(tg=tglist;tg;tg=ntg) {
-    ntg = tg->next;
-    for(th=tg->hosts;th;th=nth) {
-      nth = th->next;
-
-      freesstring(th->host);
-      free(th);
-    }
-
-    freesstring(tg->name);
-    freesstring(tg->createdby);
-    freesstring(tg->contact);
-    freesstring(tg->comment);
-    free(tg);
-  }
+  trusts_freeall();
 
   trustsdbloaded = 0;
-  tglist = NULL;
   tgmaxid = 0;
 }
 
-trustgroup *tg_getbyid(unsigned int id) {
-  trustgroup *tg;
-
-  for(tg=tglist;tg;tg=tg->next)
-    if(tg->id == id)
-      return tg;
-
-  return NULL;
-}
-
 static void loadhosts_data(const DBAPIResult *result, void *tag) {
   if(!result) {
     loaderror = 1;
@@ -102,7 +74,8 @@ static void loadhosts_data(const DBAPIResult *result, void *tag) {
 
   while(result->next(result)) {
     unsigned int groupid;
-    trusthost *th;
+    char *host;
+    unsigned int maxseen, lastseen;
     trustgroup *tg;
 
     groupid = strtoul(result->get(result, 0), NULL, 10);
@@ -113,16 +86,12 @@ static void loadhosts_data(const DBAPIResult *result, void *tag) {
       continue;
     }
 
-    th = malloc(sizeof(trusthost));
-    if(!th)
-      continue;
-
-    th->host = getsstring(result->get(result, 1), TRUSTHOSTLEN);
-    th->maxseen = strtoul(result->get(result, 2), NULL, 10);
-    th->lastseen = (time_t)strtoul(result->get(result, 3), NULL, 10);
+    maxseen = strtoul(result->get(result, 2), NULL, 10);
+    lastseen = (time_t)strtoul(result->get(result, 3), NULL, 10);
+    host = result->get(result, 1);
 
-    th->next = tg->hosts;
-    tg->hosts = th;
+    if(!th_add(tg, host, maxseen, lastseen))
+      Error("trusts", ERR_WARNING, "Error adding host to trust %d: %s", groupid, host);
   }
 
   result->clear(result);
@@ -161,33 +130,37 @@ static void loadgroups_data(const DBAPIResult *result, void *tag) {
 
   while(result->next(result)) {
     unsigned int id;
-    trustgroup *tg;
+    sstring *name, *createdby, *contact, *comment;
+    unsigned int trustedfor, mode, maxperident, maxseen;
+    time_t expires, lastseen, lastmaxuserreset;
 
     id = strtoul(result->get(result, 0), NULL, 10);
     if(id > tgmaxid)
       tgmaxid = id;
 
-    tg = malloc(sizeof(trustgroup));
-    if(!tg)
-      continue;
+    name = getsstring(result->get(result, 1), TRUSTNAMELEN);
+    trustedfor = strtoul(result->get(result, 2), NULL, 10);
+    mode = atoi(result->get(result, 3));
+    maxperident = strtoul(result->get(result, 4), NULL, 10);
+    maxseen = strtoul(result->get(result, 5), NULL, 10);
+    expires = (time_t)strtoul(result->get(result, 6), NULL, 10);
+    lastseen = (time_t)strtoul(result->get(result, 7), NULL, 10);
+    lastmaxuserreset = (time_t)strtoul(result->get(result, 8), NULL, 10);
+    createdby = getsstring(result->get(result, 9), NICKLEN);
+    contact = getsstring(result->get(result, 10), CONTACTLEN);
+    comment = getsstring(result->get(result, 11), COMMENTLEN);
+
+    if(name && createdby && contact && comment) {
+      if(!tg_add(id, name->content, trustedfor, mode, maxperident, maxseen, expires, lastseen, lastmaxuserreset, createdby->content, contact->content, comment->content))
+        Error("trusts", ERR_WARNING, "Error adding trustgroup %d: %s", id, name->content);
+    } else {
+      Error("trusts", ERR_ERROR, "Error allocating sstring in group loader, id: %d", id);
+    }
 
-    tg->id = id;
-    tg->name = getsstring(result->get(result, 1), TRUSTNAMELEN);
-    tg->trustedfor = strtoul(result->get(result, 2), NULL, 10);
-    tg->mode = atoi(result->get(result, 3));
-    tg->maxperident = strtoul(result->get(result, 4), NULL, 10);
-    tg->maxseen = strtoul(result->get(result, 5), NULL, 10);
-    tg->expires = (time_t)strtoul(result->get(result, 6), NULL, 10);
-    tg->lastseen = (time_t)strtoul(result->get(result, 7), NULL, 10);
-    tg->lastmaxuserreset = (time_t)strtoul(result->get(result, 8), NULL, 10);
-    tg->createdby = getsstring(result->get(result, 9), NICKLEN);
-    tg->contact = getsstring(result->get(result, 10), CONTACTLEN);
-    tg->comment = getsstring(result->get(result, 11), COMMENTLEN);
-
-    tg->hosts = NULL;
-
-    tg->next = tglist;
-    tglist = tg;
+    freesstring(name);
+    freesstring(createdby);
+    freesstring(contact);
+    freesstring(comment);
   }
 
   result->clear(result);  
index 974bc63ca63480f3947dcb30258591fbd5f9451a..8ccd9363e5a0f5011da7c42aa160ac033f5187ce 100644 (file)
@@ -3,8 +3,6 @@
 #include <stdio.h>
 #include <time.h>
 
-trustgroup *tglist;
-
 int trusts_loaddb(void);
 void trusts_closedb(void);
 static void statusfn(int, void *);
index 5afb5b1f8991af8880f6a967945f57c594e26482..3530586888c1ca5aa629a3d5cd1ffa1b40cfabf5 100644 (file)
 
 struct trustmigration;
 
-typedef void (*TrustMigrationGroup)(void *, unsigned int, char *, unsigned int, unsigned int, unsigned int, unsigned int, time_t, time_t, time_t, char *, char *, char *);
-typedef void (*TrustMigrationHost)(void *, unsigned int, char *, unsigned int, time_t);
-typedef void (*TrustMigrationFini)(void *, int);
-
-typedef struct trustmigration {
-  int count, cur;
-  void *schedule;
-  void *tag;
-
-  TrustMigrationGroup group;
-  TrustMigrationHost host;
-  TrustMigrationFini fini;
-} trustmigration;
-
-typedef void (*TrustDBMigrationCallback)(int, void *);
-
 typedef struct trusthost {
   sstring *host;
   unsigned int maxseen;
@@ -55,11 +39,39 @@ typedef struct trustgroup {
   struct trustgroup *next;
 } trustgroup;
 
-void trusts_reloaddb(void);
+/* trusts.c */
+char *trusts_timetostr(time_t);
 
+/* db.c */
 extern int trustsdbloaded;
+void trusts_reloaddb(void);
+
+/* data.c */
 extern trustgroup *tglist;
+void trusts_freeall(void);
+trustgroup *tg_getbyid(unsigned int);
+void th_free(trusthost *);
+int th_add(trustgroup *, char *, unsigned int, time_t);
+void tg_free(trustgroup *);
+int tg_add(unsigned int, char *, unsigned int, int, unsigned int, unsigned int, time_t, time_t, time_t, char *, char *, char *);
+
+/* migration.c */
+typedef void (*TrustMigrationGroup)(void *, unsigned int, char *, unsigned int, unsigned int, unsigned int, unsigned int, time_t, time_t, time_t, char *, char *, char *);
+typedef void (*TrustMigrationHost)(void *, unsigned int, char *, unsigned int, time_t);
+typedef void (*TrustMigrationFini)(void *, int);
 
-char *trusts_timetostr(time_t t);
+typedef struct trustmigration {
+  int count, cur;
+  void *schedule;
+  void *tag;
+
+  TrustMigrationGroup group;
+  TrustMigrationHost host;
+  TrustMigrationFini fini;
+} trustmigration;
+
+/* db-migration.c */
+
+typedef void (*TrustDBMigrationCallback)(int, void *);
 
 #endif