blob: ef6a5efea19a9bfd2aa23c5bd38aff5c83ebb09e [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;
50#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
51 string Password;
52#endif
53
54 if (argc < 2) {
55 usage();
56 return 0;
57 }
58
59 if (strcmp(argv[1], "-c") == 0)
60 action = 1; // create tar
61 else if (strcmp(argv[1], "-x") == 0)
62 action = 2; // extract tar
63 else {
64 printf("Invalid action '%s' specified.\n", argv[1]);
65 usage();
66 return -1;
67 }
68
69 for (i = 2; i < argc; i++) {
70 if (strcmp(argv[i], "-d") == 0) {
71 i++;
72 if (argc <= i) {
73 printf("No argument specified for %s\n", argv[i - 1]);
74 usage();
75 return -1;
76 } else {
77 Directory = argv[i];
78 }
79 } else if (strcmp(argv[i], "-t") == 0) {
80 i++;
81 if (argc <= i) {
82 printf("No argument specified for %s\n", argv[i - 1]);
83 usage();
84 return -1;
85 } else {
86 Tar_Filename = argv[i];
87 }
88 } else if (strcmp(argv[i], "-e") == 0) {
89#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
90 i++;
91 if (argc <= i) {
92 printf("No argument specified for %s\n", argv[i - 1]);
93 usage();
94 return -1;
95 } else {
96 use_encryption = 1;
97 Password = argv[i];
98 }
99#else
100 printf("Encrypted tar file support not present\n");
101 usage();
102 return -1;
103#endif
104 } else if (strcmp(argv[i], "-m") == 0) {
105 if (action == 2)
106 printf("NOTE: %s option not needed when extracting.\n", argv[i]);
107 has_data_media = 1;
108 } else if (strcmp(argv[i], "-z") == 0) {
109 if (action == 2)
110 printf("NOTE: %s option not needed when extracting.\n", argv[i]);
111 use_compression = 1;
112 } else if (strcmp(argv[i], "-u") == 0) {
113#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
114 if (action == 2)
115 printf("NOTE: %s option not needed when extracting.\n", argv[i]);
116 userdata_encryption = 1;
117#else
118 printf("Encrypted tar file support not present\n");
119 usage();
120 return -1;
121#endif
122 }
123 }
124
125 vector<string> excludedirs = du.get_absolute_dirs();
126 for (j = 0; j < excludedirs.size(); ++j) {
127 tar.setexcl(excludedirs.at(j));
128 }
129 tar.has_data_media = has_data_media;
130 tar.setdir(Directory);
131 tar.setfn(Tar_Filename);
132 tar.setsize(du.Get_Folder_Size(Directory));
133 tar.use_compression = use_compression;
134#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
135 if (userdata_encryption && !use_encryption) {
136 printf("userdata encryption set without encryption option\n");
137 usage();
138 return -1;
139 }
140 if (use_encryption) {
141 tar.use_encryption = use_encryption;
142 tar.userdata_encryption = userdata_encryption;
143 tar.setpassword(Password);
144 } else {
145 use_encryption = false;
146 }
147#endif
148 if (action == 1) {
149 if (tar.createTarFork() != 0) {
150 sync();
151 return -1;
152 }
153 sync();
154 printf("\n\ntar created successfully.\n");
155 } else if (action == 2) {
156 if (tar.extractTarFork() != 0) {
157 sync();
158 return -1;
159 }
160 sync();
161 printf("\n\ntar extracted successfully.\n");
162 }
163 return 0;
164}