bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | extern "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 Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 30 | #include <algorithm> |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 31 | #include "twrpDU.hpp" |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 32 | #include "twrp-functions.hpp" |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 33 | |
| 34 | using namespace std; |
| 35 | |
| 36 | twrpDU::twrpDU() { |
| 37 | add_relative_dir("."); |
| 38 | add_relative_dir(".."); |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 39 | add_relative_dir("lost+found"); |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 40 | add_absolute_dir("/data/data/com.google.android.music/files"); |
| 41 | parent = ""; |
| 42 | } |
| 43 | |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 44 | void twrpDU::add_relative_dir(const string& dir) { |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 45 | relativedir.push_back(dir); |
| 46 | } |
| 47 | |
bigbiff bigbiff | c7360dd | 2014-01-25 15:02:57 -0500 | [diff] [blame] | 48 | void twrpDU::clear_relative_dir(string dir) { |
| 49 | vector<string>::iterator iter = relativedir.begin(); |
| 50 | while (iter != relativedir.end()) { |
| 51 | if (*iter == dir) |
| 52 | iter = relativedir.erase(iter); |
| 53 | else |
| 54 | iter++; |
| 55 | } |
| 56 | } |
| 57 | |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 58 | void twrpDU::add_absolute_dir(const string& dir) { |
| 59 | absolutedir.push_back(TWFunc::Remove_Trailing_Slashes(dir)); |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | vector<string> twrpDU::get_absolute_dirs(void) { |
| 63 | return absolutedir; |
| 64 | } |
| 65 | |
| 66 | uint64_t twrpDU::Get_Folder_Size(const string& Path) { |
| 67 | DIR* d; |
| 68 | struct dirent* de; |
| 69 | struct stat st; |
| 70 | unsigned long long dusize = 0; |
| 71 | unsigned long long dutemp = 0; |
| 72 | |
| 73 | parent = Path.substr(0, Path.find_last_of('/')); |
| 74 | |
| 75 | d = opendir(Path.c_str()); |
| 76 | if (d == NULL) { |
| 77 | LOGERR("error opening '%s'\n", Path.c_str()); |
| 78 | LOGERR("error: %s\n", strerror(errno)); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | while ((de = readdir(d)) != NULL) |
| 83 | { |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 84 | if (de->d_type == DT_DIR && !check_skip_dirs(Path, de->d_name)) { |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 85 | dutemp = Get_Folder_Size((Path + "/" + de->d_name)); |
| 86 | dusize += dutemp; |
| 87 | dutemp = 0; |
| 88 | } |
| 89 | else if (de->d_type == DT_REG) { |
| 90 | stat((Path + "/" + de->d_name).c_str(), &st); |
| 91 | dusize += (uint64_t)(st.st_size); |
| 92 | } |
| 93 | } |
| 94 | closedir(d); |
| 95 | return dusize; |
| 96 | } |
| 97 | |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 98 | bool twrpDU::check_relative_skip_dirs(const string& dir) { |
| 99 | return std::find(relativedir.begin(), relativedir.end(), dir) != relativedir.end(); |
| 100 | } |
| 101 | |
| 102 | bool twrpDU::check_absolute_skip_dirs(const string& path) { |
| 103 | string normalized = TWFunc::Remove_Trailing_Slashes(path); |
| 104 | return std::find(absolutedir.begin(), absolutedir.end(), normalized) != absolutedir.end(); |
| 105 | } |
| 106 | |
| 107 | bool twrpDU::check_skip_dirs(const string& parent, const string& dir) { |
| 108 | return check_relative_skip_dirs(dir) || check_absolute_skip_dirs(parent + "/" + dir); |
| 109 | } |
| 110 | |
| 111 | bool twrpDU::check_skip_dirs(const string& path) { |
| 112 | string normalized = TWFunc::Remove_Trailing_Slashes(path); |
| 113 | size_t slashIdx = normalized.find_last_of('/'); |
| 114 | if(slashIdx != std::string::npos && slashIdx+1 < normalized.size()) { |
| 115 | if(check_relative_skip_dirs(normalized.substr(slashIdx+1))) |
| 116 | return true; |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 117 | } |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 118 | return check_absolute_skip_dirs(normalized); |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 119 | } |