]> jfr.im git - irc/quakenet/newserv.git/blob - trusts/db-migration.c
Merge.
[irc/quakenet/newserv.git] / trusts / db-migration.c
1 #include "../dbapi2/dbapi2.h"
2 #include "../core/error.h"
3 #include "../core/nsmalloc.h"
4 #include "trusts.h"
5
6 extern DBAPIConn *trustsdb;
7 static trustmigration *migration;
8
9 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 *);
10 static void tm_host(void *, unsigned int, char *, unsigned int, time_t);
11 static void tm_final(void *, int);
12
13 trustmigration *migration_start(TrustMigrationGroup, TrustMigrationHost, TrustMigrationFini, void *);
14 void migration_stop(trustmigration *);
15 void createtrusttables(int migration);
16
17 struct callbackdata {
18 void *tag;
19 unsigned int hostid;
20 TrustDBMigrationCallback callback;
21 };
22
23 int trusts_migration_start(TrustDBMigrationCallback callback, void *tag) {
24 struct callbackdata *cbd;
25
26 if(migration)
27 return 1;
28
29 cbd = nsmalloc(POOL_TRUSTS, sizeof(struct callbackdata));
30 if(!cbd)
31 return 2;
32
33 cbd->callback = callback;
34 cbd->tag = tag;
35 cbd->hostid = 1;
36
37 createtrusttables(1);
38 trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_groups");
39 trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_hosts");
40
41 migration = migration_start(tm_group, tm_host, tm_final, cbd);
42 if(!migration) {
43 nsfree(POOL_TRUSTS, cbd);
44 return 3;
45 }
46
47 return 0;
48 }
49
50 void trusts_migration_stop(void) {
51 if(!migration)
52 return;
53
54 migration_stop(migration);
55 }
56
57 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) {
58 if(id % 25 == 0)
59 Error("trusts", ERR_INFO, "Migration currently at id: %d", id);
60
61 trustsdb->squery(trustsdb,
62 "INSERT INTO ? (id, name, trustedfor, mode, maxperident, maxusage, expires, lastseen, lastmaxuserreset, createdby, contact, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
63 "Tusuuuutttsss", "migration_groups", id, name, trustedfor, mode, maxperident, maxusage, expires, lastseen, lastmaxuserreset, createdby, contact, comment
64 );
65 }
66
67 static void tm_host(void *tag, unsigned int id, char *host, unsigned int maxusage, time_t lastseen) {
68 struct callbackdata *cbd = tag;
69
70 trustsdb->squery(trustsdb,
71 "INSERT INTO ? (id, groupid, host, maxusage, lastseen) VALUES (?, ?, ?, ?, ?)",
72 "Tuusut", "migration_hosts", cbd->hostid++, id, host, maxusage, lastseen
73 );
74 }
75
76 static void tm_complete(const DBAPIResult *r, void *tag) {
77 struct callbackdata *cbd = tag;
78 int errcode = 0;
79
80 if(!r) {
81 errcode = MIGRATION_STOPPED;
82 } else {
83 if(!r->success) {
84 Error("trusts", ERR_ERROR, "A error occured executing the rename table query.");
85 errcode = MIGRATION_LASTERROR;
86 } else {
87 Error("trusts", ERR_INFO, "Migration table copying complete.");
88 }
89 r->clear(r);
90 }
91
92 if(cbd->callback)
93 cbd->callback(errcode, cbd->tag);
94
95 nsfree(POOL_TRUSTS, cbd);
96 }
97
98 static void tm_final(void *tag, int errcode) {
99 struct callbackdata *cbd = tag;
100 migration = NULL;
101
102 if(errcode) {
103 Error("trusts", ERR_ERROR, "Migration error: %d", errcode);
104 if(cbd) {
105 cbd->callback(errcode, cbd->tag);
106 nsfree(POOL_TRUSTS, cbd);
107 }
108 } else {
109 trusts_closedb(0);
110
111 Error("trusts", ERR_INFO, "Migration completed, copying tables...");
112
113 trustsdb->squery(trustsdb, "BEGIN TRANSACTION", "");
114 trustsdb->squery(trustsdb, "DROP TABLE ?", "T", "groups");
115 trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO ?", "Ts", "migration_groups", "groups");
116 trustsdb->squery(trustsdb, "DROP TABLE ?", "T", "hosts");
117 trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO ?", "Ts", "migration_hosts", "hosts");
118 trustsdb->query(trustsdb, tm_complete, cbd, "COMMIT", "");
119 }
120 }