]>
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; | |
18 | TrustDBMigrationCallback callback; | |
19 | }; | |
20 | ||
21 | int trusts_migration_start(TrustDBMigrationCallback callback, void *tag) { | |
22 | struct callbackdata *cbd; | |
23 | ||
24 | if(migration) | |
be2823bc | 25 | return 1; |
2b6e02e2 CP |
26 | |
27 | if(callback) { | |
28 | cbd = malloc(sizeof(struct callbackdata)); | |
29 | if(!cbd) | |
be2823bc | 30 | return 2; |
2b6e02e2 CP |
31 | |
32 | cbd->callback = callback; | |
33 | cbd->tag = tag; | |
34 | } else { | |
35 | cbd = NULL; | |
36 | } | |
37 | ||
be2823bc CP |
38 | createtrusttables(1); |
39 | trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_groups"); | |
40 | trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_hosts"); | |
2b6e02e2 CP |
41 | |
42 | migration = migration_start(tm_group, tm_host, tm_final, cbd); | |
43 | if(!migration) { | |
44 | free(cbd); | |
be2823bc | 45 | return 3; |
2b6e02e2 CP |
46 | } |
47 | ||
be2823bc | 48 | return 0; |
2b6e02e2 CP |
49 | } |
50 | ||
51 | void trusts_migration_stop(void) { | |
52 | if(!migration) | |
53 | return; | |
54 | ||
55 | migration_stop(migration); | |
56 | } | |
57 | ||
4be1aaf2 | 58 | 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 |
59 | if(id % 25 == 0) |
60 | Error("trusts", ERR_INFO, "Migration currently at id: %d", id); | |
61 | ||
62 | trustsdb->squery(trustsdb, | |
4be1aaf2 CP |
63 | "INSERT INTO ? (id, name, trustedfor, mode, maxperident, maxusage, expires, lastseen, lastmaxuserreset, createdby, contact, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
64 | "Tusuuuutttsss", "migration_groups", id, name, trustedfor, mode, maxperident, maxusage, expires, lastseen, lastmaxuserreset, createdby, contact, comment | |
2b6e02e2 CP |
65 | ); |
66 | } | |
67 | ||
4be1aaf2 | 68 | static void tm_host(void *tag, unsigned int id, char *host, unsigned int maxusage, time_t lastseen) { |
2b6e02e2 | 69 | trustsdb->squery(trustsdb, |
4be1aaf2 CP |
70 | "INSERT INTO ? (groupid, host, maxusage, lastseen) VALUES (?, ?, ?, ?)", |
71 | "Tusut", "migration_hosts", 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 | ||
91 | if(cbd) { | |
be2823bc | 92 | cbd->callback(errcode, cbd->tag); |
2b6e02e2 CP |
93 | free(cbd); |
94 | } | |
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 | } |