-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.cpp
408 lines (375 loc) · 16.1 KB
/
handle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* Copyright (C) Johnny Saenz
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. */
#include "handle.h"
namespace JSApm
{
/**
* Constructor for handle class.
*/
Handle::Handle()
{
//@Del-l//
objgetinfo_av = new GetInfo(1);
//@Del-l//
objgetinfo_lo = new GetInfo(2);
//@Del-l//
objutils = new Utils;
i = 0;
}
/**
* Takes an integer and squares it
* @param package: Name of package
* @param operate: The operate, default is "0"
* @param ver: The version, default is "0"
* @param nline: The nline, default is 0
* @return The struct who contents list of candidates for upgrade, install and revupgrade
*/
candidates_struct Handle::handle_candidates(std::string package, std::string operate, std::string ver, int nline)
{
std::cout << "Gathering dependencies...";
handle_dependencies(package, operate, ver, nline);
std::cout << "[OK]" << std::endl;
// Vector who contents the final list of packages, results of the handle_dependencies function.
std::vector<meta_final> rec_saver_final;
std::cout << "Verifying conflicts...";
for (std::map<std::string, meta>::iterator iTmap = rec_saver.begin(); iTmap != rec_saver.end(); iTmap++)
{
checkRepeatPkg(iTmap);
insertPkgInRecSaver(iTmap, rec_saver_final, isMoreEqualPkg(iTmap));
// Litle progress bar.
std::cout << ".";
std::cout << std::flush;
}
std::cout << "[OK]" << std::endl;
std::cout << "Generating candidates...";
candidates_struct candidates_obj = parseOpAndFillCandStruct(rec_saver_final);
std::cout << "[OK]" << std::endl;
return candidates_obj;
}
/**
* Mmember that generate rec_saver map, who contains dependencies with aditional information.
* @param package: Name of package
* @param operate: The operate, default is "0"
* @param version: The version, default is "0"
* @param nline: The nline, default is 0
*/
void Handle::handle_dependencies(std::string package, std::string operate, std::string version, int nline)
{
std::map<std::string, meta>::iterator iTrec_saver;
for (iTrec_saver = rec_saver.begin(); iTrec_saver != rec_saver.end(); iTrec_saver++)
{
//Exit if the package limit is reached
if( i >= MAX_PKG_NUM-1 )
{
std::cout << "Maximum number of dependencies reached" << std::endl;
exit(2);
}
meta meta_tmp;
std::map<std::string, meta>::iterator iTpackage = rec_saver.find(package);
if ( iTpackage != rec_saver.end() )
{
//copiamos el meta.data(que es un std::map) del actual iterador a meta_tmp
meta_tmp.data = (*iTpackage).second.data;
//insertamos si es que no existen los nuevos elementos s meta_tmp.
meta_tmp.data[make_pair(version, operate)] = nline;
//insertamos meta_tmp a rec_saver, reemplazando el contenido del antiguo indice
rec_saver[package] = meta_tmp;
i++;
return;
}
i++;
}
/***
Reservamos memoria dinamica
***/
if ( version != "" && operate != "" && nline != 0 )
{
meta meta_obj;
meta_obj.data[make_pair(version, operate)] = nline;
rec_saver[package] = meta_obj;
std::cout << ".";
std::cout << std::flush;
}
else if( version == "" && operate == "" && nline == 0 )
{
meta meta_obj;
std::string version;
if ( ! objgetinfo_av->GetPkgTag(package, 0, version) )
{
std::cout << "[BAD]" << std::endl;
std::cout << "Tag not found" << std::endl;
exit(2);
}
meta_obj.data[make_pair(version, "=")] = -1;
rec_saver[package] = meta_obj;
std::cout << ".";
std::cout << std::flush;
}
/***
Mostramos una "barrita de progreso".
***/
std::vector<Programa> vector_map;
objgetinfo_av->get_DependsMap(package, vector_map);
std::vector<Programa>::iterator it = vector_map.begin();
/***
Loop que llama en forma recursiva a esta funcion.
***/
while( it != vector_map.end() )
{
handle_dependencies((*it).name, (*it).op, (*it).ver, (*it).nline);
it++;
}
}
void Handle::MsgError(std::map<std::string, meta>::iterator &iTmap)
{
std::cout << "[BAD]" << std::endl;
std::cout << "\n" << "* " << (*iTmap).first << std::endl;
for (std::map<std::pair<std::string, std::string>, int>::iterator iT = (*iTmap).second.data.begin();
iT != (*iTmap).second.data.end(); iT++)
{
if ( (*iT).second == -1 )
{
std::cout << "[version] -> '" << (*iT).first.first << "'\t";
std::cout << "[operator] -> '" << (*iT).first.second << "'\t";
std::cout << "[origin] -> '" << (*iTmap).first << "(group)'" << std::endl;
}
else
{
std::cout << "[version] -> '" << (*iT).first.first << "'\t";
std::cout << "[operator] -> '" << (*iT).first.second << "'\t";
std::cout << "[origin] -> '" << (*iT).second << "(line)'" << std::endl;
}
}
std::cout << "\n";
exit(2);
}
void Handle::checkRepeatPkg(std::map<std::string, meta>::iterator & iTmap)
{
// First phase
// Iterator for the std::map, who is in to the struct "meta"
std::map<std::pair<std::string, std::string>, int>::iterator iTdata = (*iTmap).second.data.begin();
for(++iTdata; iTdata != (*iTmap).second.data.end(); iTdata++)
{
iTdata--;
std::map<std::pair<std::string, std::string>, int>::iterator iTtmp = iTdata;
iTdata++;
if ( (*iTtmp).first.first == (*iTdata).first.first )
{
((*iTmap).second.data).erase(iTtmp);
std::string version_tmp = (*iTdata).first.first;
int nline_tmp = (*iTdata).second;
((*iTmap).second.data).erase(iTdata);
(*iTmap).second.data[std::make_pair(version_tmp, "=")] = nline_tmp;
}
}
}
int Handle::isMoreEqualPkg(std::map<std::string, meta>::iterator & iTmap)
{
// Second phase
// return: is_more_equal
int back_is = 0; // "ge"==1; "le"==2; "ee"==3
int is_more_equal = 0;
// Iterator for the std::map, who is in to the struct "meta"
std::map<std::pair<std::string, std::string>, int>::iterator iTdata;
iTdata = (*iTmap).second.data.begin();
for (iTdata; iTdata != (*iTmap).second.data.end(); iTdata++)
{
if ( ! (*iTdata).first.second.compare(">=") )
{
if ( back_is == 1 ) { continue; } /*OK*/
if ( back_is == 2 ) { MsgError(iTmap); } // ERR[deberia ser <=]
if ( back_is == 3 && is_more_equal < 2) { MsgError(iTmap); } // ERR [deberia ser <=]
back_is = 1;
}
else if ( ! (*iTdata).first.second.compare("<=") )
{
if ( back_is == 1 ) { MsgError(iTmap); } // ERR [deberia ser >=]
if ( back_is == 2 ) { continue; } /*OK*/
if ( back_is == 3 && is_more_equal < 2) { continue; } /*OK*/
back_is = 2;
}
else if ( ! (*iTdata).first.second.compare("=") )
{
if ( back_is == 1 ) { continue; } /*OK*/
if ( back_is == 2 ) { MsgError(iTmap); } // ERR[deberia ser <=]
if ( back_is == 3 ) { MsgError(iTmap); } // ERR["=" consecutivos]
back_is = 3;
is_more_equal++;
}
else
{
std::cout << "Operador no valido!" << std::endl;
std::cout << "line '" << (*iTdata).second << "'" << std::endl;
exit(2);
}
}
return is_more_equal;
}
void Handle::insertPkgInRecSaver(std::map<std::string, meta>::iterator & iTmap, std::vector<meta_final> & vector_target, int is_more_equal)
{
// Third Phase
// Iterator for the std::map, who is in to the struct "meta"
std::map<std::pair<std::string, std::string>, int>::iterator iTdata;
iTdata = (*iTmap).second.data.begin();
std::map<std::pair<std::string, std::string>, int>::iterator iTdata_tmp;
// Now we inserted in rec_saver_final the elements.
if ( is_more_equal = 1 )
{
meta_final meta_final_str;
if ( ! (iTdata->first.second.compare("<=")) )
{
iTdata_tmp = (*iTmap).second.data.begin();
meta_final_str.package = (*iTmap).first;
meta_final_str.version = (*iTdata_tmp).first.first;
meta_final_str.operate = (*iTdata_tmp).first.second;
vector_target.push_back(meta_final_str);
}
else if ( ! (iTdata->first.second.compare(">=")) )
{
iTdata_tmp = (*iTmap).second.data.end();
iTdata_tmp--;
meta_final_str.package = (*iTmap).first;
meta_final_str.version = (*iTdata_tmp).first.first;
meta_final_str.operate = (*iTdata_tmp).first.second;
vector_target.push_back(meta_final_str);
}
else
{
iTdata_tmp = (*iTmap).second.data.begin();
for(iTdata_tmp; iTdata_tmp != (*iTmap).second.data.end(); iTdata_tmp++)
{
if( ! (iTdata_tmp->first.second.compare("=")) )
{
meta_final_str.package = (*iTmap).first;
meta_final_str.version = (*iTdata_tmp).first.first;
meta_final_str.operate = (*iTdata_tmp).first.second;
vector_target.push_back(meta_final_str);
}
}
}
}
}
candidates_struct Handle::parseOpAndFillCandStruct(std::vector<meta_final> & vector_origin)
{
// Struct who contents contents list of candidates for upgrade, install and revupgrade
candidates_struct candidates_obj;
// Generating list
for (std::vector<meta_final>::iterator iTrec_vector_origin = vector_origin.begin();
iTrec_vector_origin != vector_origin.end(); iTrec_vector_origin++)
{
std::string package((*iTrec_vector_origin).package);
std::string operate((*iTrec_vector_origin).operate);
std::string version((*iTrec_vector_origin).version);
int version_dep = objutils->transform_version((*iTrec_vector_origin).version);
std::string version_av_str; objgetinfo_av->GetPkgTag(package, 0, version_av_str);
int version_av = objutils->transform_version(version_av_str);
std::cout << ".";
std::cout << std::flush;
if ( objgetinfo_lo->PkgExist(package) )
{
std::string version_lo_str;
if ( ! objgetinfo_lo->GetPkgTag(package, 0, version_lo_str) )
{
std::cout << "No existe tag" << "Version" << std::endl;
exit(2);
}
int version_lo = objutils->transform_version(version_lo_str);
if ( operate == ">=" )
{
if ( version_lo >= version_dep ) { /* ">=Ya no se instala " << rec_saver[i][0] */ }
else
{
//cout << ">= Se actualiza " << rec_saver[i][0] << endl;
if ( version_av < version_dep )
{
std::cout << "No se ha encontrado candidato que satisfaga: " << (*iTrec_vector_origin).package << " >= " << (*iTrec_vector_origin).version << std::endl;
exit(1);
}
else
{
//cout << "Se ha encontrado candidato" << endl;
candidates_obj.candidates_upgrade.push_back(package);
}
}
}
if ( operate == "=" )
{
if ( version_lo == version_dep)
{
//cout << "Ya no se instala " << rec_saver[i][0] << endl;
}
else
{
if ( version_lo > version_dep )
{
//cout << "= Se desactualiza el paquete" << rec_saver[i][0] << endl;
if ( version_av != version_dep )
{
std::cout << "No se ha encontrado candidato que satisfaga: " << (*iTrec_vector_origin).package << " = " << (*iTrec_vector_origin).version << std::endl;
exit(1);
}
else
{
//cout << "Se ha encontrado candidato" << endl;
candidates_obj.candidates_revupgrade.push_back(package);
}
}
if ( version_lo < version_dep )
{
//cout << "= Se actualiza el paquete " << rec_saver[i][0] << endl;
if (version_av != version_dep )
{
std::cout << "No se ha encontrado candidato que satisfaga: " << (*iTrec_vector_origin).package << " = " << (*iTrec_vector_origin).version << std::endl;
exit(1);
}
else
{
//cout << "Se ha encontrado candidato" << endl;
candidates_obj.candidates_upgrade.push_back(package);
}
}
}
}
if ( operate == "<=" )
{
if ( version_lo <= version_dep)
{
//cout << "<= Ya no se instala " << rec_saver[i][0] << endl;
}
else
{
//cout << "<= Se desactualiza el paquete " << rec_saver[i][0] << endl;
if ( version_av > version_dep )
{
std::cout << "No se ha encontrado candidato que satisfaga: " << (*iTrec_vector_origin).package << " <= " << (*iTrec_vector_origin).version << std::endl;
exit(1);
}
else
{
//cout << "Se ha encontrado candidato" << endl;
candidates_obj.candidates_revupgrade.push_back(package);
}
}
}
}
else
{
//cout << "Paquete Nuevo" << endl;
candidates_obj.candidates_new.push_back(package);
}
}
return candidates_obj;
}
Handle::~Handle()
{
delete objgetinfo_av;
delete objgetinfo_lo;
}
}