]>
Commit | Line | Data |
---|---|---|
2b6e02e2 CP |
1 | #include "../dbapi2/dbapi2.h" |
2 | #include "../core/error.h" | |
3 | #include "../nick/nick.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 *tm, unsigned int id, char *host, unsigned int max, time_t lastseen); | |
11 | static void tm_final(void *, int); | |
12 | ||
13 | trustmigration *migration_start(TrustMigrationGroup, TrustMigrationHost, TrustMigrationFini, void *); | |
14 | void migration_stop(trustmigration *); | |
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) | |
25 | return 0; | |
26 | ||
27 | if(callback) { | |
28 | cbd = malloc(sizeof(struct callbackdata)); | |
29 | if(!cbd) | |
30 | return 0; | |
31 | ||
32 | cbd->callback = callback; | |
33 | cbd->tag = tag; | |
34 | } else { | |
35 | cbd = NULL; | |
36 | } | |
37 | ||
38 | trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "groups"); | |
39 | trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "hosts"); | |
40 | ||
41 | migration = migration_start(tm_group, tm_host, tm_final, cbd); | |
42 | if(!migration) { | |
43 | free(cbd); | |
44 | return 0; | |
45 | } | |
46 | ||
47 | return 1; | |
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 maxseen, 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, maxseen, expires, lastseen, lastmaxuserreset, createdby, contact, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", | |
63 | "Tusuuuutttsss", "groups", id, name, trustedfor, mode, maxperident, maxseen, expires, lastseen, lastmaxuserreset, createdby, contact, comment | |
64 | ); | |
65 | } | |
66 | ||
67 | static void tm_host(void *tag, unsigned int id, char *host, unsigned int max, time_t lastseen) { | |
68 | trustsdb->squery(trustsdb, | |
69 | "INSERT INTO ? (groupid, host, max, lastseen) VALUES (?, ?, ?, ?)", | |
70 | "Tusut", "hosts", id, host, max, lastseen | |
71 | ); | |
72 | } | |
73 | ||
74 | static void tm_final(void *tag, int errcode) { | |
75 | struct callbackdata *cbd = tag; | |
76 | migration = NULL; | |
77 | ||
78 | if(errcode) { | |
79 | Error("trusts", ERR_ERROR, "Migration error: %d", errcode); | |
80 | } else { | |
81 | Error("trusts", ERR_INFO, "Migration completed."); | |
82 | } | |
83 | ||
84 | if(cbd) { | |
85 | cbd->callback(errcode, tag); | |
86 | free(cbd); | |
87 | } | |
88 | } |