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