blob: 7f00da5c753bc86782778089e171a75510a597b7 [file] [log] [blame]
Dees Troye0a433a2013-12-02 04:10:37 +00001
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002/*
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05003 Copyright 2013 TeamWin
Dees_Troye34c1332013-02-06 19:13:00 +00004 This file is part of TWRP/TeamWin Recovery Project.
bigbiff bigbiff9c754052013-01-09 09:09:08 -05005
Dees_Troye34c1332013-02-06 19:13:00 +00006 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 bigbiff9c754052013-01-09 09:09:08 -050010
Dees_Troye34c1332013-02-06 19:13:00 +000011 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 bigbiff9c754052013-01-09 09:09:08 -050015
Dees_Troye34c1332013-02-06 19:13:00 +000016 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 bigbiff9c754052013-01-09 09:09:08 -050018*/
19
20extern "C" {
21 #include "libtar/libtar.h"
Dees_Troye34c1332013-02-06 19:13:00 +000022 #include "twrpTar.h"
23 #include "tarWrite.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050024}
25#include <sys/types.h>
26#include <sys/stat.h>
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -050027#include <sys/wait.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050028#include <string.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <fstream>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050032#include <iostream>
33#include <string>
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -050034#include <sstream>
Dees_Troy83bd4832013-05-04 12:39:56 +000035#include <vector>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050036#include <dirent.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040037#include <libgen.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050038#include <sys/mman.h>
39#include "twrpTar.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000040#include "twcommon.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "variables.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050042#include "twrp-functions.hpp"
43
44using namespace std;
45
Dees_Troy83bd4832013-05-04 12:39:56 +000046twrpTar::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 Troye0a433a2013-12-02 04:10:37 +000054 Total_Backup_Size = 0;
55 include_root_dir = true;
Dees_Troy83bd4832013-05-04 12:39:56 +000056}
57
58twrpTar::~twrpTar(void) {
59 // Do nothing
60}
61
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -050062void twrpTar::setfn(string fn) {
63 tarfn = fn;
64}
65
66void twrpTar::setdir(string dir) {
67 tardir = dir;
68}
69
Dees Troye0a433a2013-12-02 04:10:37 +000070void twrpTar::setsize(unsigned long long backup_size) {
71 Total_Backup_Size = backup_size;
72}
73
Ethan Yonker87af5632014-02-10 11:56:35 -060074void twrpTar::setpassword(string pass) {
75 password = pass;
76}
77
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -050078int twrpTar::createTarFork() {
Dees_Troy83bd4832013-05-04 12:39:56 +000079 int status = 0;
80 pid_t pid, rc_pid;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -050081 if ((pid = fork()) == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +000082 LOGINFO("create tar failed to fork.\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -050083 return -1;
84 }
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -050085 if (pid == 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +000086 // 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 Mower50248ab2014-03-31 15:58:40 -0500117 FileName = tardir + "/" + de->d_name;
118
119 if (de->d_type == DT_BLK || de->d_type == DT_CHR || du.check_skip_dirs(FileName))
Dees_Troy83bd4832013-05-04 12:39:56 +0000120 continue;
Matt Mower50248ab2014-03-31 15:58:40 -0500121 if (de->d_type == DT_DIR) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000122 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, &regular_thread_id) < 0) {
125 LOGERR("Error in Generate_TarList with regular list!\n");
126 closedir(d);
127 _exit(-1);
128 }
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500129 regular_size += du.Get_Folder_Size(FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +0000130 } else {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500131 encrypt_size += du.Get_Folder_Size(FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +0000132 }
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 Mower50248ab2014-03-31 15:58:40 -0500159 FileName = tardir + "/" + de->d_name;
160
161 if (de->d_type == DT_BLK || de->d_type == DT_CHR || du.check_skip_dirs(FileName))
Dees_Troy83bd4832013-05-04 12:39:56 +0000162 continue;
Matt Mower50248ab2014-03-31 15:58:40 -0500163 if (de->d_type == DT_DIR) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000164 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 Mower50248ab2014-03-31 15:58:40 -0500168 FileName = tardir + "/" + de->d_name;
Dees_Troy83bd4832013-05-04 12:39:56 +0000169 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 Troye0a433a2013-12-02 04:10:37 +0000200 reg.split_archives = 1;
Dees_Troy83bd4832013-05-04 12:39:56 +0000201 LOGINFO("Creating unencrypted backup...\n");
202 if (createList((void*)&reg) != 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 Yonker924a80b2014-04-02 10:54:12 -0500232 enc[i].setpassword(password);
Dees_Troy83bd4832013-05-04 12:39:56 +0000233 enc[i].use_compression = use_compression;
Dees Troye0a433a2013-12-02 04:10:37 +0000234 enc[i].split_archives = 1;
Dees_Troy83bd4832013-05-04 12:39:56 +0000235 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 Troye0a433a2013-12-02 04:10:37 +0000276 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_Troy83bd4832013-05-04 12:39:56 +0000284 _exit(-1);
Dees Troye0a433a2013-12-02 04:10:37 +0000285 }
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*)&reg) != 0) {
301 LOGERR("Error creating backup.\n");
302 _exit(-1);
303 }
304 _exit(0);
Dees_Troy83bd4832013-05-04 12:39:56 +0000305 }
306 } else {
307 if (TWFunc::Wait_For_Child(pid, &status, "createTarFork()") != 0)
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500308 return -1;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500309 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500310 return 0;
311}
312
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500313int twrpTar::extractTarFork() {
Dees_Troy83bd4832013-05-04 12:39:56 +0000314 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 Yonker924a80b2014-04-02 10:54:12 -0500378 tars[i].setpassword(password);
Dees_Troy83bd4832013-05-04 12:39:56 +0000379 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_Troy2673cec2013-04-02 20:22:16 +0000430 LOGINFO("extract tar failed to fork.\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500431 return -1;
432 }
433 return 0;
434}
435
Dees_Troy83bd4832013-05-04 12:39:56 +0000436int 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_Troy83bd4832013-05-04 12:39:56 +0000443
Dees_Troy83bd4832013-05-04 12:39:56 +0000444 d = opendir(Path.c_str());
445 if (d == NULL) {
Dees Troye0a433a2013-12-02 04:10:37 +0000446 LOGERR("Error opening '%s' -- error: %s\n", Path.c_str(), strerror(errno));
Dees_Troy83bd4832013-05-04 12:39:56 +0000447 closedir(d);
448 return -1;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500449 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000450 while ((de = readdir(d)) != NULL) {
Matt Mower50248ab2014-03-31 15:58:40 -0500451 FileName = Path + "/" + de->d_name;
Matt Mowerbb81e5d2014-03-20 18:05:41 -0500452
Matt Mower50248ab2014-03-31 15:58:40 -0500453 if (de->d_type == DT_BLK || de->d_type == DT_CHR || du.check_skip_dirs(FileName))
Dees_Troy83bd4832013-05-04 12:39:56 +0000454 continue;
455 TarItem.fn = FileName;
456 TarItem.thread_id = *thread_id;
Matt Mower50248ab2014-03-31 15:58:40 -0500457 if (de->d_type == DT_DIR) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000458 TarList->push_back(TarItem);
459 if (Generate_TarList(FileName, TarList, Target_Size, thread_id) < 0)
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500460 return -1;
Dees_Troy83bd4832013-05-04 12:39:56 +0000461 } 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 bigbiffe6594ab2013-02-17 20:18:31 -0500469 }
470 }
471 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000472 closedir(d);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500473 return 0;
474}
475
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500476int twrpTar::extractTar() {
Dees_Troye34c1332013-02-06 19:13:00 +0000477 char* charRootDir = (char*) tardir.c_str();
Dees_Troy83bd4832013-05-04 12:39:56 +0000478 if (openTar() == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500479 return -1;
480 if (tar_extract_all(t, charRootDir) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000481 LOGERR("Unable to extract tar archive '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500482 return -1;
483 }
484 if (tar_close(t) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000485 LOGERR("Unable to close tar file\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500486 return -1;
487 }
488 return 0;
489}
490
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500491int twrpTar::extract() {
Dees_Troy83bd4832013-05-04 12:39:56 +0000492 Archive_Current_Type = TWFunc::Get_File_Type(tarfn);
n0d33b511632013-03-06 21:14:15 +0200493
494 if (Archive_Current_Type == 1) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500495 //if you return the extractTGZ function directly, stack crashes happen
Dees_Troy2673cec2013-04-02 20:22:16 +0000496 LOGINFO("Extracting gzipped tar\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000497 int ret = extractTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500498 return ret;
Dees_Troy83bd4832013-05-04 12:39:56 +0000499 } else if (Archive_Current_Type == 2) {
Ethan Yonker87af5632014-02-10 11:56:35 -0600500 int ret = TWFunc::Try_Decrypting_File(tarfn, password);
Dees_Troy83bd4832013-05-04 12:39:56 +0000501 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_Troy2673cec2013-04-02 20:22:16 +0000516 LOGINFO("Extracting uncompressed tar\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500517 return extractTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500518 }
519}
520
Dees Troye0a433a2013-12-02 04:10:37 +0000521int twrpTar::tarList(std::vector<TarListStruct> *TarList, unsigned thread_id) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000522 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 Troye0a433a2013-12-02 04:10:37 +0000527 char *ptr;
Dees_Troye34c1332013-02-06 19:13:00 +0000528
Dees Troye0a433a2013-12-02 04:10:37 +0000529 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_Troy83bd4832013-05-04 12:39:56 +0000539 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 bigbiffec8fc282014-03-16 20:32:50 -0400548 lstat(buf, &st);
549 if (S_ISREG(st.st_mode)) { // item is a regular file
Dees Troyb1c6e052014-04-07 18:01:41 +0000550 if (split_archives && Archive_Current_Size + (unsigned long long)(st.st_size) > MAX_ARCHIVE_SIZE) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000551 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 Troye0a433a2013-12-02 04:10:37 +0000556 gui_print("Splitting thread ID %i into archive %i\n", thread_id, archive_count + 1);
Dees_Troy83bd4832013-05-04 12:39:56 +0000557 if (archive_count > 99) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000558 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 Troye0a433a2013-12-02 04:10:37 +0000571 LOGINFO("addFile '%s' including root: %i\n", buf, include_root_dir);
572 if (addFile(buf, include_root_dir) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000573 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_Troye34c1332013-02-06 19:13:00 +0000584 return 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500585}
586
Dees_Troy83bd4832013-05-04 12:39:56 +0000587void* twrpTar::createList(void *cookie) {
588
589 twrpTar* threadTar = (twrpTar*) cookie;
Ethan Yonkereae42092014-03-07 15:33:13 -0600590 if (threadTar->tarList(threadTar->ItemList, threadTar->thread_id) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000591 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
598void* 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 bigbiff9c754052013-01-09 09:09:08 -0500620int twrpTar::addFilesToExistingTar(vector <string> files, string fn) {
621 char* charTarFile = (char*) fn.c_str();
622
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200623 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 bigbiff9c754052013-01-09 09:09:08 -0500624 return -1;
625 removeEOT(charTarFile);
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200626 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 bigbiff9c754052013-01-09 09:09:08 -0500627 return -1;
628 for (unsigned int i = 0; i < files.size(); ++i) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500629 char* file = (char*) files.at(i).c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500630 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 bigbiff3bf2b0e2013-01-21 21:26:43 -0500640int twrpTar::createTar() {
641 char* charTarFile = (char*) tarfn.c_str();
Dees_Troye34c1332013-02-06 19:13:00 +0000642 char* charRootDir = (char*) tardir.c_str();
Dees_Troye34c1332013-02-06 19:13:00 +0000643 static tartype_t type = { open, close, read, write_tar };
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500644
Dees_Troy83bd4832013-05-04 12:39:56 +0000645 if (use_encryption && use_compression) {
646 // Compressed and encrypted
647 Archive_Current_Type = 3;
648 LOGINFO("Using encryption and compression...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000649 int i, pipes[4];
650
651 if (pipe(pipes) < 0) {
652 LOGERR("Error creating first pipe\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500653 return -1;
654 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000655 if (pipe(pipes + 2) < 0) {
656 LOGERR("Error creating second pipe\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500657 return -1;
Dees_Troy83bd4832013-05-04 12:39:56 +0000658 }
Dees Troy9d723272014-04-07 17:13:10 +0000659 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_Troy83bd4832013-05-04 12:39:56 +0000666 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400667
Dees_Troy83bd4832013-05-04 12:39:56 +0000668 if (pigz_pid < 0) {
669 LOGERR("pigz fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +0000670 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000671 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 Troy9d723272014-04-07 17:13:10 +0000684 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000685 close(pipes[0]);
686 close(pipes[3]);
687 _exit(-1);
688 }
689 } else {
690 // Parent
691 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400692
Dees_Troy83bd4832013-05-04 12:39:56 +0000693 if (oaes_pid < 0) {
694 LOGERR("openaes fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +0000695 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000696 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_Troy83bd4832013-05-04 12:39:56 +0000701 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 Yonker87af5632014-02-10 11:56:35 -0600708 if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000709 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 Bocek25fd68d2013-08-27 03:10:10 +0200720 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_Troy83bd4832013-05-04 12:39:56 +0000721 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 Troy9d723272014-04-07 17:13:10 +0000733 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_Troy83bd4832013-05-04 12:39:56 +0000739
740 if (pipe(pigzfd) < 0) {
741 LOGERR("Error creating pipe\n");
Dees Troy9d723272014-04-07 17:13:10 +0000742 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000743 return -1;
744 }
745 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400746
Dees_Troy83bd4832013-05-04 12:39:56 +0000747 if (pigz_pid < 0) {
748 LOGERR("fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +0000749 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000750 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_Troy83bd4832013-05-04 12:39:56 +0000756 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 Bocek25fd68d2013-08-27 03:10:10 +0200768 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_Troy83bd4832013-05-04 12:39:56 +0000769 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_Troy83bd4832013-05-04 12:39:56 +0000778 int oaesfd[2];
Dees Troy9d723272014-04-07 17:13:10 +0000779 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_Troy83bd4832013-05-04 12:39:56 +0000789 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400790
Dees_Troy83bd4832013-05-04 12:39:56 +0000791 if (oaes_pid < 0) {
792 LOGERR("fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +0000793 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000794 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_Troy83bd4832013-05-04 12:39:56 +0000800 dup2(oaesfd[0], 0); // remap stdin
801 dup2(output_fd, 1); // remap stdout to output file
Ethan Yonker87af5632014-02-10 11:56:35 -0600802 if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000803 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 Bocek25fd68d2013-08-27 03:10:10 +0200812 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_Troy83bd4832013-05-04 12:39:56 +0000813 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 Bocek25fd68d2013-08-27 03:10:10 +0200822 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_Troy83bd4832013-05-04 12:39:56 +0000823 LOGERR("tar_open error opening '%s'\n", tarfn.c_str());
824 return -1;
825 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500826 }
827 return 0;
828}
829
Dees_Troy83bd4832013-05-04 12:39:56 +0000830int twrpTar::openTar() {
Dees_Troye34c1332013-02-06 19:13:00 +0000831 char* charRootDir = (char*) tardir.c_str();
832 char* charTarFile = (char*) tarfn.c_str();
Dees_Troy83bd4832013-05-04 12:39:56 +0000833 string Password;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500834
Dees_Troy83bd4832013-05-04 12:39:56 +0000835 if (Archive_Current_Type == 3) {
836 LOGINFO("Opening encrypted and compressed backup...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000837 int i, pipes[4];
Dees Troy9d723272014-04-07 17:13:10 +0000838 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_Troy83bd4832013-05-04 12:39:56 +0000843
844 if (pipe(pipes) < 0) {
845 LOGERR("Error creating first pipe\n");
Dees Troy9d723272014-04-07 17:13:10 +0000846 close(input_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500847 return -1;
848 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000849 if (pipe(pipes + 2) < 0) {
850 LOGERR("Error creating second pipe\n");
Dees Troy9d723272014-04-07 17:13:10 +0000851 close(pipes[0]);
852 close(pipes[1]);
853 close(input_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500854 return -1;
855 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000856 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400857
Dees_Troy83bd4832013-05-04 12:39:56 +0000858 if (oaes_pid < 0) {
859 LOGERR("pigz fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +0000860 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000861 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_Troy83bd4832013-05-04 12:39:56 +0000869 close(0);
870 dup2(input_fd, 0);
871 close(1);
872 dup2(pipes[1], 1);
Ethan Yonker87af5632014-02-10 11:56:35 -0600873 if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000874 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 bigbiff86e77bc2013-08-26 21:36:23 -0400882
Dees_Troy83bd4832013-05-04 12:39:56 +0000883 if (pigz_pid < 0) {
884 LOGERR("openaes fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +0000885 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000886 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 Troy9d723272014-04-07 17:13:10 +0000899 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000900 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 Bocek25fd68d2013-08-27 03:10:10 +0200910 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_Troy83bd4832013-05-04 12:39:56 +0000911 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_Troy83bd4832013-05-04 12:39:56 +0000919 int oaesfd[2];
Dees Troy9d723272014-04-07 17:13:10 +0000920 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_Troy83bd4832013-05-04 12:39:56 +0000925
Dees Troy9d723272014-04-07 17:13:10 +0000926 if (pipe(oaesfd) < 0) {
927 LOGERR("Error creating pipe\n");
928 close(input_fd);
929 return -1;
930 }
931
Dees_Troy83bd4832013-05-04 12:39:56 +0000932 oaes_pid = fork();
933 if (oaes_pid < 0) {
934 LOGERR("fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +0000935 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000936 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_Troy83bd4832013-05-04 12:39:56 +0000942 close(0); // close stdin
943 dup2(oaesfd[1], 1); // remap stdout
944 dup2(input_fd, 0); // remap input fd to stdin
Ethan Yonker87af5632014-02-10 11:56:35 -0600945 if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000946 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 Bocek25fd68d2013-08-27 03:10:10 +0200955 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_Troy83bd4832013-05-04 12:39:56 +0000956 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 Troy9d723272014-04-07 17:13:10 +0000964 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_Troy83bd4832013-05-04 12:39:56 +0000974
975 pigz_pid = fork();
976 if (pigz_pid < 0) {
977 LOGERR("fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +0000978 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000979 close(pigzfd[0]);
980 close(pigzfd[1]);
981 return -1;
982 } else if (pigz_pid == 0) {
983 // Child
984 close(pigzfd[0]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000985 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 Bocek25fd68d2013-08-27 03:10:10 +0200997 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_Troy83bd4832013-05-04 12:39:56 +0000998 close(fd);
999 LOGERR("tar_fdopen failed\n");
1000 return -1;
1001 }
1002 }
Vojtech Bocek25fd68d2013-08-27 03:10:10 +02001003 } 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_Troy83bd4832013-05-04 12:39:56 +00001004 LOGERR("Unable to open tar archive '%s'\n", charTarFile);
1005 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001006 }
1007 return 0;
1008}
1009
1010string 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
1030int 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_Troy83bd4832013-05-04 12:39:56 +00001044int twrpTar::closeTar() {
Dees_Troye34c1332013-02-06 19:13:00 +00001045 flush_libtar_buffer(t->fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001046 if (tar_append_eof(t) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001047 LOGERR("tar_append_eof(): %s\n", strerror(errno));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001048 tar_close(t);
1049 return -1;
1050 }
1051 if (tar_close(t) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001052 LOGERR("Unable to close tar archive: '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001053 return -1;
1054 }
Dees_Troy2727b992013-08-14 20:09:30 +00001055 if (Archive_Current_Type > 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001056 close(fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001057 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 bigbiff9c754052013-01-09 09:09:08 -05001062 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001063 free_libtar_buffer();
Dees Troye0a433a2013-12-02 04:10:37 +00001064 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 bigbiff9c754052013-01-09 09:09:08 -05001074 return 0;
1075}
1076
1077int twrpTar::removeEOT(string tarFile) {
1078 char* charTarFile = (char*) tarFile.c_str();
1079 off_t tarFileEnd;
1080 while (th_read(t) == 0) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001081 if (TH_ISREG(t))
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001082 tar_skip_regfile(t);
1083 tarFileEnd = lseek(t->fd, 0, SEEK_CUR);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001084 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001085 if (tar_close(t) == -1)
1086 return -1;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001087 if (truncate(charTarFile, tarFileEnd) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001088 return -1;
1089 return 0;
1090}
1091
n0d33b511632013-03-06 21:14:15 +02001092int twrpTar::entryExists(string entry) {
1093 char* searchstr = (char*)entry.c_str();
1094 int ret;
1095
Dees_Troy83bd4832013-05-04 12:39:56 +00001096 Archive_Current_Type = TWFunc::Get_File_Type(tarfn);
n0d33b511632013-03-06 21:14:15 +02001097
Dees_Troy83bd4832013-05-04 12:39:56 +00001098 if (openTar() == -1)
n0d33b511632013-03-06 21:14:15 +02001099 ret = 0;
1100 else
1101 ret = tar_find(t, searchstr);
1102
Dees_Troy83bd4832013-05-04 12:39:56 +00001103 if (closeTar() != 0)
1104 LOGINFO("Unable to close tar after searching for entry.\n");
n0d33b511632013-03-06 21:14:15 +02001105
1106 return ret;
1107}
1108
Dees_Troye34c1332013-02-06 19:13:00 +00001109extern "C" ssize_t write_tar(int fd, const void *buffer, size_t size) {
1110 return (ssize_t) write_libtar_buffer(fd, buffer, size);
Dees_Troy40bbcf82013-02-12 15:01:53 +00001111}