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