blob: fa5ef5640b9e2507416bda4c59387921b84f8023 [file] [log] [blame]
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <iostream>
20#include <fstream>
21#include <sstream>
22#include <string>
23#include <vector>
24#include <string.h>
25#include <libgen.h>
26#include <unistd.h>
27#include <sys/stat.h>
28#include <dirent.h>
bigbiff bigbiff84a3f1a2013-12-28 18:32:15 -050029#include <errno.h>
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040030#include "gui/rapidxml.hpp"
31#include "fixPermissions.hpp"
32#include "twrp-functions.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000033#include "twcommon.h"
bigbiff bigbiff872a3b92013-10-18 20:50:25 -040034#ifdef HAVE_SELINUX
35#include "selinux/selinux.h"
36#include "selinux/label.h"
37#include "selinux/android.h"
38#include "selinux/label.h"
39#endif
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040040
41using namespace std;
42using namespace rapidxml;
43
bigbiff bigbiff872a3b92013-10-18 20:50:25 -040044#ifdef HAVE_SELINUX
45int fixPermissions::restorecon(string entry, struct stat *sb) {
46 char *oldcontext, *newcontext;
47 struct selabel_handle *sehandle;
48
49 sehandle = selinux_android_file_context_handle();
50 if (lgetfilecon(entry.c_str(), &oldcontext) < 0) {
51 LOGINFO("Couldn't get selinux context for %s\n", entry.c_str());
52 return -1;
53 }
54 if (selabel_lookup(sehandle, &newcontext, entry.c_str(), sb->st_mode) < 0) {
55 LOGINFO("Couldn't lookup selinux context for %s\n", entry.c_str());
56 return -1;
57 }
58 LOGINFO("Relabeling %s from %s to %s\n", entry.c_str(), oldcontext, newcontext);
59 if (lsetfilecon(entry.c_str(), newcontext) < 0) {
60 LOGINFO("Couldn't label %s with %s: %s\n", entry.c_str(), newcontext, strerror(errno));
61 }
62 freecon(oldcontext);
63 freecon(newcontext);
64 return 0;
65}
66
67int fixPermissions::fixDataDataContexts(void) {
68 DIR *d;
69 struct dirent *de;
70 struct stat sb;
71 struct selabel_handle *selinux_handle;
72 struct selinux_opt selinux_options[] = {
73 { SELABEL_OPT_PATH, "/file_contexts" }
74 };
75 selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1);
76 if (!selinux_handle)
77 printf("No file contexts for SELinux\n");
78 else
79 printf("SELinux contexts loaded from /file_contexts\n");
80 d = opendir("/data/data");
81 while (( de = readdir(d)) != NULL) {
82 stat(de->d_name, &sb);
83 string f = "/data/data/";
84 f = f + de->d_name;
85 restorecon(f, &sb);
86 }
87 return 0;
88}
89#endif
90
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040091int fixPermissions::fixPerms(bool enable_debug, bool remove_data_for_missing_apps) {
92 packageFile = "/data/system/packages.xml";
93 debug = enable_debug;
94 remove_data = remove_data_for_missing_apps;
Dees_Troy201d76b2012-11-16 17:12:02 +000095 multi_user = TWFunc::Path_Exists("/data/user");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040096
97 if (!(TWFunc::Path_Exists(packageFile))) {
Dees_Troy2673cec2013-04-02 20:22:16 +000098 gui_print("Can't check permissions\n");
99 gui_print("after Factory Reset.\n");
100 gui_print("Please boot rom and try\n");
101 gui_print("again after you reboot into\n");
102 gui_print("recovery.\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400103 return -1;
104 }
105
Dees_Troy2673cec2013-04-02 20:22:16 +0000106 gui_print("Fixing permissions...\nLoading packages...\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400107 if ((getPackages()) != 0) {
108 return -1;
109 }
Dees_Troy32f2ca82013-02-02 17:03:12 +0000110
Dees_Troy2673cec2013-04-02 20:22:16 +0000111 gui_print("Fixing /system/app permissions...\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400112 if ((fixSystemApps()) != 0) {
113 return -1;
114 }
115
grimsrudcbec59f2013-07-29 15:46:22 +0200116 gui_print("Fixing /data/app permissions...\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400117 if ((fixDataApps()) != 0) {
118 return -1;
119 }
120
Dees_Troy201d76b2012-11-16 17:12:02 +0000121 if (multi_user) {
122 DIR *d = opendir("/data/user");
123 string new_path, user_id;
124
125 if (d == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000126 LOGERR("Error opening '/data/user'\n");
Dees_Troy201d76b2012-11-16 17:12:02 +0000127 return -1;
128 }
129
130 if (d) {
131 struct dirent *p;
132 while ((p = readdir(d))) {
133 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
134 continue;
135
136 new_path = "/data/user/";
137 new_path.append(p->d_name);
138 user_id = "u";
139 user_id += p->d_name;
140 user_id += "_";
141 if (p->d_type == DT_LNK) {
142 char link[512], realPath[512];
143 strcpy(link, new_path.c_str());
144 memset(realPath, 0, sizeof(realPath));
145 while (readlink(link, realPath, sizeof(realPath)) > 0) {
146 strcpy(link, realPath);
147 memset(realPath, 0, sizeof(realPath));
148 }
149 new_path = link;
150 } else if (p->d_type != DT_DIR) {
151 continue;
152 } else {
153 new_path.append("/");
154 // We're probably going to need to fix permissions on multi user but
155 // it will have to wait for another time. Need to figure out where
156 // the uid and gid is stored for other users.
157 continue;
158 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000159 gui_print("Fixing %s permissions...\n", new_path.c_str());
Dees_Troy201d76b2012-11-16 17:12:02 +0000160 if ((fixDataData(new_path)) != 0) {
161 closedir(d);
162 return -1;
163 }
164 }
165 closedir(d);
166 }
167 } else {
grimsrudcbec59f2013-07-29 15:46:22 +0200168 gui_print("Fixing /data/data permissions...\n");
Dees_Troy201d76b2012-11-16 17:12:02 +0000169 if ((fixDataData("/data/data/")) != 0) {
170 return -1;
171 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400172 }
bigbiff bigbiff872a3b92013-10-18 20:50:25 -0400173 #ifdef HAVE_SELINUX
174 gui_print("Fixing /data/data contexts.\n");
175 fixDataDataContexts();
176 #endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000177 gui_print("Done fixing permissions.\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400178 return 0;
179}
180
181int fixPermissions::pchown(string fn, int puid, int pgid) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000182 LOGINFO("Fixing %s, uid: %d, gid: %d\n", fn.c_str(), puid, pgid);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400183 if (chown(fn.c_str(), puid, pgid) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000184 LOGERR("Unable to chown '%s' %i %i\n", fn.c_str(), puid, pgid);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400185 return -1;
186 }
187 return 0;
188}
189
190int fixPermissions::pchmod(string fn, string mode) {
191 long mask = 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000192 LOGINFO("Fixing %s, mode: %s\n", fn.c_str(), mode.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400193 for ( std::string::size_type n = 0; n < mode.length(); ++n) {
194 if (n == 0) {
195 if (mode[n] == '0')
196 continue;
197 else if (mode[n] == '1')
198 mask = S_ISVTX;
199 else if (mode[n] == '2')
200 mask = S_ISGID;
201 }
202 else if (n == 1) {
203 if (mode[n] == '7') {
204 mask |= S_IRWXU;
205 }
206 if (mode[n] == '6') {
207 mask |= S_IRUSR;
208 mask |= S_IWUSR;
209 }
210 if (mode[n] == '5') {
211 mask |= S_IRUSR;
212 mask |= S_IXUSR;
213 }
214 if (mode[n] == '4')
215 mask |= S_IRUSR;
216 if (mode[n] == '3') {
217 mask |= S_IWUSR;
218 mask |= S_IRUSR;
219 }
220 if (mode[n] == '2')
221 mask |= S_IWUSR;
222 if (mode[n] == '1')
223 mask |= S_IXUSR;
224 }
225 else if (n == 2) {
226 if (mode[n] == '7') {
227 mask |= S_IRWXG;
228 }
229 if (mode[n] == '6') {
230 mask |= S_IRGRP;
231 mask |= S_IWGRP;
232 }
233 if (mode[n] == '5') {
234 mask |= S_IRGRP;
235 mask |= S_IXGRP;
236 }
237 if (mode[n] == '4')
238 mask |= S_IRGRP;
239 if (mode[n] == '3') {
240 mask |= S_IWGRP;
241 mask |= S_IXGRP;
242 }
243 if (mode[n] == '2')
244 mask |= S_IWGRP;
245 if (mode[n] == '1')
246 mask |= S_IXGRP;
247 }
248 else if (n == 3) {
249 if (mode[n] == '7') {
250 mask |= S_IRWXO;
251 }
252 if (mode[n] == '6') {
253 mask |= S_IROTH;
254 mask |= S_IWOTH;
255 }
256 if (mode[n] == '5') {
257 mask |= S_IROTH;
258 mask |= S_IXOTH;
259 }
260 if (mode[n] == '4')
261 mask |= S_IROTH;
262 if (mode[n] == '3') {
263 mask |= S_IWOTH;
264 mask |= S_IXOTH;
265 }
266 if (mode[n] == '2')
Dees_Troy6480ce02012-10-10 10:26:54 -0400267 mask |= S_IWOTH;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400268 if (mode[n] == '1')
Dees_Troy6480ce02012-10-10 10:26:54 -0400269 mask |= S_IXOTH;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400270 }
271 }
272
273 if (chmod(fn.c_str(), mask) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000274 LOGERR("Unable to chmod '%s' %l\n", fn.c_str(), mask);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400275 return -1;
276 }
277
278 return 0;
279}
280
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400281int fixPermissions::fixSystemApps() {
282 temp = head;
283 while (temp != NULL) {
284 if (TWFunc::Path_Exists(temp->codePath)) {
285 if (temp->appDir.compare("/system/app") == 0) {
bigbiff bigbiff872a3b92013-10-18 20:50:25 -0400286 if (debug) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000287 LOGINFO("Looking at '%s'\n", temp->codePath.c_str());
288 LOGINFO("Fixing permissions on '%s'\n", temp->pkgName.c_str());
289 LOGINFO("Directory: '%s'\n", temp->appDir.c_str());
290 LOGINFO("Original package owner: %d, group: %d\n", temp->uid, temp->gid);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400291 }
292 if (pchown(temp->codePath, 0, 0) != 0)
293 return -1;
294 if (pchmod(temp->codePath, "0644") != 0)
295 return -1;
296 }
297 } else {
298 //Remove data directory since app isn't installed
299 if (remove_data && TWFunc::Path_Exists(temp->dDir) && temp->appDir.size() >= 9 && temp->appDir.substr(0, 9) != "/mnt/asec") {
300 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000301 LOGINFO("Looking at '%s', removing data dir: '%s', appDir: '%s'", temp->codePath.c_str(), temp->dDir.c_str(), temp->appDir.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500302 if (TWFunc::removeDir(temp->dDir, false) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000303 LOGINFO("Unable to removeDir '%s'\n", temp->dDir.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400304 return -1;
305 }
306 }
307 }
308 temp = temp->next;
309 }
310 return 0;
311}
312
313int fixPermissions::fixDataApps() {
314 bool fix = false;
315 int new_gid = 0;
316 string perms = "0000";
317
318 temp = head;
319 while (temp != NULL) {
320 if (TWFunc::Path_Exists(temp->codePath)) {
321 if (temp->appDir.compare("/data/app") == 0 || temp->appDir.compare("/sd-ext/app") == 0) {
322 fix = true;
323 new_gid = 1000;
324 perms = "0644";
325 } else if (temp->appDir.compare("/data/app-private") == 0 || temp->appDir.compare("/sd-ext/app-private") == 0) {
326 fix = true;
327 new_gid = temp->gid;
328 perms = "0640";
329 } else
330 fix = false;
331 if (fix) {
332 if (debug) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000333 LOGINFO("Looking at '%s'\n", temp->codePath.c_str());
334 LOGINFO("Fixing permissions on '%s'\n", temp->pkgName.c_str());
335 LOGINFO("Directory: '%s'\n", temp->appDir.c_str());
336 LOGINFO("Original package owner: %d, group: %d\n", temp->uid, temp->gid);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400337 }
338 if (pchown(temp->codePath, 1000, new_gid) != 0)
339 return -1;
340 if (pchmod(temp->codePath, perms) != 0)
341 return -1;
342 }
343 } else {
344 //Remove data directory since app isn't installed
345 if (remove_data && TWFunc::Path_Exists(temp->dDir) && temp->appDir.size() >= 9 && temp->appDir.substr(0, 9) != "/mnt/asec") {
346 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000347 LOGINFO("Looking at '%s', removing data dir: '%s', appDir: '%s'", temp->codePath.c_str(), temp->dDir.c_str(), temp->appDir.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500348 if (TWFunc::removeDir(temp->dDir, false) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000349 LOGINFO("Unable to removeDir '%s'\n", temp->dDir.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400350 return -1;
351 }
352 }
353 }
354 temp = temp->next;
355 }
356 return 0;
357}
358
359int fixPermissions::fixAllFiles(string directory, int gid, int uid, string file_perms) {
360 vector <string> files;
361 string file;
362
363 files = listAllFiles(directory);
364 for (unsigned i = 0; i < files.size(); ++i) {
365 file = directory + "/";
366 file.append(files.at(i));
367 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000368 LOGINFO("Looking at file '%s'\n", file.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400369 if (pchmod(file, file_perms) != 0)
370 return -1;
371 if (pchown(file, uid, gid) != 0)
372 return -1;
373 }
374 return 0;
375}
376
Dees_Troy201d76b2012-11-16 17:12:02 +0000377int fixPermissions::fixDataData(string dataDir) {
378 string directory, dir;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400379
380 temp = head;
381 while (temp != NULL) {
Dees_Troy201d76b2012-11-16 17:12:02 +0000382 dir = dataDir + temp->dDir;
383 if (TWFunc::Path_Exists(dir)) {
384 vector <string> dataDataDirs = listAllDirectories(dir);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400385 for (unsigned n = 0; n < dataDataDirs.size(); ++n) {
Dees_Troy201d76b2012-11-16 17:12:02 +0000386 directory = dir + "/";
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400387 directory.append(dataDataDirs.at(n));
388 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000389 LOGINFO("Looking at data directory: '%s'\n", directory.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400390 if (dataDataDirs.at(n) == ".") {
Dees_Troy201d76b2012-11-16 17:12:02 +0000391 if (pchmod(directory, "0755") != 0)
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400392 return -1;
393 if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
394 return -1;
395 if (fixAllFiles(directory, temp->uid, temp->gid, "0755") != 0)
396 return -1;
397 }
398 else if (dataDataDirs.at(n) == "..") {
399 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000400 LOGINFO("Skipping ..\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400401 continue;
402 }
403 else if (dataDataDirs.at(n) == "lib") {
404 if (pchmod(directory.c_str(), "0755") != 0)
405 return -1;
406 if (pchown(directory.c_str(), 1000, 1000) != 0)
407 return -1;
408 if (fixAllFiles(directory, temp->uid, temp->gid, "0755") != 0)
409 return -1;
410 }
411 else if (dataDataDirs.at(n) == "shared_prefs") {
412 if (pchmod(directory.c_str(), "0771") != 0)
413 return -1;
414 if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
415 return -1;
416 if (fixAllFiles(directory, temp->uid, temp->gid, "0660") != 0)
417 return -1;
418 }
419 else if (dataDataDirs.at(n) == "databases") {
420 if (pchmod(directory.c_str(), "0771") != 0)
421 return -1;
422 if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
423 return -1;
424 if (fixAllFiles(directory, temp->uid, temp->gid, "0660") != 0)
425 return -1;
426 }
427 else if (dataDataDirs.at(n) == "cache") {
428 if (pchmod(directory.c_str(), "0771") != 0)
429 return -1;
430 if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
431 return -1;
432 if (fixAllFiles(directory, temp->uid, temp->gid, "0600") != 0)
433 return -1;
434 }
435 else {
436 if (pchmod(directory.c_str(), "0771") != 0)
437 return -1;
438 if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
439 return -1;
440 if (fixAllFiles(directory, temp->uid, temp->gid, "0755") != 0)
441 return -1;
442 }
443 }
444 }
445 temp = temp->next;
446 }
447 return 0;
448}
449
450vector <string> fixPermissions::listAllDirectories(string path) {
451 DIR *dir = opendir(path.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400452 vector <string> dirs;
453
454 if (dir == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000455 LOGERR("Error opening '%s'\n", path.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400456 return dirs;
457 }
Dees_Troy201d76b2012-11-16 17:12:02 +0000458 struct dirent *entry = readdir(dir);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400459 while (entry != NULL) {
460 if (entry->d_type == DT_DIR)
461 dirs.push_back(entry->d_name);
462 entry = readdir(dir);
463 }
464 closedir(dir);
465 return dirs;
466}
467
468vector <string> fixPermissions::listAllFiles(string path) {
469 DIR *dir = opendir(path.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400470 vector <string> files;
471
472 if (dir == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000473 LOGERR("Error opening '%s'\n", path.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400474 return files;
475 }
Dees_Troy201d76b2012-11-16 17:12:02 +0000476 struct dirent *entry = readdir(dir);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400477 while (entry != NULL) {
478 if (entry->d_type == DT_REG)
479 files.push_back(entry->d_name);
480 entry = readdir(dir);
481 }
482 closedir(dir);
483 return files;
484}
485
486int fixPermissions::getPackages() {
487 int len = 0;
488 bool skiploop = false;
489 vector <string> skip;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400490 string name;
491 head = NULL;
492
493 skip.push_back("/system/framework/framework-res.apk");
494 skip.push_back("/system/framework/com.htc.resources.apk");
495
496 ifstream xmlFile(packageFile.c_str());
497 xmlFile.seekg(0, ios::end);
498 len = (int) xmlFile.tellg();
499 xmlFile.seekg(0, ios::beg);
500 char xmlBuf[len + 1];
501 xmlFile.read(&xmlBuf[0], len);
502 xmlBuf[len] = '\0';
503 xml_document<> pkgDoc;
Dees_Troy34614eb2013-04-05 12:02:14 -0500504 LOGINFO("parsing package, %i...\n", len);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400505 pkgDoc.parse<parse_full>(&xmlBuf[0]);
506
507 xml_node<> * pkgNode = pkgDoc.first_node("packages");
Dees_Troy34614eb2013-04-05 12:02:14 -0500508 if (pkgNode == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000509 LOGERR("No packages found to fix.\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400510 return -1;
511 }
Dees_Troy34614eb2013-04-05 12:02:14 -0500512 xml_node <> * next = pkgNode->first_node("package");
513 if (next == NULL) {
514 LOGERR("No package found to fix.\n");
515 return -1;
516 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400517
518 //Get packages
519 while (next->first_attribute("name") != NULL) {
520 package* temp = new package;
521 for (unsigned n = 0; n < skip.size(); ++n) {
522 if (skip.at(n).compare(next->first_attribute("codePath")->value()) == 0) {
523 skiploop = true;
524 break;
525 }
526 }
527
528 if (skiploop == true) {
529 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000530 LOGINFO("Skipping package %s\n", next->first_attribute("codePath")->value());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400531 free(temp);
532 next = next->next_sibling();
533 skiploop = false;
534 continue;
535 }
536 name.append((next->first_attribute("name")->value()));
537 temp->pkgName = next->first_attribute("name")->value();
538 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000539 LOGINFO("Loading pkg: %s\n", next->first_attribute("name")->value());
Dees_Troy201d76b2012-11-16 17:12:02 +0000540 if (next->first_attribute("codePath") == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000541 LOGINFO("Problem with codePath on %s\n", next->first_attribute("name")->value());
Dees_Troy201d76b2012-11-16 17:12:02 +0000542 } else {
543 temp->codePath = next->first_attribute("codePath")->value();
544 temp->app = basename(next->first_attribute("codePath")->value());
545 temp->appDir = dirname(next->first_attribute("codePath")->value());
546 }
547 temp->dDir = name;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400548 if ( next->first_attribute("sharedUserId") != NULL) {
549 temp->uid = atoi(next->first_attribute("sharedUserId")->value());
550 temp->gid = atoi(next->first_attribute("sharedUserId")->value());
551 }
552 else {
Dees_Troy201d76b2012-11-16 17:12:02 +0000553 if (next->first_attribute("userId") == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000554 LOGINFO("Problem with userID on %s\n", next->first_attribute("name")->value());
Dees_Troy201d76b2012-11-16 17:12:02 +0000555 } else {
556 temp->uid = atoi(next->first_attribute("userId")->value());
557 temp->gid = atoi(next->first_attribute("userId")->value());
558 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400559 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400560 temp->next = head;
561 head = temp;
Dees_Troy201d76b2012-11-16 17:12:02 +0000562 if (next->next_sibling("package") == NULL)
563 break;
564 name.clear();
565 next = next->next_sibling("package");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400566 }
567 //Get updated packages
568 next = pkgNode->first_node("updated-package");
569 if (next != NULL) {
570 while (next->first_attribute("name") != NULL) {
571 package* temp = new package;
572 for (unsigned n = 0; n < skip.size(); ++n) {
573 if (skip.at(n).compare(next->first_attribute("codePath")->value()) == 0) {
574 skiploop = true;
575 break;
576 }
577 }
578
579 if (skiploop == true) {
580 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000581 LOGINFO("Skipping package %s\n", next->first_attribute("codePath")->value());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400582 free(temp);
583 next = next->next_sibling();
584 skiploop = false;
585 continue;
586 }
587 name.append((next->first_attribute("name")->value()));
588 temp->pkgName = next->first_attribute("name")->value();
589 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000590 LOGINFO("Loading pkg: %s\n", next->first_attribute("name")->value());
Dees_Troy201d76b2012-11-16 17:12:02 +0000591 if (next->first_attribute("codePath") == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000592 LOGINFO("Problem with codePath on %s\n", next->first_attribute("name")->value());
Dees_Troy201d76b2012-11-16 17:12:02 +0000593 } else {
594 temp->codePath = next->first_attribute("codePath")->value();
595 temp->app = basename(next->first_attribute("codePath")->value());
596 temp->appDir = dirname(next->first_attribute("codePath")->value());
597 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400598
Dees_Troy201d76b2012-11-16 17:12:02 +0000599 temp->dDir = name;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400600 if ( next->first_attribute("sharedUserId") != NULL) {
601 temp->uid = atoi(next->first_attribute("sharedUserId")->value());
602 temp->gid = atoi(next->first_attribute("sharedUserId")->value());
603 }
604 else {
Dees_Troy201d76b2012-11-16 17:12:02 +0000605 if (next->first_attribute("userId") == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000606 LOGINFO("Problem with userID on %s\n", next->first_attribute("name")->value());
Dees_Troy201d76b2012-11-16 17:12:02 +0000607 } else {
608 temp->uid = atoi(next->first_attribute("userId")->value());
609 temp->gid = atoi(next->first_attribute("userId")->value());
610 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400611 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400612 temp->next = head;
613 head = temp;
Dees_Troy201d76b2012-11-16 17:12:02 +0000614 if (next->next_sibling("package") == NULL)
615 break;
616 name.clear();
617 next = next->next_sibling("package");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400618 }
619 }
620 return 0;
621}