]>
Commit | Line | Data |
---|---|---|
2b6e02e2 CP |
1 | #include "../dbapi2/dbapi2.h" |
2 | #include "../core/error.h" | |
45989eab | 3 | #include "../core/nsmalloc.h" |
2b6e02e2 CP |
4 | #include "trusts.h" |
5 | ||
6 | extern DBAPIConn *trustsdb; | |
7 | static trustmigration *migration; | |
8 | ||
82a316e7 CP |
9 | static void tm_group(void *, trustgroup *); |
10 | static void tm_host(void *, trusthost *, unsigned int); | |
2b6e02e2 CP |
11 | static void tm_final(void *, int); |
12 | ||
13 | trustmigration *migration_start(TrustMigrationGroup, TrustMigrationHost, TrustMigrationFini, void *); | |
14 | void migration_stop(trustmigration *); | |
82a316e7 | 15 | void createtrusttables(int); |
2b6e02e2 CP |
16 | |
17 | struct callbackdata { | |
18 | void *tag; | |
9bf9e8a1 | 19 | unsigned int hostid; |
2b6e02e2 CP |
20 | TrustDBMigrationCallback callback; |
21 | }; | |
22 | ||
23 | int trusts_migration_start(TrustDBMigrationCallback callback, void *tag) { | |
24 | struct callbackdata *cbd; | |
25 | ||
26 | if(migration) | |
be2823bc | 27 | return 1; |
2b6e02e2 | 28 | |
45989eab | 29 | cbd = nsmalloc(POOL_TRUSTS, sizeof(struct callbackdata)); |
9bf9e8a1 CP |
30 | if(!cbd) |
31 | return 2; | |
2b6e02e2 | 32 | |
9bf9e8a1 CP |
33 | cbd->callback = callback; |
34 | cbd->tag = tag; | |
35 | cbd->hostid = 1; | |
2b6e02e2 | 36 | |
35449aa5 | 37 | createtrusttables(TABLES_MIGRATION); |
be2823bc CP |
38 | trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_groups"); |
39 | trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_hosts"); | |
2b6e02e2 CP |
40 | |
41 | migration = migration_start(tm_group, tm_host, tm_final, cbd); | |
42 | if(!migration) { | |
45989eab | 43 | nsfree(POOL_TRUSTS, cbd); |
be2823bc | 44 | return 3; |
2b6e02e2 CP |
45 | } |
46 | ||
be2823bc | 47 | return 0; |
2b6e02e2 CP |
48 | } |
49 | ||
50 | void trusts_migration_stop(void) { | |
51 | if(!migration) | |
52 | return; | |
53 | ||
54 | migration_stop(migration); | |
55 | } | |
56 | ||
82a316e7 CP |
57 | static void tm_group(void *tag, trustgroup *tg) { |
58 | if(tg->id % 25 == 0) | |
59 | Error("trusts_migration", ERR_INFO, "Migration currently at id: %d", tg->id); | |
2b6e02e2 | 60 | |
82a316e7 | 61 | trustsdb_inserttg("migration_groups", tg); |
2b6e02e2 CP |
62 | } |
63 | ||
82a316e7 | 64 | static void tm_host(void *tag, trusthost *th, unsigned int groupid) { |
9bf9e8a1 CP |
65 | struct callbackdata *cbd = tag; |
66 | ||
82a316e7 CP |
67 | th->id = cbd->hostid++; |
68 | trustsdb_insertth("migration_hosts", th, groupid); | |
2b6e02e2 CP |
69 | } |
70 | ||
be2823bc | 71 | static void tm_complete(const DBAPIResult *r, void *tag) { |
2b6e02e2 | 72 | struct callbackdata *cbd = tag; |
be2823bc | 73 | int errcode = 0; |
2b6e02e2 | 74 | |
be2823bc CP |
75 | if(!r) { |
76 | errcode = MIGRATION_STOPPED; | |
2b6e02e2 | 77 | } else { |
be2823bc | 78 | if(!r->success) { |
82a316e7 | 79 | Error("trusts_migration", ERR_ERROR, "A error occured executing the rename table query."); |
83bccee3 | 80 | errcode = MIGRATION_LASTERROR; |
be2823bc | 81 | } else { |
82a316e7 | 82 | Error("trusts_migration", ERR_INFO, "Migration table copying complete."); |
be2823bc CP |
83 | } |
84 | r->clear(r); | |
2b6e02e2 CP |
85 | } |
86 | ||
9bf9e8a1 | 87 | if(cbd->callback) |
be2823bc | 88 | cbd->callback(errcode, cbd->tag); |
9bf9e8a1 | 89 | |
45989eab | 90 | nsfree(POOL_TRUSTS, cbd); |
2b6e02e2 | 91 | } |
be2823bc CP |
92 | |
93 | static void tm_final(void *tag, int errcode) { | |
94 | struct callbackdata *cbd = tag; | |
95 | migration = NULL; | |
96 | ||
97 | if(errcode) { | |
82a316e7 | 98 | Error("trusts_migration", ERR_ERROR, "Migration error: %d", errcode); |
be2823bc CP |
99 | if(cbd) { |
100 | cbd->callback(errcode, cbd->tag); | |
45989eab | 101 | nsfree(POOL_TRUSTS, cbd); |
be2823bc CP |
102 | } |
103 | } else { | |
83bccee3 CP |
104 | trusts_closedb(0); |
105 | ||
82a316e7 | 106 | Error("trusts_migration", ERR_INFO, "Migration completed, copying tables..."); |
83bccee3 | 107 | |
be2823bc CP |
108 | trustsdb->squery(trustsdb, "BEGIN TRANSACTION", ""); |
109 | trustsdb->squery(trustsdb, "DROP TABLE ?", "T", "groups"); | |
110 | trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO ?", "Ts", "migration_groups", "groups"); | |
111 | trustsdb->squery(trustsdb, "DROP TABLE ?", "T", "hosts"); | |
112 | trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO ?", "Ts", "migration_hosts", "hosts"); | |
113 | trustsdb->query(trustsdb, tm_complete, cbd, "COMMIT", ""); | |
114 | } | |
115 | } |