Skip to content

Commit

Permalink
Improve backup & wipe exclusion handling
Browse files Browse the repository at this point in the history
Rename twrpDU.* to exclude.*
Remove global variable for du and replace with partition specific
variables.
Use separate exclusion lists for backups and wiping.
Clean up some includes
Fix some parenthesis in twrp.cpp that I messed up.

Note: twrpTarMain command line utility compiles but probably does
not work correctly yet due to not properly setting part_settings

Change-Id: Idec9c3e6a8782ba53f3420fa79ba33394f4f85fb
  • Loading branch information
Dees-Troy committed Nov 30, 2016
1 parent 0a8a7ce commit 3fdcda4
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ LOCAL_SRC_FILES := \
twrp.cpp \
fixContexts.cpp \
twrpTar.cpp \
twrpDU.cpp \
exclude.cpp \
twrpDigest.cpp \
digest/md5.c \
find_file.cpp \
Expand Down
1 change: 1 addition & 0 deletions data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <time.h>
#include <string>
#include <sstream>
#include <fstream>
#include <cctype>
#include <cutils/properties.h>
#include <unistd.h>
Expand Down
31 changes: 12 additions & 19 deletions twrpDU.cpp → exclude.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013 TeamWin
Copyright 2013 to 2016 TeamWin
This file is part of TWRP/TeamWin Recovery Project.
TWRP is free software: you can redistribute it and/or modify
Expand All @@ -21,33 +21,30 @@ extern "C" {
}
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include "twrpDU.hpp"
#include "exclude.hpp"
#include "twrp-functions.hpp"
#include "gui/gui.hpp"
#include "twcommon.h"

using namespace std;

extern bool datamedia;

