blob: b053c08d4667d44052b52141b507f9541fde5546 [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) {
117 FileName = tardir + "/";
118 FileName += de->d_name;
119 if (has_data_media == 1 && FileName.size() >= 11 && strncmp(FileName.c_str(), "/data/media", 11) == 0)
120 continue; // Skip /data/media
121 if (de->d_type == DT_BLK || de->d_type == DT_CHR)
122 continue;
Vojtech Bocek05f87d62014-03-11 22:08:23 +0100123 if (de->d_type == DT_DIR && !du.check_skip_dirs(tardir, de->d_name)) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000124 item_len = strlen(de->d_name);
125 if (userdata_encryption && ((item_len >= 3 && strncmp(de->d_name, "app", 3) == 0) || (item_len >= 6 && strncmp(de->d_name, "dalvik", 6) == 0))) {
126 if (Generate_TarList(FileName, &RegularList, &target_size, &regular_thread_id) < 0) {
127 LOGERR("Error in Generate_TarList with regular list!\n");
128 closedir(d);
129 _exit(-1);
130 }
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500131 regular_size += du.Get_Folder_Size(FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +0000132 } else {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500133 encrypt_size += du.Get_Folder_Size(FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +0000134 }
135 } else if (de->d_type == DT_REG) {
136 stat(FileName.c_str(), &st);
137 encrypt_size += (unsigned long long)(st.st_size);
138 }
139 }
140 closedir(d);
141
142 target_size = encrypt_size / core_count;
143 target_size++;
144 LOGINFO(" Unencrypted size: %llu\n", regular_size);
145 LOGINFO(" Encrypted size : %llu\n", encrypt_size);
146 LOGINFO(" Target size : %llu\n", target_size);
147 if (!userdata_encryption) {
148 enc_thread_id = 0;
149 start_thread_id = 0;
150 core_count--;
151 }
152 Archive_Current_Size = 0;
153
154 d = opendir(tardir.c_str());
155 if (d == NULL) {
156 LOGERR("error opening '%s'\n", tardir.c_str());
157 _exit(-1);
158 }
159 // Divide up the encrypted file list for threading
160 while ((de = readdir(d)) != NULL) {
161 FileName = tardir + "/";
162 FileName += de->d_name;
163 if (has_data_media == 1 && FileName.size() >= 11 && strncmp(FileName.c_str(), "/data/media", 11) == 0)
164 continue; // Skip /data/media
165 if (de->d_type == DT_BLK || de->d_type == DT_CHR)
166 continue;
Vojtech Bocek05f87d62014-03-11 22:08:23 +0100167 if (de->d_type == DT_DIR && !du.check_skip_dirs(tardir, de->d_name)) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000168 item_len = strlen(de->d_name);
169 if (userdata_encryption && ((item_len >= 3 && strncmp(de->d_name, "app", 3) == 0) || (item_len >= 6 && strncmp(de->d_name, "dalvik", 6) == 0))) {
170 // Do nothing, we added these to RegularList earlier
171 } else {
172 FileName = tardir + "/";
173 FileName += de->d_name;
174 if (Generate_TarList(FileName, &EncryptList, &target_size, &enc_thread_id) < 0) {
175 LOGERR("Error in Generate_TarList with encrypted list!\n");
176 closedir(d);
177 _exit(-1);
178 }
179 }
180 } else if (de->d_type == DT_REG || de->d_type == DT_LNK) {
181 stat(FileName.c_str(), &st);
182 if (de->d_type == DT_REG)
183 Archive_Current_Size += (unsigned long long)(st.st_size);
184 TarItem.fn = FileName;
185 TarItem.thread_id = enc_thread_id;
186 EncryptList.push_back(TarItem);
187 }
188 }
189 closedir(d);
190 if (enc_thread_id != core_count) {
191 LOGERR("Error dividing up threads for encryption, %i threads for %i cores!\n", enc_thread_id, core_count);
192 if (enc_thread_id > core_count)
193 _exit(-1);
194 else
195 LOGERR("Continuining anyway.");
196 }
197
198 if (userdata_encryption) {
199 // Create a backup of unencrypted data
200 reg.setfn(tarfn);
201 reg.ItemList = &RegularList;
202 reg.thread_id = 0;
203 reg.use_encryption = 0;
204 reg.use_compression = use_compression;
Dees Troye0a433a2013-12-02 04:10:37 +0000205 reg.split_archives = 1;
Dees_Troy83bd4832013-05-04 12:39:56 +0000206 LOGINFO("Creating unencrypted backup...\n");
207 if (createList((void*)&reg) != 0) {
208 LOGERR("Error creating unencrypted backup.\n");
209 _exit(-1);
210 }
211 }
212
213 if (pthread_attr_init(&tattr)) {
214 LOGERR("Unable to pthread_attr_init\n");
215 _exit(-1);
216 }
217 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
218 LOGERR("Error setting pthread_attr_setdetachstate\n");
219 _exit(-1);
220 }
221 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
222 LOGERR("Error setting pthread_attr_setscope\n");
223 _exit(-1);
224 }
225 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
226 LOGERR("Error setting pthread_attr_setstacksize\n");
227 _exit(-1);
228 }*/
229
230 // Create threads for the divided up encryption lists
231 for (i = start_thread_id; i <= core_count; i++) {
232 enc[i].setdir(tardir);
233 enc[i].setfn(tarfn);
234 enc[i].ItemList = &EncryptList;
235 enc[i].thread_id = i;
236 enc[i].use_encryption = use_encryption;
237 enc[i].use_compression = use_compression;
Dees Troye0a433a2013-12-02 04:10:37 +0000238 enc[i].split_archives = 1;
Dees_Troy83bd4832013-05-04 12:39:56 +0000239 LOGINFO("Start encryption thread %i\n", i);
240 ret = pthread_create(&enc_thread[i], &tattr, createList, (void*)&enc[i]);
241 if (ret) {
242 LOGINFO("Unable to create %i thread for encryption! %i\nContinuing in same thread (backup will be slower).", i, ret);
243 if (createList((void*)&enc[i]) != 0) {
244 LOGERR("Error creating encrypted backup %i.\n", i);
245 _exit(-1);
246 } else {
247 enc[i].thread_id = i + 1;
248 }
249 }
250 usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason.
251 }
252 if (pthread_attr_destroy(&tattr)) {
253 LOGERR("Failed to pthread_attr_destroy\n");
254 }
255 for (i = start_thread_id; i <= core_count; i++) {
256 if (enc[i].thread_id == i) {
257 if (pthread_join(enc_thread[i], &thread_return)) {
258 LOGERR("Error joining thread %i\n", i);
259 _exit(-1);
260 } else {
261 LOGINFO("Joined thread %i.\n", i);
262 ret = (int)thread_return;
263 if (ret != 0) {
264 thread_error = 1;
265 LOGERR("Thread %i returned an error %i.\n", i, ret);
266 _exit(-1);
267 }
268 }
269 } else {
270 LOGINFO("Skipping joining thread %i because of pthread failure.\n", i);
271 }
272 }
273 if (thread_error) {
274 LOGERR("Error returned by one or more threads.\n");
275 _exit(-1);
276 }
277 LOGINFO("Finished encrypted backup.\n");
278 _exit(0);
279 } else {
Dees Troye0a433a2013-12-02 04:10:37 +0000280 std::vector<TarListStruct> FileList;
281 unsigned thread_id = 0;
282 unsigned long long target_size = 0;
283 twrpTar reg;
284
285 // Generate list of files to back up
286 if (Generate_TarList(tardir, &FileList, &target_size, &thread_id) < 0) {
287 LOGERR("Error in Generate_TarList!\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000288 _exit(-1);
Dees Troye0a433a2013-12-02 04:10:37 +0000289 }
290 // Create a backup
291 reg.setfn(tarfn);
292 reg.ItemList = &FileList;
293 reg.thread_id = 0;
294 reg.use_encryption = 0;
295 reg.use_compression = use_compression;
296 reg.setsize(Total_Backup_Size);
297 if (Total_Backup_Size > MAX_ARCHIVE_SIZE) {
298 gui_print("Breaking backup file into multiple archives...\n");
299 reg.split_archives = 1;
300 } else {
301 reg.split_archives = 0;
302 }
303 LOGINFO("Creating backup...\n");
304 if (createList((void*)&reg) != 0) {
305 LOGERR("Error creating backup.\n");
306 _exit(-1);
307 }
308 _exit(0);
Dees_Troy83bd4832013-05-04 12:39:56 +0000309 }
310 } else {
311 if (TWFunc::Wait_For_Child(pid, &status, "createTarFork()") != 0)
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500312 return -1;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500313 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500314 return 0;
315}
316
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500317int twrpTar::extractTarFork() {
Dees_Troy83bd4832013-05-04 12:39:56 +0000318 int status = 0;
319 pid_t pid, rc_pid;
320
321 pid = fork();
322 if (pid >= 0) // fork was successful
323 {
324 if (pid == 0) // child process
325 {
326 if (TWFunc::Path_Exists(tarfn)) {
327 LOGINFO("Single archive\n");
328 if (extract() != 0)
329 _exit(-1);
330 else
331 _exit(0);
332 } else {
333 LOGINFO("Multiple archives\n");
334 string temp;
335 char actual_filename[255];
336 twrpTar tars[9];
337 pthread_t tar_thread[9];
338 pthread_attr_t tattr;
339 int thread_count = 0, i, start_thread_id = 1, ret, thread_error = 0;
340 void *thread_return;
341
342 basefn = tarfn;
343 temp = basefn + "%i%02i";
344 tarfn += "000";
345 if (!TWFunc::Path_Exists(tarfn)) {
346 LOGERR("Unable to locate '%s' or '%s'\n", basefn.c_str(), tarfn.c_str());
347 _exit(-1);
348 }
349 if (TWFunc::Get_File_Type(tarfn) != 2) {
350 LOGINFO("First tar file '%s' not encrypted\n", tarfn.c_str());
351 tars[0].basefn = basefn;
352 tars[0].thread_id = 0;
353 if (extractMulti((void*)&tars[0]) != 0) {
354 LOGERR("Error extracting split archive.\n");
355 _exit(-1);
356 }
357 } else {
358 start_thread_id = 0;
359 }
360 // Start threading encrypted restores
361 if (pthread_attr_init(&tattr)) {
362 LOGERR("Unable to pthread_attr_init\n");
363 _exit(-1);
364 }
365 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
366 LOGERR("Error setting pthread_attr_setdetachstate\n");
367 _exit(-1);
368 }
369 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
370 LOGERR("Error setting pthread_attr_setscope\n");
371 _exit(-1);
372 }
373 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
374 LOGERR("Error setting pthread_attr_setstacksize\n");
375 _exit(-1);
376 }*/
377 for (i = start_thread_id; i < 9; i++) {
378 sprintf(actual_filename, temp.c_str(), i, 0);
379 if (TWFunc::Path_Exists(actual_filename)) {
380 thread_count++;
381 tars[i].basefn = basefn;
382 tars[i].thread_id = i;
383 LOGINFO("Creating extract thread ID %i\n", i);
384 ret = pthread_create(&tar_thread[i], &tattr, extractMulti, (void*)&tars[i]);
385 if (ret) {
386 LOGINFO("Unable to create %i thread for extraction! %i\nContinuing in same thread (restore will be slower).", i, ret);
387 if (extractMulti((void*)&tars[i]) != 0) {
388 LOGERR("Error extracting backup in thread %i.\n", i);
389 _exit(-1);
390 } else {
391 tars[i].thread_id = i + 1;
392 }
393 }
394 usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason.
395 } else {
396 break;
397 }
398 }
399 for (i = start_thread_id; i < thread_count + start_thread_id; i++) {
400 if (tars[i].thread_id == i) {
401 if (pthread_join(tar_thread[i], &thread_return)) {
402 LOGERR("Error joining thread %i\n", i);
403 _exit(-1);
404 } else {
405 LOGINFO("Joined thread %i.\n", i);
406 ret = (int)thread_return;
407 if (ret != 0) {
408 thread_error = 1;
409 LOGERR("Thread %i returned an error %i.\n", i, ret);
410 _exit(-1);
411 }
412 }
413 } else {
414 LOGINFO("Skipping joining thread %i because of pthread failure.\n", i);
415 }
416 }
417 if (thread_error) {
418 LOGERR("Error returned by one or more threads.\n");
419 _exit(-1);
420 }
421 LOGINFO("Finished encrypted backup.\n");
422 _exit(0);
423 }
424 }
425 else // parent process
426 {
427 if (TWFunc::Wait_For_Child(pid, &status, "extractTarFork()") != 0)
428 return -1;
429 }
430 }
431 else // fork has failed
432 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000433 LOGINFO("extract tar failed to fork.\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500434 return -1;
435 }
436 return 0;
437}
438
Dees_Troy83bd4832013-05-04 12:39:56 +0000439int twrpTar::Generate_TarList(string Path, std::vector<TarListStruct> *TarList, unsigned long long *Target_Size, unsigned *thread_id) {
440 DIR* d;
441 struct dirent* de;
442 struct stat st;
443 string FileName;
444 struct TarListStruct TarItem;
445 string::size_type i;
Dees_Troy83bd4832013-05-04 12:39:56 +0000446
447 if (has_data_media == 1 && Path.size() >= 11 && strncmp(Path.c_str(), "/data/media", 11) == 0)
448 return 0; // Skip /data/media
449
450 d = opendir(Path.c_str());
451 if (d == NULL) {
Dees Troye0a433a2013-12-02 04:10:37 +0000452 LOGERR("Error opening '%s' -- error: %s\n", Path.c_str(), strerror(errno));
Dees_Troy83bd4832013-05-04 12:39:56 +0000453 closedir(d);
454 return -1;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500455 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000456 while ((de = readdir(d)) != NULL) {
Dees Troye0a433a2013-12-02 04:10:37 +0000457 FileName = Path + "/";
458 FileName += de->d_name;
Matt Mowerbb81e5d2014-03-20 18:05:41 -0500459
Dees_Troy83bd4832013-05-04 12:39:56 +0000460 if (has_data_media == 1 && FileName.size() >= 11 && strncmp(FileName.c_str(), "/data/media", 11) == 0)
461 continue; // Skip /data/media
462 if (de->d_type == DT_BLK || de->d_type == DT_CHR)
463 continue;
464 TarItem.fn = FileName;
465 TarItem.thread_id = *thread_id;
Vojtech Bocek05f87d62014-03-11 22:08:23 +0100466 if (de->d_type == DT_DIR && !du.check_skip_dirs(Path, de->d_name)) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000467 TarList->push_back(TarItem);
468 if (Generate_TarList(FileName, TarList, Target_Size, thread_id) < 0)
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500469 return -1;
Dees_Troy83bd4832013-05-04 12:39:56 +0000470 } else if (de->d_type == DT_REG || de->d_type == DT_LNK) {
471 stat(FileName.c_str(), &st);
472 TarList->push_back(TarItem);
473 if (de->d_type == DT_REG)
474 Archive_Current_Size += st.st_size;
475 if (Archive_Current_Size != 0 && *Target_Size != 0 && Archive_Current_Size > *Target_Size) {
476 *thread_id = *thread_id + 1;
477 Archive_Current_Size = 0;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500478 }
479 }
480 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000481 closedir(d);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500482 return 0;
483}
484
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500485int twrpTar::extractTar() {
Dees_Troye34c1332013-02-06 19:13:00 +0000486 char* charRootDir = (char*) tardir.c_str();
Dees_Troy83bd4832013-05-04 12:39:56 +0000487 if (openTar() == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500488 return -1;
489 if (tar_extract_all(t, charRootDir) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000490 LOGERR("Unable to extract tar archive '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500491 return -1;
492 }
493 if (tar_close(t) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000494 LOGERR("Unable to close tar file\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500495 return -1;
496 }
497 return 0;
498}
499
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500500int twrpTar::extract() {
Dees_Troy83bd4832013-05-04 12:39:56 +0000501 Archive_Current_Type = TWFunc::Get_File_Type(tarfn);
n0d33b511632013-03-06 21:14:15 +0200502
503 if (Archive_Current_Type == 1) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500504 //if you return the extractTGZ function directly, stack crashes happen
Dees_Troy2673cec2013-04-02 20:22:16 +0000505 LOGINFO("Extracting gzipped tar\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000506 int ret = extractTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500507 return ret;
Dees_Troy83bd4832013-05-04 12:39:56 +0000508 } else if (Archive_Current_Type == 2) {
Ethan Yonker87af5632014-02-10 11:56:35 -0600509 int ret = TWFunc::Try_Decrypting_File(tarfn, password);
Dees_Troy83bd4832013-05-04 12:39:56 +0000510 if (ret < 1) {
511 LOGERR("Failed to decrypt tar file '%s'\n", tarfn.c_str());
512 return -1;
513 }
514 if (ret == 1) {
515 LOGERR("Decrypted file is not in tar format.\n");
516 return -1;
517 }
518 if (ret == 3) {
519 LOGINFO("Extracting encrypted and compressed tar.\n");
520 Archive_Current_Type = 3;
521 } else
522 LOGINFO("Extracting encrypted tar.\n");
523 return extractTar();
524 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000525 LOGINFO("Extracting uncompressed tar\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500526 return extractTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500527 }
528}
529
Dees Troye0a433a2013-12-02 04:10:37 +0000530int twrpTar::tarList(std::vector<TarListStruct> *TarList, unsigned thread_id) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000531 struct stat st;
532 char buf[PATH_MAX];
533 int list_size = TarList->size(), i = 0, archive_count = 0;
534 string temp;
535 char actual_filename[PATH_MAX];
Dees Troye0a433a2013-12-02 04:10:37 +0000536 char *ptr;
Dees_Troye34c1332013-02-06 19:13:00 +0000537
Dees Troye0a433a2013-12-02 04:10:37 +0000538 if (split_archives) {
539 basefn = tarfn;
540 temp = basefn + "%i%02i";
541 sprintf(actual_filename, temp.c_str(), thread_id, archive_count);
542 tarfn = actual_filename;
543 include_root_dir = true;
544 } else {
545 include_root_dir = false;
546 }
547 LOGINFO("Creating tar file '%s'\n", tarfn.c_str());
Dees_Troy83bd4832013-05-04 12:39:56 +0000548 if (createTar() != 0) {
549 LOGERR("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id);
550 return -2;
551 }
552 Archive_Current_Size = 0;
553
554 while (i < list_size) {
555 if (TarList->at(i).thread_id == thread_id) {
556 strcpy(buf, TarList->at(i).fn.c_str());
bigbiff bigbiffec8fc282014-03-16 20:32:50 -0400557 lstat(buf, &st);
558 if (S_ISREG(st.st_mode)) { // item is a regular file
Dees_Troy83bd4832013-05-04 12:39:56 +0000559 if (Archive_Current_Size + (unsigned long long)(st.st_size) > MAX_ARCHIVE_SIZE) {
560 if (closeTar() != 0) {
561 LOGERR("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id);
562 return -3;
563 }
564 archive_count++;
Dees Troye0a433a2013-12-02 04:10:37 +0000565 gui_print("Splitting thread ID %i into archive %i\n", thread_id, archive_count + 1);
Dees_Troy83bd4832013-05-04 12:39:56 +0000566 if (archive_count > 99) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000567 LOGERR("Too many archives for thread %i\n", thread_id);
568 return -4;
569 }
570 sprintf(actual_filename, temp.c_str(), thread_id, archive_count);
571 tarfn = actual_filename;
572 if (createTar() != 0) {
573 LOGERR("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id);
574 return -2;
575 }
576 Archive_Current_Size = 0;
577 }
578 Archive_Current_Size += (unsigned long long)(st.st_size);
579 }
Dees Troye0a433a2013-12-02 04:10:37 +0000580 LOGINFO("addFile '%s' including root: %i\n", buf, include_root_dir);
581 if (addFile(buf, include_root_dir) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000582 LOGERR("Error adding file '%s' to '%s'\n", buf, tarfn.c_str());
583 return -1;
584 }
585 }
586 i++;
587 }
588 if (closeTar() != 0) {
589 LOGERR("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id);
590 return -3;
591 }
592 LOGINFO("Thread id %i tarList done, %i archives.\n", thread_id, archive_count, i, list_size);
Dees_Troye34c1332013-02-06 19:13:00 +0000593 return 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500594}
595
Dees_Troy83bd4832013-05-04 12:39:56 +0000596void* twrpTar::createList(void *cookie) {
597
598 twrpTar* threadTar = (twrpTar*) cookie;
Ethan Yonkereae42092014-03-07 15:33:13 -0600599 if (threadTar->tarList(threadTar->ItemList, threadTar->thread_id) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000600 LOGINFO("ERROR tarList for thread ID %i\n", threadTar->thread_id);
601 return (void*)-2;
602 }
603 LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id);
604 return (void*)0;
605}
606
607void* twrpTar::extractMulti(void *cookie) {
608
609 twrpTar* threadTar = (twrpTar*) cookie;
610 int archive_count = 0;
611 string temp = threadTar->basefn + "%i%02i";
612 char actual_filename[255];
613 sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count);
614 while (TWFunc::Path_Exists(actual_filename)) {
615 threadTar->tarfn = actual_filename;
616 if (threadTar->extract() != 0) {
617 LOGINFO("Error extracting '%s' in thread ID %i\n", actual_filename, threadTar->thread_id);
618 return (void*)-2;
619 }
620 archive_count++;
621 if (archive_count > 99)
622 break;
623 sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count);
624 }
625 LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id);
626 return (void*)0;
627}
628
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500629int twrpTar::addFilesToExistingTar(vector <string> files, string fn) {
630 char* charTarFile = (char*) fn.c_str();
631
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200632 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 -0500633 return -1;
634 removeEOT(charTarFile);
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200635 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 -0500636 return -1;
637 for (unsigned int i = 0; i < files.size(); ++i) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500638 char* file = (char*) files.at(i).c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500639 if (tar_append_file(t, file, file) == -1)
640 return -1;
641 }
642 if (tar_append_eof(t) == -1)
643 return -1;
644 if (tar_close(t) == -1)
645 return -1;
646 return 0;
647}
648
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500649int twrpTar::createTar() {
650 char* charTarFile = (char*) tarfn.c_str();
Dees_Troye34c1332013-02-06 19:13:00 +0000651 char* charRootDir = (char*) tardir.c_str();
Dees_Troye34c1332013-02-06 19:13:00 +0000652 static tartype_t type = { open, close, read, write_tar };
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500653
Dees_Troy83bd4832013-05-04 12:39:56 +0000654 if (use_encryption && use_compression) {
655 // Compressed and encrypted
656 Archive_Current_Type = 3;
657 LOGINFO("Using encryption and compression...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000658 int i, pipes[4];
659
660 if (pipe(pipes) < 0) {
661 LOGERR("Error creating first pipe\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500662 return -1;
663 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000664 if (pipe(pipes + 2) < 0) {
665 LOGERR("Error creating second pipe\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500666 return -1;
Dees_Troy83bd4832013-05-04 12:39:56 +0000667 }
668 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400669
Dees_Troy83bd4832013-05-04 12:39:56 +0000670 if (pigz_pid < 0) {
671 LOGERR("pigz fork() failed\n");
672 for (i = 0; i < 4; i++)
673 close(pipes[i]); // close all
674 return -1;
675 } else if (pigz_pid == 0) {
676 // pigz Child
677 close(pipes[1]);
678 close(pipes[2]);
679 close(0);
680 dup2(pipes[0], 0);
681 close(1);
682 dup2(pipes[3], 1);
683 if (execlp("pigz", "pigz", "-", NULL) < 0) {
684 LOGERR("execlp pigz ERROR!\n");
685 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");
695 for (i = 0; i < 4; i++)
696 close(pipes[i]); // close all
697 return -1;
698 } else if (oaes_pid == 0) {
699 // openaes Child
Dees_Troy5612cc82013-07-24 19:45:58 +0000700 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_Troy83bd4832013-05-04 12:39:56 +0000701 if (output_fd < 0) {
702 LOGERR("Failed to open '%s'\n", tarfn.c_str());
703 for (i = 0; i < 4; i++)
704 close(pipes[i]); // close all
705 return -1;
706 }
707 close(pipes[0]);
708 close(pipes[1]);
709 close(pipes[3]);
710 close(0);
711 dup2(pipes[2], 0);
712 close(1);
713 dup2(output_fd, 1);
Ethan Yonker87af5632014-02-10 11:56:35 -0600714 if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000715 LOGERR("execlp openaes ERROR!\n");
716 close(pipes[2]);
717 close(output_fd);
718 _exit(-1);
719 }
720 } else {
721 // Parent
722 close(pipes[0]);
723 close(pipes[2]);
724 close(pipes[3]);
725 fd = pipes[1];
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200726 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 +0000727 close(fd);
728 LOGERR("tar_fdopen failed\n");
729 return -1;
730 }
731 return 0;
732 }
733 }
734 } else if (use_compression) {
735 // Compressed
736 Archive_Current_Type = 1;
737 LOGINFO("Using compression...\n");
738 int pigzfd[2];
739
740 if (pipe(pigzfd) < 0) {
741 LOGERR("Error creating pipe\n");
742 return -1;
743 }
744 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400745
Dees_Troy83bd4832013-05-04 12:39:56 +0000746 if (pigz_pid < 0) {
747 LOGERR("fork() failed\n");
748 close(pigzfd[0]);
749 close(pigzfd[1]);
750 return -1;
751 } else if (pigz_pid == 0) {
752 // Child
753 close(pigzfd[1]); // close unused output pipe
Dees_Troy5612cc82013-07-24 19:45:58 +0000754 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_Troy83bd4832013-05-04 12:39:56 +0000755 if (output_fd < 0) {
756 LOGERR("Failed to open '%s'\n", tarfn.c_str());
757 close(pigzfd[0]);
758 _exit(-1);
759 }
760 dup2(pigzfd[0], 0); // remap stdin
761 dup2(output_fd, 1); // remap stdout to output file
762 if (execlp("pigz", "pigz", "-", NULL) < 0) {
763 LOGERR("execlp pigz ERROR!\n");
764 close(output_fd);
765 close(pigzfd[0]);
766 _exit(-1);
767 }
768 } else {
769 // Parent
770 close(pigzfd[0]); // close parent input
771 fd = pigzfd[1]; // copy parent output
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200772 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 +0000773 close(fd);
774 LOGERR("tar_fdopen failed\n");
775 return -1;
776 }
777 }
778 } else if (use_encryption) {
779 // Encrypted
780 Archive_Current_Type = 2;
781 LOGINFO("Using encryption...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000782 int oaesfd[2];
783 pipe(oaesfd);
784 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400785
Dees_Troy83bd4832013-05-04 12:39:56 +0000786 if (oaes_pid < 0) {
787 LOGERR("fork() failed\n");
788 close(oaesfd[0]);
789 close(oaesfd[1]);
790 return -1;
791 } else if (oaes_pid == 0) {
792 // Child
793 close(oaesfd[1]); // close unused
Dees_Troy5612cc82013-07-24 19:45:58 +0000794 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_Troy83bd4832013-05-04 12:39:56 +0000795 if (output_fd < 0) {
796 LOGERR("Failed to open '%s'\n", tarfn.c_str());
797 _exit(-1);
798 }
799 dup2(oaesfd[0], 0); // remap stdin
800 dup2(output_fd, 1); // remap stdout to output file
Ethan Yonker87af5632014-02-10 11:56:35 -0600801 if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000802 LOGERR("execlp openaes ERROR!\n");
803 close(output_fd);
804 close(oaesfd[0]);
805 _exit(-1);
806 }
807 } else {
808 // Parent
809 close(oaesfd[0]); // close parent input
810 fd = oaesfd[1]; // copy parent output
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200811 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 +0000812 close(fd);
813 LOGERR("tar_fdopen failed\n");
814 return -1;
815 }
816 return 0;
817 }
818 } else {
819 // Not compressed or encrypted
820 init_libtar_buffer(0);
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200821 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 +0000822 LOGERR("tar_open error opening '%s'\n", tarfn.c_str());
823 return -1;
824 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500825 }
826 return 0;
827}
828
Dees_Troy83bd4832013-05-04 12:39:56 +0000829int twrpTar::openTar() {
Dees_Troye34c1332013-02-06 19:13:00 +0000830 char* charRootDir = (char*) tardir.c_str();
831 char* charTarFile = (char*) tarfn.c_str();
Dees_Troy83bd4832013-05-04 12:39:56 +0000832 string Password;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500833
Dees_Troy83bd4832013-05-04 12:39:56 +0000834 if (Archive_Current_Type == 3) {
835 LOGINFO("Opening encrypted and compressed backup...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000836 int i, pipes[4];
837
838 if (pipe(pipes) < 0) {
839 LOGERR("Error creating first pipe\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500840 return -1;
841 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000842 if (pipe(pipes + 2) < 0) {
843 LOGERR("Error creating second pipe\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500844 return -1;
845 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000846 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400847
Dees_Troy83bd4832013-05-04 12:39:56 +0000848 if (oaes_pid < 0) {
849 LOGERR("pigz fork() failed\n");
850 for (i = 0; i < 4; i++)
851 close(pipes[i]); // close all
852 return -1;
853 } else if (oaes_pid == 0) {
854 // openaes Child
855 close(pipes[0]); // Close pipes that are not used by this child
856 close(pipes[2]);
857 close(pipes[3]);
858 int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE);
859 if (input_fd < 0) {
860 LOGERR("Failed to open '%s'\n", tarfn.c_str());
861 close(pipes[1]);
862 _exit(-1);
863 }
864 close(0);
865 dup2(input_fd, 0);
866 close(1);
867 dup2(pipes[1], 1);
Ethan Yonker87af5632014-02-10 11:56:35 -0600868 if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000869 LOGERR("execlp openaes ERROR!\n");
870 close(input_fd);
871 close(pipes[1]);
872 _exit(-1);
873 }
874 } else {
875 // Parent
876 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400877
Dees_Troy83bd4832013-05-04 12:39:56 +0000878 if (pigz_pid < 0) {
879 LOGERR("openaes fork() failed\n");
880 for (i = 0; i < 4; i++)
881 close(pipes[i]); // close all
882 return -1;
883 } else if (pigz_pid == 0) {
884 // pigz Child
885 close(pipes[1]); // Close pipes not used by this child
886 close(pipes[2]);
887 close(0);
888 dup2(pipes[0], 0);
889 close(1);
890 dup2(pipes[3], 1);
891 if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) {
892 LOGERR("execlp pigz ERROR!\n");
893 close(pipes[0]);
894 close(pipes[3]);
895 _exit(-1);
896 }
897 } else {
898 // Parent
899 close(pipes[0]); // Close pipes not used by parent
900 close(pipes[1]);
901 close(pipes[3]);
902 fd = pipes[2];
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200903 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 +0000904 close(fd);
905 LOGERR("tar_fdopen failed\n");
906 return -1;
907 }
908 }
909 }
910 } else if (Archive_Current_Type == 2) {
911 LOGINFO("Opening encrypted backup...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000912 int oaesfd[2];
913
914 pipe(oaesfd);
915 oaes_pid = fork();
916 if (oaes_pid < 0) {
917 LOGERR("fork() failed\n");
918 close(oaesfd[0]);
919 close(oaesfd[1]);
920 return -1;
921 } else if (oaes_pid == 0) {
922 // Child
923 close(oaesfd[0]); // Close unused pipe
924 int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE);
925 if (input_fd < 0) {
926 LOGERR("Failed to open '%s'\n", tarfn.c_str());
927 close(oaesfd[1]);
928 _exit(-1);
929 }
930 close(0); // close stdin
931 dup2(oaesfd[1], 1); // remap stdout
932 dup2(input_fd, 0); // remap input fd to stdin
Ethan Yonker87af5632014-02-10 11:56:35 -0600933 if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000934 LOGERR("execlp openaes ERROR!\n");
935 close(input_fd);
936 close(oaesfd[1]);
937 _exit(-1);
938 }
939 } else {
940 // Parent
941 close(oaesfd[1]); // close parent output
942 fd = oaesfd[0]; // copy parent input
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200943 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 +0000944 close(fd);
945 LOGERR("tar_fdopen failed\n");
946 return -1;
947 }
948 }
949 } else if (Archive_Current_Type == 1) {
950 LOGINFO("Opening as a gzip...\n");
951 int pigzfd[2];
952 pipe(pigzfd);
953
954 pigz_pid = fork();
955 if (pigz_pid < 0) {
956 LOGERR("fork() failed\n");
957 close(pigzfd[0]);
958 close(pigzfd[1]);
959 return -1;
960 } else if (pigz_pid == 0) {
961 // Child
962 close(pigzfd[0]);
963 int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE);
964 if (input_fd < 0) {
965 LOGERR("Failed to open '%s'\n", tarfn.c_str());
966 _exit(-1);
967 }
968 dup2(input_fd, 0); // remap input fd to stdin
969 dup2(pigzfd[1], 1); // remap stdout
970 if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) {
971 close(pigzfd[1]);
972 close(input_fd);
973 LOGERR("execlp openaes ERROR!\n");
974 _exit(-1);
975 }
976 } else {
977 // Parent
978 close(pigzfd[1]); // close parent output
979 fd = pigzfd[0]; // copy parent input
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200980 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 +0000981 close(fd);
982 LOGERR("tar_fdopen failed\n");
983 return -1;
984 }
985 }
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200986 } 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 +0000987 LOGERR("Unable to open tar archive '%s'\n", charTarFile);
988 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500989 }
990 return 0;
991}
992
993string twrpTar::Strip_Root_Dir(string Path) {
994 string temp;
995 size_t slash;
996
997 if (Path.substr(0, 1) == "/")
998 temp = Path.substr(1, Path.size() - 1);
999 else
1000 temp = Path;
1001 slash = temp.find("/");
1002 if (slash == string::npos)
1003 return temp;
1004 else {
1005 string stripped;
1006
1007 stripped = temp.substr(slash, temp.size() - slash);
1008 return stripped;
1009 }
1010 return temp;
1011}
1012
1013int twrpTar::addFile(string fn, bool include_root) {
1014 char* charTarFile = (char*) fn.c_str();
1015 if (include_root) {
1016 if (tar_append_file(t, charTarFile, NULL) == -1)
1017 return -1;
1018 } else {
1019 string temp = Strip_Root_Dir(fn);
1020 char* charTarPath = (char*) temp.c_str();
1021 if (tar_append_file(t, charTarFile, charTarPath) == -1)
1022 return -1;
1023 }
1024 return 0;
1025}
1026
Dees_Troy83bd4832013-05-04 12:39:56 +00001027int twrpTar::closeTar() {
Dees_Troye34c1332013-02-06 19:13:00 +00001028 flush_libtar_buffer(t->fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001029 if (tar_append_eof(t) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001030 LOGERR("tar_append_eof(): %s\n", strerror(errno));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001031 tar_close(t);
1032 return -1;
1033 }
1034 if (tar_close(t) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001035 LOGERR("Unable to close tar archive: '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001036 return -1;
1037 }
Dees_Troy2727b992013-08-14 20:09:30 +00001038 if (Archive_Current_Type > 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001039 close(fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001040 int status;
1041 if (pigz_pid > 0 && TWFunc::Wait_For_Child(pigz_pid, &status, "pigz") != 0)
1042 return -1;
1043 if (oaes_pid > 0 && TWFunc::Wait_For_Child(oaes_pid, &status, "openaes") != 0)
1044 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001045 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001046 free_libtar_buffer();
Dees Troye0a433a2013-12-02 04:10:37 +00001047 if (use_compression && !use_encryption) {
1048 string gzname = tarfn + ".gz";
1049 if (TWFunc::Path_Exists(gzname)) {
1050 rename(gzname.c_str(), tarfn.c_str());
1051 }
1052 }
1053 if (TWFunc::Get_File_Size(tarfn) == 0) {
1054 LOGERR("Backup file size for '%s' is 0 bytes.\n", tarfn.c_str());
1055 return -1;
1056 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001057 return 0;
1058}
1059
1060int twrpTar::removeEOT(string tarFile) {
1061 char* charTarFile = (char*) tarFile.c_str();
1062 off_t tarFileEnd;
1063 while (th_read(t) == 0) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001064 if (TH_ISREG(t))
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001065 tar_skip_regfile(t);
1066 tarFileEnd = lseek(t->fd, 0, SEEK_CUR);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001067 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001068 if (tar_close(t) == -1)
1069 return -1;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001070 if (truncate(charTarFile, tarFileEnd) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001071 return -1;
1072 return 0;
1073}
1074
n0d33b511632013-03-06 21:14:15 +02001075int twrpTar::entryExists(string entry) {
1076 char* searchstr = (char*)entry.c_str();
1077 int ret;
1078
Dees_Troy83bd4832013-05-04 12:39:56 +00001079 Archive_Current_Type = TWFunc::Get_File_Type(tarfn);
n0d33b511632013-03-06 21:14:15 +02001080
Dees_Troy83bd4832013-05-04 12:39:56 +00001081 if (openTar() == -1)
n0d33b511632013-03-06 21:14:15 +02001082 ret = 0;
1083 else
1084 ret = tar_find(t, searchstr);
1085
Dees_Troy83bd4832013-05-04 12:39:56 +00001086 if (closeTar() != 0)
1087 LOGINFO("Unable to close tar after searching for entry.\n");
n0d33b511632013-03-06 21:14:15 +02001088
1089 return ret;
1090}
1091
Dees_Troye34c1332013-02-06 19:13:00 +00001092extern "C" ssize_t write_tar(int fd, const void *buffer, size_t size) {
1093 return (ssize_t) write_libtar_buffer(fd, buffer, size);
Dees_Troy40bbcf82013-02-12 15:01:53 +00001094}