Skip to content
This repository was archived by the owner on Nov 23, 2022. It is now read-only.

Commit

Permalink
Add cancel backup capability.
Browse files Browse the repository at this point in the history
This will stop the iteration of the partition objects, kill the
current twrpTar thread and remove the backup directory.

Implement TWAtomicInt class to give us a wrapper that automatically
uses mutexes before the read and write to help ensure that the
reads and writes will be atomic based on documentation.

Change-Id: I645b22bc980a292e9c7202acb24ffd22ebe68c63
  • Loading branch information
bigbiff authored and Dees-Troy committed Jan 27, 2015
1 parent 3454ade commit 7abc5fe
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ include $(CLEAR_VARS)
LOCAL_MODULE := libaosprecovery
LOCAL_MODULE_TAGS := eng optional
LOCAL_C_INCLUDES := $(LOCAL_PATH)/libmincrypt/includes
LOCAL_SRC_FILES := adb_install.cpp asn1_decoder.cpp bootloader.cpp legacy_property_service.c verifier.cpp set_metadata.c
LOCAL_SRC_FILES := adb_install.cpp asn1_decoder.cpp bootloader.cpp legacy_property_service.c verifier.cpp set_metadata.c tw_atomic.cpp
LOCAL_SHARED_LIBRARIES += libc liblog libcutils libmtdutils libfusesideload libmincrypttwrp libselinux

ifneq ($(BOARD_RECOVERY_BLDRMSG_OFFSET),)
Expand Down
52 changes: 43 additions & 9 deletions gui/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ extern "C" {

#include "rapidxml.hpp"
#include "objects.hpp"
#include "../tw_atomic.hpp"

void curtainClose(void);

Expand Down Expand Up @@ -169,6 +170,7 @@ GUIAction::GUIAction(xml_node<>* node)
mf["fixsu"] = &GUIAction::fixsu;
mf["startmtp"] = &GUIAction::startmtp;
mf["stopmtp"] = &GUIAction::stopmtp;
mf["cancelbackup"] = &GUIAction::cancelbackup;

// remember actions that run in the caller thread
for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
Expand Down Expand Up @@ -314,6 +316,13 @@ void GUIAction::simulate_progress_bar(void)
gui_print("Simulating actions...\n");
for (int i = 0; i < 5; i++)
{
if (PartitionManager.stop_backup.get_value()) {
DataManager::SetValue("tw_cancel_backup", 1);
gui_print("Backup Canceled.\n");
DataManager::SetValue("ui_progress", 0);
PartitionManager.stop_backup.set_value(0);
return;
}
usleep(500000);
DataManager::SetValue("ui_progress", i * 20);
}
Expand Down Expand Up @@ -1087,13 +1096,13 @@ int GUIAction::refreshsizes(std::string arg)

int GUIAction::nandroid(std::string arg)
{
operation_start("Nandroid");
int ret = 0;

if (simulate) {
DataManager::SetValue("tw_partition", "Simulation");
simulate_progress_bar();
} else {
operation_start("Nandroid");
int ret = 0;

if (arg == "backup") {
string Backup_Name;
DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Expand All @@ -1103,7 +1112,6 @@ int GUIAction::nandroid(std::string arg)
else {
operation_end(1);
return -1;

}
DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
} else if (arg == "restore") {
Expand All @@ -1112,16 +1120,42 @@ int GUIAction::nandroid(std::string arg)
ret = PartitionManager.Run_Restore(Restore_Name);
} else {
operation_end(1);
return -1;
}
}
DataManager::SetValue("tw_encrypt_backup", 0);
return -1;
}
DataManager::SetValue("tw_encrypt_backup", 0);
if (!PartitionManager.stop_backup.get_value()) {
if (ret == false)
ret = 1; // 1 for failure
else
ret = 0; // 0 for success
DataManager::SetValue("tw_cancel_backup", 0);
operation_end(ret);
return 0;
}
else {
DataManager::SetValue("tw_cancel_backup", 1);
gui_print("Backup Canceled.\n");
ret = 0;
}
return ret;
}
return 0;
}

int GUIAction::cancelbackup(std::string arg) {
if (simulate) {
simulate_progress_bar();
PartitionManager.stop_backup.set_value(1);
operation_end(0);
}
else {
operation_start("Cancel Backup");
int op_status = PartitionManager.Cancel_Backup();
if (op_status != 0)
op_status = 1; // failure
operation_end(op_status);
}

return 0;
}

