blob: 40ac516b031d385108d3255cc591bbc6c3c8cc39 [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;
Dees Troya7b8de52014-02-03 14:58:32 +000048 struct selinux_opt selinux_options[] = {
49 { SELABEL_OPT_PATH, "/file_contexts" }
50 };
51 sehandle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1);
bigbiff bigbiff872a3b92013-10-18 20:50:25 -040052 if (lgetfilecon(entry.c_str(), &oldcontext) < 0) {
53 LOGINFO("Couldn't get selinux context for %s\n", entry.c_str());
54 return -1;
55 }
56 if (selabel_lookup(sehandle, &newcontext, entry.c_str(), sb->st_mode) < 0) {
57 LOGINFO("Couldn't lookup selinux context for %s\n", entry.c_str());
58 return -1;
59 }
60 LOGINFO("Relabeling %s from %s to %s\n", entry.c_str(), oldcontext, newcontext);
61 if (lsetfilecon(entry.c_str(), newcontext) < 0) {
62 LOGINFO("Couldn't label %s with %s: %s\n", entry.c_str(), newcontext, strerror(errno));
63 }
64 freecon(oldcontext);
65 freecon(newcontext);
66 return 0;
67}
68
69int fixPermissions::fixDataDataContexts(void) {
70 DIR *d;
71 struct dirent *de;
72 struct stat sb;
73 struct selabel_handle *selinux_handle;
74 struct selinux_opt selinux_options[] = {
75 { SELABEL_OPT_PATH, "/file_contexts" }
76 };
77 selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1);
78 if (!selinux_handle)
79 printf("No file contexts for SELinux\n");
80 else
81 printf("SELinux contexts loaded from /file_contexts\n");
82 d = opendir("/data/data");
83 while (( de = readdir(d)) != NULL) {
84 stat(de->d_name, &sb);
85 string f = "/data/data/";
86 f = f + de->d_name;
87 restorecon(f, &sb);
88 }
89 return 0;
90}
91#endif
92
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040093int fixPermissions::fixPerms(bool enable_debug, bool remove_data_for_missing_apps) {
94 packageFile = "/data/system/packages.xml";
95 debug = enable_debug;
96 remove_data = remove_data_for_missing_apps;
Dees_Troy201d76b2012-11-16 17:12:02 +000097 multi_user = TWFunc::Path_Exists("/data/user");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040098
99 if (!(TWFunc::Path_Exists(packageFile))) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000100 gui_print("Can't check permissions\n");
101 gui_print("after Factory Reset.\n");
102 gui_print("Please boot rom and try\n");
103 gui_print("again after you reboot into\n");
104 gui_print("recovery.\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400105 return -1;
106 }
107
Dees_Troy2673cec2013-04-02 20:22:16 +0000108 gui_print("Fixing permissions...\nLoading packages...\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400109 if ((getPackages()) != 0) {
110 return -1;
111 }
Dees_Troy32f2ca82013-02-02 17:03:12 +0000112
Dees_Troy2673cec2013-04-02 20:22:16 +0000113 gui_print("Fixing /system/app permissions...\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400114 if ((fixSystemApps()) != 0) {
115 return -1;
116 }
117
grimsrudcbec59f2013-07-29 15:46:22 +0200118 gui_print("Fixing /data/app permissions...\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400119 if ((fixDataApps()) != 0) {
120 return -1;
121 }
122
Dees_Troy201d76b2012-11-16 17:12:02 +0000123 if (multi_user) {
124 DIR *d = opendir("/data/user");
125 string new_path, user_id;
126
127 if (d == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000128 LOGERR("Error opening '/data/user'\n");
Dees_Troy201d76b2012-11-16 17:12:02 +0000129 return -1;
130 }
131
132 if (d) {
133 struct dirent *p;
134 while ((p = readdir(d))) {
135 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
136 continue;
137
138 new_path = "/data/user/";
139 new_path.append(p->d_name);
140 user_id = "u";
141 user_id += p->d_name;
142 user_id += "_";
143 if (p->d_type == DT_LNK) {
144 char link[512], realPath[512];
145 strcpy(link, new_path.c_str());
146 memset(realPath, 0, sizeof(realPath));
147 while (readlink(link, realPath, sizeof(realPath)) > 0) {
148 strcpy(link, realPath);
149 memset(realPath, 0, sizeof(realPath));
150 }
151 new_path = link;
152 } else if (p->d_type != DT_DIR) {
153 continue;
154 } else {
155 new_path.append("/");
156 // We're probably going to need to fix permissions on multi user but
157 // it will have to wait for another time. Need to figure out where
158 // the uid and gid is stored for other users.
159 continue;
160 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000161 gui_print("Fixing %s permissions...\n", new_path.c_str());
Dees_Troy201d76b2012-11-16 17:12:02 +0000162 if ((fixDataData(new_path)) != 0) {
163 closedir(d);
164 return -1;
165 }
166 }
167 closedir(d);
168 }
169 } else {
grimsrudcbec59f2013-07-29 15:46:22 +0200170 gui_print("Fixing /data/data permissions...\n");
Dees_Troy201d76b2012-11-16 17:12:02 +0000171 if ((fixDataData("/data/data/")) != 0) {
172 return -1;
173 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400174 }
bigbiff bigbiff872a3b92013-10-18 20:50:25 -0400175 #ifdef HAVE_SELINUX
176 gui_print("Fixing /data/data contexts.\n");
177 fixDataDataContexts();
178 #endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000179 gui_print("Done fixing permissions.\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400180 return 0;
181}
182
183int fixPermissions::pchown(string fn, int puid, int pgid) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000184 LOGINFO("Fixing %s, uid: %d, gid: %d\n", fn.c_str(), puid, pgid);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400185 if (chown(fn.c_str(), puid, pgid) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000186 LOGERR("Unable to chown '%s' %i %i\n", fn.c_str(), puid, pgid);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400187 return -1;
188 }
189 return 0;
190}
191
192int fixPermissions::pchmod(string fn, string mode) {
193 long mask = 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000194 LOGINFO("Fixing %s, mode: %s\n", fn.c_str(), mode.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400195 for ( std::string::size_type n = 0; n < mode.length(); ++n) {
196 if (n == 0) {
197 if (mode[n] == '0')
198 continue;
199 else if (mode[n] == '1')
200 mask = S_ISVTX;
201 else if (mode[n] == '2')
202 mask = S_ISGID;
203 }
204 else if (n == 1) {
205 if (mode[n] == '7') {
206 mask |= S_IRWXU;
207 }
208 if (mode[n] == '6') {
209 mask |= S_IRUSR;
210 mask |= S_IWUSR;
211 }
212 if (mode[n] == '5') {
213 mask |= S_IRUSR;
214 mask |= S_IXUSR;
215 }
216 if (mode[n] == '4')
217 mask |= S_IRUSR;
218 if (mode[n] == '3') {
219 mask |= S_IWUSR;
220 mask |= S_IRUSR;
221 }
222 if (mode[n] == '2')
223 mask |= S_IWUSR;
224 if (mode[n] == '1')
225 mask |= S_IXUSR;
226 }
227 else if (n == 2) {
228 if (mode[n] == '7') {
229 mask |= S_IRWXG;
230 }
231 if (mode[n] == '6') {
232 mask |= S_IRGRP;
233 mask |= S_IWGRP;
234 }
235 if (mode[n] == '5') {
236 mask |= S_IRGRP;
237 mask |= S_IXGRP;
238 }
239 if (mode[n] == '4')
240 mask |= S_IRGRP;
241 if (mode[n] == '3') {
242 mask |= S_IWGRP;
243 mask |= S_IXGRP;
244 }
245 if (mode[n] == '2')
246 mask |= S_IWGRP;
247 if (mode[n] == '1')
248 mask |= S_IXGRP;
249 }
250 else if (n == 3) {
251 if (mode[n] == '7') {
252 mask |= S_IRWXO;
253 }
254 if (mode[n] == '6') {
255 mask |= S_IROTH;
256 mask |= S_IWOTH;
257 }
258 if (mode[n] == '5') {
259 mask |= S_IROTH;
260 mask |= S_IXOTH;
261 }
262 if (mode[n] == '4')
263 mask |= S_IROTH;
264 if (mode[n] == '3') {
265 mask |= S_IWOTH;
266 mask |= S_IXOTH;
267 }
268 if (mode[n] == '2')
Dees_Troy6480ce02012-10-10 10:26:54 -0400269 mask |= S_IWOTH;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400270 if (mode[n] == '1')
Dees_Troy6480ce02012-10-10 10:26:54 -0400271 mask |= S_IXOTH;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400272 }
273 }
274
275 if (chmod(fn.c_str(), mask) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000276 LOGERR("Unable to chmod '%s' %l\n", fn.c_str(), mask);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400277 return -1;
278 }
279
280 return 0;
281}
282
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400283int fixPermissions::fixSystemApps() {
284 temp = head;
285 while (temp != NULL) {
286 if (TWFunc::Path_Exists(temp->codePath)) {
287 if (temp->appDir.compare("/system/app") == 0) {
bigbiff bigbiff872a3b92013-10-18 20:50:25 -0400288 if (debug) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000289 LOGINFO("Looking at '%s'\n", temp->codePath.c_str());
290 LOGINFO("Fixing permissions on '%s'\n", temp->pkgName.c_str());
291 LOGINFO("Directory: '%s'\n", temp->appDir.c_str());
292 LOGINFO("Original package owner: %d, group: %d\n", temp->uid, temp->gid);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400293 }
294 if (pchown(temp->codePath, 0, 0) != 0)
295 return -1;
296 if (pchmod(temp->codePath, "0644") != 0)
297 return -1;
298 }
299 } else {
300 //Remove data directory since app isn't installed
301 if (remove_data && TWFunc::Path_Exists(temp->dDir) && temp->appDir.size() >= 9 && temp->appDir.substr(0, 9) != "/mnt/asec") {
302 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000303 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 -0500304 if (TWFunc::removeDir(temp->dDir, false) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000305 LOGINFO("Unable to removeDir '%s'\n", temp->dDir.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400306 return -1;
307 }
308 }
309 }
310 temp = temp->next;
311 }
312 return 0;
313}
314
315int fixPermissions::fixDataApps() {
316 bool fix = false;
317 int new_gid = 0;
318 string perms = "0000";
319
320 temp = head;
321 while (temp != NULL) {
322 if (TWFunc::Path_Exists(temp->codePath)) {
323 if (temp->appDir.compare("/data/app") == 0 || temp->appDir.compare("/sd-ext/app") == 0) {
324 fix = true;
325 new_gid = 1000;
326 perms = "0644";
327 } else if (temp->appDir.compare("/data/app-private") == 0 || temp->appDir.compare("/sd-ext/app-private") == 0) {
328 fix = true;
329 new_gid = temp->gid;
330 perms = "0640";
331 } else
332 fix = false;
333 if (fix) {
334 if (debug) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000335 LOGINFO("Looking at '%s'\n", temp->codePath.c_str());
336 LOGINFO("Fixing permissions on '%s'\n", temp->pkgName.c_str());
337 LOGINFO("Directory: '%s'\n", temp->appDir.c_str());
338 LOGINFO("Original package owner: %d, group: %d\n", temp->uid, temp->gid);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400339 }
340 if (pchown(temp->codePath, 1000, new_gid) != 0)
341 return -1;
342 if (pchmod(temp->codePath, perms) != 0)
343 return -1;
344 }
345 } else {
346 //Remove data directory since app isn't installed
347 if (remove_data && TWFunc::Path_Exists(temp->dDir) && temp->appDir.size() >= 9 && temp->appDir.substr(0, 9) != "/mnt/asec") {
348 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000349 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 -0500350 if (TWFunc::removeDir(temp->dDir, false) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000351 LOGINFO("Unable to removeDir '%s'\n", temp->dDir.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400352 return -1;
353 }
354 }
355 }
356 temp = temp->next;
357 }
358 return 0;
359}
360
361int fixPermissions::fixAllFiles(string directory, int gid, int uid, string file_perms) {
362 vector <string> files;
363 string file;
364
365 files = listAllFiles(directory);
366 for (unsigned i = 0; i < files.size(); ++i) {
367 file = directory + "/";
368 file.append(files.at(i));
369 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000370 LOGINFO("Looking at file '%s'\n", file.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400371 if (pchmod(file, file_perms) != 0)
372 return -1;
373 if (pchown(file, uid, gid) != 0)
374 return -1;
375 }
376 return 0;
377}
378
Dees_Troy201d76b2012-11-16 17:12:02 +0000379int fixPermissions::fixDataData(string dataDir) {
380 string directory, dir;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400381
382 temp = head;
383 while (temp != NULL) {
Dees_Troy201d76b2012-11-16 17:12:02 +0000384 dir = dataDir + temp->dDir;
385 if (TWFunc::Path_Exists(dir)) {
386 vector <string> dataDataDirs = listAllDirectories(dir);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400387 for (unsigned n = 0; n < dataDataDirs.size(); ++n) {
Dees_Troy201d76b2012-11-16 17:12:02 +0000388 directory = dir + "/";
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400389 directory.append(dataDataDirs.at(n));
390 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000391 LOGINFO("Looking at data directory: '%s'\n", directory.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400392 if (dataDataDirs.at(n) == ".") {
Dees_Troy201d76b2012-11-16 17:12:02 +0000393 if (pchmod(directory, "0755") != 0)
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400394 return -1;
395 if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
396 return -1;
397 if (fixAllFiles(directory, temp->uid, temp->gid, "0755") != 0)
398 return -1;
399 }
400 else if (dataDataDirs.at(n) == "..") {
401 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000402 LOGINFO("Skipping ..\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400403 continue;
404 }
405 else if (dataDataDirs.at(n) == "lib") {
406 if (pchmod(directory.c_str(), "0755") != 0)
407 return -1;
408 if (pchown(directory.c_str(), 1000, 1000) != 0)
409 return -1;
410 if (fixAllFiles(directory, temp->uid, temp->gid, "0755") != 0)
411 return -1;
412 }
413 else if (dataDataDirs.at(n) == "shared_prefs") {
414 if (pchmod(directory.c_str(), "0771") != 0)
415 return -1;
416 if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
417 return -1;
418 if (fixAllFiles(directory, temp->uid, temp->gid, "0660") != 0)
419 return -1;
420 }
421 else if (dataDataDirs.at(n) == "databases") {
422 if (pchmod(directory.c_str(), "0771") != 0)
423 return -1;
424 if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
425 return -1;
426 if (fixAllFiles(directory, temp->uid, temp->gid, "0660") != 0)
427 return -1;
428 }
429 else if (dataDataDirs.at(n) == "cache") {
430 if (pchmod(directory.c_str(), "0771") != 0)
431 return -1;
432 if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
433 return -1;
434 if (fixAllFiles(directory, temp->uid, temp->gid, "0600") != 0)
435 return -1;
436 }
437 else {
438 if (pchmod(directory.c_str(), "0771") != 0)
439 return -1;
440 if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
441 return -1;
442 if (fixAllFiles(directory, temp->uid, temp->gid, "0755") != 0)
443 return -1;
444 }
445 }
446 }
447 temp = temp->next;
448 }
449 return 0;
450}
451
452vector <string> fixPermissions::listAllDirectories(string path) {
453 DIR *dir = opendir(path.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400454 vector <string> dirs;
455
456 if (dir == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000457 LOGERR("Error opening '%s'\n", path.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400458 return dirs;
459 }
Dees_Troy201d76b2012-11-16 17:12:02 +0000460 struct dirent *entry = readdir(dir);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400461 while (entry != NULL) {
462 if (entry->d_type == DT_DIR)
463 dirs.push_back(entry->d_name);
464 entry = readdir(dir);
465 }
466 closedir(dir);
467 return dirs;
468}
469
470vector <string> fixPermissions::listAllFiles(string path) {
471 DIR *dir = opendir(path.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400472 vector <string> files;
473
474 if (dir == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000475 LOGERR("Error opening '%s'\n", path.c_str());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400476 return files;
477 }
Dees_Troy201d76b2012-11-16 17:12:02 +0000478 struct dirent *entry = readdir(dir);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400479 while (entry != NULL) {
480 if (entry->d_type == DT_REG)
481 files.push_back(entry->d_name);
482 entry = readdir(dir);
483 }
484 closedir(dir);
485 return files;
486}
487
488int fixPermissions::getPackages() {
489 int len = 0;
490 bool skiploop = false;
491 vector <string> skip;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400492 string name;
493 head = NULL;
494
495 skip.push_back("/system/framework/framework-res.apk");
496 skip.push_back("/system/framework/com.htc.resources.apk");
497
498 ifstream xmlFile(packageFile.c_str());
499 xmlFile.seekg(0, ios::end);
500 len = (int) xmlFile.tellg();
501 xmlFile.seekg(0, ios::beg);
502 char xmlBuf[len + 1];
503 xmlFile.read(&xmlBuf[0], len);
504 xmlBuf[len] = '\0';
505 xml_document<> pkgDoc;
Dees_Troy34614eb2013-04-05 12:02:14 -0500506 LOGINFO("parsing package, %i...\n", len);
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400507 pkgDoc.parse<parse_full>(&xmlBuf[0]);
508
509 xml_node<> * pkgNode = pkgDoc.first_node("packages");
Dees_Troy34614eb2013-04-05 12:02:14 -0500510 if (pkgNode == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000511 LOGERR("No packages found to fix.\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400512 return -1;
513 }
Dees_Troy34614eb2013-04-05 12:02:14 -0500514 xml_node <> * next = pkgNode->first_node("package");
515 if (next == NULL) {
516 LOGERR("No package found to fix.\n");
517 return -1;
518 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400519
520 //Get packages
521 while (next->first_attribute("name") != NULL) {
522 package* temp = new package;
523 for (unsigned n = 0; n < skip.size(); ++n) {
524 if (skip.at(n).compare(next->first_attribute("codePath")->value()) == 0) {
525 skiploop = true;
526 break;
527 }
528 }
529
530 if (skiploop == true) {
531 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000532 LOGINFO("Skipping package %s\n", next->first_attribute("codePath")->value());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400533 free(temp);
534 next = next->next_sibling();
535 skiploop = false;
536 continue;
537 }
538 name.append((next->first_attribute("name")->value()));
539 temp->pkgName = next->first_attribute("name")->value();
540 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000541 LOGINFO("Loading pkg: %s\n", next->first_attribute("name")->value());
Dees_Troy201d76b2012-11-16 17:12:02 +0000542 if (next->first_attribute("codePath") == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000543 LOGINFO("Problem with codePath on %s\n", next->first_attribute("name")->value());
Dees_Troy201d76b2012-11-16 17:12:02 +0000544 } else {
545 temp->codePath = next->first_attribute("codePath")->value();
546 temp->app = basename(next->first_attribute("codePath")->value());
547 temp->appDir = dirname(next->first_attribute("codePath")->value());
548 }
549 temp->dDir = name;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400550 if ( next->first_attribute("sharedUserId") != NULL) {
551 temp->uid = atoi(next->first_attribute("sharedUserId")->value());
552 temp->gid = atoi(next->first_attribute("sharedUserId")->value());
553 }
554 else {
Dees_Troy201d76b2012-11-16 17:12:02 +0000555 if (next->first_attribute("userId") == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000556 LOGINFO("Problem with userID on %s\n", next->first_attribute("name")->value());
Dees_Troy201d76b2012-11-16 17:12:02 +0000557 } else {
558 temp->uid = atoi(next->first_attribute("userId")->value());
559 temp->gid = atoi(next->first_attribute("userId")->value());
560 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400561 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400562 temp->next = head;
563 head = temp;
Dees_Troy201d76b2012-11-16 17:12:02 +0000564 if (next->next_sibling("package") == NULL)
565 break;
566 name.clear();
567 next = next->next_sibling("package");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400568 }
569 //Get updated packages
570 next = pkgNode->first_node("updated-package");
571 if (next != NULL) {
572 while (next->first_attribute("name") != NULL) {
573 package* temp = new package;
574 for (unsigned n = 0; n < skip.size(); ++n) {
575 if (skip.at(n).compare(next->first_attribute("codePath")->value()) == 0) {
576 skiploop = true;
577 break;
578 }
579 }
580
581 if (skiploop == true) {
582 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000583 LOGINFO("Skipping package %s\n", next->first_attribute("codePath")->value());
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400584 free(temp);
585 next = next->next_sibling();
586 skiploop = false;
587 continue;
588 }
589 name.append((next->first_attribute("name")->value()));
590 temp->pkgName = next->first_attribute("name")->value();
591 if (debug)
Dees_Troy2673cec2013-04-02 20:22:16 +0000592 LOGINFO("Loading pkg: %s\n", next->first_attribute("name")->value());
Dees_Troy201d76b2012-11-16 17:12:02 +0000593 if (next->first_attribute("codePath") == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000594 LOGINFO("Problem with codePath on %s\n", next->first_attribute("name")->value());
Dees_Troy201d76b2012-11-16 17:12:02 +0000595 } else {
596 temp->codePath = next->first_attribute("codePath")->value();
597 temp->app = basename(next->first_attribute("codePath")->value());
598 temp->appDir = dirname(next->first_attribute("codePath")->value());
599 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400600
Dees_Troy201d76b2012-11-16 17:12:02 +0000601 temp->dDir = name;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400602 if ( next->first_attribute("sharedUserId") != NULL) {
603 temp->uid = atoi(next->first_attribute("sharedUserId")->value());
604 temp->gid = atoi(next->first_attribute("sharedUserId")->value());
605 }
606 else {
Dees_Troy201d76b2012-11-16 17:12:02 +0000607 if (next->first_attribute("userId") == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000608 LOGINFO("Problem with userID on %s\n", next->first_attribute("name")->value());
Dees_Troy201d76b2012-11-16 17:12:02 +0000609 } else {
610 temp->uid = atoi(next->first_attribute("userId")->value());
611 temp->gid = atoi(next->first_attribute("userId")->value());
612 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400613 }
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400614 temp->next = head;
615 head = temp;
Dees_Troy201d76b2012-11-16 17:12:02 +0000616 if (next->next_sibling("package") == NULL)
617 break;
618 name.clear();
619 next = next->next_sibling("package");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -0400620 }
621 }
622 return 0;
623}