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" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 24 | } |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 27 | #include <sys/wait.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <fstream> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 32 | #include <iostream> |
| 33 | #include <string> |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 34 | #include <sstream> |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 35 | #include <vector> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 36 | #include <dirent.h> |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 37 | #include <libgen.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 38 | #include <sys/mman.h> |
| 39 | #include "twrpTar.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 40 | #include "twcommon.h" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 41 | #include "variables.h" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 42 | #include "twrp-functions.hpp" |
| 43 | |
| 44 | using namespace std; |
| 45 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 46 | twrpTar::twrpTar(void) { |
| 47 | use_encryption = 0; |
| 48 | userdata_encryption = 0; |
| 49 | use_compression = 0; |
| 50 | split_archives = 0; |
| 51 | has_data_media = 0; |
| 52 | pigz_pid = 0; |
| 53 | oaes_pid = 0; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 54 | Total_Backup_Size = 0; |
| 55 | include_root_dir = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | twrpTar::~twrpTar(void) { |
| 59 | // Do nothing |
| 60 | } |
| 61 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 62 | void twrpTar::setfn(string fn) { |
| 63 | tarfn = fn; |
| 64 | } |
| 65 | |
| 66 | void twrpTar::setdir(string dir) { |
| 67 | tardir = dir; |
| 68 | } |
| 69 | |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 70 | void twrpTar::setsize(unsigned long long backup_size) { |
| 71 | Total_Backup_Size = backup_size; |
| 72 | } |
| 73 | |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 74 | void twrpTar::setpassword(string pass) { |
| 75 | password = pass; |
| 76 | } |
| 77 | |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 78 | int twrpTar::createTarFork() { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 79 | int status = 0; |
| 80 | pid_t pid, rc_pid; |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 81 | if ((pid = fork()) == -1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 82 | LOGINFO("create tar failed to fork.\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 83 | return -1; |
| 84 | } |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 85 | if (pid == 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 86 | // Child process |
| 87 | if (use_encryption || userdata_encryption) { |
| 88 | LOGINFO("Using encryption\n"); |
| 89 | DIR* d; |
| 90 | struct dirent* de; |
| 91 | unsigned long long regular_size = 0, encrypt_size = 0, target_size = 0, core_count = 1; |
| 92 | unsigned enc_thread_id = 1, regular_thread_id = 0, i, start_thread_id = 1; |
| 93 | int item_len, ret, thread_error = 0; |
| 94 | std::vector<TarListStruct> RegularList; |
| 95 | std::vector<TarListStruct> EncryptList; |
| 96 | string FileName; |
| 97 | struct TarListStruct TarItem; |
| 98 | twrpTar reg, enc[9]; |
| 99 | struct stat st; |
| 100 | pthread_t enc_thread[9]; |
| 101 | pthread_attr_t tattr; |
| 102 | void *thread_return; |
| 103 | |
| 104 | core_count = sysconf(_SC_NPROCESSORS_CONF); |
| 105 | if (core_count > 8) |
| 106 | core_count = 8; |
| 107 | LOGINFO(" Core Count : %llu\n", core_count); |
| 108 | Archive_Current_Size = 0; |
| 109 | |
| 110 | d = opendir(tardir.c_str()); |
| 111 | if (d == NULL) { |
| 112 | LOGERR("error opening '%s'\n", tardir.c_str()); |
| 113 | _exit(-1); |
| 114 | } |
| 115 | // Figure out the size of all data to be encrypted and create a list of unencrypted files |
| 116 | while ((de = readdir(d)) != NULL) { |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 117 | FileName = tardir + "/" + de->d_name; |
| 118 | |
| 119 | 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] | 120 | continue; |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 121 | if (de->d_type == DT_DIR) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 122 | item_len = strlen(de->d_name); |
| 123 | if (userdata_encryption && ((item_len >= 3 && strncmp(de->d_name, "app", 3) == 0) || (item_len >= 6 && strncmp(de->d_name, "dalvik", 6) == 0))) { |
| 124 | if (Generate_TarList(FileName, &RegularList, &target_size, ®ular_thread_id) < 0) { |
| 125 | LOGERR("Error in Generate_TarList with regular list!\n"); |
| 126 | closedir(d); |
| 127 | _exit(-1); |
| 128 | } |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 129 | regular_size += du.Get_Folder_Size(FileName); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 130 | } else { |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 131 | encrypt_size += du.Get_Folder_Size(FileName); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 132 | } |
| 133 | } else if (de->d_type == DT_REG) { |
| 134 | stat(FileName.c_str(), &st); |
| 135 | encrypt_size += (unsigned long long)(st.st_size); |
| 136 | } |
| 137 | } |
| 138 | closedir(d); |
| 139 | |
| 140 | target_size = encrypt_size / core_count; |
| 141 | target_size++; |
| 142 | LOGINFO(" Unencrypted size: %llu\n", regular_size); |
| 143 | LOGINFO(" Encrypted size : %llu\n", encrypt_size); |
| 144 | LOGINFO(" Target size : %llu\n", target_size); |
| 145 | if (!userdata_encryption) { |
| 146 | enc_thread_id = 0; |
| 147 | start_thread_id = 0; |
| 148 | core_count--; |
| 149 | } |
| 150 | Archive_Current_Size = 0; |
| 151 | |
| 152 | d = opendir(tardir.c_str()); |
| 153 | if (d == NULL) { |
| 154 | LOGERR("error opening '%s'\n", tardir.c_str()); |
| 155 | _exit(-1); |
| 156 | } |
| 157 | // Divide up the encrypted file list for threading |
| 158 | while ((de = readdir(d)) != NULL) { |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 159 | FileName = tardir + "/" + de->d_name; |
| 160 | |
| 161 | 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] | 162 | continue; |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 163 | if (de->d_type == DT_DIR) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 164 | item_len = strlen(de->d_name); |
| 165 | if (userdata_encryption && ((item_len >= 3 && strncmp(de->d_name, "app", 3) == 0) || (item_len >= 6 && strncmp(de->d_name, "dalvik", 6) == 0))) { |
| 166 | // Do nothing, we added these to RegularList earlier |
| 167 | } else { |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 168 | FileName = tardir + "/" + de->d_name; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 169 | if (Generate_TarList(FileName, &EncryptList, &target_size, &enc_thread_id) < 0) { |
| 170 | LOGERR("Error in Generate_TarList with encrypted list!\n"); |
| 171 | closedir(d); |
| 172 | _exit(-1); |
| 173 | } |
| 174 | } |
| 175 | } else if (de->d_type == DT_REG || de->d_type == DT_LNK) { |
| 176 | stat(FileName.c_str(), &st); |
| 177 | if (de->d_type == DT_REG) |
| 178 | Archive_Current_Size += (unsigned long long)(st.st_size); |
| 179 | TarItem.fn = FileName; |
| 180 | TarItem.thread_id = enc_thread_id; |
| 181 | EncryptList.push_back(TarItem); |
| 182 | } |
| 183 | } |
| 184 | closedir(d); |
| 185 | if (enc_thread_id != core_count) { |
| 186 | LOGERR("Error dividing up threads for encryption, %i threads for %i cores!\n", enc_thread_id, core_count); |
| 187 | if (enc_thread_id > core_count) |
| 188 | _exit(-1); |
| 189 | else |
| 190 | LOGERR("Continuining anyway."); |
| 191 | } |
| 192 | |
| 193 | if (userdata_encryption) { |
| 194 | // Create a backup of unencrypted data |
| 195 | reg.setfn(tarfn); |
| 196 | reg.ItemList = &RegularList; |
| 197 | reg.thread_id = 0; |
| 198 | reg.use_encryption = 0; |
| 199 | reg.use_compression = use_compression; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 200 | reg.split_archives = 1; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 201 | LOGINFO("Creating unencrypted backup...\n"); |
| 202 | if (createList((void*)®) != 0) { |
| 203 | LOGERR("Error creating unencrypted backup.\n"); |
| 204 | _exit(-1); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if (pthread_attr_init(&tattr)) { |
| 209 | LOGERR("Unable to pthread_attr_init\n"); |
| 210 | _exit(-1); |
| 211 | } |
| 212 | if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) { |
| 213 | LOGERR("Error setting pthread_attr_setdetachstate\n"); |
| 214 | _exit(-1); |
| 215 | } |
| 216 | if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) { |
| 217 | LOGERR("Error setting pthread_attr_setscope\n"); |
| 218 | _exit(-1); |
| 219 | } |
| 220 | /*if (pthread_attr_setstacksize(&tattr, 524288)) { |
| 221 | LOGERR("Error setting pthread_attr_setstacksize\n"); |
| 222 | _exit(-1); |
| 223 | }*/ |
| 224 | |
| 225 | // Create threads for the divided up encryption lists |
| 226 | for (i = start_thread_id; i <= core_count; i++) { |
| 227 | enc[i].setdir(tardir); |
| 228 | enc[i].setfn(tarfn); |
| 229 | enc[i].ItemList = &EncryptList; |
| 230 | enc[i].thread_id = i; |
| 231 | enc[i].use_encryption = use_encryption; |
Ethan Yonker | 924a80b | 2014-04-02 10:54:12 -0500 | [diff] [blame] | 232 | enc[i].setpassword(password); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 233 | enc[i].use_compression = use_compression; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 234 | enc[i].split_archives = 1; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 235 | LOGINFO("Start encryption thread %i\n", i); |
| 236 | ret = pthread_create(&enc_thread[i], &tattr, createList, (void*)&enc[i]); |
| 237 | if (ret) { |
| 238 | LOGINFO("Unable to create %i thread for encryption! %i\nContinuing in same thread (backup will be slower).", i, ret); |
| 239 | if (createList((void*)&enc[i]) != 0) { |
| 240 | LOGERR("Error creating encrypted backup %i.\n", i); |
| 241 | _exit(-1); |
| 242 | } else { |
| 243 | enc[i].thread_id = i + 1; |
| 244 | } |
| 245 | } |
| 246 | usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason. |
| 247 | } |
| 248 | if (pthread_attr_destroy(&tattr)) { |
| 249 | LOGERR("Failed to pthread_attr_destroy\n"); |
| 250 | } |
| 251 | for (i = start_thread_id; i <= core_count; i++) { |
| 252 | if (enc[i].thread_id == i) { |
| 253 | if (pthread_join(enc_thread[i], &thread_return)) { |
| 254 | LOGERR("Error joining thread %i\n", i); |
| 255 | _exit(-1); |
| 256 | } else { |
| 257 | LOGINFO("Joined thread %i.\n", i); |
| 258 | ret = (int)thread_return; |
| 259 | if (ret != 0) { |
| 260 | thread_error = 1; |
| 261 | LOGERR("Thread %i returned an error %i.\n", i, ret); |
| 262 | _exit(-1); |
| 263 | } |
| 264 | } |
| 265 | } else { |
| 266 | LOGINFO("Skipping joining thread %i because of pthread failure.\n", i); |
| 267 | } |
| 268 | } |
| 269 | if (thread_error) { |
| 270 | LOGERR("Error returned by one or more threads.\n"); |
| 271 | _exit(-1); |
| 272 | } |
| 273 | LOGINFO("Finished encrypted backup.\n"); |
| 274 | _exit(0); |
| 275 | } else { |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 276 | std::vector<TarListStruct> FileList; |
| 277 | unsigned thread_id = 0; |
| 278 | unsigned long long target_size = 0; |
| 279 | twrpTar reg; |
| 280 | |
| 281 | // Generate list of files to back up |
| 282 | if (Generate_TarList(tardir, &FileList, &target_size, &thread_id) < 0) { |
| 283 | LOGERR("Error in Generate_TarList!\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 284 | _exit(-1); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 285 | } |
| 286 | // Create a backup |
| 287 | reg.setfn(tarfn); |
| 288 | reg.ItemList = &FileList; |
| 289 | reg.thread_id = 0; |
| 290 | reg.use_encryption = 0; |
| 291 | reg.use_compression = use_compression; |
| 292 | reg.setsize(Total_Backup_Size); |
| 293 | if (Total_Backup_Size > MAX_ARCHIVE_SIZE) { |
| 294 | gui_print("Breaking backup file into multiple archives...\n"); |
| 295 | reg.split_archives = 1; |
| 296 | } else { |
| 297 | reg.split_archives = 0; |
| 298 | } |
| 299 | LOGINFO("Creating backup...\n"); |
| 300 | if (createList((void*)®) != 0) { |
| 301 | LOGERR("Error creating backup.\n"); |
| 302 | _exit(-1); |
| 303 | } |
| 304 | _exit(0); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 305 | } |
| 306 | } else { |
| 307 | if (TWFunc::Wait_For_Child(pid, &status, "createTarFork()") != 0) |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 308 | return -1; |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 309 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 313 | int twrpTar::extractTarFork() { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 314 | int status = 0; |
| 315 | pid_t pid, rc_pid; |
| 316 | |
| 317 | pid = fork(); |
| 318 | if (pid >= 0) // fork was successful |
| 319 | { |
| 320 | if (pid == 0) // child process |
| 321 | { |
| 322 | if (TWFunc::Path_Exists(tarfn)) { |
| 323 | LOGINFO("Single archive\n"); |
| 324 | if (extract() != 0) |
| 325 | _exit(-1); |
| 326 | else |
| 327 | _exit(0); |
| 328 | } else { |
| 329 | LOGINFO("Multiple archives\n"); |
| 330 | string temp; |
| 331 | char actual_filename[255]; |
| 332 | twrpTar tars[9]; |
| 333 | pthread_t tar_thread[9]; |
| 334 | pthread_attr_t tattr; |
| 335 | int thread_count = 0, i, start_thread_id = 1, ret, thread_error = 0; |
| 336 | void *thread_return; |
| 337 | |
| 338 | basefn = tarfn; |
| 339 | temp = basefn + "%i%02i"; |
| 340 | tarfn += "000"; |
| 341 | if (!TWFunc::Path_Exists(tarfn)) { |
| 342 | LOGERR("Unable to locate '%s' or '%s'\n", basefn.c_str(), tarfn.c_str()); |
| 343 | _exit(-1); |
| 344 | } |
| 345 | if (TWFunc::Get_File_Type(tarfn) != 2) { |
| 346 | LOGINFO("First tar file '%s' not encrypted\n", tarfn.c_str()); |
| 347 | tars[0].basefn = basefn; |
| 348 | tars[0].thread_id = 0; |
| 349 | if (extractMulti((void*)&tars[0]) != 0) { |
| 350 | LOGERR("Error extracting split archive.\n"); |
| 351 | _exit(-1); |
| 352 | } |
| 353 | } else { |
| 354 | start_thread_id = 0; |
| 355 | } |
| 356 | // Start threading encrypted restores |
| 357 | if (pthread_attr_init(&tattr)) { |
| 358 | LOGERR("Unable to pthread_attr_init\n"); |
| 359 | _exit(-1); |
| 360 | } |
| 361 | if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) { |
| 362 | LOGERR("Error setting pthread_attr_setdetachstate\n"); |
| 363 | _exit(-1); |
| 364 | } |
| 365 | if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) { |
| 366 | LOGERR("Error setting pthread_attr_setscope\n"); |
| 367 | _exit(-1); |
| 368 | } |
| 369 | /*if (pthread_attr_setstacksize(&tattr, 524288)) { |
| 370 | LOGERR("Error setting pthread_attr_setstacksize\n"); |
| 371 | _exit(-1); |
| 372 | }*/ |
| 373 | for (i = start_thread_id; i < 9; i++) { |
| 374 | sprintf(actual_filename, temp.c_str(), i, 0); |
| 375 | if (TWFunc::Path_Exists(actual_filename)) { |
| 376 | thread_count++; |
| 377 | tars[i].basefn = basefn; |
Ethan Yonker | 924a80b | 2014-04-02 10:54:12 -0500 | [diff] [blame] | 378 | tars[i].setpassword(password); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 379 | tars[i].thread_id = i; |
| 380 | LOGINFO("Creating extract thread ID %i\n", i); |
| 381 | ret = pthread_create(&tar_thread[i], &tattr, extractMulti, (void*)&tars[i]); |
| 382 | if (ret) { |
| 383 | LOGINFO("Unable to create %i thread for extraction! %i\nContinuing in same thread (restore will be slower).", i, ret); |
| 384 | if (extractMulti((void*)&tars[i]) != 0) { |
| 385 | LOGERR("Error extracting backup in thread %i.\n", i); |
| 386 | _exit(-1); |
| 387 | } else { |
| 388 | tars[i].thread_id = i + 1; |
| 389 | } |
| 390 | } |
| 391 | usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason. |
| 392 | } else { |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | for (i = start_thread_id; i < thread_count + start_thread_id; i++) { |
| 397 | if (tars[i].thread_id == i) { |
| 398 | if (pthread_join(tar_thread[i], &thread_return)) { |
| 399 | LOGERR("Error joining thread %i\n", i); |
| 400 | _exit(-1); |
| 401 | } else { |
| 402 | LOGINFO("Joined thread %i.\n", i); |
| 403 | ret = (int)thread_return; |
| 404 | if (ret != 0) { |
| 405 | thread_error = 1; |
| 406 | LOGERR("Thread %i returned an error %i.\n", i, ret); |
| 407 | _exit(-1); |
| 408 | } |
| 409 | } |
| 410 | } else { |
| 411 | LOGINFO("Skipping joining thread %i because of pthread failure.\n", i); |
| 412 | } |
| 413 | } |
| 414 | if (thread_error) { |
| 415 | LOGERR("Error returned by one or more threads.\n"); |
| 416 | _exit(-1); |
| 417 | } |
| 418 | LOGINFO("Finished encrypted backup.\n"); |
| 419 | _exit(0); |
| 420 | } |
| 421 | } |
| 422 | else // parent process |
| 423 | { |
| 424 | if (TWFunc::Wait_For_Child(pid, &status, "extractTarFork()") != 0) |
| 425 | return -1; |
| 426 | } |
| 427 | } |
| 428 | else // fork has failed |
| 429 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 430 | LOGINFO("extract tar failed to fork.\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 431 | return -1; |
| 432 | } |
| 433 | return 0; |
| 434 | } |
| 435 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 436 | int twrpTar::Generate_TarList(string Path, std::vector<TarListStruct> *TarList, unsigned long long *Target_Size, unsigned *thread_id) { |
| 437 | DIR* d; |
| 438 | struct dirent* de; |
| 439 | struct stat st; |
| 440 | string FileName; |
| 441 | struct TarListStruct TarItem; |
| 442 | string::size_type i; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 443 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 444 | d = opendir(Path.c_str()); |
| 445 | if (d == NULL) { |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 446 | LOGERR("Error opening '%s' -- error: %s\n", Path.c_str(), strerror(errno)); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 447 | closedir(d); |
| 448 | return -1; |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 449 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 450 | while ((de = readdir(d)) != NULL) { |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 451 | FileName = Path + "/" + de->d_name; |
Matt Mower | bb81e5d | 2014-03-20 18:05:41 -0500 | [diff] [blame] | 452 | |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 453 | 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] | 454 | continue; |
| 455 | TarItem.fn = FileName; |
| 456 | TarItem.thread_id = *thread_id; |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 457 | if (de->d_type == DT_DIR) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 458 | TarList->push_back(TarItem); |
| 459 | if (Generate_TarList(FileName, TarList, Target_Size, thread_id) < 0) |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 460 | return -1; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 461 | } else if (de->d_type == DT_REG || de->d_type == DT_LNK) { |
| 462 | stat(FileName.c_str(), &st); |
| 463 | TarList->push_back(TarItem); |
| 464 | if (de->d_type == DT_REG) |
| 465 | Archive_Current_Size += st.st_size; |
| 466 | if (Archive_Current_Size != 0 && *Target_Size != 0 && Archive_Current_Size > *Target_Size) { |
| 467 | *thread_id = *thread_id + 1; |
| 468 | Archive_Current_Size = 0; |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 472 | closedir(d); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 473 | return 0; |
| 474 | } |
| 475 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 476 | int twrpTar::extractTar() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 477 | char* charRootDir = (char*) tardir.c_str(); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 478 | if (openTar() == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 479 | return -1; |
| 480 | if (tar_extract_all(t, charRootDir) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 481 | LOGERR("Unable to extract tar archive '%s'\n", tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 482 | return -1; |
| 483 | } |
| 484 | if (tar_close(t) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 485 | LOGERR("Unable to close tar file\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 486 | return -1; |
| 487 | } |
| 488 | return 0; |
| 489 | } |
| 490 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 491 | int twrpTar::extract() { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 492 | Archive_Current_Type = TWFunc::Get_File_Type(tarfn); |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 493 | |
| 494 | if (Archive_Current_Type == 1) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 495 | //if you return the extractTGZ function directly, stack crashes happen |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 496 | LOGINFO("Extracting gzipped tar\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 497 | int ret = extractTar(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 498 | return ret; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 499 | } else if (Archive_Current_Type == 2) { |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 500 | int ret = TWFunc::Try_Decrypting_File(tarfn, password); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 501 | if (ret < 1) { |
| 502 | LOGERR("Failed to decrypt tar file '%s'\n", tarfn.c_str()); |
| 503 | return -1; |
| 504 | } |
| 505 | if (ret == 1) { |
| 506 | LOGERR("Decrypted file is not in tar format.\n"); |
| 507 | return -1; |
| 508 | } |
| 509 | if (ret == 3) { |
| 510 | LOGINFO("Extracting encrypted and compressed tar.\n"); |
| 511 | Archive_Current_Type = 3; |
| 512 | } else |
| 513 | LOGINFO("Extracting encrypted tar.\n"); |
| 514 | return extractTar(); |
| 515 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 516 | LOGINFO("Extracting uncompressed tar\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 517 | return extractTar(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 521 | int twrpTar::tarList(std::vector<TarListStruct> *TarList, unsigned thread_id) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 522 | struct stat st; |
| 523 | char buf[PATH_MAX]; |
| 524 | int list_size = TarList->size(), i = 0, archive_count = 0; |
| 525 | string temp; |
| 526 | char actual_filename[PATH_MAX]; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 527 | char *ptr; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 528 | |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 529 | if (split_archives) { |
| 530 | basefn = tarfn; |
| 531 | temp = basefn + "%i%02i"; |
| 532 | sprintf(actual_filename, temp.c_str(), thread_id, archive_count); |
| 533 | tarfn = actual_filename; |
| 534 | include_root_dir = true; |
| 535 | } else { |
| 536 | include_root_dir = false; |
| 537 | } |
| 538 | LOGINFO("Creating tar file '%s'\n", tarfn.c_str()); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 539 | if (createTar() != 0) { |
| 540 | LOGERR("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id); |
| 541 | return -2; |
| 542 | } |
| 543 | Archive_Current_Size = 0; |
| 544 | |
| 545 | while (i < list_size) { |
| 546 | if (TarList->at(i).thread_id == thread_id) { |
| 547 | strcpy(buf, TarList->at(i).fn.c_str()); |
bigbiff bigbiff | ec8fc28 | 2014-03-16 20:32:50 -0400 | [diff] [blame] | 548 | lstat(buf, &st); |
| 549 | if (S_ISREG(st.st_mode)) { // item is a regular file |
Dees Troy | b1c6e05 | 2014-04-07 18:01:41 +0000 | [diff] [blame] | 550 | if (split_archives && Archive_Current_Size + (unsigned long long)(st.st_size) > MAX_ARCHIVE_SIZE) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 551 | if (closeTar() != 0) { |
| 552 | LOGERR("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id); |
| 553 | return -3; |
| 554 | } |
| 555 | archive_count++; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 556 | 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] | 557 | if (archive_count > 99) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 558 | LOGERR("Too many archives for thread %i\n", thread_id); |
| 559 | return -4; |
| 560 | } |
| 561 | sprintf(actual_filename, temp.c_str(), thread_id, archive_count); |
| 562 | tarfn = actual_filename; |
| 563 | if (createTar() != 0) { |
| 564 | LOGERR("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id); |
| 565 | return -2; |
| 566 | } |
| 567 | Archive_Current_Size = 0; |
| 568 | } |
| 569 | Archive_Current_Size += (unsigned long long)(st.st_size); |
| 570 | } |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 571 | LOGINFO("addFile '%s' including root: %i\n", buf, include_root_dir); |
| 572 | if (addFile(buf, include_root_dir) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 573 | LOGERR("Error adding file '%s' to '%s'\n", buf, tarfn.c_str()); |
| 574 | return -1; |
| 575 | } |
| 576 | } |
| 577 | i++; |
| 578 | } |
| 579 | if (closeTar() != 0) { |
| 580 | LOGERR("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id); |
| 581 | return -3; |
| 582 | } |
| 583 | LOGINFO("Thread id %i tarList done, %i archives.\n", thread_id, archive_count, i, list_size); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 584 | return 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 585 | } |
| 586 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 587 | void* twrpTar::createList(void *cookie) { |
| 588 | |
| 589 | twrpTar* threadTar = (twrpTar*) cookie; |
Ethan Yonker | eae4209 | 2014-03-07 15:33:13 -0600 | [diff] [blame] | 590 | if (threadTar->tarList(threadTar->ItemList, threadTar->thread_id) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 591 | LOGINFO("ERROR tarList for thread ID %i\n", threadTar->thread_id); |
| 592 | return (void*)-2; |
| 593 | } |
| 594 | LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id); |
| 595 | return (void*)0; |
| 596 | } |
| 597 | |
| 598 | void* twrpTar::extractMulti(void *cookie) { |
| 599 | |
| 600 | twrpTar* threadTar = (twrpTar*) cookie; |
| 601 | int archive_count = 0; |
| 602 | string temp = threadTar->basefn + "%i%02i"; |
| 603 | char actual_filename[255]; |
| 604 | sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count); |
| 605 | while (TWFunc::Path_Exists(actual_filename)) { |
| 606 | threadTar->tarfn = actual_filename; |
| 607 | if (threadTar->extract() != 0) { |
| 608 | LOGINFO("Error extracting '%s' in thread ID %i\n", actual_filename, threadTar->thread_id); |
| 609 | return (void*)-2; |
| 610 | } |
| 611 | archive_count++; |
| 612 | if (archive_count > 99) |
| 613 | break; |
| 614 | sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count); |
| 615 | } |
| 616 | LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id); |
| 617 | return (void*)0; |
| 618 | } |
| 619 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 620 | int twrpTar::addFilesToExistingTar(vector <string> files, string fn) { |
| 621 | char* charTarFile = (char*) fn.c_str(); |
| 622 | |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 623 | 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] | 624 | return -1; |
| 625 | removeEOT(charTarFile); |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 626 | 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] | 627 | return -1; |
| 628 | for (unsigned int i = 0; i < files.size(); ++i) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 629 | char* file = (char*) files.at(i).c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 630 | if (tar_append_file(t, file, file) == -1) |
| 631 | return -1; |
| 632 | } |
| 633 | if (tar_append_eof(t) == -1) |
| 634 | return -1; |
| 635 | if (tar_close(t) == -1) |
| 636 | return -1; |
| 637 | return 0; |
| 638 | } |
| 639 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 640 | int twrpTar::createTar() { |
| 641 | char* charTarFile = (char*) tarfn.c_str(); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 642 | char* charRootDir = (char*) tardir.c_str(); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 643 | static tartype_t type = { open, close, read, write_tar }; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 644 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 645 | if (use_encryption && use_compression) { |
| 646 | // Compressed and encrypted |
| 647 | Archive_Current_Type = 3; |
| 648 | LOGINFO("Using encryption and compression...\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 649 | int i, pipes[4]; |
| 650 | |
| 651 | if (pipe(pipes) < 0) { |
| 652 | LOGERR("Error creating first pipe\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 653 | return -1; |
| 654 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 655 | if (pipe(pipes + 2) < 0) { |
| 656 | LOGERR("Error creating second pipe\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 657 | return -1; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 658 | } |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 659 | 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); |
| 660 | if (output_fd < 0) { |
| 661 | LOGERR("Failed to open '%s'\n", tarfn.c_str()); |
| 662 | for (i = 0; i < 4; i++) |
| 663 | close(pipes[i]); // close all |
| 664 | return -1; |
| 665 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 666 | pigz_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 667 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 668 | if (pigz_pid < 0) { |
| 669 | LOGERR("pigz fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 670 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 671 | for (i = 0; i < 4; i++) |
| 672 | close(pipes[i]); // close all |
| 673 | return -1; |
| 674 | } else if (pigz_pid == 0) { |
| 675 | // pigz Child |
| 676 | close(pipes[1]); |
| 677 | close(pipes[2]); |
| 678 | close(0); |
| 679 | dup2(pipes[0], 0); |
| 680 | close(1); |
| 681 | dup2(pipes[3], 1); |
| 682 | if (execlp("pigz", "pigz", "-", NULL) < 0) { |
| 683 | LOGERR("execlp pigz ERROR!\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 684 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 685 | close(pipes[0]); |
| 686 | close(pipes[3]); |
| 687 | _exit(-1); |
| 688 | } |
| 689 | } else { |
| 690 | // Parent |
| 691 | oaes_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 692 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 693 | if (oaes_pid < 0) { |
| 694 | LOGERR("openaes fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 695 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 696 | for (i = 0; i < 4; i++) |
| 697 | close(pipes[i]); // close all |
| 698 | return -1; |
| 699 | } else if (oaes_pid == 0) { |
| 700 | // openaes Child |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 701 | close(pipes[0]); |
| 702 | close(pipes[1]); |
| 703 | close(pipes[3]); |
| 704 | close(0); |
| 705 | dup2(pipes[2], 0); |
| 706 | close(1); |
| 707 | dup2(output_fd, 1); |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 708 | if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 709 | LOGERR("execlp openaes ERROR!\n"); |
| 710 | close(pipes[2]); |
| 711 | close(output_fd); |
| 712 | _exit(-1); |
| 713 | } |
| 714 | } else { |
| 715 | // Parent |
| 716 | close(pipes[0]); |
| 717 | close(pipes[2]); |
| 718 | close(pipes[3]); |
| 719 | fd = pipes[1]; |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 720 | 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] | 721 | close(fd); |
| 722 | LOGERR("tar_fdopen failed\n"); |
| 723 | return -1; |
| 724 | } |
| 725 | return 0; |
| 726 | } |
| 727 | } |
| 728 | } else if (use_compression) { |
| 729 | // Compressed |
| 730 | Archive_Current_Type = 1; |
| 731 | LOGINFO("Using compression...\n"); |
| 732 | int pigzfd[2]; |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 733 | 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); |
| 734 | if (output_fd < 0) { |
| 735 | LOGERR("Failed to open '%s'\n", tarfn.c_str()); |
| 736 | close(pigzfd[0]); |
| 737 | return -1; |
| 738 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 739 | |
| 740 | if (pipe(pigzfd) < 0) { |
| 741 | LOGERR("Error creating pipe\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 742 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 743 | return -1; |
| 744 | } |
| 745 | pigz_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 746 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 747 | if (pigz_pid < 0) { |
| 748 | LOGERR("fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 749 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 750 | close(pigzfd[0]); |
| 751 | close(pigzfd[1]); |
| 752 | return -1; |
| 753 | } else if (pigz_pid == 0) { |
| 754 | // Child |
| 755 | close(pigzfd[1]); // close unused output pipe |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 756 | dup2(pigzfd[0], 0); // remap stdin |
| 757 | dup2(output_fd, 1); // remap stdout to output file |
| 758 | if (execlp("pigz", "pigz", "-", NULL) < 0) { |
| 759 | LOGERR("execlp pigz ERROR!\n"); |
| 760 | close(output_fd); |
| 761 | close(pigzfd[0]); |
| 762 | _exit(-1); |
| 763 | } |
| 764 | } else { |
| 765 | // Parent |
| 766 | close(pigzfd[0]); // close parent input |
| 767 | fd = pigzfd[1]; // copy parent output |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 768 | 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] | 769 | close(fd); |
| 770 | LOGERR("tar_fdopen failed\n"); |
| 771 | return -1; |
| 772 | } |
| 773 | } |
| 774 | } else if (use_encryption) { |
| 775 | // Encrypted |
| 776 | Archive_Current_Type = 2; |
| 777 | LOGINFO("Using encryption...\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 778 | int oaesfd[2]; |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 779 | 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); |
| 780 | if (output_fd < 0) { |
| 781 | LOGERR("Failed to open '%s'\n", tarfn.c_str()); |
| 782 | return -1; |
| 783 | } |
| 784 | if (pipe(oaesfd) < 0) { |
| 785 | LOGERR("Error creating pipe\n"); |
| 786 | close(output_fd); |
| 787 | return -1; |
| 788 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 789 | oaes_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 790 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 791 | if (oaes_pid < 0) { |
| 792 | LOGERR("fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 793 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 794 | close(oaesfd[0]); |
| 795 | close(oaesfd[1]); |
| 796 | return -1; |
| 797 | } else if (oaes_pid == 0) { |
| 798 | // Child |
| 799 | close(oaesfd[1]); // close unused |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 800 | dup2(oaesfd[0], 0); // remap stdin |
| 801 | dup2(output_fd, 1); // remap stdout to output file |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 802 | if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 803 | LOGERR("execlp openaes ERROR!\n"); |
| 804 | close(output_fd); |
| 805 | close(oaesfd[0]); |
| 806 | _exit(-1); |
| 807 | } |
| 808 | } else { |
| 809 | // Parent |
| 810 | close(oaesfd[0]); // close parent input |
| 811 | fd = oaesfd[1]; // copy parent output |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 812 | 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] | 813 | close(fd); |
| 814 | LOGERR("tar_fdopen failed\n"); |
| 815 | return -1; |
| 816 | } |
| 817 | return 0; |
| 818 | } |
| 819 | } else { |
| 820 | // Not compressed or encrypted |
| 821 | init_libtar_buffer(0); |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 822 | 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] | 823 | LOGERR("tar_open error opening '%s'\n", tarfn.c_str()); |
| 824 | return -1; |
| 825 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 826 | } |
| 827 | return 0; |
| 828 | } |
| 829 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 830 | int twrpTar::openTar() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 831 | char* charRootDir = (char*) tardir.c_str(); |
| 832 | char* charTarFile = (char*) tarfn.c_str(); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 833 | string Password; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 834 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 835 | if (Archive_Current_Type == 3) { |
| 836 | LOGINFO("Opening encrypted and compressed backup...\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 837 | int i, pipes[4]; |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 838 | int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE); |
| 839 | if (input_fd < 0) { |
| 840 | LOGERR("Failed to open '%s'\n", tarfn.c_str()); |
| 841 | return -1; |
| 842 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 843 | |
| 844 | if (pipe(pipes) < 0) { |
| 845 | LOGERR("Error creating first pipe\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 846 | close(input_fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 847 | return -1; |
| 848 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 849 | if (pipe(pipes + 2) < 0) { |
| 850 | LOGERR("Error creating second pipe\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 851 | close(pipes[0]); |
| 852 | close(pipes[1]); |
| 853 | close(input_fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 854 | return -1; |
| 855 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 856 | oaes_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 857 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 858 | if (oaes_pid < 0) { |
| 859 | LOGERR("pigz fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 860 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 861 | for (i = 0; i < 4; i++) |
| 862 | close(pipes[i]); // close all |
| 863 | return -1; |
| 864 | } else if (oaes_pid == 0) { |
| 865 | // openaes Child |
| 866 | close(pipes[0]); // Close pipes that are not used by this child |
| 867 | close(pipes[2]); |
| 868 | close(pipes[3]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 869 | close(0); |
| 870 | dup2(input_fd, 0); |
| 871 | close(1); |
| 872 | dup2(pipes[1], 1); |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 873 | if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 874 | LOGERR("execlp openaes ERROR!\n"); |
| 875 | close(input_fd); |
| 876 | close(pipes[1]); |
| 877 | _exit(-1); |
| 878 | } |
| 879 | } else { |
| 880 | // Parent |
| 881 | pigz_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 882 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 883 | if (pigz_pid < 0) { |
| 884 | LOGERR("openaes fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 885 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 886 | for (i = 0; i < 4; i++) |
| 887 | close(pipes[i]); // close all |
| 888 | return -1; |
| 889 | } else if (pigz_pid == 0) { |
| 890 | // pigz Child |
| 891 | close(pipes[1]); // Close pipes not used by this child |
| 892 | close(pipes[2]); |
| 893 | close(0); |
| 894 | dup2(pipes[0], 0); |
| 895 | close(1); |
| 896 | dup2(pipes[3], 1); |
| 897 | if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) { |
| 898 | LOGERR("execlp pigz ERROR!\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 899 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 900 | close(pipes[0]); |
| 901 | close(pipes[3]); |
| 902 | _exit(-1); |
| 903 | } |
| 904 | } else { |
| 905 | // Parent |
| 906 | close(pipes[0]); // Close pipes not used by parent |
| 907 | close(pipes[1]); |
| 908 | close(pipes[3]); |
| 909 | fd = pipes[2]; |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 910 | 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] | 911 | close(fd); |
| 912 | LOGERR("tar_fdopen failed\n"); |
| 913 | return -1; |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | } else if (Archive_Current_Type == 2) { |
| 918 | LOGINFO("Opening encrypted backup...\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 919 | int oaesfd[2]; |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 920 | int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE); |
| 921 | if (input_fd < 0) { |
| 922 | LOGERR("Failed to open '%s'\n", tarfn.c_str()); |
| 923 | return -1; |
| 924 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 925 | |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 926 | if (pipe(oaesfd) < 0) { |
| 927 | LOGERR("Error creating pipe\n"); |
| 928 | close(input_fd); |
| 929 | return -1; |
| 930 | } |
| 931 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 932 | oaes_pid = fork(); |
| 933 | if (oaes_pid < 0) { |
| 934 | LOGERR("fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 935 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 936 | close(oaesfd[0]); |
| 937 | close(oaesfd[1]); |
| 938 | return -1; |
| 939 | } else if (oaes_pid == 0) { |
| 940 | // Child |
| 941 | close(oaesfd[0]); // Close unused pipe |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 942 | close(0); // close stdin |
| 943 | dup2(oaesfd[1], 1); // remap stdout |
| 944 | dup2(input_fd, 0); // remap input fd to stdin |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 945 | if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 946 | LOGERR("execlp openaes ERROR!\n"); |
| 947 | close(input_fd); |
| 948 | close(oaesfd[1]); |
| 949 | _exit(-1); |
| 950 | } |
| 951 | } else { |
| 952 | // Parent |
| 953 | close(oaesfd[1]); // close parent output |
| 954 | fd = oaesfd[0]; // copy parent input |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 955 | 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] | 956 | close(fd); |
| 957 | LOGERR("tar_fdopen failed\n"); |
| 958 | return -1; |
| 959 | } |
| 960 | } |
| 961 | } else if (Archive_Current_Type == 1) { |
| 962 | LOGINFO("Opening as a gzip...\n"); |
| 963 | int pigzfd[2]; |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 964 | int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE); |
| 965 | if (input_fd < 0) { |
| 966 | LOGERR("Failed to open '%s'\n", tarfn.c_str()); |
| 967 | return -1; |
| 968 | } |
| 969 | if (pipe(pigzfd) < 0) { |
| 970 | LOGERR("Error creating pipe\n"); |
| 971 | close(input_fd); |
| 972 | return -1; |
| 973 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 974 | |
| 975 | pigz_pid = fork(); |
| 976 | if (pigz_pid < 0) { |
| 977 | LOGERR("fork() failed\n"); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 978 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 979 | close(pigzfd[0]); |
| 980 | close(pigzfd[1]); |
| 981 | return -1; |
| 982 | } else if (pigz_pid == 0) { |
| 983 | // Child |
| 984 | close(pigzfd[0]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 985 | dup2(input_fd, 0); // remap input fd to stdin |
| 986 | dup2(pigzfd[1], 1); // remap stdout |
| 987 | if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) { |
| 988 | close(pigzfd[1]); |
| 989 | close(input_fd); |
| 990 | LOGERR("execlp openaes ERROR!\n"); |
| 991 | _exit(-1); |
| 992 | } |
| 993 | } else { |
| 994 | // Parent |
| 995 | close(pigzfd[1]); // close parent output |
| 996 | fd = pigzfd[0]; // copy parent input |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 997 | 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] | 998 | close(fd); |
| 999 | LOGERR("tar_fdopen failed\n"); |
| 1000 | return -1; |
| 1001 | } |
| 1002 | } |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 1003 | } 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] | 1004 | LOGERR("Unable to open tar archive '%s'\n", charTarFile); |
| 1005 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1006 | } |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | string twrpTar::Strip_Root_Dir(string Path) { |
| 1011 | string temp; |
| 1012 | size_t slash; |
| 1013 | |
| 1014 | if (Path.substr(0, 1) == "/") |
| 1015 | temp = Path.substr(1, Path.size() - 1); |
| 1016 | else |
| 1017 | temp = Path; |
| 1018 | slash = temp.find("/"); |
| 1019 | if (slash == string::npos) |
| 1020 | return temp; |
| 1021 | else { |
| 1022 | string stripped; |
| 1023 | |
| 1024 | stripped = temp.substr(slash, temp.size() - slash); |
| 1025 | return stripped; |
| 1026 | } |
| 1027 | return temp; |
| 1028 | } |
| 1029 | |
| 1030 | int twrpTar::addFile(string fn, bool include_root) { |
| 1031 | char* charTarFile = (char*) fn.c_str(); |
| 1032 | if (include_root) { |
| 1033 | if (tar_append_file(t, charTarFile, NULL) == -1) |
| 1034 | return -1; |
| 1035 | } else { |
| 1036 | string temp = Strip_Root_Dir(fn); |
| 1037 | char* charTarPath = (char*) temp.c_str(); |
| 1038 | if (tar_append_file(t, charTarFile, charTarPath) == -1) |
| 1039 | return -1; |
| 1040 | } |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1044 | int twrpTar::closeTar() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 1045 | flush_libtar_buffer(t->fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1046 | if (tar_append_eof(t) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1047 | LOGERR("tar_append_eof(): %s\n", strerror(errno)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1048 | tar_close(t); |
| 1049 | return -1; |
| 1050 | } |
| 1051 | if (tar_close(t) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1052 | LOGERR("Unable to close tar archive: '%s'\n", tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1053 | return -1; |
| 1054 | } |
Dees_Troy | 2727b99 | 2013-08-14 20:09:30 +0000 | [diff] [blame] | 1055 | if (Archive_Current_Type > 0) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1056 | close(fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1057 | int status; |
| 1058 | if (pigz_pid > 0 && TWFunc::Wait_For_Child(pigz_pid, &status, "pigz") != 0) |
| 1059 | return -1; |
| 1060 | if (oaes_pid > 0 && TWFunc::Wait_For_Child(oaes_pid, &status, "openaes") != 0) |
| 1061 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1062 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1063 | free_libtar_buffer(); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 1064 | if (use_compression && !use_encryption) { |
| 1065 | string gzname = tarfn + ".gz"; |
| 1066 | if (TWFunc::Path_Exists(gzname)) { |
| 1067 | rename(gzname.c_str(), tarfn.c_str()); |
| 1068 | } |
| 1069 | } |
| 1070 | if (TWFunc::Get_File_Size(tarfn) == 0) { |
| 1071 | LOGERR("Backup file size for '%s' is 0 bytes.\n", tarfn.c_str()); |
| 1072 | return -1; |
| 1073 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1074 | return 0; |
| 1075 | } |
| 1076 | |
| 1077 | int twrpTar::removeEOT(string tarFile) { |
| 1078 | char* charTarFile = (char*) tarFile.c_str(); |
| 1079 | off_t tarFileEnd; |
| 1080 | while (th_read(t) == 0) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1081 | if (TH_ISREG(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1082 | tar_skip_regfile(t); |
| 1083 | tarFileEnd = lseek(t->fd, 0, SEEK_CUR); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1084 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1085 | if (tar_close(t) == -1) |
| 1086 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1087 | if (truncate(charTarFile, tarFileEnd) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1088 | return -1; |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 1092 | int twrpTar::entryExists(string entry) { |
| 1093 | char* searchstr = (char*)entry.c_str(); |
| 1094 | int ret; |
| 1095 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1096 | Archive_Current_Type = TWFunc::Get_File_Type(tarfn); |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 1097 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1098 | if (openTar() == -1) |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 1099 | ret = 0; |
| 1100 | else |
| 1101 | ret = tar_find(t, searchstr); |
| 1102 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1103 | if (closeTar() != 0) |
| 1104 | LOGINFO("Unable to close tar after searching for entry.\n"); |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 1105 | |
| 1106 | return ret; |
| 1107 | } |
| 1108 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 1109 | extern "C" ssize_t write_tar(int fd, const void *buffer, size_t size) { |
| 1110 | return (ssize_t) write_libtar_buffer(fd, buffer, size); |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame] | 1111 | } |