Ethan Yonker | 5038197 | 2014-02-11 11:44:06 -0600 | [diff] [blame] | 1 | |
| 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" |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 22 | #include "../exclude.hpp" |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 23 | #include "../progresstracking.hpp" |
| 24 | #include "../gui/gui.hpp" |
| 25 | #include "../gui/twmsg.h" |
Ethan Yonker | 5038197 | 2014-02-11 11:44:06 -0600 | [diff] [blame] | 26 | #include <string.h> |
| 27 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 28 | void gui_msg(const char* text) |
| 29 | { |
| 30 | if (text) { |
| 31 | Message msg = Msg(text); |
| 32 | gui_msg(msg); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void gui_warn(const char* text) |
| 37 | { |
| 38 | if (text) { |
| 39 | Message msg = Msg(msg::kWarning, text); |
| 40 | gui_msg(msg); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void gui_err(const char* text) |
| 45 | { |
| 46 | if (text) { |
| 47 | Message msg = Msg(msg::kError, text); |
| 48 | gui_msg(msg); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void gui_highlight(const char* text) |
| 53 | { |
| 54 | if (text) { |
| 55 | Message msg = Msg(msg::kHighlight, text); |
| 56 | gui_msg(msg); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void gui_msg(Message msg) |
| 61 | { |
| 62 | std::string output = msg; |
| 63 | output += "\n"; |
| 64 | fputs(output.c_str(), stdout); |
| 65 | } |
| 66 | |
Ethan Yonker | 5038197 | 2014-02-11 11:44:06 -0600 | [diff] [blame] | 67 | void usage() { |
| 68 | printf("twrpTar <action> [options]\n\n"); |
| 69 | printf("actions: -c create\n"); |
| 70 | printf(" -x extract\n\n"); |
| 71 | printf(" -d target directory\n"); |
| 72 | printf(" -t output file\n"); |
| 73 | printf(" -m skip media subfolder (has data media)\n"); |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 74 | printf(" -z compress backup (/system/bin/pigz must be present)\n"); |
Ethan Yonker | 5038197 | 2014-02-11 11:44:06 -0600 | [diff] [blame] | 75 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 76 | printf(" -e encrypt/decrypt backup followed by password (/system/bin/openaes must be present)\n"); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 77 | printf(" -u encrypt using userdata encryption (must be used with -e)\n"); |
Ethan Yonker | 5038197 | 2014-02-11 11:44:06 -0600 | [diff] [blame] | 78 | #endif |
| 79 | printf("\n\n"); |
| 80 | printf("Example: twrpTar -c -d /cache -t /sdcard/test.tar\n"); |
| 81 | printf(" twrpTar -x -d /cache -t /sdcard/test.tar\n"); |
| 82 | } |
| 83 | |
| 84 | int main(int argc, char **argv) { |
| 85 | twrpTar tar; |
| 86 | int use_encryption = 0, userdata_encryption = 0, has_data_media = 0, use_compression = 0, include_root = 0; |
| 87 | int i, action = 0; |
| 88 | unsigned j; |
| 89 | string Directory, Tar_Filename; |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 90 | ProgressTracking progress(1); |
Ethan Yonker | 068c768 | 2015-09-25 11:25:20 -0500 | [diff] [blame] | 91 | pid_t tar_fork_pid = 0; |
Ethan Yonker | 5038197 | 2014-02-11 11:44:06 -0600 | [diff] [blame] | 92 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 93 | string Password; |
| 94 | #endif |
| 95 | |
| 96 | if (argc < 2) { |
| 97 | usage(); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | if (strcmp(argv[1], "-c") == 0) |
| 102 | action = 1; // create tar |
| 103 | else if (strcmp(argv[1], "-x") == 0) |
| 104 | action = 2; // extract tar |
| 105 | else { |
| 106 | printf("Invalid action '%s' specified.\n", argv[1]); |
| 107 | usage(); |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | for (i = 2; i < argc; i++) { |
| 112 | if (strcmp(argv[i], "-d") == 0) { |
| 113 | i++; |
| 114 | if (argc <= i) { |
| 115 | printf("No argument specified for %s\n", argv[i - 1]); |
| 116 | usage(); |
| 117 | return -1; |
| 118 | } else { |
| 119 | Directory = argv[i]; |
| 120 | } |
| 121 | } else if (strcmp(argv[i], "-t") == 0) { |
| 122 | i++; |
| 123 | if (argc <= i) { |
| 124 | printf("No argument specified for %s\n", argv[i - 1]); |
| 125 | usage(); |
| 126 | return -1; |
| 127 | } else { |
| 128 | Tar_Filename = argv[i]; |
| 129 | } |
| 130 | } else if (strcmp(argv[i], "-e") == 0) { |
| 131 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 132 | i++; |
| 133 | if (argc <= i) { |
| 134 | printf("No argument specified for %s\n", argv[i - 1]); |
| 135 | usage(); |
| 136 | return -1; |
| 137 | } else { |
| 138 | use_encryption = 1; |
| 139 | Password = argv[i]; |
| 140 | } |
| 141 | #else |
| 142 | printf("Encrypted tar file support not present\n"); |
| 143 | usage(); |
| 144 | return -1; |
| 145 | #endif |
| 146 | } else if (strcmp(argv[i], "-m") == 0) { |
| 147 | if (action == 2) |
| 148 | printf("NOTE: %s option not needed when extracting.\n", argv[i]); |
| 149 | has_data_media = 1; |
| 150 | } else if (strcmp(argv[i], "-z") == 0) { |
| 151 | if (action == 2) |
| 152 | printf("NOTE: %s option not needed when extracting.\n", argv[i]); |
| 153 | use_compression = 1; |
| 154 | } else if (strcmp(argv[i], "-u") == 0) { |
| 155 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 156 | if (action == 2) |
| 157 | printf("NOTE: %s option not needed when extracting.\n", argv[i]); |
| 158 | userdata_encryption = 1; |
| 159 | #else |
| 160 | printf("Encrypted tar file support not present\n"); |
| 161 | usage(); |
| 162 | return -1; |
| 163 | #endif |
| 164 | } |
| 165 | } |
| 166 | |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 167 | TWExclude exclude; |
| 168 | exclude.add_absolute_dir("/data/media"); |
Ethan Yonker | 5038197 | 2014-02-11 11:44:06 -0600 | [diff] [blame] | 169 | tar.has_data_media = has_data_media; |
| 170 | tar.setdir(Directory); |
| 171 | tar.setfn(Tar_Filename); |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 172 | tar.setsize(exclude.Get_Folder_Size(Directory)); |
Ethan Yonker | 5038197 | 2014-02-11 11:44:06 -0600 | [diff] [blame] | 173 | tar.use_compression = use_compression; |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 174 | tar.backup_exclusions = &exclude; |
Ethan Yonker | 5038197 | 2014-02-11 11:44:06 -0600 | [diff] [blame] | 175 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 176 | if (userdata_encryption && !use_encryption) { |
| 177 | printf("userdata encryption set without encryption option\n"); |
| 178 | usage(); |
| 179 | return -1; |
| 180 | } |
| 181 | if (use_encryption) { |
| 182 | tar.use_encryption = use_encryption; |
| 183 | tar.userdata_encryption = userdata_encryption; |
| 184 | tar.setpassword(Password); |
| 185 | } else { |
| 186 | use_encryption = false; |
| 187 | } |
| 188 | #endif |
| 189 | if (action == 1) { |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 190 | if (tar.createTarFork(&tar_fork_pid) != 0) { |
Ethan Yonker | 5038197 | 2014-02-11 11:44:06 -0600 | [diff] [blame] | 191 | sync(); |
| 192 | return -1; |
| 193 | } |
| 194 | sync(); |
| 195 | printf("\n\ntar created successfully.\n"); |
| 196 | } else if (action == 2) { |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 197 | if (tar.extractTarFork() != 0) { |
Ethan Yonker | 5038197 | 2014-02-11 11:44:06 -0600 | [diff] [blame] | 198 | sync(); |
| 199 | return -1; |
| 200 | } |
| 201 | sync(); |
| 202 | printf("\n\ntar extracted successfully.\n"); |
| 203 | } |
| 204 | return 0; |
| 205 | } |