blob: 28965e18b5fdc0645b640a1edb5ef7584cce59c4 [file] [log] [blame]
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001/*
2 Copyright 2013 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
19extern "C" {
20 #include "libtar/libtar.h"
21}
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <string.h>
25#include <errno.h>
26#include <fcntl.h>
27#include <fstream>
28#include <string>
29#include <vector>
Vojtech Bocek05f87d62014-03-11 22:08:23 +010030#include <algorithm>
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050031#include "twrpDU.hpp"
Vojtech Bocek05f87d62014-03-11 22:08:23 +010032#include "twrp-functions.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050033
34using namespace std;
35
36twrpDU::twrpDU() {
37 add_relative_dir(".");
38 add_relative_dir("..");
Vojtech Bocek05f87d62014-03-11 22:08:23 +010039 add_relative_dir("lost+found");
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050040 add_absolute_dir("/data/data/com.google.android.music/files");
Matt Mowerf77994d2014-03-20 20:21:09 -050041#ifdef RECOVERY_SDCARD_ON_DATA
42 add_absolute_dir("/data/media");
43#endif
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050044 parent = "";
45}
46
Vojtech Bocek05f87d62014-03-11 22:08:23 +010047void twrpDU::add_relative_dir(const string& dir) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050048 relativedir.push_back(dir);
49}
50
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -050051void twrpDU::clear_relative_dir(string dir) {
52 vector<string>::iterator iter = relativedir.begin();
53 while (iter != relativedir.end()) {
54 if (*iter == dir)
55 iter = relativedir.erase(iter);
56 else
57 iter++;
58 }
59}
60
Vojtech Bocek05f87d62014-03-11 22:08:23 +010061void twrpDU::add_absolute_dir(const string& dir) {
62 absolutedir.push_back(TWFunc::Remove_Trailing_Slashes(dir));
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050063}
64
65vector<string> twrpDU::get_absolute_dirs(void) {
66 return absolutedir;
67}
68
69uint64_t twrpDU::Get_Folder_Size(const string& Path) {
70 DIR* d;
71 struct dirent* de;
72 struct stat st;
73 unsigned long long dusize = 0;
74 unsigned long long dutemp = 0;
75
76 parent = Path.substr(0, Path.find_last_of('/'));
77
78 d = opendir(Path.c_str());
79 if (d == NULL) {
80 LOGERR("error opening '%s'\n", Path.c_str());
81 LOGERR("error: %s\n", strerror(errno));
82 return 0;
83 }
84
85 while ((de = readdir(d)) != NULL)
86 {
Vojtech Bocek05f87d62014-03-11 22:08:23 +010087 if (de->d_type == DT_DIR && !check_skip_dirs(Path, de->d_name)) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050088 dutemp = Get_Folder_Size((Path + "/" + de->d_name));
89 dusize += dutemp;
90 dutemp = 0;
91 }
92 else if (de->d_type == DT_REG) {
93 stat((Path + "/" + de->d_name).c_str(), &st);
94 dusize += (uint64_t)(st.st_size);
95 }
96 }
97 closedir(d);
98 return dusize;
99}
100
Vojtech Bocek05f87d62014-03-11 22:08:23 +0100101bool twrpDU::check_relative_skip_dirs(const string& dir) {
102 return std::find(relativedir.begin(), relativedir.end(), dir) != relativedir.end();
103}
104
105bool twrpDU::check_absolute_skip_dirs(const string& path) {
106 string normalized = TWFunc::Remove_Trailing_Slashes(path);
107 return std::find(absolutedir.begin(), absolutedir.end(), normalized) != absolutedir.end();
108}
109
110bool twrpDU::check_skip_dirs(const string& parent, const string& dir) {
111 return check_relative_skip_dirs(dir) || check_absolute_skip_dirs(parent + "/" + dir);
112}
113
114bool twrpDU::check_skip_dirs(const string& path) {
115 string normalized = TWFunc::Remove_Trailing_Slashes(path);
116 size_t slashIdx = normalized.find_last_of('/');
117 if(slashIdx != std::string::npos && slashIdx+1 < normalized.size()) {
118 if(check_relative_skip_dirs(normalized.substr(slashIdx+1)))
119 return true;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500120 }
Vojtech Bocek05f87d62014-03-11 22:08:23 +0100121 return check_absolute_skip_dirs(normalized);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500122}