ADB Backup: add ability for TWRP GUI to restore

Restore adb backup files that TWRP made to your PC.
Put files in your backup directory to see them.

e.g. /sdcard/TWRP/BACKUPS/<sn>

Change-Id: I2c57970d77b64c39a302159041456e761c185259
diff --git a/adbbu/Android.mk b/adbbu/Android.mk
index 33d3ad7..efb5f9a 100644
--- a/adbbu/Android.mk
+++ b/adbbu/Android.mk
@@ -1,10 +1,35 @@
 LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libtwadbbu
+LOCAL_MODULE_TAGS := optional
+LOCAL_CFLAGS = -fno-strict-aliasing -D_LARGFILE_SOURCE
+LOCAL_C_INCLUDES += bionic external/zlib
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
+    LOCAL_C_INCLUDES += external/stlport/stlport
+endif
+
+LOCAL_SRC_FILES = \
+        libtwadbbu.cpp \
+        twrpback.cpp
+
+LOCAL_SHARED_LIBRARIES += libz libc libstdc++ libtwrpdigest
+
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
+    LOCAL_SHARED_LIBRARIES += libstlport
+else
+    LOCAL_SHARED_LIBRARIES += libc++
+endif
+
+include $(BUILD_SHARED_LIBRARY)
+
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES:= \
-	twrpback.cpp
+        adbbumain.cpp
 
-LOCAL_SHARED_LIBRARIES += libstdc++ libz libtwrpdigest
+LOCAL_SHARED_LIBRARIES += libstdc++ libz libtwadbbu
+
 ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
     LOCAL_C_INCLUDES += external/stlport/stlport
     LOCAL_SHARED_LIBRARIES += libstlport
@@ -21,21 +46,3 @@
 LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
 include $(BUILD_EXECUTABLE)
 
