blob: 6cc629d7d0258944a068331b776d003810a9039a [file] [log] [blame]
Ethan Yonker50381972014-02-11 11:44:06 -06001
2/*
3 Copyright 2014 TeamWin
4 This file is part of TWRP/TeamWin Recovery Project.
5
6 TWRP is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 TWRP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include "../twrp-functions.hpp"
21#include "../twrpTar.hpp"
22#include "../twrpDU.hpp"
23#include <string.h>
24
25twrpDU du;
26
27void usage() {
28 printf("twrpTar <action> [options]\n\n");
29 printf("actions: -c create\n");
30 printf(" -x extract\n\n");
31 printf(" -d target directory\n");
32 printf(" -t output file\n");
33 printf(" -m skip media subfolder (has data media)\n");
34 printf(" -z compress backup (/sbin/pigz must be present)\n");
35#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
36 printf(" -e encrypt/decrypt backup followed by password (/sbin/openaes must be present)\n");
37 printf(" -u encrypt using userdata encryption (must be used with -e\n");
38#endif
39 printf("\n\n");
40 printf("Example: twrpTar -c -d /cache -t /sdcard/test.tar\n");
41 printf(" twrpTar -x -d /cache -t /sdcard/test.tar\n");
42}
43
44int main(int argc, char **argv) {
45 twrpTar tar;
46 int use_encryption = 0, userdata_encryption = 0, has_data_media = 0, use_compression = 0, include_root = 0;
47 int i, action = 0;
48 unsigned j;
49 string Directory, Tar_Filename;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050050 unsigned long long temp1 = 0, temp2 = 0;
Ethan Yonker068c7682015-09-25 11:25:20 -050051 pid_t tar_fork_pid = 0;
Ethan Yonker50381972014-02-11 11:44:06 -060052#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
53 string Password;
54#endif
55
56 if (argc < 2) {
57 usage();
58 return 0;
59 }
60
61 if (strcmp(argv[1], "-c") == 0)
62 action = 1; // create tar
63 else if (strcmp(argv[1], "-x") == 0)
64 action = 2; // extract tar
65 else {
66 printf("Invalid action '%s' specified.\n", argv[1]);
67 usage();
68 return -1;
69 }
70
71 for (i = 2; i < argc; i++) {
72 if (strcmp(argv[i], "-d") == 0) {
73 i++;
74 if (argc <= i) {
75 printf("No argument specified for %s\n", argv[i - 1]);
76 usage();
77 return -1;
78 } else {
79 Directory = argv[i];
80 }
81 } else if (strcmp(argv[i], "-t") == 0) {
82 i++;
83 if (argc <= i) {
84 printf("No argument specified for %s\n", argv[i - 1]);
85 usage();
86 return -1;
87 } else {
88 Tar_Filename = argv[i];
89 }
90 } else if (strcmp(argv[i], "-e") == 0) {
91#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
92 i++;
93 if (argc <= i) {
94 printf("No argument specified for %s\n", argv[i - 1]);
95 usage();
96 return -1;
97 } else {
98 use_encryption = 1;
99 Password = argv[i];
100 }
101#else
102 printf("Encrypted tar file support not present\n");
103 usage();
104 return -1;
105#endif
106 } else if (strcmp(argv[i], "-m") == 0) {
107 if (action == 2)
108 printf("NOTE: %s option not needed when extracting.\n", argv[i]);
109 has_data_media = 1;
110 } else if (strcmp(argv[i], "-z") == 0) {
111 if (action == 2)
112 printf("NOTE: %s option not needed when extracting.\n", argv[i]);
113 use_compression = 1;
114 } else if (strcmp(argv[i], "-u") == 0) {
115#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
116 if (action == 2)
117 printf("NOTE: %s option not needed when extracting.\n", argv[i]);
118 userdata_encryption = 1;
119#else
120 printf("Encrypted tar file support not present\n");
121 usage();
122 return -1;
123#endif
124 }
125 }
126
Ethan Yonker50381972014-02-11 11:44:06 -0600127 tar.has_data_media = has_data_media;
128 tar.setdir(Directory);
129 tar.setfn(Tar_Filename);
130 tar.setsize(du.Get_Folder_Size(Directory));
131 tar.use_compression = use_compression;
132#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
133 if (userdata_encryption && !use_encryption) {
134 printf("userdata encryption set without encryption option\n");
135 usage();
136 return -1;
137 }
138 if (use_encryption) {
139 tar.use_encryption = use_encryption;
140 tar.userdata_encryption = userdata_encryption;
141 tar.setpassword(Password);
142 } else {
143 use_encryption = false;
144 }
145#endif
146 if (action == 1) {
Ethan Yonker068c7682015-09-25 11:25:20 -0500147 if (tar.createTarFork(&temp1, &temp2, tar_fork_pid) != 0) {
Ethan Yonker50381972014-02-11 11:44:06 -0600148 sync();
149 return -1;
150 }
151 sync();
152 printf("\n\ntar created successfully.\n");
153 } else if (action == 2) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500154 if (tar.extractTarFork(&temp1, &temp2) != 0) {
Ethan Yonker50381972014-02-11 11:44:06 -0600155 sync();
156 return -1;
157 }
158 sync();
159 printf("\n\ntar extracted successfully.\n");
160 }
161 return 0;
162}