X-Git-Url: https://jfr.im/git/irc/quakenet/newserv.git/blobdiff_plain/4be1aaf251a0c861f1ef0064eaef03797290ad82..9e5f0d797a0957172a9fd7b79f20115bc6fdcbd7:/trusts/db-migration.c diff --git a/trusts/db-migration.c b/trusts/db-migration.c index 82555d38..a5f87960 100644 --- a/trusts/db-migration.c +++ b/trusts/db-migration.c @@ -1,20 +1,22 @@ #include "../dbapi2/dbapi2.h" #include "../core/error.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 *, unsigned int, char *, unsigned int, time_t); +static void tm_group(void *, trustgroup *); +static void tm_host(void *, trusthost *, unsigned int); static void tm_final(void *, int); trustmigration *migration_start(TrustMigrationGroup, TrustMigrationHost, TrustMigrationFini, void *); void migration_stop(trustmigration *); -void createtrusttables(int migration); +void createtrusttables(int); struct callbackdata { void *tag; + unsigned int hostid; TrustDBMigrationCallback callback; }; @@ -24,24 +26,21 @@ int trusts_migration_start(TrustDBMigrationCallback callback, void *tag) { if(migration) return 1; - if(callback) { - cbd = malloc(sizeof(struct callbackdata)); - if(!cbd) - return 2; + 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; - createtrusttables(1); + createtrusttables(TABLES_MIGRATION); 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); + nsfree(POOL_TRUSTS, cbd); return 3; } @@ -55,21 +54,18 @@ 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 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); +static void tm_group(void *tag, trustgroup *tg) { + if(tg->id % 25 == 0) + Error("trusts_migration", ERR_INFO, "Migration currently at id: %d", tg->id); - trustsdb->squery(trustsdb, - "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 - ); + trustsdb_inserttg("migration_groups", tg); } -static void tm_host(void *tag, unsigned int id, char *host, unsigned int maxusage, time_t lastseen) { - trustsdb->squery(trustsdb, - "INSERT INTO ? (groupid, host, maxusage, lastseen) VALUES (?, ?, ?, ?)", - "Tusut", "migration_hosts", id, host, maxusage, lastseen - ); +static void tm_host(void *tag, trusthost *th, unsigned int groupid) { + struct callbackdata *cbd = tag; + + th->id = cbd->hostid++; + trustsdb_insertth("migration_hosts", th, groupid); } static void tm_complete(const DBAPIResult *r, void *tag) { @@ -80,18 +76,18 @@ static void tm_complete(const DBAPIResult *r, void *tag) { errcode = MIGRATION_STOPPED; } else { if(!r->success) { - Error("trusts", ERR_ERROR, "A error occured executing the rename table query."); - errcode = 100; + Error("trusts_migration", ERR_ERROR, "A error occured executing the rename table query."); + errcode = MIGRATION_LASTERROR; } else { - Error("trusts", ERR_INFO, "Migration table copying complete."); + Error("trusts_migration", ERR_INFO, "Migration table copying complete."); } r->clear(r); } - if(cbd) { + if(cbd->callback) cbd->callback(errcode, cbd->tag); - free(cbd); - } + + nsfree(POOL_TRUSTS, cbd); } static void tm_final(void *tag, int errcode) { @@ -99,33 +95,21 @@ static void tm_final(void *tag, int errcode) { migration = NULL; if(errcode) { - Error("trusts", ERR_ERROR, "Migration error: %d", errcode); + Error("trusts_migration", ERR_ERROR, "Migration error: %d", errcode); if(cbd) { cbd->callback(errcode, cbd->tag); - free(cbd); + nsfree(POOL_TRUSTS, cbd); } } else { - Error("trusts", ERR_INFO, "Migration completed, copying tables..."); -/* - trustsdb->query(trustsdb, cbd?tm_complete:NULL, cbd, - "BEGIN TRANSACTION; DROP TABLE ?; ALTER TABLE ? RENAME TO ?; DROP TABLE ?; ALTER TABLE ? RENAME TO ?; COMMIT;", - "TTsTTs", "groups", "migration_groups", "groups", "hosts", "migration_hosts", "hosts"); -*/ -/* - trustsdb->query(trustsdb, cbd?tm_complete:NULL, cbd, - "BEGIN TRANSACTION; DELETE FROM ?; INSERT INTO ? SELECT * FROM ?; DELETE FROM ?; INSERT INTO ? SELECT * FROM ?; COMMIT;", - "TTTTTT", "groups", "groups", "migration_groups", "hosts", "hosts", "migration_hosts"); -*/ -/* - trustsdb->query(trustsdb, cbd?tm_complete:NULL, cbd, - "DELETE FROM ?; INSERT INTO ? SELECT * FROM ?; DELETE FROM ?; INSERT INTO ? SELECT * FROM ?;", - "TTTTTT", "groups", "groups", "migration_groups", "hosts", "hosts", "migration_hosts"); -*/ + trusts_closedb(0); + + Error("trusts_migration", ERR_INFO, "Migration completed, copying tables..."); + 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, "ALTER TABLE ? RENAME TO groups", "T", "migration_groups"); trustsdb->squery(trustsdb, "DROP TABLE ?", "T", "hosts"); - trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO ?", "Ts", "migration_hosts", "hosts"); + trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO hosts", "T", "migration_hosts"); trustsdb->query(trustsdb, tm_complete, cbd, "COMMIT", ""); } }