]> git.stg.codes - stg.git/blob - projects/convertor/main.cpp
Fix firebird store plugin compilation errors
[stg.git] / projects / convertor / main.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21  /*
22  $Revision: 1.11 $
23  $Date: 2010/03/25 12:32:30 $
24  $Author: faust $
25  */
26
27 #include <dlfcn.h>
28
29 #include <string>
30 #include <vector>
31 #include <iostream>
32 #include <ctime>
33 #include <algorithm>
34
35 #include "common.h"
36 #include "store.h"
37 #include "settings_impl.h"
38 #include "conffiles.h"
39
40 #include "user_stat.h"
41 #include "user_conf.h"
42 #include "corp_conf.h"
43 #include "service_conf.h"
44 #include "admin_conf.h"
45 #include "tariff_conf.h"
46 #include "settings.h"
47 #include "stg_message.h"
48
49 using namespace std;
50
51 volatile time_t stgTime = time(NULL);
52
53 int main(int argc, char **argv)
54 {
55 printfd(__FILE__, "Start\n");
56
57 STORE * fromStore = NULL;
58 STORE * toStore = NULL;
59
60 SETTINGS_IMPL * settings = NULL;
61
62 string modulePath;
63
64 MODULE_SETTINGS fromStoreSettings;
65 MODULE_SETTINGS toStoreSettings;
66
67 ADMIN_CONF ac;
68 USER_CONF uc;
69 USER_STAT us;
70 STG_MSG msg;
71 TARIFF_DATA td;
72 CORP_CONF cc;
73 SERVICE_CONF sc;
74 vector<STG_MSG_HDR> hdrs;
75
76 if (argc == 2)
77     settings = new SETTINGS_IMPL(argv[1]);
78 else
79     settings = new SETTINGS_IMPL();
80
81 if (settings->ReadSettings())
82     {
83     printfd(__FILE__, "Error reading settings\n");
84     delete settings;
85     return -1;
86     }
87
88 fromStoreSettings = settings->GetSourceStoreModuleSettings();
89 toStoreSettings = settings->GetDestStoreModuleSettings();
90 modulePath = settings->GetModulesPath();
91
92 string sourcePlugin(modulePath + "/mod_" + fromStoreSettings.moduleName + ".so");
93 string destPlugin(modulePath + "/mod_" + toStoreSettings.moduleName + ".so");
94
95 void * src_lh = dlopen(sourcePlugin.c_str(), RTLD_NOW);
96 if (!src_lh)
97     {
98     printfd(__FILE__, "Source storage plugin loading failed: %s\n", dlerror());
99     delete settings;
100     return -1;
101     }
102
103 void * dst_lh = dlopen(destPlugin.c_str(), RTLD_NOW);
104 if (!dst_lh)
105     {
106     printfd(__FILE__, "Destination storage plugin loading failed: %s\n", dlerror());
107     delete settings;
108     return -1;
109     }
110
111 STORE * (*GetSourceStore)();
112 STORE * (*GetDestStore)();
113 GetSourceStore = (STORE * (*)())dlsym(src_lh, "GetStore");
114 if (!GetSourceStore)
115     {
116     printfd(__FILE__, "Source storage plugin loading failed. GetStore not found: %s\n", dlerror());
117     delete settings;
118     return -1;
119     }
120 GetDestStore = (STORE * (*)())dlsym(dst_lh, "GetStore");
121 if (!GetDestStore)
122     {
123     printfd(__FILE__, "Storage plugin (firebird) loading failed. GetStore not found: %s\n", dlerror());
124     delete settings;
125     return -1;
126     }
127
128 fromStore = GetSourceStore();
129 toStore = GetDestStore();
130
131 vector<string> entities;
132 vector<string> ready;
133 fromStore->SetSettings(fromStoreSettings);
134 fromStore->ParseSettings();
135 toStore->SetSettings(toStoreSettings);
136 toStore->ParseSettings();
137
138 printfd(__FILE__, "Importing admins:\n");
139 entities.erase(entities.begin(), entities.end());
140 ready.erase(ready.begin(), ready.end());
141 if (fromStore->GetAdminsList(&entities))
142     {
143     printfd(__FILE__, "Error getting admins list: %s\n", fromStore->GetStrError().c_str());
144     dlclose(src_lh);
145     dlclose(dst_lh);
146     delete settings;
147     return -1;
148     }
149 if (toStore->GetAdminsList(&ready))
150     {
151     printfd(__FILE__, "Error getting admins list: %s\n", toStore->GetStrError().c_str());
152     dlclose(src_lh);
153     dlclose(dst_lh);
154     delete settings;
155     return -1;
156     }
157
158 vector<string>::const_iterator it;
159 for (it = entities.begin(); it != entities.end(); ++it)
160     {
161     printfd(__FILE__, "\t - %s\n", it->c_str());
162     if (find(ready.begin(), ready.end(), *it) == ready.end())
163         if (toStore->AddAdmin(*it))
164             {
165             printfd(__FILE__, "Error adding admin: %s\n", toStore->GetStrError().c_str());
166             dlclose(src_lh);
167             dlclose(dst_lh);
168             delete settings;
169             return -1;
170             }
171     if (fromStore->RestoreAdmin(&ac, *it))
172         {
173         printfd(__FILE__, "Error getting admin's confi: %s\n", fromStore->GetStrError().c_str());
174         dlclose(src_lh);
175         dlclose(dst_lh);
176         delete settings;
177         return -1;
178         }
179     ac.login = *it;
180     if (toStore->SaveAdmin(ac))
181         {
182         printfd(__FILE__, "Error saving admin's conf: %s\n", toStore->GetStrError().c_str());
183         dlclose(src_lh);
184         dlclose(dst_lh);
185         delete settings;
186         return -1;
187         }
188     }
189
190 printfd(__FILE__, "Importing tariffs:\n");
191 entities.erase(entities.begin(), entities.end());
192 ready.erase(ready.begin(), ready.end());
193 if (fromStore->GetTariffsList(&entities))
194     {
195     printfd(__FILE__, "Error getting tariffs list: %s\n", fromStore->GetStrError().c_str());
196     dlclose(src_lh);
197     dlclose(dst_lh);
198     delete settings;
199     return -1;
200     }
201 if (toStore->GetTariffsList(&ready))
202     {
203     printfd(__FILE__, "Error getting tariffs list: %s\n", toStore->GetStrError().c_str());
204     dlclose(src_lh);
205     dlclose(dst_lh);
206     delete settings;
207     return -1;
208     }
209
210 for (it = entities.begin(); it != entities.end(); ++it)
211     {
212     printfd(__FILE__, "\t - %s\n", it->c_str());
213     if (find(ready.begin(), ready.end(), *it) == ready.end())
214         if (toStore->AddTariff(*it))
215             {
216             printfd(__FILE__, "Error adding tariff: %s\n", toStore->GetStrError().c_str());
217             dlclose(src_lh);
218             dlclose(dst_lh);
219             delete settings;
220             return -1;
221             }
222     if (fromStore->RestoreTariff(&td, *it))
223         {
224         printfd(__FILE__, "Error getting tariff's data: %s\n", fromStore->GetStrError().c_str());
225         dlclose(src_lh);
226         dlclose(dst_lh);
227         delete settings;
228         return -1;
229         }
230     if (toStore->SaveTariff(td, *it))
231         {
232         printfd(__FILE__, "Error saving tariff's data: %s\n", toStore->GetStrError().c_str());
233         dlclose(src_lh);
234         dlclose(dst_lh);
235         delete settings;
236         return -1;
237         }
238     }
239
240 printfd(__FILE__, "Importing services:\n");
241 entities.erase(entities.begin(), entities.end());
242 ready.erase(ready.begin(), ready.end());
243 if (fromStore->GetServicesList(&entities))
244     {
245     printfd(__FILE__, "Error getting service list: %s\n", fromStore->GetStrError().c_str());
246     dlclose(src_lh);
247     dlclose(dst_lh);
248     delete settings;
249     return -1;
250     }
251 if (toStore->GetServicesList(&ready))
252     {
253     printfd(__FILE__, "Error getting service list: %s\n", toStore->GetStrError().c_str());
254     dlclose(src_lh);
255     dlclose(dst_lh);
256     delete settings;
257     return -1;
258     }
259
260 for (it = entities.begin(); it != entities.end(); ++it)
261     {
262     printfd(__FILE__, "\t - %s\n", it->c_str());
263     if (find(ready.begin(), ready.end(), *it) == ready.end())
264         if (toStore->AddService(*it))
265             {
266             printfd(__FILE__, "Error adding service: %s\n", toStore->GetStrError().c_str());
267             dlclose(src_lh);
268             dlclose(dst_lh);
269             delete settings;
270             return -1;
271             }
272     if (fromStore->RestoreService(&sc, *it))
273         {
274         printfd(__FILE__, "Error getting service's data: %s\n", fromStore->GetStrError().c_str());
275         dlclose(src_lh);
276         dlclose(dst_lh);
277         delete settings;
278         return -1;
279         }
280     if (toStore->SaveService(sc))
281         {
282         printfd(__FILE__, "Error saving service's data: %s\n", toStore->GetStrError().c_str());
283         dlclose(src_lh);
284         dlclose(dst_lh);
285         delete settings;
286         return -1;
287         }
288     }
289
290 printfd(__FILE__, "Importing corporations:\n");
291 entities.erase(entities.begin(), entities.end());
292 ready.erase(ready.begin(), ready.end());
293 if (fromStore->GetCorpsList(&entities))
294     {
295     printfd(__FILE__, "Error getting corporations list: %s\n", fromStore->GetStrError().c_str());
296     dlclose(src_lh);
297     dlclose(dst_lh);
298     delete settings;
299     return -1;
300     }
301 if (toStore->GetCorpsList(&ready))
302     {
303     printfd(__FILE__, "Error getting corporations list: %s\n", toStore->GetStrError().c_str());
304     dlclose(src_lh);
305     dlclose(dst_lh);
306     delete settings;
307     return -1;
308     }
309
310 for (it = entities.begin(); it != entities.end(); ++it)
311     {
312     printfd(__FILE__, "\t - %s\n", it->c_str());
313     if (find(ready.begin(), ready.end(), *it) == ready.end())
314         if (toStore->AddCorp(*it))
315             {
316             printfd(__FILE__, "Error adding corporation: %s\n", toStore->GetStrError().c_str());
317             dlclose(src_lh);
318             dlclose(dst_lh);
319             delete settings;
320             return -1;
321             }
322     if (fromStore->RestoreCorp(&cc, *it))
323         {
324         printfd(__FILE__, "Error getting corporation's data: %s\n", fromStore->GetStrError().c_str());
325         dlclose(src_lh);
326         dlclose(dst_lh);
327         delete settings;
328         return -1;
329         }
330     if (toStore->SaveCorp(cc))
331         {
332         printfd(__FILE__, "Error saving corporation's data: %s\n", toStore->GetStrError().c_str());
333         dlclose(src_lh);
334         dlclose(dst_lh);
335         delete settings;
336         return -1;
337         }
338     }
339
340 printfd(__FILE__, "Importing users:\n");
341 entities.erase(entities.begin(), entities.end());
342 ready.erase(ready.begin(), ready.end());
343 if (fromStore->GetUsersList(&entities))
344     {
345     printfd(__FILE__, "Error getting users list: %s\n", fromStore->GetStrError().c_str());
346     dlclose(src_lh);
347     dlclose(dst_lh);
348     delete settings;
349     return -1;
350     }
351 if (toStore->GetUsersList(&ready))
352     {
353     printfd(__FILE__, "Error getting users list: %s\n", toStore->GetStrError().c_str());
354     dlclose(src_lh);
355     dlclose(dst_lh);
356     delete settings;
357     return -1;
358     }
359
360 sort(ready.begin(), ready.end());
361 for (it = entities.begin(); it != entities.end(); ++it)
362     {
363     printfd(__FILE__, "\t - %s\n", it->c_str());
364     if (!binary_search(ready.begin(), ready.end(), *it)) {
365         if (toStore->AddUser(*it))
366             {
367             printfd(__FILE__, "Error adding user: %s\n", toStore->GetStrError().c_str());
368             dlclose(src_lh);
369             dlclose(dst_lh);
370             delete settings;
371             return -1;
372             }
373     } else {
374         printfd(__FILE__, "\t\t(adding passed)\n");
375     }
376     if (fromStore->RestoreUserConf(&uc, *it))
377         {
378         printfd(__FILE__, "Error getting user's conf: %s\n", fromStore->GetStrError().c_str());
379         dlclose(src_lh);
380         dlclose(dst_lh);
381         delete settings;
382         return -1;
383         }
384     if (fromStore->RestoreUserStat(&us, *it))
385         {
386         printfd(__FILE__, "Error getting user's stat: %s\n", fromStore->GetStrError().c_str());
387         dlclose(src_lh);
388         dlclose(dst_lh);
389         delete settings;
390         return -1;
391         }
392     if (toStore->SaveUserConf(uc, *it))
393         {
394         printfd(__FILE__, "Error saving user's conf: %s\n", toStore->GetStrError().c_str());
395         dlclose(src_lh);
396         dlclose(dst_lh);
397         delete settings;
398         return -1;
399         }
400     if (toStore->SaveUserStat(us, *it))
401         {
402         printfd(__FILE__, "Error saving user's stat: %s\n", toStore->GetStrError().c_str());
403         dlclose(src_lh);
404         dlclose(dst_lh);
405         delete settings;
406         return -1;
407         }
408     hdrs.erase(hdrs.begin(), hdrs.end());
409     if (fromStore->GetMessageHdrs(&hdrs, *it))
410         {
411         printfd(__FILE__, "Error getting user's messages: %s\n", fromStore->GetStrError().c_str());
412         dlclose(src_lh);
413         dlclose(dst_lh);
414         delete settings;
415         return -1;
416         }
417     vector<STG_MSG_HDR>::iterator mit;
418     for (mit = hdrs.begin(); mit != hdrs.end(); ++mit)
419         {
420         if (fromStore->GetMessage(mit->id, &msg, *it))
421             {
422             printfd(__FILE__, "Error getting message for a user: %s\n", fromStore->GetStrError().c_str());
423             dlclose(src_lh);
424             dlclose(dst_lh);
425             delete settings;
426             return -1;
427             }
428         printfd(__FILE__, "\t\t * %s\n", msg.text.c_str());
429         if (toStore->AddMessage(&msg, *it))
430             {
431             printfd(__FILE__, "Error adding message to a user: %s\n", toStore->GetStrError().c_str());
432             dlclose(src_lh);
433             dlclose(dst_lh);
434             delete settings;
435             return -1;
436             }
437         }
438     }
439
440 dlclose(src_lh);
441 dlclose(dst_lh);
442 printfd(__FILE__, "Done\n");
443 delete settings;
444 return 0;
445 }