blob: 08dfdcbea5e4e20be24145ebd0b10f131285cb84 [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"
Ethan Yonker74db1572015-10-28 12:44:49 -050033#include "gui/gui.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050034
35using namespace std;
36
Ethan Yonker6277c792014-09-15 14:54:30 -050037extern bool datamedia;
38
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050039twrpDU::twrpDU() {
Ethan Yonker6277c792014-09-15 14:54:30 -050040 add_relative_dir(".");
41 add_relative_dir("..");
42 add_relative_dir("lost+found");
43 add_absolute_dir("/data/data/com.google.android.music/files");
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050044}
45
Vojtech Bocek05f87d62014-03-11 22:08:23 +010046void twrpDU::add_relative_dir(const string& dir) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050047 relativedir.push_back(dir);
48}
49
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -050050void twrpDU::clear_relative_dir(string dir) {
51 vector<string>::iterator iter = relativedir.begin();
52 while (iter != relativedir.end()) {
53 if (*iter == dir)
54 iter = relativedir.erase(iter);
55 else
56 iter++;
57 }
58}
59
Vojtech Bocek05f87d62014-03-11 22:08:23 +010060void twrpDU::add_absolute_dir(const string& dir) {
61 absolutedir.push_back(TWFunc::Remove_Trailing_Slashes(dir));
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050062}
63
64vector<string> twrpDU::get_absolute_dirs(void) {
65 return absolutedir;
66}
67
68uint64_t twrpDU::Get_Folder_Size(const string& Path) {
69 DIR* d;
70 struct dirent* de;
71 struct stat st;
Matt Mower2f1b2ab2014-03-20 20:20:13 -050072 uint64_t dusize = 0;
Ethan Yonker82ce2812014-09-04 13:36:45 -050073 string FullPath;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050074
75 d = opendir(Path.c_str());
76 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -050077 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Path)(strerror(errno)));
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050078 return 0;
79 }
80
Matt Mower2f1b2ab2014-03-20 20:20:13 -050081 while ((de = readdir(d)) != NULL) {
Ethan Yonker82ce2812014-09-04 13:36:45 -050082 FullPath = Path + "/";
83 FullPath += de->d_name;
84 if (lstat(FullPath.c_str(), &st)) {
Ethan Yonker74db1572015-10-28 12:44:49 -050085 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(FullPath)(strerror(errno)));
86 LOGINFO("Real error: Unable to stat '%s'\n", FullPath.c_str());
Ethan Yonker82ce2812014-09-04 13:36:45 -050087 continue;
88 }
89 if ((st.st_mode & S_IFDIR) && !check_skip_dirs(FullPath) && de->d_type != DT_SOCK) {
90 dusize += Get_Folder_Size(FullPath);
91 } else if (st.st_mode & S_IFREG) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050092 dusize += (uint64_t)(st.st_size);
93 }
94 }
95 closedir(d);
96 return dusize;
97}
98
Vojtech Bocek05f87d62014-03-11 22:08:23 +010099bool twrpDU::check_relative_skip_dirs(const string& dir) {
100 return std::find(relativedir.begin(), relativedir.end(), dir) != relativedir.end();
101}
102
103bool twrpDU::check_absolute_skip_dirs(const string& path) {
Matt Mower50248ab2014-03-31 15:58:40 -0500104 return std::find(absolutedir.begin(), absolutedir.end(), path) != absolutedir.end();
Vojtech Bocek05f87d62014-03-11 22:08:23 +0100105}
106
107bool twrpDU::check_skip_dirs(const string& path) {
108 string normalized = TWFunc::Remove_Trailing_Slashes(path);
109 size_t slashIdx = normalized.find_last_of('/');
110 if(slashIdx != std::string::npos && slashIdx+1 < normalized.size()) {
111 if(check_relative_skip_dirs(normalized.substr(slashIdx+1)))
112 return true;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500113 }
Vojtech Bocek05f87d62014-03-11 22:08:23 +0100114 return check_absolute_skip_dirs(normalized);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500115}