summaryrefslogtreecommitdiffstats
path: root/twrpTarMain/twrpTarMain.cpp
blob: ff35f47ce6e4307fbb3b0548050155f096226e17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

/*
	Copyright 2014 TeamWin
	This file is part of TWRP/TeamWin Recovery Project.

	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
	(at your option) any later version.

	TWRP is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with TWRP.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "../twrp-functions.hpp"
#include "../twrpTar.hpp"
#include "../twrpDU.hpp"
#include "../progresstracking.hpp"
#include "../gui/gui.hpp"
#include "../gui/twmsg.h"
#include <string.h>

twrpDU du;

void gui_msg(const char* text)
{
	if (text) {
		Message msg = Msg(text);
		gui_msg(msg);
	}
}

void gui_warn(const char* text)
{
	if (text) {
		Message msg = Msg(msg::kWarning, text);
		gui_msg(msg);
	}
}

void gui_err(const char* text)
{
	if (text) {
		Message msg = Msg(msg::kError, text);
		gui_msg(msg);
	}
}

void gui_highlight(const char* text)
{
	if (text) {
		Message msg = Msg(msg::kHighlight, text);
		gui_msg(msg);
	}
}

void gui_msg(Message msg)
{
	std::string output = msg;
	output += "\n";
	fputs(output.c_str(), stdout);
}

void usage() {
	printf("twrpTar <action> [options]\n\n");
	printf("actions: -c create\n");
	printf("         -x extract\n\n");
	printf(" -d    target directory\n");
	printf(" -t    output file\n");
	printf(" -m    skip media subfolder (has data media)\n");
	printf(" -z    compress backup (/sbin/pigz must be present)\n");
#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
	printf(" -e    encrypt/decrypt backup followed by password (/sbin/openaes must be present)\n");
	printf(" -u    encrypt using userdata encryption (must be used with -e)\n");
#endif
	printf("\n\n");
	printf("Example: twrpTar -c -d /cache -t /sdcard/test.tar\n");
	printf("         twrpTar -x -d /cache -t /sdcard/test.tar\n");
}

int main(int argc, char **argv) {
	twrpTar tar;
	int use_encryption = 0, userdata_encryption = 0, has_data_media = 0, use_compression = 0, include_root = 0;
	int i, action = 0;
	unsigned j;
	string Directory, Tar_Filename;
	ProgressTracking progress(1);
	pid_t tar_fork_pid = 0;
#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
	string Password;
#endif

	if (argc < 2) {
		usage();
		return 0;
	}

	if (strcmp(argv[1], "-c") == 0)
		action = 1; // create tar
	else if (strcmp(argv[1], "-x") == 0)
		action = 2; // extract tar
	else {
		printf("Invalid action '%s' specified.\n", argv[1]);
		usage();
		return -1;
	}

	for (i = 2; i < argc; i++) {
		if (strcmp(argv[i], "-d") == 0) {
			i++;
			if (argc <= i) {
				printf("No argument specified for %s\n", argv[i - 1]);
				usage();
				return -1;
			} else {
				Directory = argv[i];
			}
		} else if (strcmp(argv[i], "-t") == 0) {
			i++;
			if (argc <= i) {
				printf("No argument specified for %s\n", argv[i - 1]);
				usage();
				return -1;
			} else {
				Tar_Filename = argv[i];
			}
		} else if (strcmp(argv[i], "-e") == 0) {
#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
			i++;
			if (argc <= i) {
				printf("No argument specified for %s\n", argv[i - 1]);
				usage();
				return -1;
			} else {
				use_encryption = 1;
				Password = argv[i];
			}
#else
			printf("Encrypted tar file support not present\n");
			usage();
			return -1;
#endif
		} else if (strcmp(argv[i], "-m") == 0) {
			if (action == 2)
				printf("NOTE: %s option not needed when extracting.\n", argv[i]);
			has_data_media = 1;
		} else if (strcmp(argv[i], "-z") == 0) {
			if (action == 2)
				printf("NOTE: %s option not needed when extracting.\n", argv[i]);
			use_compression = 1;
		} else if (strcmp(argv[i], "-u") == 0) {
#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
			if (action == 2)
				printf("NOTE: %s option not needed when extracting.\n", argv[i]);
			userdata_encryption = 1;
#else
			printf("Encrypted tar file support not present\n");
			usage();
			return -1;
#endif
		}
	}

	tar.has_data_media = has_data_media;
	tar.setdir(Directory);
	tar.setfn(Tar_Filename);
	tar.setsize(du.Get_Folder_Size(Directory));
	tar.use_compression = use_compression;
#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
	if (userdata_encryption && !use_encryption) {
		printf("userdata encryption set without encryption option\n");
		usage();
		return -1;
	}
	if (use_encryption) {
		tar.use_encryption = use_encryption;
		tar.userdata_encryption = userdata_encryption;
		tar.setpassword(Password);
	} else {
		use_encryption = false;
	}
#endif
	if (action == 1) {
		if (tar.createTarFork(&progress, tar_fork_pid) != 0) {
			sync();
			return -1;
		}
		sync();
		printf("\n\ntar created successfully.\n");
	} else if (action == 2) {
		if (tar.extractTarFork(&progress) != 0) {
			sync();
			return -1;
		}
		sync();
		printf("\n\ntar extracted successfully.\n");
	}
	return 0;
}