int GUIAction::fixpermissions(std::string arg)
Expand Down
1 change: 1 addition & 0 deletions gui/devices/480x854/res/ui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<variable name="col1_x" value="10" />
<variable name="col2_x" value="240" />
<variable name="col_center_x" value="128" />
<variable name="col_center_medium_x" value="183" />
<variable name="center_x" value="240" />
<variable name="row1_y" value="140" />
<variable name="row2_y" value="290" />
Expand Down
25 changes: 24 additions & 1 deletion gui/devices/landscape/res/landscape.xml
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@
<object type="button">
<highlight color="%highlight_color%" />
<condition var1="tw_has_cancel" var2="1" />
<placement x="%col4_x%" y="%slider_y%" />
<placement x="%col_center_x%" y="%cancel_button_y%" />
<font resource="font" color="%button_text_color%" />
<text>Cancel</text>
<image resource="main_button" />
Expand Down Expand Up @@ -2157,6 +2157,17 @@

<object type="template" name="action_page_console" />

<object type="button">
<highlight color="%highlight_color%" />
<placement x="%col_center_medium_x%" y="%row3_y%" />
<font resource="font" color="%button_text_color%" />
<text>Cancel</text>
<image resource="medium_button" />
<actions>
<action function="cancelbackup"></action>
</actions>
</object>

<object type="template" name="progress_bar" />

<object type="action">
Expand All @@ -2165,6 +2176,7 @@

<object type="action">
<condition var1="tw_operation_state" var2="1" />
<condition var1="tw_cancel_backup" var2="0" />
<actions>
<action function="set">tw_back=backup</action>
<action function="set">tw_complete_text1=Backup Complete</action>
Expand All @@ -2173,6 +2185,17 @@
</actions>
</object>

<object type="action">
<condition var1="tw_operation_state" var2="1" />
<condition var1="tw_cancel_backup" var2="1" />
<actions>
<action function="set">tw_back=backup</action>
<action function="set">tw_complete_text1=Backup Cancelled</action>
<action function="set">tw_show_reboot=1</action>
<action function="page">action_complete</action>
</actions>
</object>

<object type="template" name="footer" />
</page>

Expand Down
23 changes: 23 additions & 0 deletions gui/devices/portrait/res/portrait.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,17 @@

<object type="template" name="action_page_console" />

<object type="button">
<highlight color="%highlight_color%" />
<placement x="%col_center_medium_x%" y="%row4_y%" />
<font resource="font" color="%button_text_color%" />
<text>Cancel</text>
<image resource="medium_button" />
<actions>
<action function="cancelbackup"></action>
</actions>
</object>

<object type="template" name="progress_bar" />

<object type="action">
Expand All @@ -1875,13 +1886,25 @@

<object type="action">
<condition var1="tw_operation_state" var2="1" />
<condition var1="tw_cancel_backup" var2="0" />
<actions>
<action function="set">tw_back=backup</action>
<action function="set">tw_complete_text1=Backup Complete</action>
<action function="set">tw_show_reboot=1</action>
<action function="page">action_complete</action>
</actions>
</object>

<object type="action">
<condition var1="tw_operation_state" var2="1" />
<condition var1="tw_cancel_backup" var2="1" />
<actions>
<action function="set">tw_back=backup</action>
<action function="set">tw_complete_text1=Backup Cancelled</action>
<action function="set">tw_show_reboot=1</action>
<action function="page">action_complete</action>
</actions>
</object>
</page>

<page name="restore">
Expand Down
27 changes: 27 additions & 0 deletions gui/devices/watch/res/watch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,21 @@

<object type="template" name="action_page_console" />



<object type="template" name="progress_bar" />

<object type="button">
<highlight color="%highlight_color%" />
<placement x="%col_center_medium_x%" y="%row4_y%" />
<font resource="font" color="%button_text_color%" />
<text>Cancel</text>
<image resource="medium_button" />
<actions>
<action function="cancelbackup"></action>
</actions>
</object>

<object type="template" name="progress_bar" />

<object type="action">
Expand All @@ -1867,13 +1882,25 @@

<object type="action">
<condition var1="tw_operation_state" var2="1" />
<condition var1="tw_cancel_backup" var2="0" />
<actions>
<action function="set">tw_back=backup</action>
<action function="set">tw_complete_text1=Backup Complete</action>
<action function="set">tw_show_reboot=1</action>
<action function="page">action_complete</action>
</actions>
</object>

<object type="action">
<condition var1="tw_operation_state" var2="1" />
<condition var1="tw_cancel_backup" var2="1" />
<actions>
<action function="set">tw_back=backup</action>
<action function="set">tw_complete_text1=Backup Cancelled</action>
<action function="set">tw_show_reboot=1</action>
<action function="page">action_complete</action>
</actions>
</object>
</page>

<page name="restore">
Expand Down
1 change: 1 addition & 0 deletions gui/objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ class GUIAction : public GUIObject, public ActionObject
int startmtp(std::string arg);
int stopmtp(std::string arg);
int flashimage(std::string arg);
int cancelbackup(std::string arg);

