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