bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 1 | /* |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 2 | Copyright 2013 to 2016 TeamWin |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 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> |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 24 | #include <dirent.h> |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 25 | #include <errno.h> |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 26 | #include <string> |
| 27 | #include <vector> |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 28 | #include "exclude.hpp" |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 29 | #include "twrp-functions.hpp" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 30 | #include "gui/gui.hpp" |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 31 | #include "twcommon.h" |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 32 | |
| 33 | using namespace std; |
| 34 | |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 35 | extern bool datamedia; |
| 36 | |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 37 | TWExclude::TWExclude() { |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 38 | add_relative_dir("."); |
| 39 | add_relative_dir(".."); |
| 40 | add_relative_dir("lost+found"); |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 41 | } |
| 42 | |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 43 | void TWExclude::add_relative_dir(const string& dir) { |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 44 | relativedir.push_back(dir); |
| 45 | } |
| 46 | |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 47 | void TWExclude::clear_relative_dir(string dir) { |
bigbiff bigbiff | c7360dd | 2014-01-25 15:02:57 -0500 | [diff] [blame] | 48 | vector<string>::iterator iter = relativedir.begin(); |
| 49 | while (iter != relativedir.end()) { |
| 50 | if (*iter == dir) |
| 51 | iter = relativedir.erase(iter); |
| 52 | else |
| 53 | iter++; |
| 54 | } |
| 55 | } |
| 56 | |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 57 | void TWExclude::add_absolute_dir(const string& dir) { |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 58 | absolutedir.push_back(TWFunc::Remove_Trailing_Slashes(dir)); |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 59 | } |
| 60 | |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 61 | uint64_t TWExclude::Get_Folder_Size(const string& Path) { |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 62 | DIR* d; |
| 63 | struct dirent* de; |
| 64 | struct stat st; |
Matt Mower | 2f1b2ab | 2014-03-20 20:20:13 -0500 | [diff] [blame] | 65 | uint64_t dusize = 0; |
Ethan Yonker | 82ce281 | 2014-09-04 13:36:45 -0500 | [diff] [blame] | 66 | string FullPath; |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 67 | |
| 68 | d = opendir(Path.c_str()); |
| 69 | if (d == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 70 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Path)(strerror(errno))); |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
Matt Mower | 2f1b2ab | 2014-03-20 20:20:13 -0500 | [diff] [blame] | 74 | while ((de = readdir(d)) != NULL) { |
Ethan Yonker | 82ce281 | 2014-09-04 13:36:45 -0500 | [diff] [blame] | 75 | FullPath = Path + "/"; |
| 76 | FullPath += de->d_name; |
| 77 | if (lstat(FullPath.c_str(), &st)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 78 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(FullPath)(strerror(errno))); |
| 79 | LOGINFO("Real error: Unable to stat '%s'\n", FullPath.c_str()); |
Ethan Yonker | 82ce281 | 2014-09-04 13:36:45 -0500 | [diff] [blame] | 80 | continue; |
| 81 | } |
| 82 | if ((st.st_mode & S_IFDIR) && !check_skip_dirs(FullPath) && de->d_type != DT_SOCK) { |
| 83 | dusize += Get_Folder_Size(FullPath); |
| 84 | } else if (st.st_mode & S_IFREG) { |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 85 | dusize += (uint64_t)(st.st_size); |
| 86 | } |
| 87 | } |
| 88 | closedir(d); |
| 89 | return dusize; |
| 90 | } |
| 91 | |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 92 | bool TWExclude::check_relative_skip_dirs(const string& dir) { |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 93 | return std::find(relativedir.begin(), relativedir.end(), dir) != relativedir.end(); |
| 94 | } |
| 95 | |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 96 | bool TWExclude::check_absolute_skip_dirs(const string& path) { |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 97 | return std::find(absolutedir.begin(), absolutedir.end(), path) != absolutedir.end(); |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 100 | bool TWExclude::check_skip_dirs(const string& path) { |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 101 | string normalized = TWFunc::Remove_Trailing_Slashes(path); |
| 102 | size_t slashIdx = normalized.find_last_of('/'); |
| 103 | if(slashIdx != std::string::npos && slashIdx+1 < normalized.size()) { |
| 104 | if(check_relative_skip_dirs(normalized.substr(slashIdx+1))) |
| 105 | return true; |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 106 | } |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 107 | return check_absolute_skip_dirs(normalized); |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 108 | } |