int simulate;
};
Expand Down
11 changes: 6 additions & 5 deletions partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1253,9 +1253,10 @@ bool TWPartition::Repair() {
return false;
}

bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size) {
if (Backup_Method == FILES)
return Backup_Tar(backup_folder, overall_size, other_backups_size);
bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
if (Backup_Method == FILES) {
return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
}
else if (Backup_Method == DD)
return Backup_DD(backup_folder);
else if (Backup_Method == FLASH_UTILS)
Expand Down Expand Up @@ -1702,7 +1703,7 @@ bool TWPartition::Wipe_Data_Without_Wiping_Media() {
#endif // ifdef TW_OEM_BUILD
}

bool TWPartition::Backup_Tar(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size) {
bool TWPartition::Backup_Tar(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
char back_name[255], split_index[5];
string Full_FileName, Split_FileName, Tar_Args, Command;
int use_compression, use_encryption = 0, index, backup_count;
Expand Down Expand Up @@ -1744,7 +1745,7 @@ bool TWPartition::Backup_Tar(string backup_folder, const unsigned long long *ove
tar.setsize(Backup_Size);
tar.partition_name = Backup_Name;
tar.backup_folder = backup_folder;
if (tar.createTarFork(overall_size, other_backups_size) != 0)
if (tar.createTarFork(overall_size, other_backups_size, tar_fork_pid) != 0)
return false;
return true;
}
Expand Down
36 changes: 33 additions & 3 deletions partitionmanager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012 bigbiff/Dees_Troy TeamWin
Copyright 2014 TeamWin
This file is part of TWRP/TeamWin Recovery Project.
TWRP is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -39,6 +39,7 @@
#include "twrpDigest.hpp"
#include "twrpDU.hpp"
#include "set_metadata.h"
#include "tw_atomic.hpp"

#ifdef TW_HAS_MTP
#include "mtp/mtp_MtpServer.hpp"
Expand All @@ -59,6 +60,8 @@ extern bool datamedia;
TWPartitionManager::TWPartitionManager(void) {
mtp_was_enabled = false;
mtp_write_fd = -1;
stop_backup.set_value(0);
tar_fork_pid = 0;
}

int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Expand Down Expand Up @@ -559,7 +562,7 @@ bool TWPartitionManager::Backup_Partition(TWPartition* Part, string Backup_Folde
TWFunc::SetPerformanceMode(true);
time(&start);

if (Part->Backup(Backup_Folder, &total_size, &current_size)) {
if (Part->Backup(Backup_Folder, &total_size, &current_size, tar_fork_pid)) {
bool md5Success = false;
current_size += Part->Backup_Size;
pos = (float)((float)(current_size) / (float)(total_size));
Expand All @@ -569,7 +572,7 @@ bool TWPartitionManager::Backup_Partition(TWPartition* Part, string Backup_Folde

for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size)) {
if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size, tar_fork_pid)) {
TWFunc::SetPerformanceMode(false);
return false;
}
Expand Down Expand Up @@ -608,6 +611,30 @@ bool TWPartitionManager::Backup_Partition(TWPartition* Part, string Backup_Folde
TWFunc::SetPerformanceMode(false);
return false;
}
return 0;
}

int TWPartitionManager::Cancel_Backup() {
string Backup_Folder, Backup_Name, Full_Backup_Path;

stop_backup.set_value(1);

if (tar_fork_pid != 0) {
DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
LOGINFO("Killing pid: %d\n", tar_fork_pid);
kill(tar_fork_pid, SIGUSR2);
while (kill(tar_fork_pid, 0) == 0) {
usleep(1000);
}
LOGINFO("Backup_Run stopped and returning false, backup cancelled.\n");
LOGINFO("Removing directory %s\n", Full_Backup_Path.c_str());
TWFunc::removeDir(Full_Backup_Path, false);
tar_fork_pid = 0;
}

return 0;
}

int TWPartitionManager::Run_Backup(void) {
Expand All @@ -621,6 +648,7 @@ int TWPartitionManager::Run_Backup(void) {
struct tm *t;
time_t start, stop, seconds, total_start, total_stop;
size_t start_pos = 0, end_pos = 0;
stop_backup.set_value(0);
seconds = time(0);
t = localtime(&seconds);

Expand Down Expand Up @@ -718,6 +746,8 @@ int TWPartitionManager::Run_Backup(void) {
start_pos = 0;
end_pos = Backup_List.find(";", start_pos);
while (end_pos != string::npos && start_pos < Backup_List.size()) {
if (stop_backup.get_value() != 0)
return -1;
backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
backup_part = Find_Partition_By_Path(backup_path);
if (backup_part != NULL) {
Expand Down
Loading

0 comments on commit 7abc5fe

Please sign in to comment.