blob: 4f5efec4c880e609669a5565b535ee2e3ef1242e [file] [log] [blame]
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001/*
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>
27
28#include "twrpback.hpp"
29#include "twadbstream.h"
30
31
32int main(int argc, char **argv) {
33 int index;
bigbiff bigbiffadcb4d82017-09-25 10:51:56 -040034 int pos = 0;
35 bool ret = false;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040036 int maxpos = sizeof(TWRPARG + 2);
37 std::string command;
38 twrpback tw;
39
40 tw.adblogwrite("Starting adb backup and restore\n");
41 command = argv[1];
42 for (index = 2; index < argc; index++) {
43 command = command + " " + argv[index];
44 }
45
46 pos = command.find(TWRP_BACKUP_ARG);
47 if (pos < 0 || pos > (maxpos + sizeof(TWRP_BACKUP_ARG) + 1)) {
48 pos = command.find(TWRP_RESTORE_ARG);
49 }
50 if (pos < 0 || pos > maxpos + sizeof(TWRP_STREAM_ARG + 1)) {
51 pos = command.find(TWRP_STREAM_ARG);
52 }
53
54 tw.adblogwrite("command: " + command + "\n");
55 command.erase(0, pos);
56 command.erase(std::remove(command.begin(), command.end(), '\''), command.end());
57
58 if (command.substr(0, sizeof(TWRP_BACKUP_ARG) - 1) == TWRP_BACKUP_ARG) {
59 tw.adblogwrite("Starting adb backup\n");
60 if (isdigit(*argv[1]))
61 tw.adbd_fd = atoi(argv[1]);
62 else
63 tw.adbd_fd = 1;
64 ret = tw.backup(command);
65 }
66 else if (command.substr(0, sizeof(TWRP_RESTORE_ARG) - 1) == TWRP_RESTORE_ARG) {
67 tw.adblogwrite("Starting adb restore\n");
68 if (isdigit(*argv[1]))
69 tw.adbd_fd = atoi(argv[1]);
70 else
71 tw.adbd_fd = 0;
72 ret = tw.restore();
73 }
74 else if (command.substr(0, sizeof(TWRP_STREAM_ARG) - 1) == TWRP_STREAM_ARG) {
75 tw.setStreamFileName(argv[3]);
76 tw.threadStream();
bigbiff bigbiffadcb4d82017-09-25 10:51:56 -040077 ret = true;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040078 }
bigbiff bigbiffadcb4d82017-09-25 10:51:56 -040079 if (ret)
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040080 tw.adblogwrite("Adb backup/restore completed\n");
81 else
82 tw.adblogwrite("Adb backup/restore failed\n");
83
84 if (unlink(TW_ADB_BU_CONTROL) < 0) {
85 std::stringstream str;
86 str << strerror(errno);
87 tw.adblogwrite("Unable to remove TW_ADB_BU_CONTROL: " + str.str());
88 }
89 unlink(TW_ADB_TWRP_CONTROL);
bigbiff bigbiffadcb4d82017-09-25 10:51:56 -040090 if (ret)
91 return 0;
92 else
93 return -1;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040094}