blob: 66c803104f6cd52deabd9b86cff5b57d9031ad03 [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
46void twrpDU::add_absolute_dir(string dir) {
47 absolutedir.push_back(dir);
48}
49
50vector<string> twrpDU::get_absolute_dirs(void) {
51 return absolutedir;
52}
53
54uint64_t twrpDU::Get_Folder_Size(const string& Path) {
55 DIR* d;
56 struct dirent* de;
57 struct stat st;
58 unsigned long long dusize = 0;
59 unsigned long long dutemp = 0;
60
61 parent = Path.substr(0, Path.find_last_of('/'));
62
63 d = opendir(Path.c_str());
64 if (d == NULL) {
65 LOGERR("error opening '%s'\n", Path.c_str());
66 LOGERR("error: %s\n", strerror(errno));
67 return 0;
68 }
69
70 while ((de = readdir(d)) != NULL)
71 {
72 bool skip_dir = false;
73 if (de->d_type == DT_DIR) {
74 string dir = de->d_name;
75 skip_dir = check_skip_dirs(dir);
76 }
77 if (de->d_type == DT_DIR && !skip_dir) {
78 dutemp = Get_Folder_Size((Path + "/" + de->d_name));
79 dusize += dutemp;
80 dutemp = 0;
81 }
82 else if (de->d_type == DT_REG) {
83 stat((Path + "/" + de->d_name).c_str(), &st);
84 dusize += (uint64_t)(st.st_size);
85 }
86 }
87 closedir(d);
88 return dusize;
89}
90
91bool twrpDU::check_skip_dirs(string& dir) {
92 bool result = false;
93 for (int i = 0; i < relativedir.size(); ++i) {
94 if (dir == relativedir.at(i)) {
95 result = true;
96 break;
97 }
98 }
99 for (int i = 0; i < absolutedir.size(); ++i) {
100 //string absdir = parent + dir;
101 if (dir == absolutedir.at(i)) {
102 result = true;
103 break;
104 }
105 }
106 return result;
107}