blob: bd96b2040626cc913a5ce44cd61c7e41e79cd13f [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>
bigbiff bigbiff5b7cb882017-11-28 19:26:27 -050027#include <algorithm>
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040028
29#include "twrpback.hpp"
30#include "twadbstream.h"
31
32
33int main(int argc, char **argv) {
34 int index;
Ethan Yonker58f21322018-08-24 11:17:36 -050035 size_t pos = 0;
bigbiff bigbiffadcb4d82017-09-25 10:51:56 -040036 bool ret = false;
Ethan Yonker58f21322018-08-24 11:17:36 -050037 size_t maxpos = strlen(TWRPARG) + 2;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040038 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 Yonker58f21322018-08-24 11:17:36 -050048 if (pos == std::string::npos || pos > (maxpos + strlen(TWRP_BACKUP_ARG) + 1)) {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040049 pos = command.find(TWRP_RESTORE_ARG);
50 }
Ethan Yonker58f21322018-08-24 11:17:36 -050051 if (pos == std::string::npos || pos > maxpos + strlen(TWRP_STREAM_ARG) + 1) {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040052 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 bigbiffadcb4d82017-09-25 10:51:56 -040078 ret = true;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040079 }
bigbiff bigbiffadcb4d82017-09-25 10:51:56 -040080 if (ret)
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040081 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 bigbiffadcb4d82017-09-25 10:51:56 -040091 if (ret)
92 return 0;
93 else
94 return -1;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040095}