| 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 |  | 
| Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 36 | extern bool datamedia; | 
 | 37 |  | 
| bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 38 | twrpDU::twrpDU() { | 
| Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 39 | 	add_relative_dir("."); | 
 | 40 | 	add_relative_dir(".."); | 
 | 41 | 	add_relative_dir("lost+found"); | 
 | 42 | 	add_absolute_dir("/data/data/com.google.android.music/files"); | 
| bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 43 | } | 
 | 44 |  | 
| Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 45 | void twrpDU::add_relative_dir(const string& dir) { | 
| bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 46 | 	relativedir.push_back(dir); | 
 | 47 | } | 
 | 48 |  | 
| bigbiff bigbiff | c7360dd | 2014-01-25 15:02:57 -0500 | [diff] [blame] | 49 | void twrpDU::clear_relative_dir(string dir) { | 
 | 50 | 	vector<string>::iterator iter = relativedir.begin(); | 
 | 51 | 	while (iter != relativedir.end()) { | 
 | 52 | 		if (*iter == dir) | 
 | 53 | 			iter = relativedir.erase(iter); | 
 | 54 | 		else | 
 | 55 | 			iter++; | 
 | 56 | 	} | 
 | 57 | } | 
 | 58 |  | 
| Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 59 | void twrpDU::add_absolute_dir(const string& dir) { | 
 | 60 | 	absolutedir.push_back(TWFunc::Remove_Trailing_Slashes(dir)); | 
| bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 61 | } | 
 | 62 |  | 
 | 63 | vector<string> twrpDU::get_absolute_dirs(void) { | 
 | 64 | 	return absolutedir; | 
 | 65 | } | 
 | 66 |  | 
 | 67 | uint64_t twrpDU::Get_Folder_Size(const string& Path) { | 
 | 68 | 	DIR* d; | 
 | 69 | 	struct dirent* de; | 
 | 70 | 	struct stat st; | 
| Matt Mower | 2f1b2ab | 2014-03-20 20:20:13 -0500 | [diff] [blame] | 71 | 	uint64_t dusize = 0; | 
| Ethan Yonker | 82ce281 | 2014-09-04 13:36:45 -0500 | [diff] [blame] | 72 | 	string FullPath; | 
| bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 73 |  | 
 | 74 | 	d = opendir(Path.c_str()); | 
 | 75 | 	if (d == NULL) { | 
 | 76 | 		LOGERR("error opening '%s'\n", Path.c_str()); | 
 | 77 | 		LOGERR("error: %s\n", strerror(errno)); | 
 | 78 | 		return 0; | 
 | 79 | 	} | 
 | 80 |  | 
| Matt Mower | 2f1b2ab | 2014-03-20 20:20:13 -0500 | [diff] [blame] | 81 | 	while ((de = readdir(d)) != NULL) { | 
| Ethan Yonker | 82ce281 | 2014-09-04 13:36:45 -0500 | [diff] [blame] | 82 | 		FullPath = Path + "/"; | 
 | 83 | 		FullPath += de->d_name; | 
 | 84 | 		if (lstat(FullPath.c_str(), &st)) { | 
 | 85 | 			LOGERR("Unable to stat '%s'\n", FullPath.c_str()); | 
 | 86 | 			continue; | 
 | 87 | 		} | 
 | 88 | 		if ((st.st_mode & S_IFDIR) && !check_skip_dirs(FullPath) && de->d_type != DT_SOCK) { | 
 | 89 | 			dusize += Get_Folder_Size(FullPath); | 
 | 90 | 		} else if (st.st_mode & S_IFREG) { | 
| bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 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) { | 
| Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 103 | 	return std::find(absolutedir.begin(), absolutedir.end(), path) != absolutedir.end(); | 
| Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 104 | } | 
 | 105 |  | 
 | 106 | bool twrpDU::check_skip_dirs(const string& path) { | 
 | 107 | 	string normalized = TWFunc::Remove_Trailing_Slashes(path); | 
 | 108 | 	size_t slashIdx = normalized.find_last_of('/'); | 
 | 109 | 	if(slashIdx != std::string::npos && slashIdx+1 < normalized.size()) { | 
 | 110 | 		if(check_relative_skip_dirs(normalized.substr(slashIdx+1))) | 
 | 111 | 			return true; | 
| bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 112 | 	} | 
| Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 113 | 	return check_absolute_skip_dirs(normalized); | 
| bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 114 | } |