1 | #include "unInstall.h"
|
---|
2 |
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <stdarg.h>
|
---|
5 | #include "File.h"
|
---|
6 |
|
---|
7 | bool installManager::openLog(string filename, bool write)
|
---|
8 | { string command;
|
---|
9 | stringArray params;
|
---|
10 |
|
---|
11 | if (this->logfile.rdbuf()->is_open())
|
---|
12 | { this->closeLog();
|
---|
13 | }
|
---|
14 | this->logfileName = filename;
|
---|
15 | this->logfile.open(filename.c_str(), ios::out | ios::in);
|
---|
16 | if (write == false)
|
---|
17 | { while ((command = this->readCommand(params)) != "")
|
---|
18 | { if (command[0] == '[' && command[command.length()-1] == ']')
|
---|
19 | { this->setModule(command.substr(1, command.length() - 2));
|
---|
20 | }
|
---|
21 | else
|
---|
22 | { unInstallCommand action(command, params);
|
---|
23 | this->storeCommand(action);
|
---|
24 | }
|
---|
25 | }
|
---|
26 | }
|
---|
27 | this->setModule("default");
|
---|
28 | return true;
|
---|
29 | }
|
---|
30 |
|
---|
31 | bool installManager::reopenLog()
|
---|
32 | { if (this->logfile.rdbuf()->is_open())
|
---|
33 | { this->logfile.close();
|
---|
34 | }
|
---|
35 | else
|
---|
36 | { this->logfile.close();
|
---|
37 | }
|
---|
38 |
|
---|
39 | this->logfile.open(this->logfileName.c_str(), ios::out | ios::in);
|
---|
40 | return true;
|
---|
41 | }
|
---|
42 |
|
---|
43 | void installManager::setModule(string moduleName)
|
---|
44 | { this->currentModule = moduleName;
|
---|
45 | }
|
---|
46 |
|
---|
47 | bool installManager::storeCommand(unInstallCommand &command)
|
---|
48 | { this->modules[this->currentModule].push_back(command);
|
---|
49 | return true;
|
---|
50 | }
|
---|
51 |
|
---|
52 | bool installManager::writeCommand(unInstallCommand &command)
|
---|
53 | { if (!this->writeString(command.command))
|
---|
54 | { return false;
|
---|
55 | }
|
---|
56 | for (unsigned int p = 0; p < command.parameters.size(); p ++)
|
---|
57 | { if (!this->writeSeparator())
|
---|
58 | { return false;
|
---|
59 | }
|
---|
60 | if (!this->writeString(command.parameters[p]))
|
---|
61 | { return false;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | if (!this->writeString("\n"))
|
---|
65 | { return false;
|
---|
66 | }
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 |
|
---|
70 | bool installManager::writeString(char *buffer)
|
---|
71 | { string s(buffer);
|
---|
72 | return this->writeString(s);
|
---|
73 | }
|
---|
74 |
|
---|
75 | bool installManager::writeString(string str)
|
---|
76 | { bool quote;
|
---|
77 |
|
---|
78 | // TODO: check for space characters in str and quote if necessary
|
---|
79 | quote = str.find_first_of(' ') < str.length();
|
---|
80 |
|
---|
81 | if (quote)
|
---|
82 | { this->logfile << "\"";
|
---|
83 | }
|
---|
84 | this->logfile << str;
|
---|
85 | if (quote)
|
---|
86 | { this->logfile << "\"";
|
---|
87 | }
|
---|
88 | return true;
|
---|
89 | }
|
---|
90 |
|
---|
91 | bool installManager::writeSeparator()
|
---|
92 | { this->logfile << " ";
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 |
|
---|
96 | string installManager::readString()
|
---|
97 | { string reply = "";
|
---|
98 | char c;
|
---|
99 |
|
---|
100 | if (this->logfile.eof())
|
---|
101 | { return reply;
|
---|
102 | }
|
---|
103 |
|
---|
104 | this->logfile >> c;
|
---|
105 | while (c <= ' ' && !this->logfile.eof())
|
---|
106 | { this->logfile.get();
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (this->logfile.eof())
|
---|
110 | { return reply;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (c == '\"')
|
---|
114 | { do
|
---|
115 | { c = this->logfile.get();
|
---|
116 | if (c != '\"')
|
---|
117 | { reply += c;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | while (c != '\"' && !this->logfile.eof());
|
---|
121 | }
|
---|
122 | else
|
---|
123 | { while (c > ' ')
|
---|
124 | { reply += c;
|
---|
125 | if (this->logfile.eof())
|
---|
126 | { break;
|
---|
127 | }
|
---|
128 | c = this->logfile.get();
|
---|
129 | }
|
---|
130 | if (!this->logfile.eof())
|
---|
131 | { this->logfile.putback(c);
|
---|
132 | }
|
---|
133 | //this->logfile >> reply;
|
---|
134 | }
|
---|
135 | return reply;
|
---|
136 | }
|
---|
137 |
|
---|
138 | string installManager::readCommand(stringArray &array)
|
---|
139 | { string reply = "";
|
---|
140 | char c;
|
---|
141 |
|
---|
142 | array.clear();
|
---|
143 |
|
---|
144 | if (this->logfile.eof())
|
---|
145 | { return reply;
|
---|
146 | }
|
---|
147 |
|
---|
148 | reply = this->readString();
|
---|
149 | if (reply == "")
|
---|
150 | { return reply;
|
---|
151 | }
|
---|
152 |
|
---|
153 | while(!this->logfile.eof() &&
|
---|
154 | (c = this->logfile.get()) != '\n')
|
---|
155 | { this->logfile.putback(c);
|
---|
156 | array.add(this->readString());
|
---|
157 | }
|
---|
158 | if (!this->logfile.eof())
|
---|
159 | { this->logfile.putback(c);
|
---|
160 | }
|
---|
161 |
|
---|
162 | return reply;
|
---|
163 | }
|
---|
164 |
|
---|
165 | string installManager::popCommand(stringArray &array)
|
---|
166 | { string command;
|
---|
167 |
|
---|
168 | if (this->modules[this->currentModule].size() == 0)
|
---|
169 | { command = "";
|
---|
170 | }
|
---|
171 | else
|
---|
172 | { unsigned int last;
|
---|
173 |
|
---|
174 | last = this->modules[this->currentModule].size() - 1;
|
---|
175 | command = this->modules[this->currentModule][last].commandName();
|
---|
176 | array = this->modules[this->currentModule][last].parameterList();
|
---|
177 | this->modules[this->currentModule].erase(this->modules[this->currentModule].begin() + last);
|
---|
178 | }
|
---|
179 | return command;
|
---|
180 | }
|
---|
181 |
|
---|
182 | bool installManager::closeLog()
|
---|
183 | { if (this->logfile.rdbuf()->is_open() == false)
|
---|
184 | { return false;
|
---|
185 | }
|
---|
186 |
|
---|
187 | unInstallCommandMap::iterator here = this->modules.begin();
|
---|
188 | unInstallCommandMap::iterator end = this->modules.end();
|
---|
189 |
|
---|
190 | while (here != end)
|
---|
191 | { this->writeString("[" + (*here).first + "]\n");
|
---|
192 |
|
---|
193 | unInstallCommandList::iterator ahere = (*here).second.begin();
|
---|
194 | unInstallCommandList::iterator aend = (*here).second.end();
|
---|
195 | while (ahere != aend)
|
---|
196 | { this->writeCommand(*ahere);
|
---|
197 | ahere ++;
|
---|
198 | }
|
---|
199 | here ++;
|
---|
200 | }
|
---|
201 | this->logfile.close();
|
---|
202 | return true;
|
---|
203 | }
|
---|
204 |
|
---|
205 | bool installManager::isEmpty()
|
---|
206 | { unInstallCommandMap::iterator here = this->modules.begin();
|
---|
207 | unInstallCommandMap::iterator end = this->modules.end();
|
---|
208 |
|
---|
209 | while (here != end)
|
---|
210 | { this->writeString("[" + (*here).first + "]\n");
|
---|
211 |
|
---|
212 | unInstallCommandList::iterator ahere = (*here).second.begin();
|
---|
213 | unInstallCommandList::iterator aend = (*here).second.end();
|
---|
214 | if (ahere != aend)
|
---|
215 | { return false;
|
---|
216 | }
|
---|
217 | here ++;
|
---|
218 | }
|
---|
219 | return true;
|
---|
220 | }
|
---|
221 |
|
---|
222 | installManager::~installManager()
|
---|
223 | { if (this->logfile.rdbuf()->is_open())
|
---|
224 | { this->closeLog();
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | unInstaller::unInstaller(string &fileName)
|
---|
229 | { if (fileName != "")
|
---|
230 | { this->readFile(fileName);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | unInstaller::unInstaller(const char *fileName)
|
---|
235 | { string filename = fileName;
|
---|
236 |
|
---|
237 | if (fileName != "")
|
---|
238 | { this->readFile(filename);
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | bool unInstaller::readFile(string &fileName)
|
---|
243 | { FILE *f;
|
---|
244 | File finfo(fileName.c_str());
|
---|
245 | string currentGroup;
|
---|
246 | char *line, *eol;
|
---|
247 |
|
---|
248 | f = fopen(fileName.c_str(), "rb");
|
---|
249 | if (f == NULL)
|
---|
250 | { return false;
|
---|
251 | }
|
---|
252 |
|
---|
253 | char *buffer = new char[finfo.getFileSize()];
|
---|
254 | if (buffer == NULL)
|
---|
255 | { return false;
|
---|
256 | }
|
---|
257 |
|
---|
258 | if (fread(buffer, 1, finfo.getFileSize(), f) < finfo.getFileSize())
|
---|
259 | { return false;
|
---|
260 | }
|
---|
261 | fclose(f);
|
---|
262 |
|
---|
263 | //parse file into lines and send to the line fn
|
---|
264 | line = buffer;
|
---|
265 | while ((eol = strchr(line, '\x0a')) != NULL) // use 0a for the eol to remove
|
---|
266 | // dependency on dos 0d,0a eol
|
---|
267 | { *eol = '\0';
|
---|
268 |
|
---|
269 | this->getLine(line, currentGroup);
|
---|
270 |
|
---|
271 | line = eol + 1;
|
---|
272 | }
|
---|
273 | this->getLine(line, currentGroup);
|
---|
274 |
|
---|
275 | delete buffer;
|
---|
276 | return true;
|
---|
277 | }
|
---|
278 |
|
---|
279 | bool unInstaller::getLine(char *line, string &group)
|
---|
280 | { char *eol; // we (potentially) need to clean up the end of
|
---|
281 | // line, and we won't use the eol from the main
|
---|
282 | // getManifest fn. as for the last line it'd be
|
---|
283 | // null.
|
---|
284 |
|
---|
285 | // clean up the start of the line
|
---|
286 | while (*line <= ' ' && *line != '\0')
|
---|
287 | { line ++;
|
---|
288 | }
|
---|
289 |
|
---|
290 | // it's a blank line - don't bother!
|
---|
291 | if (line == '\0')
|
---|
292 | { return true;
|
---|
293 | }
|
---|
294 |
|
---|
295 | // clean up the end of line
|
---|
296 | eol = line + strlen(line) - 1;
|
---|
297 | while (*eol <= ' ' && eol >= line)
|
---|
298 | { *eol = '\0';
|
---|
299 | eol --;
|
---|
300 | }
|
---|
301 |
|
---|
302 | // if it's a new group, read in the group name
|
---|
303 | if (*line == '[' && *eol == ']')
|
---|
304 | { string newgroup(line, 1, eol - line - 1);
|
---|
305 | group = newgroup;
|
---|
306 | return true;
|
---|
307 | }
|
---|
308 | // otherwise it should be a command
|
---|
309 | else
|
---|
310 | { char *equals = strchr(line, '=');
|
---|
311 |
|
---|
312 | // if it has no equals it's invalid; ignore it
|
---|
313 | if (equals == NULL)
|
---|
314 | { return true;
|
---|
315 | }
|
---|
316 | else
|
---|
317 | { unInstallCommand command;
|
---|
318 |
|
---|
319 | string cmd(line, 0, equals-line);
|
---|
320 | command.command = cmd;
|
---|
321 |
|
---|
322 | stringArray params = stringArray::words(equals+1);
|
---|
323 | command.parameters = params;
|
---|
324 |
|
---|
325 | this->_map[group].push_back(command);
|
---|
326 | }
|
---|
327 | }
|
---|
328 | return true;
|
---|
329 | }
|
---|
330 |
|
---|
331 | bool unInstaller::saveToFile(string fileName)
|
---|
332 | { unInstallCommandMap::iterator here = this->_map.begin();
|
---|
333 | unInstallCommandMap::iterator end = this->_map.end();
|
---|
334 | string group = "";
|
---|
335 |
|
---|
336 | FILE *f;
|
---|
337 | f = fopen(fileName.c_str(), "wb");
|
---|
338 | if (f == NULL)
|
---|
339 | { return false;
|
---|
340 | }
|
---|
341 |
|
---|
342 | while (here != end)
|
---|
343 | { // if we've changed groups, note the start of the new one
|
---|
344 | fprintf(f, "[%s]\n", (*here).first.c_str());
|
---|
345 |
|
---|
346 | // write the group line-by-line
|
---|
347 | unInstallCommandList::iterator groupHere = (*here).second.begin();
|
---|
348 | unInstallCommandList::iterator groupEnd = (*here).second.end();
|
---|
349 | while (groupHere != groupEnd)
|
---|
350 | { fprintf(f, "%s=", (*groupHere).command.c_str());
|
---|
351 |
|
---|
352 | fprintf(f, "%s", (*groupHere).parameters[0].c_str());
|
---|
353 | for (unsigned int p = 1; p < (*groupHere).parameters.size(); p ++)
|
---|
354 | { fprintf(f, " %s", (*groupHere).parameters[p].c_str());
|
---|
355 | }
|
---|
356 | groupEnd ++;
|
---|
357 | }
|
---|
358 | here ++;
|
---|
359 | }
|
---|
360 | fclose(f);
|
---|
361 | return true;
|
---|
362 | }
|
---|
363 |
|
---|
364 | void unInstaller::unInstall(string &listName)
|
---|
365 | { if (listName == uninstall_ALL)
|
---|
366 | { unInstallCommandMap::reverse_iterator here = this->_map.rbegin();
|
---|
367 | unInstallCommandMap::reverse_iterator end = this->_map.rend();
|
---|
368 |
|
---|
369 | while (here != end)
|
---|
370 | { this->unInstallGroup((*here).second);
|
---|
371 | here ++;
|
---|
372 | }
|
---|
373 | this->_map.empty();
|
---|
374 | }
|
---|
375 | else
|
---|
376 | { this->unInstallGroup(this->_map[listName]);
|
---|
377 | this->_map.erase(listName);
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | void unInstaller::unInstallGroup(unInstallCommandList &list)
|
---|
382 | { for (int i = list.size() - 1; i >= 0; i --)
|
---|
383 | { // execute uninstall
|
---|
384 | this->execCommand(list[i].command, list[i].parameters);
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | bool unInstaller::execCommand(string &name, stringArray ¶ms)
|
---|
389 | { // undo the command
|
---|
390 | return true;
|
---|
391 | }
|
---|
392 |
|
---|
393 | bool unInstaller::record(string group, string cmd, string parameter)
|
---|
394 | { unInstallCommand command;
|
---|
395 |
|
---|
396 | command.command = cmd;
|
---|
397 | command.parameters.add(parameter);
|
---|
398 |
|
---|
399 | this->_map[group].push_back(command);
|
---|
400 | return true;
|
---|
401 | }
|
---|
402 |
|
---|
403 | bool unInstaller::record(string group, string cmd, int count, ...)
|
---|
404 | { va_list list;
|
---|
405 |
|
---|
406 | va_start(list, count);
|
---|
407 | unInstallCommand command;
|
---|
408 |
|
---|
409 | command.command = cmd;
|
---|
410 | command.parameters.add(*va_arg(list, string *));
|
---|
411 | va_end(list);
|
---|
412 |
|
---|
413 | this->_map[group].push_back(command);
|
---|
414 |
|
---|
415 | return true;
|
---|
416 | }
|
---|