From 7abc5fe195cfa203d215781e2a04672cebecca1e Mon Sep 17 00:00:00 2001 From: bigbiff Date: Sat, 17 Jan 2015 16:53:12 -0500 Subject: Add cancel backup capability. 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 --- twrpTar.cpp | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'twrpTar.cpp') diff --git a/twrpTar.cpp b/twrpTar.cpp index 7f057df98..dfaa541d9 100644 --- a/twrpTar.cpp +++ b/twrpTar.cpp @@ -34,6 +34,7 @@ extern "C" { #include #include #include +#include #include #include #include @@ -83,9 +84,13 @@ void twrpTar::setpassword(string pass) { password = pass; } -int twrpTar::createTarFork(const unsigned long long *overall_size, const unsigned long long *other_backups_size) { +void twrpTar::Signal_Kill(int signum) { + _exit(255); +} + +int twrpTar::createTarFork(const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &fork_pid) { int status = 0; - pid_t pid, rc_pid; + pid_t rc_pid, tar_fork_pid; int progress_pipe[2], ret; file_count = 0; @@ -94,16 +99,17 @@ int twrpTar::createTarFork(const unsigned long long *overall_size, const unsigne LOGERR("Error creating progress tracking pipe\n"); return -1; } - if ((pid = fork()) == -1) { + if ((tar_fork_pid = fork()) == -1) { LOGINFO("create tar failed to fork.\n"); close(progress_pipe[0]); close(progress_pipe[1]); return -1; } - if (pid == 0) { - // Child process + if (tar_fork_pid == 0) { + // Child process // Child closes input side of progress pipe + signal(SIGUSR2, twrpTar::Signal_Kill); close(progress_pipe[0]); progress_pipe_fd = progress_pipe[1]; @@ -375,6 +381,8 @@ int twrpTar::createTarFork(const unsigned long long *overall_size, const unsigne files_backup = 0; size_backup = 0; + fork_pid = tar_fork_pid; + // Parent closes output side close(progress_pipe[1]); @@ -422,7 +430,7 @@ int twrpTar::createTarFork(const unsigned long long *overall_size, const unsigne backup_info.SetValue("file_count", files_backup); backup_info.SaveValues(); #endif //ndef BUILD_TWRPTAR_MAIN - if (TWFunc::Wait_For_Child(pid, &status, "createTarFork()") != 0) + if (TWFunc::Wait_For_Child(tar_fork_pid, &status, "createTarFork()") != 0) return -1; } return 0; @@ -430,7 +438,7 @@ int twrpTar::createTarFork(const unsigned long long *overall_size, const unsigne int twrpTar::extractTarFork(const unsigned long long *overall_size, unsigned long long *other_backups_size) { int status = 0; - pid_t pid, rc_pid; + pid_t rc_pid, tar_fork_pid; int progress_pipe[2], ret; if (pipe(progress_pipe) < 0) { @@ -438,10 +446,10 @@ int twrpTar::extractTarFork(const unsigned long long *overall_size, unsigned lon return -1; } - pid = fork(); - if (pid >= 0) // fork was successful + tar_fork_pid = fork(); + if (tar_fork_pid >= 0) // fork was successful { - if (pid == 0) // child process + if (tar_fork_pid == 0) // child process { close(progress_pipe[0]); progress_pipe_fd = progress_pipe[1]; @@ -585,7 +593,7 @@ int twrpTar::extractTarFork(const unsigned long long *overall_size, unsigned lon #endif //ndef BUILD_TWRPTAR_MAIN *other_backups_size += size_backup; - if (TWFunc::Wait_For_Child(pid, &status, "extractTarFork()") != 0) + if (TWFunc::Wait_For_Child(tar_fork_pid, &status, "extractTarFork()") != 0) return -1; } } -- cgit v1.2.3