From 19fb79c722622ac4a068e258501ab4b161420cda Mon Sep 17 00:00:00 2001 From: bigbiff bigbiff Date: Mon, 5 Sep 2016 21:04:51 -0400 Subject: 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/ Change-Id: I2c57970d77b64c39a302159041456e761c185259 --- adbbu/twrpback.cpp | 158 +++++++++++++++++++++-------------------------------- 1 file changed, 62 insertions(+), 96 deletions(-) (limited to 'adbbu/twrpback.cpp') diff --git a/adbbu/twrpback.cpp b/adbbu/twrpback.cpp index 8652de2ac..2c7ea6966 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 #include #include -#include #include +#include #include +#include +#include #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 @@ twrpback::twrpback(void) { 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 @@ int twrpback::backup(std::string command) { 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 @@ int twrpback::backup(std::string command) { 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 @@ int twrpback::backup(std::string command) { 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 @@ int twrpback::restore(void) { 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 @@ int twrpback::restore(void) { 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 @@ int twrpback::restore(void) { return 0; } -int main(int argc, char **argv) { - int index; - int ret = 0, pos = 0; - std::string command; - twrpback tw; - - 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; - } - 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; - } - - 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]; - } +void twrpback::streamFileForTWRP(void) { + adblogwrite("streamFileForTwrp" + streamFn + "\n"); +} - 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(); +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 (ret == 0) - tw.adblogwrite("Adb backup/restore completed\n"); - else - tw.adblogwrite("Adb backup/restore failed\n"); + restore(); +} - 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); } -- cgit v1.2.3