blob: 050c9bc4a1b24e428a42c7d7610a5d588dd47ae2 [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;
34 int ret = 0, pos = 0;
35 int maxpos = sizeof(TWRPARG + 2);
36 std::string command;
37 twrpback tw;
38
39 tw.adblogwrite("Starting adb backup and restore\n");
40 command = argv[1];
41 for (index = 2; index < argc; index++) {
42 command = command + " " + argv[index];
43 }
44
45 pos = command.find(TWRP_BACKUP_ARG);
46 if (pos < 0 || pos > (maxpos + sizeof(TWRP_BACKUP_ARG) + 1)) {
47 pos = command.find(TWRP_RESTORE_ARG);
48 }
49 if (pos < 0 || pos > maxpos + sizeof(TWRP_STREAM_ARG + 1)) {
50 pos = command.find(TWRP_STREAM_ARG);
51 }
52
53 tw.adblogwrite("command: " + command + "\n");
54 command.erase(0, pos);
55 command.erase(std::remove(command.begin(), command.end(), '\''), command.end());
56
57 if (command.substr(0, sizeof(TWRP_BACKUP_ARG) - 1) == TWRP_BACKUP_ARG) {
58 tw.adblogwrite("Starting adb backup\n");
59 if (isdigit(*argv[1]))
60 tw.adbd_fd = atoi(argv[1]);
61 else
62 tw.adbd_fd = 1;
63 ret = tw.backup(command);
64 }
65 else if (command.substr(0, sizeof(TWRP_RESTORE_ARG) - 1) == TWRP_RESTORE_ARG) {
66 tw.adblogwrite("Starting adb restore\n");
67 if (isdigit(*argv[1]))
68 tw.adbd_fd = atoi(argv[1]);
69 else
70 tw.adbd_fd = 0;
71 ret = tw.restore();
72 }
73 else if (command.substr(0, sizeof(TWRP_STREAM_ARG) - 1) == TWRP_STREAM_ARG) {
74 tw.setStreamFileName(argv[3]);
75 tw.threadStream();
76 }
77 if (ret == 0)
78 tw.adblogwrite("Adb backup/restore completed\n");
79 else
80 tw.adblogwrite("Adb backup/restore failed\n");
81
82 if (unlink(TW_ADB_BU_CONTROL) < 0) {
83 std::stringstream str;
84 str << strerror(errno);
85 tw.adblogwrite("Unable to remove TW_ADB_BU_CONTROL: " + str.str());
86 }
87 unlink(TW_ADB_TWRP_CONTROL);
88 return ret;
89}