]> jfr.im git - irc/quakenet/newserv.git/blobdiff - trusts/db-migration.c
fix indentation
[irc/quakenet/newserv.git] / trusts / db-migration.c
index 9ecdf473a1f83a6e210cf740c30cbbb20db32d32..6ef93afe1c68b6744e73bd570a0b840fcd3e3ad1 100644 (file)
@@ -1,20 +1,22 @@
 #include "../dbapi2/dbapi2.h"
 #include "../core/error.h"
-#include "../nick/nick.h"
+#include "../core/nsmalloc.h"
 #include "trusts.h"
 
 extern DBAPIConn *trustsdb;
 static trustmigration *migration;
 
 static void tm_group(void *, unsigned int, char *, unsigned int, unsigned int, unsigned int, unsigned int, time_t, time_t, time_t, char *, char *, char *);
-static void tm_host(void *tm, unsigned int id, char *host, unsigned int max, time_t lastseen);
+static void tm_host(void *, unsigned int, char *, unsigned int, time_t);
 static void tm_final(void *, int);
 
 trustmigration *migration_start(TrustMigrationGroup, TrustMigrationHost, TrustMigrationFini, void *);
 void migration_stop(trustmigration *);
+void createtrusttables(int migration);
 
 struct callbackdata {
   void *tag;
+  unsigned int hostid;
   TrustDBMigrationCallback callback;
 };
 
@@ -22,29 +24,27 @@ int trusts_migration_start(TrustDBMigrationCallback callback, void *tag) {
   struct callbackdata *cbd;
 
   if(migration)
-    return 0;
+    return 1;
 
-  if(callback) {
-    cbd = malloc(sizeof(struct callbackdata));
-    if(!cbd)
-      return 0;
+  cbd = nsmalloc(POOL_TRUSTS, sizeof(struct callbackdata));
+  if(!cbd)
+    return 2;
 
-    cbd->callback = callback;
-    cbd->tag = tag;
-  } else {
-    cbd = NULL;
-  }
+  cbd->callback = callback;
+  cbd->tag = tag;
+  cbd->hostid = 1;
 
-  trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "groups");
-  trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "hosts");
+  createtrusttables(1);
+  trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_groups");
+  trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_hosts");
 
   migration = migration_start(tm_group, tm_host, tm_final, cbd);
   if(!migration) {
-    free(cbd);
-    return 0;
+    nsfree(POOL_TRUSTS, cbd);
+    return 3;
   }
 
-  return 1;
+  return 0;
 }
 
 void trusts_migration_stop(void) {
@@ -54,35 +54,67 @@ void trusts_migration_stop(void) {
   migration_stop(migration);
 }
 
-static void tm_group(void *tag, unsigned int id, char *name, unsigned int trustedfor, unsigned int mode, unsigned int maxperident, unsigned int maxseen, time_t expires, time_t lastseen, time_t lastmaxuserreset, char *createdby, char *contact, char *comment) {
+static void tm_group(void *tag, unsigned int id, char *name, unsigned int trustedfor, unsigned int mode, unsigned int maxperident, unsigned int maxusage, time_t expires, time_t lastseen, time_t lastmaxuserreset, char *createdby, char *contact, char *comment) {
   if(id % 25 == 0)
     Error("trusts", ERR_INFO, "Migration currently at id: %d", id);
 
   trustsdb->squery(trustsdb, 
-    "INSERT INTO ? (id, name, trustedfor, mode, maxperident, maxseen, expires, lastseen, lastmaxuserreset, createdby, contact, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
-    "Tusuuuutttsss", "groups", id, name, trustedfor, mode, maxperident, maxseen, expires, lastseen, lastmaxuserreset, createdby, contact, comment
+    "INSERT INTO ? (id, name, trustedfor, mode, maxperident, maxusage, expires, lastseen, lastmaxuserreset, createdby, contact, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+    "Tusuuuutttsss", "migration_groups", id, name, trustedfor, mode, maxperident, maxusage, expires, lastseen, lastmaxuserreset, createdby, contact, comment
   );
 }
 
-static void tm_host(void *tag, unsigned int id, char *host, unsigned int max, time_t lastseen) {
+static void tm_host(void *tag, unsigned int id, char *host, unsigned int maxusage, time_t lastseen) {
+  struct callbackdata *cbd = tag;
+
   trustsdb->squery(trustsdb, 
-    "INSERT INTO ? (groupid, host, max, lastseen) VALUES (?, ?, ?, ?)",
-    "Tusut", "hosts", id, host, max, lastseen
+    "INSERT INTO ? (id, groupid, host, maxusage, lastseen) VALUES (?, ?, ?, ?, ?)",
+    "Tuusut", "migration_hosts", cbd->hostid++, id, host, maxusage, lastseen
   );
 }
 
+static void tm_complete(const DBAPIResult *r, void *tag) {
+  struct callbackdata *cbd = tag;
+  int errcode = 0;
+
+  if(!r) {
+    errcode = MIGRATION_STOPPED;
+  } else {
+    if(!r->success) {
+      Error("trusts", ERR_ERROR, "A error occured executing the rename table query.");
+      errcode = MIGRATION_LASTERROR;
+    } else {
+      Error("trusts", ERR_INFO, "Migration table copying complete.");
+    }
+    r->clear(r);
+  }
+
+  if(cbd->callback)
+    cbd->callback(errcode, cbd->tag);
+
+  nsfree(POOL_TRUSTS, cbd);
+}
+
 static void tm_final(void *tag, int errcode) {
   struct callbackdata *cbd = tag;
   migration = NULL;
 
   if(errcode) {
     Error("trusts", ERR_ERROR, "Migration error: %d", errcode);
+    if(cbd) {
+      cbd->callback(errcode, cbd->tag);
+      nsfree(POOL_TRUSTS, cbd);
+    }
   } else {
-    Error("trusts", ERR_INFO, "Migration completed.");
-  }
+    trusts_closedb(0);
+
+    Error("trusts", ERR_INFO, "Migration completed, copying tables...");
 
-  if(cbd) {
-    cbd->callback(errcode, tag);
-    free(cbd);
+    trustsdb->squery(trustsdb, "BEGIN TRANSACTION", "");
+    trustsdb->squery(trustsdb, "DROP TABLE ?", "T", "groups");
+    trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO ?", "Ts", "migration_groups", "groups");
+    trustsdb->squery(trustsdb, "DROP TABLE ?", "T", "hosts");
+    trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO ?", "Ts", "migration_hosts", "hosts");
+    trustsdb->query(trustsdb, tm_complete, cbd, "COMMIT", "");
   }
 }