bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2013 to 2017 TeamWin |
| 3 | TWRP is free software: you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
| 5 | the Free Software Foundation, either version 3 of the License, or |
| 6 | (at your option) any later version. |
| 7 | |
| 8 | TWRP is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | #include <string.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <errno.h> |
| 24 | #include <ctype.h> |
| 25 | #include <string> |
| 26 | #include <sstream> |
bigbiff bigbiff | 5b7cb88 | 2017-11-28 19:26:27 -0500 | [diff] [blame] | 27 | #include <algorithm> |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 28 | |
| 29 | #include "twrpback.hpp" |
| 30 | #include "twadbstream.h" |
| 31 | |
| 32 | |
| 33 | int main(int argc, char **argv) { |
| 34 | int index; |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 35 | size_t pos = 0; |
bigbiff bigbiff | adcb4d8 | 2017-09-25 10:51:56 -0400 | [diff] [blame] | 36 | bool ret = false; |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 37 | size_t maxpos = strlen(TWRPARG) + 2; |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 38 | std::string command; |
| 39 | twrpback tw; |
| 40 | |
| 41 | tw.adblogwrite("Starting adb backup and restore\n"); |
| 42 | command = argv[1]; |
| 43 | for (index = 2; index < argc; index++) { |
| 44 | command = command + " " + argv[index]; |
| 45 | } |
| 46 | |
| 47 | pos = command.find(TWRP_BACKUP_ARG); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 48 | if (pos == std::string::npos || pos > (maxpos + strlen(TWRP_BACKUP_ARG) + 1)) { |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 49 | pos = command.find(TWRP_RESTORE_ARG); |
| 50 | } |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 51 | if (pos == std::string::npos || pos > maxpos + strlen(TWRP_STREAM_ARG) + 1) { |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 52 | pos = command.find(TWRP_STREAM_ARG); |
| 53 | } |
| 54 | |
| 55 | tw.adblogwrite("command: " + command + "\n"); |
| 56 | command.erase(0, pos); |
| 57 | command.erase(std::remove(command.begin(), command.end(), '\''), command.end()); |
| 58 | |
| 59 | if (command.substr(0, sizeof(TWRP_BACKUP_ARG) - 1) == TWRP_BACKUP_ARG) { |
| 60 | tw.adblogwrite("Starting adb backup\n"); |
| 61 | if (isdigit(*argv[1])) |
| 62 | tw.adbd_fd = atoi(argv[1]); |
| 63 | else |
| 64 | tw.adbd_fd = 1; |
| 65 | ret = tw.backup(command); |
| 66 | } |
| 67 | else if (command.substr(0, sizeof(TWRP_RESTORE_ARG) - 1) == TWRP_RESTORE_ARG) { |
| 68 | tw.adblogwrite("Starting adb restore\n"); |
| 69 | if (isdigit(*argv[1])) |
| 70 | tw.adbd_fd = atoi(argv[1]); |
| 71 | else |
| 72 | tw.adbd_fd = 0; |
| 73 | ret = tw.restore(); |
| 74 | } |
| 75 | else if (command.substr(0, sizeof(TWRP_STREAM_ARG) - 1) == TWRP_STREAM_ARG) { |
| 76 | tw.setStreamFileName(argv[3]); |
| 77 | tw.threadStream(); |
bigbiff bigbiff | adcb4d8 | 2017-09-25 10:51:56 -0400 | [diff] [blame] | 78 | ret = true; |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 79 | } |
bigbiff bigbiff | adcb4d8 | 2017-09-25 10:51:56 -0400 | [diff] [blame] | 80 | if (ret) |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 81 | tw.adblogwrite("Adb backup/restore completed\n"); |
| 82 | else |
| 83 | tw.adblogwrite("Adb backup/restore failed\n"); |
| 84 | |
| 85 | if (unlink(TW_ADB_BU_CONTROL) < 0) { |
| 86 | std::stringstream str; |
| 87 | str << strerror(errno); |
| 88 | tw.adblogwrite("Unable to remove TW_ADB_BU_CONTROL: " + str.str()); |
| 89 | } |
| 90 | unlink(TW_ADB_TWRP_CONTROL); |
bigbiff bigbiff | adcb4d8 | 2017-09-25 10:51:56 -0400 | [diff] [blame] | 91 | if (ret) |
| 92 | return 0; |
| 93 | else |
| 94 | return -1; |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 95 | } |