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