blob: c4446d42a786906a729a4deb1baf3c22eea473d5 [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>
30#include "twrpDU.hpp"
31
32using namespace std;
33
34twrpDU::twrpDU() {
35 add_relative_dir(".");
36 add_relative_dir("..");
37 add_relative_dir("lost_found");
38 add_absolute_dir("/data/data/com.google.android.music/files");
39 parent = "";
40}
41
42void twrpDU::add_relative_dir(string dir) {
43 relativedir.push_back(dir);
44}
45
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -050046void twrpDU::clear_relative_dir(string dir) {
47 vector<string>::iterator iter = relativedir.begin();
48 while (iter != relativedir.end()) {
49 if (*iter == dir)
50 iter = relativedir.erase(iter);
51 else
52 iter++;
53 }
54}
55
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050056void twrpDU::add_absolute_dir(string dir) {
57 absolutedir.push_back(dir);
58}
59
60vector<string> twrpDU::get_absolute_dirs(void) {
61 return absolutedir;
62}
63
64uint64_t twrpDU::Get_Folder_Size(const string& Path) {
65 DIR* d;
66 struct dirent* de;
67 struct stat st;
68 unsigned long long dusize = 0;
69 unsigned long long dutemp = 0;
70
71 parent = Path.substr(0, Path.find_last_of('/'));
72
73 d = opendir(Path.c_str());
74 if (d == NULL) {
75 LOGERR("error opening '%s'\n", Path.c_str());
76 LOGERR("error: %s\n", strerror(errno));
77 return 0;
78 }
79
80 while ((de = readdir(d)) != NULL)
81 {
82 bool skip_dir = false;
83 if (de->d_type == DT_DIR) {
84 string dir = de->d_name;
85 skip_dir = check_skip_dirs(dir);
86 }
87 if (de->d_type == DT_DIR && !skip_dir) {
88 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
101bool twrpDU::check_skip_dirs(string& dir) {
102 bool result = false;
103 for (int i = 0; i < relativedir.size(); ++i) {
104 if (dir == relativedir.at(i)) {
105 result = true;
106 break;
107 }
108 }
109 for (int i = 0; i < absolutedir.size(); ++i) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500110 if (dir == absolutedir.at(i)) {
111 result = true;
112 break;
113 }
114 }
115 return result;
116}