exclude.cpp 3.03 KB
Newer Older
1
/*
2
		Copyright 2013 to 2016 TeamWin
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
		This file is part of TWRP/TeamWin Recovery Project.

		TWRP is free software: you can redistribute it and/or modify
		it under the terms of the GNU General Public License as published by
		the Free Software Foundation, either version 3 of the License, or
		(at your option) any later version.

		TWRP is distributed in the hope that it will be useful,
		but WITHOUT ANY WARRANTY; without even the implied warranty of
		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
		GNU General Public License for more details.

		You should have received a copy of the GNU General Public License
		along with TWRP.  If not, see <http://www.gnu.org/licenses/>.
*/

extern "C" {
	#include "libtar/libtar.h"
}
#include <sys/types.h>
#include <sys/stat.h>
24
#include <dirent.h>
25 26 27
#include <errno.h>
#include <string>
#include <vector>
28
#include "exclude.hpp"
29
#include "twrp-functions.hpp"
Ethan Yonker's avatar
Ethan Yonker committed
30
#include "gui/gui.hpp"
31
#include "twcommon.h"
32 33 34

using namespace std;

35 36
extern bool datamedia;

37
TWExclude::TWExclude() {
38 39 40
	add_relative_dir(".");
	add_relative_dir("..");
	add_relative_dir("lost+found");
41 42
}

43
void TWExclude::add_relative_dir(const string& dir) {
44 45 46
	relativedir.push_back(dir);
}

47
void TWExclude::clear_relative_dir(string dir) {
48 49 50 51 52 53 54 55 56
	vector<string>::iterator iter = relativedir.begin();
	while (iter != relativedir.end()) {
		if (*iter == dir)
			iter = relativedir.erase(iter);
		else
			iter++;
	}
}

57
void TWExclude::add_absolute_dir(const string& dir) {
58
	absolutedir.push_back(TWFunc::Remove_Trailing_Slashes(dir));
59 60
}

61
uint64_t TWExclude::Get_Folder_Size(const string& Path) {
62 63 64
	DIR* d;
	struct dirent* de;
	struct stat st;
65
	uint64_t dusize = 0;
66
	string FullPath;
67 68 69

	d = opendir(Path.c_str());
	if (d == NULL) {
Ethan Yonker's avatar
Ethan Yonker committed
70
		gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Path)(strerror(errno)));
71 72 73
		return 0;
	}

74
	while ((de = readdir(d)) != NULL) {
75 76 77
		FullPath = Path + "/";
		FullPath += de->d_name;
		if (lstat(FullPath.c_str(), &st)) {
Ethan Yonker's avatar
Ethan Yonker committed
78 79
			gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(FullPath)(strerror(errno)));
			LOGINFO("Real error: Unable to stat '%s'\n", FullPath.c_str());
80 81 82 83
			continue;
		}
		if ((st.st_mode & S_IFDIR) && !check_skip_dirs(FullPath) && de->d_type != DT_SOCK) {
			dusize += Get_Folder_Size(FullPath);
84
		} else if (st.st_mode & S_IFREG || st.st_mode & S_IFLNK) {
85 86 87 88 89 90 91
			dusize += (uint64_t)(st.st_size);
		}
	}
	closedir(d);
	return dusize;
}

92
bool TWExclude::check_relative_skip_dirs(const string& dir) {
93 94 95
	return std::find(relativedir.begin(), relativedir.end(), dir) != relativedir.end();
}

96
bool TWExclude::check_absolute_skip_dirs(const string& path) {
97
	return std::find(absolutedir.begin(), absolutedir.end(), path) != absolutedir.end();
98 99
}

100
bool TWExclude::check_skip_dirs(const string& path) {
101 102
	string normalized = TWFunc::Remove_Trailing_Slashes(path);
	size_t slashIdx = normalized.find_last_of('/');
Matt Mower's avatar
Matt Mower committed
103 104
	if (slashIdx != std::string::npos && slashIdx+1 < normalized.size()) {
		if (check_relative_skip_dirs(normalized.substr(slashIdx+1)))
105
			return true;
106
	}
107
	return check_absolute_skip_dirs(normalized);
108
}