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