Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 1 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2 | /* |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 3 | Copyright 2013 TeamWin |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 4 | This file is part of TWRP/TeamWin Recovery Project. |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 5 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 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. |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 10 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 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. |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 15 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 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/>. |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | extern "C" { |
| 21 | #include "libtar/libtar.h" |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 22 | #include "twrpTar.h" |
| 23 | #include "tarWrite.h" |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 24 | #include "set_metadata.h" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 25 | } |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 28 | #include <sys/wait.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 29 | #include <string.h> |
| 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <fstream> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 33 | #include <iostream> |
| 34 | #include <string> |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 35 | #include <sstream> |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 36 | #include <vector> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 37 | #include <dirent.h> |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 38 | #include <libgen.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 39 | #include <sys/mman.h> |
| 40 | #include "twrpTar.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 41 | #include "twcommon.h" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 42 | #include "variables.h" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 43 | #include "twrp-functions.hpp" |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 44 | #ifndef BUILD_TWRPTAR_MAIN |
| 45 | #include "data.hpp" |
| 46 | #include "infomanager.hpp" |
Ethan Yonker | 960f030 | 2014-12-21 21:54:00 -0600 | [diff] [blame] | 47 | extern "C" { |
| 48 | #include "set_metadata.h" |
| 49 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 50 | #endif //ndef BUILD_TWRPTAR_MAIN |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 51 | |
| 52 | using namespace std; |
| 53 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 54 | twrpTar::twrpTar(void) { |
| 55 | use_encryption = 0; |
| 56 | userdata_encryption = 0; |
| 57 | use_compression = 0; |
| 58 | split_archives = 0; |
| 59 | has_data_media = 0; |
| 60 | pigz_pid = 0; |
| 61 | oaes_pid = 0; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 62 | Total_Backup_Size = 0; |
| 63 | include_root_dir = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | twrpTar::~twrpTar(void) { |
| 67 | // Do nothing |
| 68 | } |
| 69 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 70 | void twrpTar::setfn(string fn) { |
| 71 | tarfn = fn; |
| 72 | } |
| 73 | |
| 74 | void twrpTar::setdir(string dir) { |
| 75 | tardir = dir; |
| 76 | } |
| 77 | |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 78 | void twrpTar::setsize(unsigned long long backup_size) { |
| 79 | Total_Backup_Size = backup_size; |
| 80 | } |
| 81 | |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 82 | void twrpTar::setpassword(string pass) { |
| 83 | password = pass; |
| 84 | } |
| 85 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 86 | int twrpTar::createTarFork(const unsigned long long *overall_size, const unsigned long long *other_backups_size) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 87 | int status = 0; |
| 88 | pid_t pid, rc_pid; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 89 | int progress_pipe[2], ret; |
| 90 | |
| 91 | file_count = 0; |
| 92 | |
| 93 | if (pipe(progress_pipe) < 0) { |
| 94 | LOGERR("Error creating progress tracking pipe\n"); |
| 95 | return -1; |
| 96 | } |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 97 | if ((pid = fork()) == -1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 98 | LOGINFO("create tar failed to fork.\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 99 | close(progress_pipe[0]); |
| 100 | close(progress_pipe[1]); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 101 | return -1; |
| 102 | } |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 103 | if (pid == 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 104 | // Child process |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 105 | |
| 106 | // Child closes input side of progress pipe |
| 107 | close(progress_pipe[0]); |
| 108 | progress_pipe_fd = progress_pipe[1]; |
| 109 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 110 | if (use_encryption || userdata_encryption) { |
| 111 | LOGINFO("Using encryption\n"); |
| 112 | DIR* d; |
| 113 | struct dirent* de; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 114 | unsigned long long regular_size = 0, encrypt_size = 0, target_size = 0, core_count = 1, total_size; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 115 | unsigned enc_thread_id = 1, regular_thread_id = 0, i, start_thread_id = 1; |
| 116 | int item_len, ret, thread_error = 0; |
| 117 | std::vector<TarListStruct> RegularList; |
| 118 | std::vector<TarListStruct> EncryptList; |
| 119 | string FileName; |
| 120 | struct TarListStruct TarItem; |
| 121 | twrpTar reg, enc[9]; |
| 122 | struct stat st; |
| 123 | pthread_t enc_thread[9]; |
| 124 | pthread_attr_t tattr; |
| 125 | void *thread_return; |
| 126 | |
| 127 | core_count = sysconf(_SC_NPROCESSORS_CONF); |
| 128 | if (core_count > 8) |
| 129 | core_count = 8; |
| 130 | LOGINFO(" Core Count : %llu\n", core_count); |
| 131 | Archive_Current_Size = 0; |
| 132 | |
| 133 | d = opendir(tardir.c_str()); |
| 134 | if (d == NULL) { |
| 135 | LOGERR("error opening '%s'\n", tardir.c_str()); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 136 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 137 | _exit(-1); |
| 138 | } |
| 139 | // Figure out the size of all data to be encrypted and create a list of unencrypted files |
| 140 | while ((de = readdir(d)) != NULL) { |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 141 | FileName = tardir + "/" + de->d_name; |
| 142 | |
| 143 | if (de->d_type == DT_BLK || de->d_type == DT_CHR || du.check_skip_dirs(FileName)) |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 144 | continue; |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 145 | if (de->d_type == DT_DIR) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 146 | item_len = strlen(de->d_name); |
| 147 | if (userdata_encryption && ((item_len >= 3 && strncmp(de->d_name, "app", 3) == 0) || (item_len >= 6 && strncmp(de->d_name, "dalvik", 6) == 0))) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 148 | ret = Generate_TarList(FileName, &RegularList, &target_size, ®ular_thread_id); |
| 149 | if (ret < 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 150 | LOGERR("Error in Generate_TarList with regular list!\n"); |
| 151 | closedir(d); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 152 | close(progress_pipe_fd); |
| 153 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 154 | _exit(-1); |
| 155 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 156 | file_count = (unsigned long long)(ret); |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 157 | regular_size += du.Get_Folder_Size(FileName); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 158 | } else { |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 159 | encrypt_size += du.Get_Folder_Size(FileName); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 160 | } |
| 161 | } else if (de->d_type == DT_REG) { |
| 162 | stat(FileName.c_str(), &st); |
| 163 | encrypt_size += (unsigned long long)(st.st_size); |
| 164 | } |
| 165 | } |
| 166 | closedir(d); |
| 167 | |
| 168 | target_size = encrypt_size / core_count; |
| 169 | target_size++; |
| 170 | LOGINFO(" Unencrypted size: %llu\n", regular_size); |
| 171 | LOGINFO(" Encrypted size : %llu\n", encrypt_size); |
| 172 | LOGINFO(" Target size : %llu\n", target_size); |
| 173 | if (!userdata_encryption) { |
| 174 | enc_thread_id = 0; |
| 175 | start_thread_id = 0; |
| 176 | core_count--; |
| 177 | } |
| 178 | Archive_Current_Size = 0; |
| 179 | |
| 180 | d = opendir(tardir.c_str()); |
| 181 | if (d == NULL) { |
| 182 | LOGERR("error opening '%s'\n", tardir.c_str()); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 183 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 184 | _exit(-1); |
| 185 | } |
| 186 | // Divide up the encrypted file list for threading |
| 187 | while ((de = readdir(d)) != NULL) { |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 188 | FileName = tardir + "/" + de->d_name; |
| 189 | |
| 190 | if (de->d_type == DT_BLK || de->d_type == DT_CHR || du.check_skip_dirs(FileName)) |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 191 | continue; |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 192 | if (de->d_type == DT_DIR) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 193 | item_len = strlen(de->d_name); |
| 194 | if (userdata_encryption && ((item_len >= 3 && strncmp(de->d_name, "app", 3) == 0) || (item_len >= 6 && strncmp(de->d_name, "dalvik", 6) == 0))) { |
| 195 | // Do nothing, we added these to RegularList earlier |
| 196 | } else { |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 197 | FileName = tardir + "/" + de->d_name; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 198 | ret = Generate_TarList(FileName, &EncryptList, &target_size, &enc_thread_id); |
| 199 | if (ret < 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 200 | LOGERR("Error in Generate_TarList with encrypted list!\n"); |
| 201 | closedir(d); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 202 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 203 | _exit(-1); |
| 204 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 205 | file_count += (unsigned long long)(ret); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 206 | } |
| 207 | } else if (de->d_type == DT_REG || de->d_type == DT_LNK) { |
| 208 | stat(FileName.c_str(), &st); |
| 209 | if (de->d_type == DT_REG) |
| 210 | Archive_Current_Size += (unsigned long long)(st.st_size); |
| 211 | TarItem.fn = FileName; |
| 212 | TarItem.thread_id = enc_thread_id; |
| 213 | EncryptList.push_back(TarItem); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 214 | file_count++; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | closedir(d); |
| 218 | if (enc_thread_id != core_count) { |
| 219 | LOGERR("Error dividing up threads for encryption, %i threads for %i cores!\n", enc_thread_id, core_count); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 220 | if (enc_thread_id > core_count) { |
| 221 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 222 | _exit(-1); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 223 | } else { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 224 | LOGERR("Continuining anyway."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 225 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 228 | // Send file count to parent |
| 229 | write(progress_pipe_fd, &file_count, sizeof(file_count)); |
| 230 | // Send backup size to parent |
| 231 | total_size = regular_size + encrypt_size; |
| 232 | write(progress_pipe_fd, &total_size, sizeof(total_size)); |
| 233 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 234 | if (userdata_encryption) { |
| 235 | // Create a backup of unencrypted data |
| 236 | reg.setfn(tarfn); |
| 237 | reg.ItemList = &RegularList; |
| 238 | reg.thread_id = 0; |
| 239 | reg.use_encryption = 0; |
| 240 | reg.use_compression = use_compression; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 241 | reg.split_archives = 1; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 242 | reg.progress_pipe_fd = progress_pipe_fd; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 243 | LOGINFO("Creating unencrypted backup...\n"); |
| 244 | if (createList((void*)®) != 0) { |
| 245 | LOGERR("Error creating unencrypted backup.\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 246 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 247 | _exit(-1); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | if (pthread_attr_init(&tattr)) { |
| 252 | LOGERR("Unable to pthread_attr_init\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 253 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 254 | _exit(-1); |
| 255 | } |
| 256 | if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) { |
| 257 | LOGERR("Error setting pthread_attr_setdetachstate\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 258 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 259 | _exit(-1); |
| 260 | } |
| 261 | if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) { |
| 262 | LOGERR("Error setting pthread_attr_setscope\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 263 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 264 | _exit(-1); |
| 265 | } |
| 266 | /*if (pthread_attr_setstacksize(&tattr, 524288)) { |
| 267 | LOGERR("Error setting pthread_attr_setstacksize\n"); |
| 268 | _exit(-1); |
| 269 | }*/ |
| 270 | |
| 271 | // Create threads for the divided up encryption lists |
| 272 | for (i = start_thread_id; i <= core_count; i++) { |
| 273 | enc[i].setdir(tardir); |
| 274 | enc[i].setfn(tarfn); |
| 275 | enc[i].ItemList = &EncryptList; |
| 276 | enc[i].thread_id = i; |
| 277 | enc[i].use_encryption = use_encryption; |
Ethan Yonker | 924a80b | 2014-04-02 10:54:12 -0500 | [diff] [blame] | 278 | enc[i].setpassword(password); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 279 | enc[i].use_compression = use_compression; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 280 | enc[i].split_archives = 1; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 281 | enc[i].progress_pipe_fd = progress_pipe_fd; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 282 | LOGINFO("Start encryption thread %i\n", i); |
| 283 | ret = pthread_create(&enc_thread[i], &tattr, createList, (void*)&enc[i]); |
| 284 | if (ret) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 285 | LOGINFO("Unable to create %i thread for encryption! %i\nContinuing in same thread (backup will be slower).\n", i, ret); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 286 | if (createList((void*)&enc[i]) != 0) { |
| 287 | LOGERR("Error creating encrypted backup %i.\n", i); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 288 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 289 | _exit(-1); |
| 290 | } else { |
| 291 | enc[i].thread_id = i + 1; |
| 292 | } |
| 293 | } |
| 294 | usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason. |
| 295 | } |
| 296 | if (pthread_attr_destroy(&tattr)) { |
| 297 | LOGERR("Failed to pthread_attr_destroy\n"); |
| 298 | } |
| 299 | for (i = start_thread_id; i <= core_count; i++) { |
| 300 | if (enc[i].thread_id == i) { |
| 301 | if (pthread_join(enc_thread[i], &thread_return)) { |
| 302 | LOGERR("Error joining thread %i\n", i); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 303 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 304 | _exit(-1); |
| 305 | } else { |
| 306 | LOGINFO("Joined thread %i.\n", i); |
Ethan Yonker | bcc502c | 2014-11-10 11:22:10 -0600 | [diff] [blame] | 307 | ret = *((int *)thread_return); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 308 | if (ret != 0) { |
| 309 | thread_error = 1; |
| 310 | LOGERR("Thread %i returned an error %i.\n", i, ret); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 311 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 312 | _exit(-1); |
| 313 | } |
| 314 | } |
| 315 | } else { |
| 316 | LOGINFO("Skipping joining thread %i because of pthread failure.\n", i); |
| 317 | } |
| 318 | } |
| 319 | if (thread_error) { |
| 320 | LOGERR("Error returned by one or more threads.\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 321 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 322 | _exit(-1); |
| 323 | } |
| 324 | LOGINFO("Finished encrypted backup.\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 325 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 326 | _exit(0); |
| 327 | } else { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 328 | // Not encrypted |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 329 | std::vector<TarListStruct> FileList; |
| 330 | unsigned thread_id = 0; |
| 331 | unsigned long long target_size = 0; |
| 332 | twrpTar reg; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 333 | int ret; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 334 | |
| 335 | // Generate list of files to back up |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 336 | ret = Generate_TarList(tardir, &FileList, &target_size, &thread_id); |
| 337 | if (ret < 0) { |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 338 | LOGERR("Error in Generate_TarList!\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 339 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 340 | _exit(-1); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 341 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 342 | file_count = (unsigned long long)(ret); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 343 | // Create a backup |
| 344 | reg.setfn(tarfn); |
| 345 | reg.ItemList = &FileList; |
| 346 | reg.thread_id = 0; |
| 347 | reg.use_encryption = 0; |
| 348 | reg.use_compression = use_compression; |
| 349 | reg.setsize(Total_Backup_Size); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 350 | reg.progress_pipe_fd = progress_pipe_fd; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 351 | if (Total_Backup_Size > MAX_ARCHIVE_SIZE) { |
| 352 | gui_print("Breaking backup file into multiple archives...\n"); |
| 353 | reg.split_archives = 1; |
| 354 | } else { |
| 355 | reg.split_archives = 0; |
| 356 | } |
| 357 | LOGINFO("Creating backup...\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 358 | write(progress_pipe_fd, &file_count, sizeof(file_count)); |
| 359 | write(progress_pipe_fd, &Total_Backup_Size, sizeof(Total_Backup_Size)); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 360 | if (createList((void*)®) != 0) { |
| 361 | LOGERR("Error creating backup.\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 362 | close(progress_pipe[1]); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 363 | _exit(-1); |
| 364 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 365 | close(progress_pipe[1]); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 366 | _exit(0); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 367 | } |
| 368 | } else { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 369 | // Parent side |
| 370 | unsigned long long fs, size_backup, files_backup, total_backup_size; |
| 371 | int first_data = 0; |
| 372 | double display_percent, progress_percent; |
| 373 | char file_progress[1024]; |
| 374 | char size_progress[1024]; |
| 375 | files_backup = 0; |
| 376 | size_backup = 0; |
| 377 | |
| 378 | // Parent closes output side |
| 379 | close(progress_pipe[1]); |
| 380 | |
| 381 | // Read progress data from children |
| 382 | while (read(progress_pipe[0], &fs, sizeof(fs)) > 0) { |
| 383 | if (first_data == 0) { |
| 384 | // First incoming data is the file count |
| 385 | file_count = fs; |
| 386 | if (file_count == 0) file_count = 1; // prevent division by 0 below |
| 387 | first_data = 1; |
| 388 | } else if (first_data == 1) { |
| 389 | // Second incoming data is total size |
| 390 | total_backup_size = fs; |
| 391 | first_data = 2; |
| 392 | } else { |
| 393 | files_backup++; |
| 394 | size_backup += fs; |
| 395 | display_percent = (double)(files_backup) / (double)(file_count) * 100; |
| 396 | sprintf(file_progress, "%llu of %llu files, %i%%", files_backup, file_count, (int)(display_percent)); |
| 397 | #ifndef BUILD_TWRPTAR_MAIN |
| 398 | DataManager::SetValue("tw_file_progress", file_progress); |
| 399 | display_percent = (double)(size_backup + *other_backups_size) / (double)(*overall_size) * 100; |
| 400 | sprintf(size_progress, "%lluMB of %lluMB, %i%%", (size_backup + *other_backups_size) / 1048576, *overall_size / 1048576, (int)(display_percent)); |
| 401 | DataManager::SetValue("tw_size_progress", size_progress); |
| 402 | progress_percent = (display_percent / 100); |
| 403 | DataManager::SetProgress((float)(progress_percent)); |
| 404 | #endif //ndef BUILD_TWRPTAR_MAIN |
| 405 | } |
| 406 | } |
| 407 | close(progress_pipe[0]); |
| 408 | #ifndef BUILD_TWRPTAR_MAIN |
| 409 | DataManager::SetValue("tw_file_progress", ""); |
| 410 | DataManager::SetValue("tw_size_progress", ""); |
| 411 | |
| 412 | InfoManager backup_info(backup_folder + partition_name + ".info"); |
| 413 | backup_info.SetValue("backup_size", size_backup); |
| 414 | if (use_compression && use_encryption) |
| 415 | backup_info.SetValue("backup_type", 3); |
| 416 | else if (use_encryption) |
| 417 | backup_info.SetValue("backup_type", 2); |
| 418 | else if (use_compression) |
| 419 | backup_info.SetValue("backup_type", 1); |
| 420 | else |
| 421 | backup_info.SetValue("backup_type", 0); |
| 422 | backup_info.SetValue("file_count", files_backup); |
| 423 | backup_info.SaveValues(); |
| 424 | #endif //ndef BUILD_TWRPTAR_MAIN |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 425 | if (TWFunc::Wait_For_Child(pid, &status, "createTarFork()") != 0) |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 426 | return -1; |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 427 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 431 | int twrpTar::extractTarFork(const unsigned long long *overall_size, unsigned long long *other_backups_size) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 432 | int status = 0; |
| 433 | pid_t pid, rc_pid; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 434 | int progress_pipe[2], ret; |
| 435 | |
| 436 | if (pipe(progress_pipe) < 0) { |
| 437 | LOGERR("Error creating progress tracking pipe\n"); |
| 438 | return -1; |
| 439 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 440 | |
| 441 | pid = fork(); |
| 442 | if (pid >= 0) // fork was successful |
| 443 | { |
| 444 | if (pid == 0) // child process |
| 445 | { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 446 | close(progress_pipe[0]); |
| 447 | progress_pipe_fd = progress_pipe[1]; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 448 | if (TWFunc::Path_Exists(tarfn)) { |
| 449 | LOGINFO("Single archive\n"); |
| 450 | if (extract() != 0) |
| 451 | _exit(-1); |
| 452 | else |
| 453 | _exit(0); |
| 454 | } else { |
| 455 | LOGINFO("Multiple archives\n"); |
| 456 | string temp; |
| 457 | char actual_filename[255]; |
| 458 | twrpTar tars[9]; |
| 459 | pthread_t tar_thread[9]; |
| 460 | pthread_attr_t tattr; |
| 461 | int thread_count = 0, i, start_thread_id = 1, ret, thread_error = 0; |
| 462 | void *thread_return; |
| 463 | |
| 464 | basefn = tarfn; |
| 465 | temp = basefn + "%i%02i"; |
| 466 | tarfn += "000"; |
| 467 | if (!TWFunc::Path_Exists(tarfn)) { |
| 468 | LOGERR("Unable to locate '%s' or '%s'\n", basefn.c_str(), tarfn.c_str()); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 469 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 470 | _exit(-1); |
| 471 | } |
| 472 | if (TWFunc::Get_File_Type(tarfn) != 2) { |
| 473 | LOGINFO("First tar file '%s' not encrypted\n", tarfn.c_str()); |
| 474 | tars[0].basefn = basefn; |
| 475 | tars[0].thread_id = 0; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 476 | tars[0].progress_pipe_fd = progress_pipe_fd; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 477 | if (extractMulti((void*)&tars[0]) != 0) { |
| 478 | LOGERR("Error extracting split archive.\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 479 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 480 | _exit(-1); |
| 481 | } |
| 482 | } else { |
| 483 | start_thread_id = 0; |
| 484 | } |
| 485 | // Start threading encrypted restores |
| 486 | if (pthread_attr_init(&tattr)) { |
| 487 | LOGERR("Unable to pthread_attr_init\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 488 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 489 | _exit(-1); |
| 490 | } |
| 491 | if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) { |
| 492 | LOGERR("Error setting pthread_attr_setdetachstate\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 493 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 494 | _exit(-1); |
| 495 | } |
| 496 | if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) { |
| 497 | LOGERR("Error setting pthread_attr_setscope\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 498 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 499 | _exit(-1); |
| 500 | } |
| 501 | /*if (pthread_attr_setstacksize(&tattr, 524288)) { |
| 502 | LOGERR("Error setting pthread_attr_setstacksize\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 503 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 504 | _exit(-1); |
| 505 | }*/ |
| 506 | for (i = start_thread_id; i < 9; i++) { |
| 507 | sprintf(actual_filename, temp.c_str(), i, 0); |
| 508 | if (TWFunc::Path_Exists(actual_filename)) { |
| 509 | thread_count++; |
| 510 | tars[i].basefn = basefn; |
Ethan Yonker | 924a80b | 2014-04-02 10:54:12 -0500 | [diff] [blame] | 511 | tars[i].setpassword(password); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 512 | tars[i].thread_id = i; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 513 | tars[i].progress_pipe_fd = progress_pipe_fd; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 514 | LOGINFO("Creating extract thread ID %i\n", i); |
| 515 | ret = pthread_create(&tar_thread[i], &tattr, extractMulti, (void*)&tars[i]); |
| 516 | if (ret) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 517 | LOGINFO("Unable to create %i thread for extraction! %i\nContinuing in same thread (restore will be slower).\n", i, ret); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 518 | if (extractMulti((void*)&tars[i]) != 0) { |
| 519 | LOGERR("Error extracting backup in thread %i.\n", i); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 520 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 521 | _exit(-1); |
| 522 | } else { |
| 523 | tars[i].thread_id = i + 1; |
| 524 | } |
| 525 | } |
| 526 | usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason. |
| 527 | } else { |
| 528 | break; |
| 529 | } |
| 530 | } |
| 531 | for (i = start_thread_id; i < thread_count + start_thread_id; i++) { |
| 532 | if (tars[i].thread_id == i) { |
| 533 | if (pthread_join(tar_thread[i], &thread_return)) { |
| 534 | LOGERR("Error joining thread %i\n", i); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 535 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 536 | _exit(-1); |
| 537 | } else { |
| 538 | LOGINFO("Joined thread %i.\n", i); |
Ethan Yonker | bcc502c | 2014-11-10 11:22:10 -0600 | [diff] [blame] | 539 | ret = *((int *)thread_return); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 540 | if (ret != 0) { |
| 541 | thread_error = 1; |
| 542 | LOGERR("Thread %i returned an error %i.\n", i, ret); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 543 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 544 | _exit(-1); |
| 545 | } |
| 546 | } |
| 547 | } else { |
| 548 | LOGINFO("Skipping joining thread %i because of pthread failure.\n", i); |
| 549 | } |
| 550 | } |
| 551 | if (thread_error) { |
| 552 | LOGERR("Error returned by one or more threads.\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 553 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 554 | _exit(-1); |
| 555 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 556 | LOGINFO("Finished encrypted restore.\n"); |
| 557 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 558 | _exit(0); |
| 559 | } |
| 560 | } |
| 561 | else // parent process |
| 562 | { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 563 | unsigned long long fs, size_backup; |
| 564 | double display_percent, progress_percent; |
| 565 | char size_progress[1024]; |
| 566 | size_backup = 0; |
| 567 | |
| 568 | // Parent closes output side |
| 569 | close(progress_pipe[1]); |
| 570 | |
| 571 | // Read progress data from children |
| 572 | while (read(progress_pipe[0], &fs, sizeof(fs)) > 0) { |
| 573 | size_backup += fs; |
| 574 | display_percent = (double)(size_backup + *other_backups_size) / (double)(*overall_size) * 100; |
| 575 | sprintf(size_progress, "%lluMB of %lluMB, %i%%", (size_backup + *other_backups_size) / 1048576, *overall_size / 1048576, (int)(display_percent)); |
| 576 | progress_percent = (display_percent / 100); |
| 577 | #ifndef BUILD_TWRPTAR_MAIN |
| 578 | DataManager::SetValue("tw_size_progress", size_progress); |
| 579 | DataManager::SetProgress((float)(progress_percent)); |
| 580 | #endif //ndef BUILD_TWRPTAR_MAIN |
| 581 | } |
| 582 | close(progress_pipe[0]); |
| 583 | #ifndef BUILD_TWRPTAR_MAIN |
| 584 | DataManager::SetValue("tw_file_progress", ""); |
| 585 | #endif //ndef BUILD_TWRPTAR_MAIN |
| 586 | *other_backups_size += size_backup; |
| 587 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 588 | if (TWFunc::Wait_For_Child(pid, &status, "extractTarFork()") != 0) |
| 589 | return -1; |
| 590 | } |
| 591 | } |
| 592 | else // fork has failed |
| 593 | { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 594 | close(progress_pipe[0]); |
| 595 | close(progress_pipe[1]); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 596 | LOGINFO("extract tar failed to fork.\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 597 | return -1; |
| 598 | } |
| 599 | return 0; |
| 600 | } |
| 601 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 602 | int twrpTar::Generate_TarList(string Path, std::vector<TarListStruct> *TarList, unsigned long long *Target_Size, unsigned *thread_id) { |
| 603 | DIR* d; |
| 604 | struct dirent* de; |
| 605 | struct stat st; |
| 606 | string FileName; |
| 607 | struct TarListStruct TarItem; |
| 608 | string::size_type i; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 609 | int ret, file_count; |
| 610 | file_count = 0; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 611 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 612 | d = opendir(Path.c_str()); |
| 613 | if (d == NULL) { |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 614 | LOGERR("Error opening '%s' -- error: %s\n", Path.c_str(), strerror(errno)); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 615 | closedir(d); |
| 616 | return -1; |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 617 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 618 | while ((de = readdir(d)) != NULL) { |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 619 | FileName = Path + "/" + de->d_name; |
Matt Mower | bb81e5d | 2014-03-20 18:05:41 -0500 | [diff] [blame] | 620 | |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 621 | if (de->d_type == DT_BLK || de->d_type == DT_CHR || du.check_skip_dirs(FileName)) |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 622 | continue; |
| 623 | TarItem.fn = FileName; |
| 624 | TarItem.thread_id = *thread_id; |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 625 | if (de->d_type == DT_DIR) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 626 | TarList->push_back(TarItem); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 627 | ret = Generate_TarList(FileName, TarList, Target_Size, thread_id); |
| 628 | if (ret < 0) |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 629 | return -1; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 630 | file_count += ret; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 631 | } else if (de->d_type == DT_REG || de->d_type == DT_LNK) { |
| 632 | stat(FileName.c_str(), &st); |
| 633 | TarList->push_back(TarItem); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 634 | if (de->d_type == DT_REG) { |
| 635 | file_count++; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 636 | Archive_Current_Size += st.st_size; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 637 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 638 | if (Archive_Current_Size != 0 && *Target_Size != 0 && Archive_Current_Size > *Target_Size) { |
| 639 | *thread_id = *thread_id + 1; |
| 640 | Archive_Current_Size = 0; |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 644 | closedir(d); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 645 | return file_count; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 646 | } |
| 647 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 648 | int twrpTar::extractTar() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 649 | char* charRootDir = (char*) tardir.c_str(); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 650 | if (openTar() == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 651 | return -1; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 652 | if (tar_extract_all(t, charRootDir, &progress_pipe_fd) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 653 | LOGERR("Unable to extract tar archive '%s'\n", tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 654 | return -1; |
| 655 | } |
| 656 | if (tar_close(t) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 657 | LOGERR("Unable to close tar file\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 658 | return -1; |
| 659 | } |
| 660 | return 0; |
| 661 | } |
| 662 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 663 | int twrpTar::extract() { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 664 | Archive_Current_Type = TWFunc::Get_File_Type(tarfn); |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 665 | |
| 666 | if (Archive_Current_Type == 1) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 667 | //if you return the extractTGZ function directly, stack crashes happen |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 668 | LOGINFO("Extracting gzipped tar\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 669 | int ret = extractTar(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 670 | return ret; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 671 | } else if (Archive_Current_Type == 2) { |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 672 | int ret = TWFunc::Try_Decrypting_File(tarfn, password); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 673 | if (ret < 1) { |
| 674 | LOGERR("Failed to decrypt tar file '%s'\n", tarfn.c_str()); |
| 675 | return -1; |
| 676 | } |
| 677 | if (ret == 1) { |
| 678 | LOGERR("Decrypted file is not in tar format.\n"); |
| 679 | return -1; |
| 680 | } |
| 681 | if (ret == 3) { |
| 682 | LOGINFO("Extracting encrypted and compressed tar.\n"); |
| 683 | Archive_Current_Type = 3; |
| 684 | } else |
| 685 | LOGINFO("Extracting encrypted tar.\n"); |
| 686 | return extractTar(); |
| 687 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 688 | LOGINFO("Extracting uncompressed tar\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 689 | return extractTar(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 693 | int twrpTar::tarList(std::vector<TarListStruct> *TarList, unsigned thread_id) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 694 | struct stat st; |
| 695 | char buf[PATH_MAX]; |
| 696 | int list_size = TarList->size(), i = 0, archive_count = 0; |
| 697 | string temp; |
| 698 | char actual_filename[PATH_MAX]; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 699 | char *ptr; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 700 | unsigned long long fs; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 701 | |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 702 | if (split_archives) { |
| 703 | basefn = tarfn; |
| 704 | temp = basefn + "%i%02i"; |
| 705 | sprintf(actual_filename, temp.c_str(), thread_id, archive_count); |
| 706 | tarfn = actual_filename; |
| 707 | include_root_dir = true; |
| 708 | } else { |
| 709 | include_root_dir = false; |
| 710 | } |
| 711 | LOGINFO("Creating tar file '%s'\n", tarfn.c_str()); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 712 | if (createTar() != 0) { |
| 713 | LOGERR("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id); |
| 714 | return -2; |
| 715 | } |
| 716 | Archive_Current_Size = 0; |
| 717 | |
| 718 | while (i < list_size) { |
| 719 | if (TarList->at(i).thread_id == thread_id) { |
| 720 | strcpy(buf, TarList->at(i).fn.c_str()); |
bigbiff bigbiff | ec8fc28 | 2014-03-16 20:32:50 -0400 | [diff] [blame] | 721 | lstat(buf, &st); |
| 722 | if (S_ISREG(st.st_mode)) { // item is a regular file |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 723 | fs = (unsigned long long)(st.st_size); |
| 724 | if (split_archives && Archive_Current_Size + fs > MAX_ARCHIVE_SIZE) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 725 | if (closeTar() != 0) { |
| 726 | LOGERR("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id); |
| 727 | return -3; |
| 728 | } |
| 729 | archive_count++; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 730 | gui_print("Splitting thread ID %i into archive %i\n", thread_id, archive_count + 1); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 731 | if (archive_count > 99) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 732 | LOGERR("Too many archives for thread %i\n", thread_id); |
| 733 | return -4; |
| 734 | } |
| 735 | sprintf(actual_filename, temp.c_str(), thread_id, archive_count); |
| 736 | tarfn = actual_filename; |
| 737 | if (createTar() != 0) { |
| 738 | LOGERR("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id); |
| 739 | return -2; |
| 740 | } |
| 741 | Archive_Current_Size = 0; |
| 742 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 743 | Archive_Current_Size += fs; |
| 744 | write(progress_pipe_fd, &fs, sizeof(fs)); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 745 | } |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 746 | LOGINFO("addFile '%s' including root: %i\n", buf, include_root_dir); |
| 747 | if (addFile(buf, include_root_dir) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 748 | LOGERR("Error adding file '%s' to '%s'\n", buf, tarfn.c_str()); |
| 749 | return -1; |
| 750 | } |
| 751 | } |
| 752 | i++; |
| 753 | } |
| 754 | if (closeTar() != 0) { |
| 755 | LOGERR("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id); |
| 756 | return -3; |
| 757 | } |
Ethan Yonker | bcc502c | 2014-11-10 11:22:10 -0600 | [diff] [blame] | 758 | LOGINFO("Thread id %i tarList done, %i archives.\n", thread_id, archive_count); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 759 | return 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 760 | } |
| 761 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 762 | void* twrpTar::createList(void *cookie) { |
| 763 | |
| 764 | twrpTar* threadTar = (twrpTar*) cookie; |
Ethan Yonker | eae4209 | 2014-03-07 15:33:13 -0600 | [diff] [blame] | 765 | if (threadTar->tarList(threadTar->ItemList, threadTar->thread_id) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 766 | LOGINFO("ERROR tarList for thread ID %i\n", threadTar->thread_id); |
| 767 | return (void*)-2; |
| 768 | } |
| 769 | LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id); |
| 770 | return (void*)0; |
| 771 | } |
| 772 | |
| 773 | void* twrpTar::extractMulti(void *cookie) { |
| 774 | |
| 775 | twrpTar* threadTar = (twrpTar*) cookie; |
| 776 | int archive_count = 0; |
| 777 | string temp = threadTar->basefn + "%i%02i"; |
| 778 | char actual_filename[255]; |
| 779 | sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count); |
| 780 | while (TWFunc::Path_Exists(actual_filename)) { |
| 781 | threadTar->tarfn = actual_filename; |
| 782 | if (threadTar->extract() != 0) { |
| 783 | LOGINFO("Error extracting '%s' in thread ID %i\n", actual_filename, threadTar->thread_id); |
| 784 | return (void*)-2; |
| 785 | } |
| 786 | archive_count++; |
| 787 | if (archive_count > 99) |
| 788 | break; |
| 789 | sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count); |
| 790 | } |
| 791 | LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id); |
| 792 | return (void*)0; |
| 793 | } |
| 794 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 795 | int twrpTar::addFilesToExistingTar(vector <string> files, string fn) { |
| 796 | char* charTarFile = (char*) fn.c_str(); |
| 797 | |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 798 | if (tar_open(&t, charTarFile, NULL, O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 799 | return -1; |
| 800 | removeEOT(charTarFile); |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 801 | if (tar_open(&t, charTarFile, NULL, O_WRONLY | O_APPEND | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 802 | return -1; |
| 803 | for (unsigned int i = 0; i < files.size(); ++i) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 804 | char* file = (char*) files.at(i).c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 805 | if (tar_append_file(t, file, file) == -1) |
| 806 | return -1; |
| 807 | } |
| 808 | if (tar_append_eof(t) == -1) |
| 809 | return -1; |
| 810 | if (tar_close(t) == -1) |
| 811 | return -1; |
| 812 | return 0; |
| 813 | } |
| 814 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 815 | int twrpTar::createTar() { |
| 816 | char* charTarFile = (char*) tarfn.c_str(); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 817 | char* charRootDir = (char*) tardir.c_str(); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 818 | static tartype_t type = { open, close, read, write_tar }; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 819 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 820 | if (use_encryption && use_compression) { |
| 821 | // Compressed and encrypted |
| 822 | Archive_Current_Type = 3; |
| 823 | LOGINFO("Using encryption and compression...\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 824 | int i, pipes[4]; |
| 825 | |
| 826 | if (pipe(pipes) < 0) { |
| 827 | LOGERR("Error creating first pipe\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 828 | return -1; |
| 829 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 830 | if (pipe(pipes + 2) < 0) { |
| 831 | LOGERR("Error creating second pipe\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 832 | return -1; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 833 | } |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 834 | int output_fd = open(tarfn.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 835 | if (output_fd < 0) { |
| 836 | LOGERR("Failed to open '%s'\n", tarfn.c_str()); |
| 837 | for (i = 0; i < 4; i++) |
| 838 | close(pipes[i]); // close all |
| 839 | return -1; |
| 840 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 841 | pigz_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 842 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 843 | if (pigz_pid < 0) { |
| 844 | LOGERR("pigz fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 845 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 846 | for (i = 0; i < 4; i++) |
| 847 | close(pipes[i]); // close all |
| 848 | return -1; |
| 849 | } else if (pigz_pid == 0) { |
| 850 | // pigz Child |
| 851 | close(pipes[1]); |
| 852 | close(pipes[2]); |
| 853 | close(0); |
| 854 | dup2(pipes[0], 0); |
| 855 | close(1); |
| 856 | dup2(pipes[3], 1); |
| 857 | if (execlp("pigz", "pigz", "-", NULL) < 0) { |
| 858 | LOGERR("execlp pigz ERROR!\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 859 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 860 | close(pipes[0]); |
| 861 | close(pipes[3]); |
| 862 | _exit(-1); |
| 863 | } |
| 864 | } else { |
| 865 | // Parent |
| 866 | oaes_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 867 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 868 | if (oaes_pid < 0) { |
| 869 | LOGERR("openaes fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 870 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 871 | for (i = 0; i < 4; i++) |
| 872 | close(pipes[i]); // close all |
| 873 | return -1; |
| 874 | } else if (oaes_pid == 0) { |
| 875 | // openaes Child |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 876 | close(pipes[0]); |
| 877 | close(pipes[1]); |
| 878 | close(pipes[3]); |
| 879 | close(0); |
| 880 | dup2(pipes[2], 0); |
| 881 | close(1); |
| 882 | dup2(output_fd, 1); |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 883 | if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 884 | LOGERR("execlp openaes ERROR!\n"); |
| 885 | close(pipes[2]); |
| 886 | close(output_fd); |
| 887 | _exit(-1); |
| 888 | } |
| 889 | } else { |
| 890 | // Parent |
| 891 | close(pipes[0]); |
| 892 | close(pipes[2]); |
| 893 | close(pipes[3]); |
| 894 | fd = pipes[1]; |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 895 | if(tar_fdopen(&t, fd, charRootDir, NULL, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 896 | close(fd); |
| 897 | LOGERR("tar_fdopen failed\n"); |
| 898 | return -1; |
| 899 | } |
| 900 | return 0; |
| 901 | } |
| 902 | } |
| 903 | } else if (use_compression) { |
| 904 | // Compressed |
| 905 | Archive_Current_Type = 1; |
| 906 | LOGINFO("Using compression...\n"); |
| 907 | int pigzfd[2]; |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 908 | int output_fd = open(tarfn.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 909 | if (output_fd < 0) { |
| 910 | LOGERR("Failed to open '%s'\n", tarfn.c_str()); |
| 911 | close(pigzfd[0]); |
| 912 | return -1; |
| 913 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 914 | |
| 915 | if (pipe(pigzfd) < 0) { |
| 916 | LOGERR("Error creating pipe\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 917 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 918 | return -1; |
| 919 | } |
| 920 | pigz_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 921 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 922 | if (pigz_pid < 0) { |
| 923 | LOGERR("fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 924 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 925 | close(pigzfd[0]); |
| 926 | close(pigzfd[1]); |
| 927 | return -1; |
| 928 | } else if (pigz_pid == 0) { |
| 929 | // Child |
| 930 | close(pigzfd[1]); // close unused output pipe |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 931 | dup2(pigzfd[0], 0); // remap stdin |
| 932 | dup2(output_fd, 1); // remap stdout to output file |
| 933 | if (execlp("pigz", "pigz", "-", NULL) < 0) { |
| 934 | LOGERR("execlp pigz ERROR!\n"); |
| 935 | close(output_fd); |
| 936 | close(pigzfd[0]); |
| 937 | _exit(-1); |
| 938 | } |
| 939 | } else { |
| 940 | // Parent |
| 941 | close(pigzfd[0]); // close parent input |
| 942 | fd = pigzfd[1]; // copy parent output |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 943 | if(tar_fdopen(&t, fd, charRootDir, NULL, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 944 | close(fd); |
| 945 | LOGERR("tar_fdopen failed\n"); |
| 946 | return -1; |
| 947 | } |
| 948 | } |
| 949 | } else if (use_encryption) { |
| 950 | // Encrypted |
| 951 | Archive_Current_Type = 2; |
| 952 | LOGINFO("Using encryption...\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 953 | int oaesfd[2]; |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 954 | int output_fd = open(tarfn.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 955 | if (output_fd < 0) { |
| 956 | LOGERR("Failed to open '%s'\n", tarfn.c_str()); |
| 957 | return -1; |
| 958 | } |
| 959 | if (pipe(oaesfd) < 0) { |
| 960 | LOGERR("Error creating pipe\n"); |
| 961 | close(output_fd); |
| 962 | return -1; |
| 963 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 964 | oaes_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 965 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 966 | if (oaes_pid < 0) { |
| 967 | LOGERR("fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 968 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 969 | close(oaesfd[0]); |
| 970 | close(oaesfd[1]); |
| 971 | return -1; |
| 972 | } else if (oaes_pid == 0) { |
| 973 | // Child |
| 974 | close(oaesfd[1]); // close unused |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 975 | dup2(oaesfd[0], 0); // remap stdin |
| 976 | dup2(output_fd, 1); // remap stdout to output file |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 977 | if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 978 | LOGERR("execlp openaes ERROR!\n"); |
| 979 | close(output_fd); |
| 980 | close(oaesfd[0]); |
| 981 | _exit(-1); |
| 982 | } |
| 983 | } else { |
| 984 | // Parent |
| 985 | close(oaesfd[0]); // close parent input |
| 986 | fd = oaesfd[1]; // copy parent output |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 987 | if(tar_fdopen(&t, fd, charRootDir, NULL, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 988 | close(fd); |
| 989 | LOGERR("tar_fdopen failed\n"); |
| 990 | return -1; |
| 991 | } |
| 992 | return 0; |
| 993 | } |
| 994 | } else { |
| 995 | // Not compressed or encrypted |
| 996 | init_libtar_buffer(0); |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 997 | if (tar_open(&t, charTarFile, &type, O_WRONLY | O_CREAT | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) == -1) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 998 | LOGERR("tar_open error opening '%s'\n", tarfn.c_str()); |
| 999 | return -1; |
| 1000 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1001 | } |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1005 | int twrpTar::openTar() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 1006 | char* charRootDir = (char*) tardir.c_str(); |
| 1007 | char* charTarFile = (char*) tarfn.c_str(); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1008 | string Password; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1009 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1010 | if (Archive_Current_Type == 3) { |
| 1011 | LOGINFO("Opening encrypted and compressed backup...\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1012 | int i, pipes[4]; |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1013 | int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE); |
| 1014 | if (input_fd < 0) { |
| 1015 | LOGERR("Failed to open '%s'\n", tarfn.c_str()); |
| 1016 | return -1; |
| 1017 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1018 | |
| 1019 | if (pipe(pipes) < 0) { |
| 1020 | LOGERR("Error creating first pipe\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1021 | close(input_fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1022 | return -1; |
| 1023 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1024 | if (pipe(pipes + 2) < 0) { |
| 1025 | LOGERR("Error creating second pipe\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1026 | close(pipes[0]); |
| 1027 | close(pipes[1]); |
| 1028 | close(input_fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1029 | return -1; |
| 1030 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1031 | oaes_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 1032 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1033 | if (oaes_pid < 0) { |
| 1034 | LOGERR("pigz fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1035 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1036 | for (i = 0; i < 4; i++) |
| 1037 | close(pipes[i]); // close all |
| 1038 | return -1; |
| 1039 | } else if (oaes_pid == 0) { |
| 1040 | // openaes Child |
| 1041 | close(pipes[0]); // Close pipes that are not used by this child |
| 1042 | close(pipes[2]); |
| 1043 | close(pipes[3]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1044 | close(0); |
| 1045 | dup2(input_fd, 0); |
| 1046 | close(1); |
| 1047 | dup2(pipes[1], 1); |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 1048 | if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1049 | LOGERR("execlp openaes ERROR!\n"); |
| 1050 | close(input_fd); |
| 1051 | close(pipes[1]); |
| 1052 | _exit(-1); |
| 1053 | } |
| 1054 | } else { |
| 1055 | // Parent |
| 1056 | pigz_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 1057 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1058 | if (pigz_pid < 0) { |
| 1059 | LOGERR("openaes fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1060 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1061 | for (i = 0; i < 4; i++) |
| 1062 | close(pipes[i]); // close all |
| 1063 | return -1; |
| 1064 | } else if (pigz_pid == 0) { |
| 1065 | // pigz Child |
| 1066 | close(pipes[1]); // Close pipes not used by this child |
| 1067 | close(pipes[2]); |
| 1068 | close(0); |
| 1069 | dup2(pipes[0], 0); |
| 1070 | close(1); |
| 1071 | dup2(pipes[3], 1); |
| 1072 | if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) { |
| 1073 | LOGERR("execlp pigz ERROR!\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1074 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1075 | close(pipes[0]); |
| 1076 | close(pipes[3]); |
| 1077 | _exit(-1); |
| 1078 | } |
| 1079 | } else { |
| 1080 | // Parent |
| 1081 | close(pipes[0]); // Close pipes not used by parent |
| 1082 | close(pipes[1]); |
| 1083 | close(pipes[3]); |
| 1084 | fd = pipes[2]; |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 1085 | if(tar_fdopen(&t, fd, charRootDir, NULL, O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1086 | close(fd); |
| 1087 | LOGERR("tar_fdopen failed\n"); |
| 1088 | return -1; |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | } else if (Archive_Current_Type == 2) { |
| 1093 | LOGINFO("Opening encrypted backup...\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1094 | int oaesfd[2]; |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1095 | int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE); |
| 1096 | if (input_fd < 0) { |
| 1097 | LOGERR("Failed to open '%s'\n", tarfn.c_str()); |
| 1098 | return -1; |
| 1099 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1100 | |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1101 | if (pipe(oaesfd) < 0) { |
| 1102 | LOGERR("Error creating pipe\n"); |
| 1103 | close(input_fd); |
| 1104 | return -1; |
| 1105 | } |
| 1106 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1107 | oaes_pid = fork(); |
| 1108 | if (oaes_pid < 0) { |
| 1109 | LOGERR("fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1110 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1111 | close(oaesfd[0]); |
| 1112 | close(oaesfd[1]); |
| 1113 | return -1; |
| 1114 | } else if (oaes_pid == 0) { |
| 1115 | // Child |
| 1116 | close(oaesfd[0]); // Close unused pipe |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1117 | close(0); // close stdin |
| 1118 | dup2(oaesfd[1], 1); // remap stdout |
| 1119 | dup2(input_fd, 0); // remap input fd to stdin |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 1120 | if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1121 | LOGERR("execlp openaes ERROR!\n"); |
| 1122 | close(input_fd); |
| 1123 | close(oaesfd[1]); |
| 1124 | _exit(-1); |
| 1125 | } |
| 1126 | } else { |
| 1127 | // Parent |
| 1128 | close(oaesfd[1]); // close parent output |
| 1129 | fd = oaesfd[0]; // copy parent input |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 1130 | if(tar_fdopen(&t, fd, charRootDir, NULL, O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1131 | close(fd); |
| 1132 | LOGERR("tar_fdopen failed\n"); |
| 1133 | return -1; |
| 1134 | } |
| 1135 | } |
| 1136 | } else if (Archive_Current_Type == 1) { |
| 1137 | LOGINFO("Opening as a gzip...\n"); |
| 1138 | int pigzfd[2]; |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1139 | int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE); |
| 1140 | if (input_fd < 0) { |
| 1141 | LOGERR("Failed to open '%s'\n", tarfn.c_str()); |
| 1142 | return -1; |
| 1143 | } |
| 1144 | if (pipe(pigzfd) < 0) { |
| 1145 | LOGERR("Error creating pipe\n"); |
| 1146 | close(input_fd); |
| 1147 | return -1; |
| 1148 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1149 | |
| 1150 | pigz_pid = fork(); |
| 1151 | if (pigz_pid < 0) { |
| 1152 | LOGERR("fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1153 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1154 | close(pigzfd[0]); |
| 1155 | close(pigzfd[1]); |
| 1156 | return -1; |
| 1157 | } else if (pigz_pid == 0) { |
| 1158 | // Child |
| 1159 | close(pigzfd[0]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1160 | dup2(input_fd, 0); // remap input fd to stdin |
| 1161 | dup2(pigzfd[1], 1); // remap stdout |
| 1162 | if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) { |
| 1163 | close(pigzfd[1]); |
| 1164 | close(input_fd); |
| 1165 | LOGERR("execlp openaes ERROR!\n"); |
| 1166 | _exit(-1); |
| 1167 | } |
| 1168 | } else { |
| 1169 | // Parent |
| 1170 | close(pigzfd[1]); // close parent output |
| 1171 | fd = pigzfd[0]; // copy parent input |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 1172 | if(tar_fdopen(&t, fd, charRootDir, NULL, O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1173 | close(fd); |
| 1174 | LOGERR("tar_fdopen failed\n"); |
| 1175 | return -1; |
| 1176 | } |
| 1177 | } |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 1178 | } else if (tar_open(&t, charTarFile, NULL, O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1179 | LOGERR("Unable to open tar archive '%s'\n", charTarFile); |
| 1180 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1181 | } |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | string twrpTar::Strip_Root_Dir(string Path) { |
| 1186 | string temp; |
| 1187 | size_t slash; |
| 1188 | |
| 1189 | if (Path.substr(0, 1) == "/") |
| 1190 | temp = Path.substr(1, Path.size() - 1); |
| 1191 | else |
| 1192 | temp = Path; |
| 1193 | slash = temp.find("/"); |
| 1194 | if (slash == string::npos) |
| 1195 | return temp; |
| 1196 | else { |
| 1197 | string stripped; |
| 1198 | |
| 1199 | stripped = temp.substr(slash, temp.size() - slash); |
| 1200 | return stripped; |
| 1201 | } |
| 1202 | return temp; |
| 1203 | } |
| 1204 | |
| 1205 | int twrpTar::addFile(string fn, bool include_root) { |
| 1206 | char* charTarFile = (char*) fn.c_str(); |
| 1207 | if (include_root) { |
| 1208 | if (tar_append_file(t, charTarFile, NULL) == -1) |
| 1209 | return -1; |
| 1210 | } else { |
| 1211 | string temp = Strip_Root_Dir(fn); |
| 1212 | char* charTarPath = (char*) temp.c_str(); |
| 1213 | if (tar_append_file(t, charTarFile, charTarPath) == -1) |
| 1214 | return -1; |
| 1215 | } |
| 1216 | return 0; |
| 1217 | } |
| 1218 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1219 | int twrpTar::closeTar() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 1220 | flush_libtar_buffer(t->fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1221 | if (tar_append_eof(t) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1222 | LOGERR("tar_append_eof(): %s\n", strerror(errno)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1223 | tar_close(t); |
| 1224 | return -1; |
| 1225 | } |
| 1226 | if (tar_close(t) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1227 | LOGERR("Unable to close tar archive: '%s'\n", tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1228 | return -1; |
| 1229 | } |
Dees_Troy | 2727b99 | 2013-08-14 20:09:30 +0000 | [diff] [blame] | 1230 | if (Archive_Current_Type > 0) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1231 | close(fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1232 | int status; |
| 1233 | if (pigz_pid > 0 && TWFunc::Wait_For_Child(pigz_pid, &status, "pigz") != 0) |
| 1234 | return -1; |
| 1235 | if (oaes_pid > 0 && TWFunc::Wait_For_Child(oaes_pid, &status, "openaes") != 0) |
| 1236 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1237 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1238 | free_libtar_buffer(); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 1239 | if (use_compression && !use_encryption) { |
| 1240 | string gzname = tarfn + ".gz"; |
| 1241 | if (TWFunc::Path_Exists(gzname)) { |
| 1242 | rename(gzname.c_str(), tarfn.c_str()); |
| 1243 | } |
| 1244 | } |
| 1245 | if (TWFunc::Get_File_Size(tarfn) == 0) { |
| 1246 | LOGERR("Backup file size for '%s' is 0 bytes.\n", tarfn.c_str()); |
| 1247 | return -1; |
| 1248 | } |
Ethan Yonker | 960f030 | 2014-12-21 21:54:00 -0600 | [diff] [blame] | 1249 | #ifndef BUILD_TWRPTAR_MAIN |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 1250 | tw_set_default_metadata(tarfn.c_str()); |
Ethan Yonker | 960f030 | 2014-12-21 21:54:00 -0600 | [diff] [blame] | 1251 | #endif |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | int twrpTar::removeEOT(string tarFile) { |
| 1256 | char* charTarFile = (char*) tarFile.c_str(); |
| 1257 | off_t tarFileEnd; |
| 1258 | while (th_read(t) == 0) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1259 | if (TH_ISREG(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1260 | tar_skip_regfile(t); |
| 1261 | tarFileEnd = lseek(t->fd, 0, SEEK_CUR); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1262 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1263 | if (tar_close(t) == -1) |
| 1264 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1265 | if (truncate(charTarFile, tarFileEnd) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1266 | return -1; |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 1270 | int twrpTar::entryExists(string entry) { |
| 1271 | char* searchstr = (char*)entry.c_str(); |
| 1272 | int ret; |
| 1273 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1274 | Archive_Current_Type = TWFunc::Get_File_Type(tarfn); |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 1275 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1276 | if (openTar() == -1) |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 1277 | ret = 0; |
| 1278 | else |
| 1279 | ret = tar_find(t, searchstr); |
| 1280 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1281 | if (closeTar() != 0) |
| 1282 | LOGINFO("Unable to close tar after searching for entry.\n"); |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 1283 | |
| 1284 | return ret; |
| 1285 | } |
| 1286 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1287 | unsigned long long twrpTar::get_size() { |
| 1288 | if (TWFunc::Path_Exists(tarfn)) { |
| 1289 | LOGINFO("Single archive\n"); |
| 1290 | int type = 0; |
| 1291 | return uncompressedSize(tarfn, &type); |
| 1292 | } else { |
| 1293 | LOGINFO("Multiple archives\n"); |
| 1294 | string temp; |
| 1295 | char actual_filename[255]; |
| 1296 | int archive_count = 0, i, type = 0, temp_type = 0; |
| 1297 | unsigned long long total_restore_size = 0; |
| 1298 | |
| 1299 | basefn = tarfn; |
| 1300 | temp = basefn + "%i%02i"; |
| 1301 | tarfn += "000"; |
| 1302 | thread_id = 0; |
| 1303 | sprintf(actual_filename, temp.c_str(), thread_id, archive_count); |
| 1304 | if (!TWFunc::Path_Exists(actual_filename)) { |
| 1305 | LOGERR("Unable to locate '%s' or '%s'\n", basefn.c_str(), tarfn.c_str()); |
| 1306 | return 0; |
| 1307 | } |
| 1308 | for (i = 0; i < 9; i++) { |
| 1309 | archive_count = 0; |
| 1310 | sprintf(actual_filename, temp.c_str(), i, archive_count); |
| 1311 | while (TWFunc::Path_Exists(actual_filename)) { |
| 1312 | total_restore_size += uncompressedSize(actual_filename, &temp_type); |
| 1313 | if (temp_type > type) |
| 1314 | type = temp_type; |
| 1315 | archive_count++; |
| 1316 | if (archive_count > 99) |
| 1317 | break; |
| 1318 | sprintf(actual_filename, temp.c_str(), i, archive_count); |
| 1319 | } |
| 1320 | } |
| 1321 | #ifndef BUILD_TWRPTAR_MAIN |
| 1322 | InfoManager backup_info(backup_folder + "/" + partition_name + ".info"); |
| 1323 | backup_info.SetValue("backup_size", total_restore_size); |
| 1324 | backup_info.SetValue("backup_type", type); |
| 1325 | backup_info.SaveValues(); |
| 1326 | #endif //ndef BUILD_TWRPTAR_MAIN |
| 1327 | return total_restore_size; |
| 1328 | } |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | unsigned long long twrpTar::uncompressedSize(string filename, int *archive_type) { |
| 1333 | int type = 0; |
| 1334 | unsigned long long total_size = 0; |
| 1335 | string Tar, Command, result; |
| 1336 | vector<string> split; |
| 1337 | |
| 1338 | Tar = TWFunc::Get_Filename(filename); |
| 1339 | type = TWFunc::Get_File_Type(filename); |
| 1340 | if (type == 0) { |
| 1341 | total_size = TWFunc::Get_File_Size(filename); |
| 1342 | *archive_type = 0; |
| 1343 | } else if (type == 1) { |
| 1344 | // Compressed |
| 1345 | Command = "pigz -l '" + filename + "'"; |
| 1346 | /* if we set Command = "pigz -l " + tarfn + " | sed '1d' | cut -f5 -d' '"; |
| 1347 | we get the uncompressed size at once. */ |
| 1348 | TWFunc::Exec_Cmd(Command, result); |
| 1349 | if (!result.empty()) { |
| 1350 | /* Expected output: |
| 1351 | compressed original reduced name |
| 1352 | 95855838 179403776 -1.3% data.yaffs2.win |
| 1353 | ^ |
| 1354 | split[5] |
| 1355 | */ |
| 1356 | split = TWFunc::split_string(result, ' ', true); |
| 1357 | if (split.size() > 4) |
| 1358 | total_size = atoi(split[5].c_str()); |
| 1359 | } |
| 1360 | *archive_type = 1; |
| 1361 | } else if (type == 2) { |
| 1362 | // File is encrypted and may be compressed |
| 1363 | int ret = TWFunc::Try_Decrypting_File(filename, password); |
| 1364 | *archive_type = 2; |
| 1365 | if (ret < 1) { |
| 1366 | LOGERR("Failed to decrypt tar file '%s'\n", filename.c_str()); |
| 1367 | total_size = TWFunc::Get_File_Size(filename); |
| 1368 | } else if (ret == 1) { |
| 1369 | LOGERR("Decrypted file is not in tar format.\n"); |
| 1370 | total_size = TWFunc::Get_File_Size(filename); |
| 1371 | } else if (ret == 3) { |
| 1372 | *archive_type = 3; |
| 1373 | Command = "openaes dec --key \"" + password + "\" --in '" + filename + "' | pigz -l"; |
| 1374 | /* if we set Command = "pigz -l " + tarfn + " | sed '1d' | cut -f5 -d' '"; |
| 1375 | we get the uncompressed size at once. */ |
| 1376 | TWFunc::Exec_Cmd(Command, result); |
| 1377 | if (!result.empty()) { |
| 1378 | LOGINFO("result was: '%s'\n", result.c_str()); |
| 1379 | /* Expected output: |
| 1380 | compressed original reduced name |
| 1381 | 95855838 179403776 -1.3% data.yaffs2.win |
| 1382 | ^ |
| 1383 | split[5] |
| 1384 | */ |
| 1385 | split = TWFunc::split_string(result, ' ', true); |
| 1386 | if (split.size() > 4) |
| 1387 | total_size = atoi(split[5].c_str()); |
| 1388 | } |
| 1389 | } else { |
| 1390 | total_size = TWFunc::Get_File_Size(filename); |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | return total_size; |
| 1395 | } |
| 1396 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 1397 | extern "C" ssize_t write_tar(int fd, const void *buffer, size_t size) { |
| 1398 | return (ssize_t) write_libtar_buffer(fd, buffer, size); |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame] | 1399 | } |