twrpDU::twrpDU() {
TWExclude::TWExclude() {
add_relative_dir(".");
add_relative_dir("..");
add_relative_dir("lost+found");
add_absolute_dir("/data/data/com.google.android.music/files");
}

void twrpDU::add_relative_dir(const string& dir) {
void TWExclude::add_relative_dir(const string& dir) {
relativedir.push_back(dir);
}

void twrpDU::clear_relative_dir(string dir) {
void TWExclude::clear_relative_dir(string dir) {
vector<string>::iterator iter = relativedir.begin();
while (iter != relativedir.end()) {
if (*iter == dir)
Expand All @@ -57,15 +54,11 @@ void twrpDU::clear_relative_dir(string dir) {
}
}

void twrpDU::add_absolute_dir(const string& dir) {
void TWExclude::add_absolute_dir(const string& dir) {
absolutedir.push_back(TWFunc::Remove_Trailing_Slashes(dir));
}

vector<string> twrpDU::get_absolute_dirs(void) {
return absolutedir;
}

uint64_t twrpDU::Get_Folder_Size(const string& Path) {
uint64_t TWExclude::Get_Folder_Size(const string& Path) {
DIR* d;
struct dirent* de;
struct stat st;
Expand Down Expand Up @@ -96,15 +89,15 @@ uint64_t twrpDU::Get_Folder_Size(const string& Path) {
return dusize;
}

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

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

bool twrpDU::check_skip_dirs(const string& path) {
bool TWExclude::check_skip_dirs(const string& path) {
string normalized = TWFunc::Remove_Trailing_Slashes(path);
size_t slashIdx = normalized.find_last_of('/');
if(slashIdx != std::string::npos && slashIdx+1 < normalized.size()) {
Expand Down
23 changes: 6 additions & 17 deletions twrpDU.hpp → exclude.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013 TeamWin
Copyright 2013 to 2016 TeamWin
This file is part of TWRP/TeamWin Recovery Project.
TWRP is free software: you can redistribute it and/or modify
Expand All @@ -16,39 +16,28 @@
along with TWRP. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef TWRPDU_HPP
#define TWRPDU_HPP

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <fstream>
#ifndef TWEXCLUDE_HPP
#define TWEXCLUDE_HPP

#include <string>
#include <vector>
#include "twcommon.h"

using namespace std;

class twrpDU {
class TWExclude {

public:
twrpDU();
TWExclude();
uint64_t Get_Folder_Size(const string& Path); // Gets the folder's size using stat
void add_absolute_dir(const string& Path);
void add_relative_dir(const string& Path);
bool check_relative_skip_dirs(const string& dir);
bool check_absolute_skip_dirs(const string& path);
bool check_skip_dirs(const string& path);
vector<string> get_absolute_dirs(void);
void clear_relative_dir(string dir);
private:
vector<string> absolutedir;
vector<string> relativedir;
};

extern twrpDU du;
#endif
2 changes: 1 addition & 1 deletion gui/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <stdio.h>
#include <stdlib.h>

#include <sys/stat.h>
#include <string>

extern "C" {
Expand Down
1 change: 1 addition & 0 deletions gui/pages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include "../twrp-functions.hpp"
#include "../partitions.hpp"

Expand Down
1 change: 1 addition & 0 deletions gui/resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <sstream>
#include <iostream>
#include <iomanip>
#include <fcntl.h>

#include "../minzip/Zip.h"
extern "C" {
Expand Down
22 changes: 14 additions & 8 deletions partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include "twrp-functions.hpp"
#include "twrpDigest.hpp"
#include "twrpTar.hpp"
#include "twrpDU.hpp"
#include "exclude.hpp"
#include "infomanager.hpp"
#include "set_metadata.h"
#include "gui/gui.hpp"
Expand Down Expand Up @@ -866,17 +866,22 @@ void TWPartition::Setup_Data_Media() {
}
DataManager::SetValue("tw_has_internal", 1);
DataManager::SetValue("tw_has_data_media", 1);
du.add_absolute_dir(Mount_Point + "/misc/vold");
du.add_absolute_dir(Mount_Point + "/.layout_version");
du.add_absolute_dir(Mount_Point + "/system/storage.xml");
backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
backup_exclusions.add_absolute_dir(Mount_Point + "/misc/vold");
wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold");
backup_exclusions.add_absolute_dir(Mount_Point + "/.layout_version");
wipe_exclusions.add_absolute_dir(Mount_Point + "/.layout_version");
backup_exclusions.add_absolute_dir(Mount_Point + "/system/storage.xml");
wipe_exclusions.add_absolute_dir(Mount_Point + "/system/storage.xml");
} else {
if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
Storage_Path = Mount_Point + "/media/0";
Symlink_Path = Storage_Path;
UnMount(true);
}
}
du.add_absolute_dir(Mount_Point + "/media");
backup_exclusions.add_absolute_dir(Mount_Point + "/media");
wipe_exclusions.add_absolute_dir(Mount_Point + "/media");
}

void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Expand Down Expand Up @@ -2113,7 +2118,7 @@ bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unu

dir = parent;
dir.append(de->d_name);
if (du.check_skip_dirs(dir)) {
if (wipe_exclusions.check_skip_dirs(dir)) {
LOGINFO("skipped '%s'\n", dir.c_str());
continue;
}
Expand Down Expand Up @@ -2168,6 +2173,7 @@ bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_p
Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
tar.has_data_media = Has_Data_Media;
tar.part_settings = part_settings;
tar.backup_exclusions = &backup_exclusions;
tar.setdir(Backup_Path);
tar.setfn(Full_FileName);
tar.setsize(Backup_Size);
Expand Down Expand Up @@ -2490,7 +2496,7 @@ bool TWPartition::Update_Size(bool Display_Error) {
if (Has_Data_Media) {
if (Mount(Display_Error)) {
unsigned long long data_media_used, actual_data;
Used = du.Get_Folder_Size(Mount_Point);
Used = backup_exclusions.Get_Folder_Size(Mount_Point);
Backup_Size = Used;
int bak = (int)(Used / 1048576LLU);
int fre = (int)(Free / 1048576LLU);
Expand All @@ -2502,7 +2508,7 @@ bool TWPartition::Update_Size(bool Display_Error) {
}
} else if (Has_Android_Secure) {
if (Mount(Display_Error))
Backup_Size = du.Get_Folder_Size(Backup_Path);
Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
else {
if (!Was_Already_Mounted)
UnMount(false);
Expand Down
9 changes: 5 additions & 4 deletions partitionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "twrp-functions.hpp"
#include "fixContexts.hpp"
#include "twrpDigest.hpp"
#include "twrpDU.hpp"
#include "exclude.hpp"
#include "set_metadata.h"
#include "tw_atomic.hpp"
#include "gui/gui.hpp"
Expand Down Expand Up @@ -835,9 +835,10 @@ int TWPartitionManager::Run_Backup(bool adbbackup) {
int total_time = (int) difftime(total_stop, total_start);

uint64_t actual_backup_size;
if (!adbbackup)
actual_backup_size = du.Get_Folder_Size(part_settings.Backup_Folder);
else
if (!adbbackup) {
TWExclude twe;
actual_backup_size = twe.Get_Folder_Size(part_settings.Backup_Folder);
} else
actual_backup_size = part_settings.file_bytes + part_settings.img_bytes;
actual_backup_size /= (1024LLU * 1024LLU);

Expand Down
4 changes: 3 additions & 1 deletion partitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include <vector>
#include <string>
#include "twrpDU.hpp"
#include "exclude.hpp"
#include "tw_atomic.hpp"
#include "progresstracking.hpp"

Expand Down Expand Up @@ -215,6 +215,8 @@ class TWPartition
bool Can_Flash_Img; // Indicates if this partition can have images flashed to it via the GUI
bool Mount_Read_Only; // Only mount this partition as read-only
bool Is_Adopted_Storage; // Indicates that this partition is for adopted storage (android_expand)
TWExclude backup_exclusions;
TWExclude wipe_exclusions;

friend class TWPartitionManager;
friend class DataManager;
Expand Down
12 changes: 5 additions & 7 deletions twrp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ extern "C" {
#include "partitions.hpp"
#include "openrecoveryscript.hpp"
#include "variables.h"
#include "twrpDU.hpp"
#ifdef TW_USE_NEW_MINADBD
#include "adb.h"
#else
Expand All @@ -64,7 +63,6 @@ extern int adb_server_main(int is_daemon, int server_port, int /* reply_fd */);
TWPartitionManager PartitionManager;
int Log_Offset;
bool datamedia;
twrpDU du;

static void Print_Prop(const char *key, const char *name, void *cookie) {
printf("%s=%s\n", key, name);
Expand Down Expand Up @@ -235,19 +233,19 @@ int main(int argc, char **argv) {
} else if (*argptr == 'p') {
Shutdown = true;
} else if (*argptr == 's') {
if (strncmp(argptr, "send_intent", strlen("send_intent") == 0)) {
if (strncmp(argptr, "send_intent", strlen("send_intent")) == 0) {
ptr = argptr + strlen("send_intent") + 1;
Send_Intent = *ptr;
} else if (strncmp(argptr, "security", strlen("security") == 0)) {
} else if (strncmp(argptr, "security", strlen("security")) == 0) {
LOGINFO("Security update\n");
} else if (strncmp(argptr, "sideload", strlen("sideload") == 0)) {
} else if (strncmp(argptr, "sideload", strlen("sideload")) == 0) {
if (!OpenRecoveryScript::Insert_ORS_Command("sideload\n"))
break;
} else if (strncmp(argptr, "stages", strlen("stages") == 0)) {
} else if (strncmp(argptr, "stages", strlen("stages")) == 0) {
LOGINFO("ignoring stages command\n");
}
} else if (*argptr == 'r') {
if (strncmp(argptr, "reason", strlen("reason") == 0)) {
if (strncmp(argptr, "reason", strlen("reason")) == 0) {
ptr = argptr + strlen("reason") + 1;
gui_print("%s\n", ptr);
}
Expand Down
Loading

0 comments on commit 3fdcda4

Please sign in to comment.