summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--partition.cpp10
-rw-r--r--twrpTar.cpp145
-rw-r--r--twrpTar.hpp10
3 files changed, 126 insertions, 39 deletions
diff --git a/partition.cpp b/partition.cpp
index 53e7ba636..73aa8af16 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -1313,7 +1313,7 @@ bool TWPartition::Backup_Tar(string backup_folder) {
sprintf(back_name, "%s", Backup_Path.c_str());
tar.setdir(back_name);
tar.setfn(Full_FileName);
- backup_count = tar.splitArchiveThread();
+ backup_count = tar.splitArchiveFork();
if (backup_count == -1) {
LOGE("Error tarring split files!\n");
return false;
@@ -1324,7 +1324,7 @@ bool TWPartition::Backup_Tar(string backup_folder) {
if (use_compression) {
tar.setdir(Backup_Path);
tar.setfn(Full_FileName);
- if (tar.createTarGZThread() != 0)
+ if (tar.createTarGZFork() != 0)
return -1;
string gzname = Full_FileName + ".gz";
rename(gzname.c_str(), Full_FileName.c_str());
@@ -1332,7 +1332,7 @@ bool TWPartition::Backup_Tar(string backup_folder) {
else {
tar.setdir(Backup_Path);
tar.setfn(Full_FileName);
- if (tar.createTarThread() != 0)
+ if (tar.createTarFork() != 0)
return -1;
}
if (TWFunc::Get_File_Size(Full_FileName) == 0) {
@@ -1424,7 +1424,7 @@ bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System)
twrpTar tar;
tar.setdir("/");
tar.setfn(Full_FileName);
- if (tar.extractTarThread() != 0)
+ if (tar.extractTarFork() != 0)
return false;
sprintf(split_index, "%03i", index);
Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
@@ -1438,7 +1438,7 @@ bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System)
twrpTar tar;
tar.setdir(Backup_Path);
tar.setfn(Full_FileName);
- if (tar.extractTarThread() != 0)
+ if (tar.extractTarFork() != 0)
return false;
}
return true;
diff --git a/twrpTar.cpp b/twrpTar.cpp
index 43448672c..5d1752131 100644
--- a/twrpTar.cpp
+++ b/twrpTar.cpp
@@ -24,6 +24,7 @@ extern "C" {
}
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/wait.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
@@ -49,51 +50,139 @@ void twrpTar::setdir(string dir) {
tardir = dir;
}
-int twrpTar::createTarGZThread() {
- pthread_t thread;
- ThreadPtr tarptr = &twrpTar::createTGZ;
- PThreadPtr p = *(PThreadPtr*)&tarptr;
- pthread_create(&thread, NULL, p, this);
- if(pthread_join(thread, NULL)) {
+int twrpTar::createTarGZFork() {
+ int status;
+ pid_t pid;
+ if ((pid = fork()) == -1) {
+ LOGI("create tar failed to fork.\n");
return -1;
}
- TWFunc::drop_caches();
+ if (pid == 0) {
+ if (createTGZ() != 0)
+ exit(-1);
+ else
+ exit(0);
+ }
+ else {
+ if ((pid = wait(&status)) == -1) {
+ LOGI("Tar creation failed\n");
+ return -1;
+ }
+ else {
+ if (WIFSIGNALED(status) != 0) {
+ LOGI("Child process ended with signal: %d\n", WTERMSIG(status));
+ return -1;
+ }
+ else if (WIFEXITED(status) != 0)
+ LOGI("Tar creation successful\n");
+ else {
+ LOGI("Tar creation failed\n");
+ return -1;
+ }
+ }
+ }
return 0;
}
-int twrpTar::createTarThread() {
- pthread_t thread;
- ThreadPtr tarptr = &twrpTar::create;
- PThreadPtr p = *(PThreadPtr*)&tarptr;
- pthread_create(&thread, NULL, p, this);
- if(pthread_join(thread, NULL)) {
+int twrpTar::createTarFork() {
+ int status;
+ pid_t pid;
+ if ((pid = fork()) == -1) {
+ LOGI("create tar failed to fork.\n");
return -1;
}
- TWFunc::drop_caches();
+ if (pid == 0) {
+ if (create() != 0)
+ exit(-1);
+ else
+ exit(0);
+ }
+ else {
+ if ((pid = wait(&status)) == -1) {
+ LOGI("Tar creation failed\n");
+ return -1;
+ }
+ else {
+ if (WIFSIGNALED(status) != 0) {
+ LOGI("Child process ended with signal: %d\n", WTERMSIG(status));
+ return -1;
+ }
+ else if (WIFEXITED(status) != 0)
+ LOGI("Tar creation successful\n");
+ else {
+ LOGI("Tar creation failed\n");
+ return -1;
+ }
+ }
+ }
return 0;
}
-int twrpTar::extractTarThread() {
- pthread_t thread;
- ThreadPtr tarptr = &twrpTar::extract;
- PThreadPtr p = *(PThreadPtr*)&tarptr;
- pthread_create(&thread, NULL, p, this);
- if(pthread_join(thread, NULL)) {
+int twrpTar::extractTarFork() {
+ int status;
+ pid_t pid;
+ if ((pid = fork()) == -1) {
+ LOGI("create tar failed to fork.\n");
return -1;
}
- TWFunc::drop_caches();
+ if (pid == 0) {
+ if (extract() != 0)
+ exit(-1);
+ else
+ exit(0);
+ }
+ else {
+ if ((pid = wait(&status)) == -1) {
+ LOGI("Tar creation failed\n");
+ return -1;
+ }
+ else {
+ if (WIFSIGNALED(status) != 0) {
+ LOGI("Child process ended with signal: %d\n", WTERMSIG(status));
+ return -1;
+ }
+ else if (WIFEXITED(status) != 0)
+ LOGI("Tar creation successful\n");
+ else {
+ LOGI("Tar creation failed\n");
+ return -1;
+ }
+ }
+ }
return 0;
}
-int twrpTar::splitArchiveThread() {
- pthread_t thread;
- ThreadPtr tarptr = &twrpTar::Split_Archive;
- PThreadPtr p = *(PThreadPtr*)&tarptr;
- pthread_create(&thread, NULL, p, this);
- if(pthread_join(thread, NULL)) {
+int twrpTar::splitArchiveFork() {
+ int status;
+ pid_t pid;
+ if ((pid = fork()) == -1) {
+ LOGI("create tar failed to fork.\n");
return -1;
}
- TWFunc::drop_caches();
+ if (pid == 0) {
+ if (Split_Archive() != 0)
+ exit(-1);
+ else
+ exit(0);
+ }
+ else {
+ if ((pid = wait(&status)) == -1) {
+ LOGI("Tar creation failed\n");
+ return -1;
+ }
+ else {
+ if (WIFSIGNALED(status) != 0) {
+ LOGI("Child process ended with signal: %d\n", WTERMSIG(status));
+ return -1;
+ }
+ else if (WIFEXITED(status) != 0)
+ LOGI("Tar creation successful\n");
+ else {
+ LOGI("Tar creation failed\n");
+ return -1;
+ }
+ }
+ }
return 0;
}
diff --git a/twrpTar.hpp b/twrpTar.hpp
index db9cf9bb8..427e6d1c3 100644
--- a/twrpTar.hpp
+++ b/twrpTar.hpp
@@ -39,10 +39,10 @@ class twrpTar {
int createTar();
int addFile(string fn, bool include_root);
int closeTar(bool gzip);
- int createTarGZThread();
- int createTarThread();
- int extractTarThread();
- int splitArchiveThread();
+ int createTarGZFork();
+ int createTarFork();
+ int extractTarFork();
+ int splitArchiveFork();
void setfn(string fn);
void setdir(string dir);
private:
@@ -65,6 +65,4 @@ class twrpTar {
string tardir;
string tarfn;
string basefn;
- typedef int (twrpTar::*ThreadPtr)(void);
- typedef void* (*PThreadPtr)(void*);
};