-include $(CLEAR_VARS)
-LOCAL_MODULE := libtwadbbu
-LOCAL_MODULE_TAGS := optional
-LOCAL_CFLAGS = -D_FILE_OFFSET_BITS=64 -fno-strict-aliasing
-LOCAL_C_INCLUDES += bionic external/zlib
-ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
-    LOCAL_C_INCLUDES += external/stlport/stlport
-    LOCAL_SHARED_LIBRARIES += libstlport
-else
-    LOCAL_SHARED_LIBRARIES += libc++
-endif
-
-LOCAL_SRC_FILES = \
-    libtwadbbu.cpp
-
-LOCAL_SHARED_LIBRARIES += libz libc libstdc++
-
-include $(BUILD_SHARED_LIBRARY)
diff --git a/adbbu/adbbumain.cpp b/adbbu/adbbumain.cpp
new file mode 100644
index 0000000..050c9bc
--- /dev/null
+++ b/adbbu/adbbumain.cpp
@@ -0,0 +1,89 @@
+/*
+		Copyright 2013 to 2017 TeamWin
+		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/>.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <ctype.h>
+#include <string>
+#include <sstream>
+
+#include "twrpback.hpp"
+#include "twadbstream.h"
+
+
+int main(int argc, char **argv) {
+	int index;
+	int ret = 0, pos = 0;
+	int maxpos = sizeof(TWRPARG + 2);
+	std::string command;
+	twrpback tw;
+
+	tw.adblogwrite("Starting adb backup and restore\n");
+	command = argv[1];
+	for (index = 2; index < argc; index++) {
+		command = command + " " + argv[index];
+	}
+
+	pos = command.find(TWRP_BACKUP_ARG);
+	if (pos < 0 || pos > (maxpos + sizeof(TWRP_BACKUP_ARG) + 1)) {
+		pos = command.find(TWRP_RESTORE_ARG);
+	}
+	if (pos < 0 || pos > maxpos + sizeof(TWRP_STREAM_ARG + 1)) {
+		pos = command.find(TWRP_STREAM_ARG);
+	}
+
+	tw.adblogwrite("command: " + command + "\n");
+	command.erase(0, pos);
+	command.erase(std::remove(command.begin(), command.end(), '\''), command.end());
+
+	if (command.substr(0, sizeof(TWRP_BACKUP_ARG) - 1) == TWRP_BACKUP_ARG) {
+		tw.adblogwrite("Starting adb backup\n");
+		if (isdigit(*argv[1]))
+			tw.adbd_fd = atoi(argv[1]);
+		else
+			tw.adbd_fd = 1;
+		ret = tw.backup(command);
+	}
+	else if (command.substr(0, sizeof(TWRP_RESTORE_ARG) - 1) == TWRP_RESTORE_ARG) {
+		tw.adblogwrite("Starting adb restore\n");
+		if (isdigit(*argv[1]))
+			tw.adbd_fd = atoi(argv[1]);
+		else
+			tw.adbd_fd = 0;
+		ret = tw.restore();
+	}
+	else if (command.substr(0, sizeof(TWRP_STREAM_ARG) - 1) == TWRP_STREAM_ARG) {
+		tw.setStreamFileName(argv[3]);
+		tw.threadStream();
+	}
+	if (ret == 0)
+		tw.adblogwrite("Adb backup/restore completed\n");
+	else
+		tw.adblogwrite("Adb backup/restore failed\n");
+
+	if (unlink(TW_ADB_BU_CONTROL) < 0) {
+		std::stringstream str;
+		str << strerror(errno);
+		tw.adblogwrite("Unable to remove TW_ADB_BU_CONTROL: " + str.str());
+	}
+	unlink(TW_ADB_TWRP_CONTROL);
+	return ret;
+}
diff --git a/adbbu/libtwadbbu.cpp b/adbbu/libtwadbbu.cpp
index cdc2170..a13ecb2 100644
--- a/adbbu/libtwadbbu.cpp
+++ b/adbbu/libtwadbbu.cpp
@@ -1,5 +1,5 @@
 /*
-		Copyright 2013 to 2016 TeamWin
+		Copyright 2013 to 2017 TeamWin
 		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
@@ -27,12 +27,107 @@
 #include <sys/select.h>
 #include <sys/time.h>
 #include <string>
+#include <vector>
 #include <fstream>
 #include <sstream>
 
 #include "twadbstream.h"
 #include "libtwadbbu.hpp"
 
+bool twadbbu::Check_ADB_Backup_File(std::string fname) {
+	struct AdbBackupStreamHeader adbbuhdr;
+	uint32_t crc, adbbuhdrcrc;
+	unsigned char buf[MAX_ADB_READ];
+	int bytes;
+
+	int fd = open(fname.c_str(), O_RDONLY);
+	if (fd < 0) {
+		printf("Unable to open %s for reading: %s.\n", fname.c_str(), strerror(errno));
+		close(fd);
+		return false;
+	}
+	bytes = read(fd, &buf, sizeof(buf));
+	close(fd);
+
+	if (memcpy(&adbbuhdr, buf, sizeof(adbbuhdr)) < 0) {
+		printf("Unable to memcpy: %s.\n", fname.c_str(), strerror(errno));
+		return false;
+	}
+	adbbuhdrcrc = adbbuhdr.crc;
+	memset(&adbbuhdr.crc, 0, sizeof(adbbuhdr.crc));
+	crc = crc32(0L, Z_NULL, 0);
+	crc = crc32(crc, (const unsigned char*) &adbbuhdr, sizeof(adbbuhdr));
+
+	return (crc == adbbuhdrcrc);
+}
+
+std::vector<std::string> twadbbu::Get_ADB_Backup_Files(std::string fname) {
+	unsigned char buf[MAX_ADB_READ];
+	struct AdbBackupControlType structcmd;
+	std::vector<std::string> adb_partitions;
+
+	int fd = open(fname.c_str(), O_RDONLY);
+	if (fd < 0) {
+		printf("Unable to open %s for reading: %s\n", fname.c_str(), strerror(errno));
+		close(fd);
+		return std::vector<std::string>();
+	}
+
+	while (1) {
+		std::string cmdstr;
+		int readbytes;
+		if (readbytes = read(fd, &buf, sizeof(buf)) > 0) {
+			memcpy(&structcmd, buf, sizeof(structcmd));
+			assert(structcmd.type == TWENDADB || structcmd.type == TWIMG || structcmd.type == TWFN);
+			cmdstr = structcmd.type;
+			std::string cmdtype = cmdstr.substr(0, sizeof(structcmd.type) - 1);
+			if (cmdtype == TWENDADB) {
+				struct AdbBackupControlType endadb;
+				uint32_t crc, endadbcrc;
+
+				memcpy(&endadb, buf, sizeof(endadb));
+				endadbcrc = endadb.crc;
+				memset(&endadb.crc, 0, sizeof(endadb.crc));
+				crc = crc32(0L, Z_NULL, 0);
+				crc = crc32(crc, (const unsigned char*) &endadb, sizeof(endadb));
+
+				if (crc == endadbcrc) {
+					break;
+				}
+				else {
+					printf("ADB TWENDADB crc header doesn't match\n");
+					close(fd);
+					return std::vector<std::string>();
+				}
+			}
+			else if (cmdtype == TWIMG || cmdtype == TWFN) {
+				struct twfilehdr twfilehdr;
+				uint32_t crc, twfilehdrcrc;
+
+				memcpy(&twfilehdr, buf, sizeof(twfilehdr));
+				twfilehdrcrc = twfilehdr.crc;
+				memset(&twfilehdr.crc, 0, sizeof(twfilehdr.crc));
+
+				crc = crc32(0L, Z_NULL, 0);
+				crc = crc32(crc, (const unsigned char*) &twfilehdr, sizeof(twfilehdr));
+				if (crc == twfilehdrcrc) {
+					std::string adbfile = twfilehdr.name;
+					int pos = adbfile.find_last_of("/") + 1;
+					adbfile = adbfile.substr(pos, adbfile.size());
+					adb_partitions.push_back(adbfile);
+				}
+				else {
+					printf("ADB crc header doesn't match\n");
+					close(fd);
+					return std::vector<std::string>();
+				}
+			}
+		}
+	}
+	close(fd);
+	return adb_partitions;
+}
+
 bool twadbbu::Write_ADB_Stream_Header(uint64_t partition_count) {
 	struct AdbBackupStreamHeader twhdr;
 	int adb_control_bu_fd;
@@ -40,7 +135,7 @@
 	memset(&twhdr, 0, sizeof(twhdr));
 	adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK);
 	if (adb_control_bu_fd < 0) {
-		printf("Cannot write to TW_ADB_BU_CONTROL.\n");
+		printf("Cannot write to TW_ADB_BU_CONTROL: %s\n", strerror(errno));
 		return false;
 	}
 
@@ -52,7 +147,7 @@
 	twhdr.crc = crc32(0L, Z_NULL, 0);
 	twhdr.crc = crc32(twhdr.crc, (const unsigned char*) &twhdr, sizeof(twhdr));
 	if (write(adb_control_bu_fd, &twhdr, sizeof(twhdr)) < 0) {
-		printf("Cannot write to adb control channel\n");
+		printf("Cannot write to adb control channel: %s\n", strerror(errno));
 		close(adb_control_bu_fd);
 		return false;
 	}
@@ -67,7 +162,7 @@
 
 	adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY);
 	if (adb_control_bu_fd < 0) {
-		printf("Error opening adb_control_bu_fd\n");
+		printf("Error opening adb_control_bu_fd: %s\n", strerror(errno));
 		return false;
 	}
 	strncpy(endadb.start_of_header, TWRP, sizeof(endadb.start_of_header));
@@ -75,7 +170,7 @@
 	endadb.crc = crc32(0L, Z_NULL, 0);
 	endadb.crc = crc32(endadb.crc, (const unsigned char*) &endadb, sizeof(endadb));
 	if (write(adb_control_bu_fd, &endadb, sizeof(endadb)) < 0) {
-		printf("Cannot write to ADB control.\n");
+		printf("Cannot write to ADB control: %s\n", strerror(errno));
 		close(adb_control_bu_fd);
 		return false;
 	}
@@ -97,10 +192,11 @@
 
 	printf("Sending TWFN to adb\n");
 	if (write(adb_control_bu_fd, &twfilehdr, sizeof(twfilehdr)) < 1) {
-		printf("Cannot that write to adb_control_bu_fd\n");
+		printf("Cannot that write to adb_control_bu_fd: %s\n", strerror(errno));
 		close(adb_control_bu_fd);
 		return false;
 	}
+	fsync(adb_control_bu_fd);
 	close(adb_control_bu_fd);
 	return true;
 }
@@ -118,7 +214,7 @@
 	twimghdr.crc = crc32(twimghdr.crc, (const unsigned char*) &twimghdr, sizeof(twimghdr));
 	printf("Sending TWIMG to adb\n");
 	if (write(adb_control_bu_fd, &twimghdr, sizeof(twimghdr)) < 1) {
-		printf("Cannot write to adb control channel\n");
+		printf("Cannot write to adb control channel: %s\n", strerror(errno));
 		return false;
 	}
 
@@ -168,7 +264,7 @@
 	twerror.crc = crc32(0L, Z_NULL, 0);
 	twerror.crc = crc32(twerror.crc, (const unsigned char*) &twerror, sizeof(twerror));
 	if (write(adb_control_bu_fd, &twerror, sizeof(twerror)) < 0) {
-		printf("Cannot write to adb control channel");
+		printf("Cannot write to adb control channel: %s\n", strerror(errno));
 		return false;
 	}
 	close(adb_control_bu_fd);
diff --git a/adbbu/libtwadbbu.hpp b/adbbu/libtwadbbu.hpp
index bcd8b6a..ff9a3eb 100644
--- a/adbbu/libtwadbbu.hpp
+++ b/adbbu/libtwadbbu.hpp
@@ -1,5 +1,5 @@
 /*
-		Copyright 2013 to 2016 TeamWin
+		Copyright 2013 to 2017 TeamWin
 		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
@@ -13,6 +13,8 @@
 		You should have received a copy of the GNU General Public License
 		along with TWRP.  If not, see <http://www.gnu.org/licenses/>.
 */
+#ifndef _LIBTWADBBU_HPP
+#define _LIBTWADBBU_HPP
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -26,6 +28,7 @@
 #include <sys/select.h>
 #include <sys/time.h>
 #include <string>
+#include <vector>
 #include <fstream>
 #include <sstream>
 
@@ -34,6 +37,8 @@
 
 class twadbbu {
 public:
+	static bool Check_ADB_Backup_File(std::string fname);                                          //Check if file is ADB Backup file
+	static std::vector<std::string> Get_ADB_Backup_Files(std::string fname);                       //List ADB Files in String Vector
 	static bool Write_ADB_Stream_Header(uint64_t partition_count);                                 //Write ADB Stream Header to stream
 	static bool Write_ADB_Stream_Trailer();                                                        //Write ADB Stream Trailer to stream
 	static bool Write_TWFN(std::string Backup_FileName, uint64_t file_size, bool use_compression); //Write a tar image to stream
@@ -42,3 +47,5 @@
 	static bool Write_TWERROR();                                                                   //Write error message occurred to stream
 	static bool Write_TWENDADB();                                                                  //Write ADB End-Of-Stream command to stream
 };
+
+#endif //__LIBTWADBBU_HPP
diff --git a/adbbu/twadbstream.h b/adbbu/twadbstream.h
index 5fa6bde..3c73084 100644
--- a/adbbu/twadbstream.h
+++ b/adbbu/twadbstream.h
@@ -1,27 +1,33 @@
 /*
-		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 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.
+	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/>.
+	You should have received a copy of the GNU General Public License
+	along with TWRP.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifndef __TWADBSTREAM_H
 #define __TWADBSTREAM_H
 
+#define TWRPARG "--twrp"
+#define TWRP_BACKUP_ARG "backup"
+#define TWRP_RESTORE_ARG "restore"
+#define TWRP_STREAM_ARG "stream"
 #define TW_ADB_BACKUP "/tmp/twadbbackup"		//FIFO for adb backup
 #define TW_ADB_RESTORE "/tmp/twadbrestore"		//FIFO for adb restore
 #define TW_ADB_BU_CONTROL "/tmp/twadbbucontrol"		//FIFO for sending control from TWRP to ADB Backup
 #define TW_ADB_TWRP_CONTROL "/tmp/twadbtwrpcontrol"	//FIFO for sending control from ADB Backup to TWRP
 #define TWRP "TWRP"					//Magic Value
 #define ADB_BU_MAX_ERROR 20				//Max amount of errors for while loops
+#define ADB_BACKUP_OP "adbbackup"
+#define ADB_RESTORE_OP "adbrestore"
 
 //ADB Backup Control Commands
 #define TWSTREAMHDR "twstreamheader"			//TWRP Parititon Count Control
diff --git a/adbbu/twrpback.cpp b/adbbu/twrpback.cpp
index 8652de2..2c7ea69 100644
--- a/adbbu/twrpback.cpp
+++ b/adbbu/twrpback.cpp
@@ -1,5 +1,5 @@
 /*
-		Copyright 2013 to 2016 TeamWin
+		Copyright 2013 to 2017 TeamWin
 		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
@@ -28,14 +28,17 @@
 #include <ctype.h>
 #include <semaphore.h>
 #include <string>
-#include <fstream>
 #include <sstream>
+#include <fstream>
 #include <algorithm>
+#include <utils/threads.h>
+#include <pthread.h>
 
 #include "twadbstream.h"
 #include "twrpback.hpp"
 #include "../twrpDigest/twrpDigest.hpp"
 #include "../twrpDigest/twrpMD5.hpp"
+#include "../twrpAdbBuFifo.hpp"
 
 twrpback::twrpback(void) {
 	read_fd = 0;
@@ -47,11 +50,40 @@
 	adb_write_fd = 0;
 	ors_fd = 0;
 	firstPart = true;
+	createFifos();
 	adbloginit();
 }
 
 twrpback::~twrpback(void) {
 	adblogfile.close();
+	closeFifos();
+}
+
+void twrpback::createFifos(void) {
+        if (mkfifo(TW_ADB_BU_CONTROL, 0666) < 0) {
+                std::stringstream str;
+                str << strerror(errno);
+                adblogwrite("Unable to create TW_ADB_BU_CONTROL fifo: " + str.str() + "\n");
+        }
+        if (mkfifo(TW_ADB_TWRP_CONTROL, 0666) < 0) {
+                std::stringstream str;
+                str << strerror(errno);
+                adblogwrite("Unable to create TW_ADB_TWRP_CONTROL fifo: " + str.str() + "\n");
+                unlink(TW_ADB_BU_CONTROL);
+        }
+}
+
+void twrpback::closeFifos(void) {
+        if (unlink(TW_ADB_BU_CONTROL) < 0) {
+                std::stringstream str;
+                str << strerror(errno);
+                adblogwrite("Unable to remove TW_ADB_BU_CONTROL: " + str.str());
+        }
+        if (unlink(TW_ADB_TWRP_CONTROL) < 0) {
+                std::stringstream str;
+                str << strerror(errno);
+                adblogwrite("Unable to remove TW_ADB_TWRP_CONTROL: " + str.str());
+	}
 }
 
 void twrpback::adbloginit(void) {
@@ -119,14 +151,14 @@
 		return -1;
 	}
 
-	adblogwrite("opening ORS_INPUT_FILE\n");
-	write_fd = open(ORS_INPUT_FILE, O_WRONLY);
+	adblogwrite("opening TW_ADB_FIFO\n");
+	write_fd = open(TW_ADB_FIFO, O_WRONLY);
 	while (write_fd < 0) {
-		write_fd = open(ORS_INPUT_FILE, O_WRONLY);
+		write_fd = open(TW_ADB_FIFO, O_WRONLY);
 		usleep(10000);
 		errctr++;
 		if (errctr > ADB_BU_MAX_ERROR) {
-			adblogwrite("Unable to open ORS_INPUT_FILE\n");
+			adblogwrite("Unable to open TW_ADB_FIFO\n");
 			close_backup_fds();
 			return -1;
 		}
@@ -144,14 +176,6 @@
 		return -1;
 	}
 
-	adblogwrite("opening ORS_OUTPUT_FILE\n");
-	ors_fd = open(ORS_OUTPUT_FILE, O_RDONLY);
-	if (ors_fd < 0) {
-		adblogwrite("Unable to open ORS_OUTPUT_FILE\n");
-		close_backup_fds();
-		return -1;
-	}
-
 	memset(&result, 0, sizeof(result));
 	memset(&cmd, 0, sizeof(cmd));
 
@@ -263,7 +287,7 @@
 				count = totalbytes / MAX_ADB_READ + 1;
 				count = count * MAX_ADB_READ;
 
-				while ((bytes = read(adb_read_fd, &result, sizeof(result))) == MAX_ADB_READ) {
+				while ((bytes = read(adb_read_fd, &result, sizeof(result))) > 0) {
 					totalbytes += bytes;
 					char *writeresult = new char [bytes];
 					memcpy(writeresult, result, bytes);
@@ -421,14 +445,14 @@
 		return -1;
 	}
 
-	adblogwrite("opening ORS_INPUT_FILE\n");
-	write_fd = open(ORS_INPUT_FILE, O_WRONLY);
+	adblogwrite("opening TW_ADB_FIFO\n");
+	write_fd = open(TW_ADB_FIFO, O_WRONLY);
 
 	while (write_fd < 0) {
-		write_fd = open(ORS_INPUT_FILE, O_WRONLY);
+		write_fd = open(TW_ADB_FIFO, O_WRONLY);
 		errctr++;
 		if (errctr > ADB_BU_MAX_ERROR) {
-			adblogwrite("Unable to open ORS_INPUT_FILE\n");
+			adblogwrite("Unable to open TW_ADB_FIFO\n");
 			close_restore_fds();
 			return -1;
 		}
@@ -437,16 +461,7 @@
 	memset(operation, 0, sizeof(operation));
 	sprintf(operation, "adbrestore");
 	if (write(write_fd, operation, sizeof(operation)) != sizeof(operation)) {
-		adblogwrite("Unable to write to ORS_INPUT_FILE\n");
-		close_restore_fds();
-		return -1;
-	}
-
-	ors_fd = open(ORS_OUTPUT_FILE, O_RDONLY);
-	if (ors_fd < 0) {
-		std::stringstream str;
-		str << strerror(errno);
-		adblogwrite("Unable to write to ORS_OUTPUT_FILE: " + str.str() + "\n");
+		adblogwrite("Unable to write to TW_ADB_FIFO\n");
 		close_restore_fds();
 		return -1;
 	}
@@ -748,74 +763,25 @@
 	return 0;
 }
 
-int main(int argc, char **argv) {
-	int index;
-	int ret = 0, pos = 0;
-	std::string command;
-	twrpback tw;
+void twrpback::streamFileForTWRP(void) {
+	adblogwrite("streamFileForTwrp" + streamFn + "\n");
+}
 
-	tw.adblogwrite("Starting adb backup and restore\n");
-	if (mkfifo(TW_ADB_BU_CONTROL, 0666) < 0) {
-		std::stringstream str;
-		str << strerror(errno);
-		tw.adblogwrite("Unable to create TW_ADB_BU_CONTROL fifo: " + str.str() + "\n");
-		unlink(TW_ADB_BU_CONTROL);
-		return -1;
+void twrpback::setStreamFileName(std::string fn) {
+	streamFn = fn;
+	adbd_fd = open(fn.c_str(), O_RDONLY);
+	if (adbd_fd < 0) {
+		adblogwrite("Unable to open adb_fd\n");
+		close(adbd_fd);
+		return;
 	}
-	if (mkfifo(TW_ADB_TWRP_CONTROL, 0666) < 0) {
-		std::stringstream str;
-		str << strerror(errno);
-		tw.adblogwrite("Unable to create TW_ADB_TWRP_CONTROL fifo: " + str.str() + "\n");
-		unlink(TW_ADB_TWRP_CONTROL);
-		unlink(TW_ADB_BU_CONTROL);
-		return -1;
-	}
+	restore();
+}
 
-	if (argc <= 1) {
-		tw.adblogwrite("No parameters given, exiting...\n");
-		tw.close_restore_fds();
-		return -1;
-	}
-
-	command = argv[1];
-	for (index = 2; index < argc; index++) {
-		command = command + " " + argv[index];
-	}
-
-	pos = command.find("backup");
-	if (pos < 0) {
-		pos = command.find("restore");
-	}
-	command.erase(0, pos);
-	command.erase(std::remove(command.begin(), command.end(), '\''), command.end());
-	tw.adblogwrite("command: " + command + "\n");
-
-	if (command.substr(0, sizeof("backup") - 1) == "backup") {
-		tw.adblogwrite("Starting adb backup\n");
-		if (isdigit(*argv[1]))
-			tw.adbd_fd = atoi(argv[1]);
-		else
-			tw.adbd_fd = 1;
-		ret = tw.backup(command);
-	}
-	else if (command.substr(0, sizeof("restore") - 1) == "restore") {
-		tw.adblogwrite("Starting adb restore\n");
-		if (isdigit(*argv[1]))
-			tw.adbd_fd = atoi(argv[1]);
-		else
-			tw.adbd_fd = 0;
-		ret = tw.restore();
-	}
-	if (ret == 0)
-		tw.adblogwrite("Adb backup/restore completed\n");
-	else
-		tw.adblogwrite("Adb backup/restore failed\n");
-
-	if (unlink(TW_ADB_BU_CONTROL) < 0) {
-		std::stringstream str;
-		str << strerror(errno);
-		tw.adblogwrite("Unable to remove TW_ADB_BU_CONTROL: " + str.str());
-	}
-	unlink(TW_ADB_TWRP_CONTROL);
-	return ret;
+void twrpback::threadStream(void) {
+	pthread_t thread;
+	ThreadPtr streamPtr = &twrpback::streamFileForTWRP;
+	PThreadPtr p = *(PThreadPtr*)&streamPtr;
+	pthread_create(&thread, NULL, p, this);
+	pthread_join(thread, NULL);
 }
diff --git a/adbbu/twrpback.hpp b/adbbu/twrpback.hpp
index 752d35e..1c6b09f 100644
--- a/adbbu/twrpback.hpp
+++ b/adbbu/twrpback.hpp
@@ -1,5 +1,5 @@
 /*
-		Copyright 2013 to 2016 TeamWin
+		Copyright 2013 to 2017 TeamWin
 		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
@@ -14,23 +14,24 @@
 		along with TWRP.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include <fstream>
+#ifndef _TWRPBACK_HPP
+#define _TWRPBACK_HPP
 
-#include "../orscmd/orscmd.h"
-#include "../variables.h"
-#include "../twcommon.h"
+#include <fstream>
 
 class twrpback {
 public:
 	int adbd_fd;                                                             // adbd data stream
-
 	twrpback(void);
 	virtual ~twrpback(void);
 	int backup(std::string command);                                         // adb backup stream
 	int restore(void);                                                       // adb restore stream
 	void adblogwrite(std::string writemsg);                                  // adb debugging log function
-	void close_backup_fds();                                                 // close backup resources
-	void close_restore_fds();                                                // close restore resources
+	void createFifos(void);                                                  // create fifos needed for adb backup
+	void closeFifos(void);                                                   // close created fifos
+	void streamFileForTWRP(void);                                            // stream file to twrp via bu
+	void setStreamFileName(std::string fn);                                  // tell adb backup what file to load on storage
+	void threadStream(void);                                                 // thread bu for streaming
 
 private:
 	int read_fd;                                                             // ors input fd
@@ -45,5 +46,12 @@
 	char cmd[512];                                                           // store result of commands
 	char operation[512];                                                     // operation to send to ors
 	std::ofstream adblogfile;                                                // adb stream log file
+	std::string streamFn;
+	typedef void (twrpback::*ThreadPtr)(void);
+	typedef void* (*PThreadPtr)(void *);
 	void adbloginit(void);                                                   // setup adb log stream file
+	void close_backup_fds();                                                 // close backup resources
+	void close_restore_fds();                                                // close restore resources
 };
+
+#endif // _TWRPBACK_HPP