blob: 90c7ea34c2528403daa03c32fb7ab980e8df0697 [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"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060024 #include "set_metadata.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050025}
26#include <sys/types.h>
27#include <sys/stat.h>
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -050028#include <sys/wait.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029#include <string.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <fstream>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050033#include <iostream>
34#include <string>
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -050035#include <sstream>
Dees_Troy83bd4832013-05-04 12:39:56 +000036#include <vector>
bigbiff7abc5fe2015-01-17 16:53:12 -050037#include <csignal>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050038#include <dirent.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040039#include <libgen.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050040#include <sys/mman.h>
41#include "twrpTar.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000042#include "twcommon.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050043#include "variables.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050044#include "twrp-functions.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050045#ifndef BUILD_TWRPTAR_MAIN
46#include "data.hpp"
47#include "infomanager.hpp"
Ethan Yonker960f0302014-12-21 21:54:00 -060048extern "C" {
49 #include "set_metadata.h"
50}
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050051#endif //ndef BUILD_TWRPTAR_MAIN
bigbiff bigbiff9c754052013-01-09 09:09:08 -050052
53using namespace std;
54
Dees_Troy83bd4832013-05-04 12:39:56 +000055twrpTar::twrpTar(void) {
56 use_encryption = 0;
57 userdata_encryption = 0;
58 use_compression = 0;
59 split_archives = 0;
60 has_data_media = 0;
61 pigz_pid = 0;
62 oaes_pid = 0;
Dees Troye0a433a2013-12-02 04:10:37 +000063 Total_Backup_Size = 0;
Ethan Yonker1a147a42015-02-27 13:14:44 -060064 Archive_Current_Size = 0;
Dees Troye0a433a2013-12-02 04:10:37 +000065 include_root_dir = true;
Dees_Troy83bd4832013-05-04 12:39:56 +000066}
67
68twrpTar::~twrpTar(void) {
69 // Do nothing
70}
71
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -050072void twrpTar::setfn(string fn) {
73 tarfn = fn;
74}
75
76void twrpTar::setdir(string dir) {
77 tardir = dir;
78}
79
Dees Troye0a433a2013-12-02 04:10:37 +000080void twrpTar::setsize(unsigned long long backup_size) {
81 Total_Backup_Size = backup_size;
82}
83
Ethan Yonker87af5632014-02-10 11:56:35 -060084void twrpTar::setpassword(string pass) {
85 password = pass;
86}
87
bigbiff7abc5fe2015-01-17 16:53:12 -050088void twrpTar::Signal_Kill(int signum) {
89 _exit(255);
90}
91
92int twrpTar::createTarFork(const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &fork_pid) {
Dees_Troy83bd4832013-05-04 12:39:56 +000093 int status = 0;
bigbiff7abc5fe2015-01-17 16:53:12 -050094 pid_t rc_pid, tar_fork_pid;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050095 int progress_pipe[2], ret;
96
97 file_count = 0;
98
99 if (pipe(progress_pipe) < 0) {
100 LOGERR("Error creating progress tracking pipe\n");
101 return -1;
102 }
bigbiff7abc5fe2015-01-17 16:53:12 -0500103 if ((tar_fork_pid = fork()) == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000104 LOGINFO("create tar failed to fork.\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500105 close(progress_pipe[0]);
106 close(progress_pipe[1]);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500107 return -1;
108 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500109
bigbiff7abc5fe2015-01-17 16:53:12 -0500110 if (tar_fork_pid == 0) {
111 // Child process
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500112 // Child closes input side of progress pipe
bigbiff7abc5fe2015-01-17 16:53:12 -0500113 signal(SIGUSR2, twrpTar::Signal_Kill);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500114 close(progress_pipe[0]);
115 progress_pipe_fd = progress_pipe[1];
116
Dees_Troy83bd4832013-05-04 12:39:56 +0000117 if (use_encryption || userdata_encryption) {
118 LOGINFO("Using encryption\n");
119 DIR* d;
120 struct dirent* de;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500121 unsigned long long regular_size = 0, encrypt_size = 0, target_size = 0, core_count = 1, total_size;
Dees_Troy83bd4832013-05-04 12:39:56 +0000122 unsigned enc_thread_id = 1, regular_thread_id = 0, i, start_thread_id = 1;
123 int item_len, ret, thread_error = 0;
124 std::vector<TarListStruct> RegularList;
125 std::vector<TarListStruct> EncryptList;
126 string FileName;
127 struct TarListStruct TarItem;
128 twrpTar reg, enc[9];
129 struct stat st;
130 pthread_t enc_thread[9];
131 pthread_attr_t tattr;
132 void *thread_return;
133
134 core_count = sysconf(_SC_NPROCESSORS_CONF);
135 if (core_count > 8)
136 core_count = 8;
137 LOGINFO(" Core Count : %llu\n", core_count);
138 Archive_Current_Size = 0;
139
140 d = opendir(tardir.c_str());
141 if (d == NULL) {
142 LOGERR("error opening '%s'\n", tardir.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500143 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000144 _exit(-1);
145 }
146 // Figure out the size of all data to be encrypted and create a list of unencrypted files
147 while ((de = readdir(d)) != NULL) {
Matt Mower50248ab2014-03-31 15:58:40 -0500148 FileName = tardir + "/" + de->d_name;
149
150 if (de->d_type == DT_BLK || de->d_type == DT_CHR || du.check_skip_dirs(FileName))
Dees_Troy83bd4832013-05-04 12:39:56 +0000151 continue;
Matt Mower50248ab2014-03-31 15:58:40 -0500152 if (de->d_type == DT_DIR) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000153 item_len = strlen(de->d_name);
154 if (userdata_encryption && ((item_len >= 3 && strncmp(de->d_name, "app", 3) == 0) || (item_len >= 6 && strncmp(de->d_name, "dalvik", 6) == 0))) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500155 ret = Generate_TarList(FileName, &RegularList, &target_size, &regular_thread_id);
156 if (ret < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000157 LOGERR("Error in Generate_TarList with regular list!\n");
158 closedir(d);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500159 close(progress_pipe_fd);
160 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000161 _exit(-1);
162 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500163 file_count = (unsigned long long)(ret);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500164 regular_size += du.Get_Folder_Size(FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +0000165 } else {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500166 encrypt_size += du.Get_Folder_Size(FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +0000167 }
168 } else if (de->d_type == DT_REG) {
169 stat(FileName.c_str(), &st);
170 encrypt_size += (unsigned long long)(st.st_size);
171 }
172 }
173 closedir(d);
174
175 target_size = encrypt_size / core_count;
176 target_size++;
177 LOGINFO(" Unencrypted size: %llu\n", regular_size);
178 LOGINFO(" Encrypted size : %llu\n", encrypt_size);
179 LOGINFO(" Target size : %llu\n", target_size);
180 if (!userdata_encryption) {
181 enc_thread_id = 0;
182 start_thread_id = 0;
183 core_count--;
184 }
185 Archive_Current_Size = 0;
186
187 d = opendir(tardir.c_str());
188 if (d == NULL) {
189 LOGERR("error opening '%s'\n", tardir.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500190 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000191 _exit(-1);
192 }
193 // Divide up the encrypted file list for threading
194 while ((de = readdir(d)) != NULL) {
Matt Mower50248ab2014-03-31 15:58:40 -0500195 FileName = tardir + "/" + de->d_name;
196
197 if (de->d_type == DT_BLK || de->d_type == DT_CHR || du.check_skip_dirs(FileName))
Dees_Troy83bd4832013-05-04 12:39:56 +0000198 continue;
Matt Mower50248ab2014-03-31 15:58:40 -0500199 if (de->d_type == DT_DIR) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000200 item_len = strlen(de->d_name);
201 if (userdata_encryption && ((item_len >= 3 && strncmp(de->d_name, "app", 3) == 0) || (item_len >= 6 && strncmp(de->d_name, "dalvik", 6) == 0))) {
202 // Do nothing, we added these to RegularList earlier
203 } else {
Matt Mower50248ab2014-03-31 15:58:40 -0500204 FileName = tardir + "/" + de->d_name;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500205 ret = Generate_TarList(FileName, &EncryptList, &target_size, &enc_thread_id);
206 if (ret < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000207 LOGERR("Error in Generate_TarList with encrypted list!\n");
208 closedir(d);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500209 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000210 _exit(-1);
211 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500212 file_count += (unsigned long long)(ret);
Dees_Troy83bd4832013-05-04 12:39:56 +0000213 }
214 } else if (de->d_type == DT_REG || de->d_type == DT_LNK) {
215 stat(FileName.c_str(), &st);
216 if (de->d_type == DT_REG)
217 Archive_Current_Size += (unsigned long long)(st.st_size);
218 TarItem.fn = FileName;
219 TarItem.thread_id = enc_thread_id;
220 EncryptList.push_back(TarItem);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500221 file_count++;
Dees_Troy83bd4832013-05-04 12:39:56 +0000222 }
223 }
224 closedir(d);
225 if (enc_thread_id != core_count) {
226 LOGERR("Error dividing up threads for encryption, %i threads for %i cores!\n", enc_thread_id, core_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500227 if (enc_thread_id > core_count) {
228 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000229 _exit(-1);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500230 } else {
Dees_Troy83bd4832013-05-04 12:39:56 +0000231 LOGERR("Continuining anyway.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500232 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000233 }
234
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500235 // Send file count to parent
236 write(progress_pipe_fd, &file_count, sizeof(file_count));
237 // Send backup size to parent
238 total_size = regular_size + encrypt_size;
239 write(progress_pipe_fd, &total_size, sizeof(total_size));
240
Dees_Troy83bd4832013-05-04 12:39:56 +0000241 if (userdata_encryption) {
242 // Create a backup of unencrypted data
243 reg.setfn(tarfn);
244 reg.ItemList = &RegularList;
245 reg.thread_id = 0;
246 reg.use_encryption = 0;
247 reg.use_compression = use_compression;
Dees Troye0a433a2013-12-02 04:10:37 +0000248 reg.split_archives = 1;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500249 reg.progress_pipe_fd = progress_pipe_fd;
Dees_Troy83bd4832013-05-04 12:39:56 +0000250 LOGINFO("Creating unencrypted backup...\n");
251 if (createList((void*)&reg) != 0) {
252 LOGERR("Error creating unencrypted backup.\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500253 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000254 _exit(-1);
255 }
256 }
257
258 if (pthread_attr_init(&tattr)) {
259 LOGERR("Unable to pthread_attr_init\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500260 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000261 _exit(-1);
262 }
263 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
264 LOGERR("Error setting pthread_attr_setdetachstate\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500265 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000266 _exit(-1);
267 }
268 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
269 LOGERR("Error setting pthread_attr_setscope\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500270 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000271 _exit(-1);
272 }
273 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
274 LOGERR("Error setting pthread_attr_setstacksize\n");
275 _exit(-1);
276 }*/
277
278 // Create threads for the divided up encryption lists
279 for (i = start_thread_id; i <= core_count; i++) {
280 enc[i].setdir(tardir);
281 enc[i].setfn(tarfn);
282 enc[i].ItemList = &EncryptList;
283 enc[i].thread_id = i;
284 enc[i].use_encryption = use_encryption;
Ethan Yonker924a80b2014-04-02 10:54:12 -0500285 enc[i].setpassword(password);
Dees_Troy83bd4832013-05-04 12:39:56 +0000286 enc[i].use_compression = use_compression;
Dees Troye0a433a2013-12-02 04:10:37 +0000287 enc[i].split_archives = 1;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500288 enc[i].progress_pipe_fd = progress_pipe_fd;
Dees_Troy83bd4832013-05-04 12:39:56 +0000289 LOGINFO("Start encryption thread %i\n", i);
290 ret = pthread_create(&enc_thread[i], &tattr, createList, (void*)&enc[i]);
291 if (ret) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500292 LOGINFO("Unable to create %i thread for encryption! %i\nContinuing in same thread (backup will be slower).\n", i, ret);
Dees_Troy83bd4832013-05-04 12:39:56 +0000293 if (createList((void*)&enc[i]) != 0) {
294 LOGERR("Error creating encrypted backup %i.\n", i);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500295 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000296 _exit(-1);
297 } else {
298 enc[i].thread_id = i + 1;
299 }
300 }
301 usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason.
302 }
303 if (pthread_attr_destroy(&tattr)) {
304 LOGERR("Failed to pthread_attr_destroy\n");
305 }
306 for (i = start_thread_id; i <= core_count; i++) {
307 if (enc[i].thread_id == i) {
308 if (pthread_join(enc_thread[i], &thread_return)) {
309 LOGERR("Error joining thread %i\n", i);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500310 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000311 _exit(-1);
312 } else {
313 LOGINFO("Joined thread %i.\n", i);
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600314 ret = *((int *)thread_return);
Dees_Troy83bd4832013-05-04 12:39:56 +0000315 if (ret != 0) {
316 thread_error = 1;
317 LOGERR("Thread %i returned an error %i.\n", i, ret);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500318 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000319 _exit(-1);
320 }
321 }
322 } else {
323 LOGINFO("Skipping joining thread %i because of pthread failure.\n", i);
324 }
325 }
326 if (thread_error) {
327 LOGERR("Error returned by one or more threads.\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500328 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000329 _exit(-1);
330 }
331 LOGINFO("Finished encrypted backup.\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500332 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000333 _exit(0);
334 } else {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500335 // Not encrypted
Dees Troye0a433a2013-12-02 04:10:37 +0000336 std::vector<TarListStruct> FileList;
337 unsigned thread_id = 0;
338 unsigned long long target_size = 0;
339 twrpTar reg;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500340 int ret;
Dees Troye0a433a2013-12-02 04:10:37 +0000341
342 // Generate list of files to back up
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500343 ret = Generate_TarList(tardir, &FileList, &target_size, &thread_id);
344 if (ret < 0) {
Dees Troye0a433a2013-12-02 04:10:37 +0000345 LOGERR("Error in Generate_TarList!\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500346 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000347 _exit(-1);
Dees Troye0a433a2013-12-02 04:10:37 +0000348 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500349 file_count = (unsigned long long)(ret);
Dees Troye0a433a2013-12-02 04:10:37 +0000350 // Create a backup
351 reg.setfn(tarfn);
352 reg.ItemList = &FileList;
353 reg.thread_id = 0;
354 reg.use_encryption = 0;
355 reg.use_compression = use_compression;
356 reg.setsize(Total_Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500357 reg.progress_pipe_fd = progress_pipe_fd;
Dees Troye0a433a2013-12-02 04:10:37 +0000358 if (Total_Backup_Size > MAX_ARCHIVE_SIZE) {
359 gui_print("Breaking backup file into multiple archives...\n");
360 reg.split_archives = 1;
361 } else {
362 reg.split_archives = 0;
363 }
364 LOGINFO("Creating backup...\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500365 write(progress_pipe_fd, &file_count, sizeof(file_count));
366 write(progress_pipe_fd, &Total_Backup_Size, sizeof(Total_Backup_Size));
Dees Troye0a433a2013-12-02 04:10:37 +0000367 if (createList((void*)&reg) != 0) {
368 LOGERR("Error creating backup.\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500369 close(progress_pipe[1]);
Dees Troye0a433a2013-12-02 04:10:37 +0000370 _exit(-1);
371 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500372 close(progress_pipe[1]);
Dees Troye0a433a2013-12-02 04:10:37 +0000373 _exit(0);
Dees_Troy83bd4832013-05-04 12:39:56 +0000374 }
375 } else {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500376 // Parent side
377 unsigned long long fs, size_backup, files_backup, total_backup_size;
378 int first_data = 0;
379 double display_percent, progress_percent;
380 char file_progress[1024];
381 char size_progress[1024];
382 files_backup = 0;
383 size_backup = 0;
384
bigbiff7abc5fe2015-01-17 16:53:12 -0500385 fork_pid = tar_fork_pid;
386
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500387 // Parent closes output side
388 close(progress_pipe[1]);
389
390 // Read progress data from children
391 while (read(progress_pipe[0], &fs, sizeof(fs)) > 0) {
392 if (first_data == 0) {
393 // First incoming data is the file count
394 file_count = fs;
395 if (file_count == 0) file_count = 1; // prevent division by 0 below
396 first_data = 1;
397 } else if (first_data == 1) {
398 // Second incoming data is total size
399 total_backup_size = fs;
400 first_data = 2;
401 } else {
402 files_backup++;
403 size_backup += fs;
404 display_percent = (double)(files_backup) / (double)(file_count) * 100;
405 sprintf(file_progress, "%llu of %llu files, %i%%", files_backup, file_count, (int)(display_percent));
406#ifndef BUILD_TWRPTAR_MAIN
407 DataManager::SetValue("tw_file_progress", file_progress);
408 display_percent = (double)(size_backup + *other_backups_size) / (double)(*overall_size) * 100;
409 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (size_backup + *other_backups_size) / 1048576, *overall_size / 1048576, (int)(display_percent));
410 DataManager::SetValue("tw_size_progress", size_progress);
411 progress_percent = (display_percent / 100);
412 DataManager::SetProgress((float)(progress_percent));
413#endif //ndef BUILD_TWRPTAR_MAIN
414 }
415 }
416 close(progress_pipe[0]);
417#ifndef BUILD_TWRPTAR_MAIN
418 DataManager::SetValue("tw_file_progress", "");
419 DataManager::SetValue("tw_size_progress", "");
420
421 InfoManager backup_info(backup_folder + partition_name + ".info");
422 backup_info.SetValue("backup_size", size_backup);
423 if (use_compression && use_encryption)
424 backup_info.SetValue("backup_type", 3);
425 else if (use_encryption)
426 backup_info.SetValue("backup_type", 2);
427 else if (use_compression)
428 backup_info.SetValue("backup_type", 1);
429 else
430 backup_info.SetValue("backup_type", 0);
431 backup_info.SetValue("file_count", files_backup);
432 backup_info.SaveValues();
433#endif //ndef BUILD_TWRPTAR_MAIN
bigbiff7abc5fe2015-01-17 16:53:12 -0500434 if (TWFunc::Wait_For_Child(tar_fork_pid, &status, "createTarFork()") != 0)
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500435 return -1;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500436 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500437 return 0;
438}
439
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500440int twrpTar::extractTarFork(const unsigned long long *overall_size, unsigned long long *other_backups_size) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000441 int status = 0;
bigbiff7abc5fe2015-01-17 16:53:12 -0500442 pid_t rc_pid, tar_fork_pid;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500443 int progress_pipe[2], ret;
444
445 if (pipe(progress_pipe) < 0) {
446 LOGERR("Error creating progress tracking pipe\n");
447 return -1;
448 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000449
bigbiff7abc5fe2015-01-17 16:53:12 -0500450 tar_fork_pid = fork();
451 if (tar_fork_pid >= 0) // fork was successful
Dees_Troy83bd4832013-05-04 12:39:56 +0000452 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500453 if (tar_fork_pid == 0) // child process
Dees_Troy83bd4832013-05-04 12:39:56 +0000454 {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500455 close(progress_pipe[0]);
456 progress_pipe_fd = progress_pipe[1];
Dees_Troy83bd4832013-05-04 12:39:56 +0000457 if (TWFunc::Path_Exists(tarfn)) {
458 LOGINFO("Single archive\n");
459 if (extract() != 0)
460 _exit(-1);
461 else
462 _exit(0);
463 } else {
464 LOGINFO("Multiple archives\n");
465 string temp;
466 char actual_filename[255];
467 twrpTar tars[9];
468 pthread_t tar_thread[9];
469 pthread_attr_t tattr;
470 int thread_count = 0, i, start_thread_id = 1, ret, thread_error = 0;
471 void *thread_return;
472
473 basefn = tarfn;
474 temp = basefn + "%i%02i";
475 tarfn += "000";
476 if (!TWFunc::Path_Exists(tarfn)) {
477 LOGERR("Unable to locate '%s' or '%s'\n", basefn.c_str(), tarfn.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500478 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000479 _exit(-1);
480 }
481 if (TWFunc::Get_File_Type(tarfn) != 2) {
482 LOGINFO("First tar file '%s' not encrypted\n", tarfn.c_str());
483 tars[0].basefn = basefn;
484 tars[0].thread_id = 0;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500485 tars[0].progress_pipe_fd = progress_pipe_fd;
Dees_Troy83bd4832013-05-04 12:39:56 +0000486 if (extractMulti((void*)&tars[0]) != 0) {
487 LOGERR("Error extracting split archive.\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500488 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000489 _exit(-1);
490 }
491 } else {
492 start_thread_id = 0;
493 }
494 // Start threading encrypted restores
495 if (pthread_attr_init(&tattr)) {
496 LOGERR("Unable to pthread_attr_init\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500497 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000498 _exit(-1);
499 }
500 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
501 LOGERR("Error setting pthread_attr_setdetachstate\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500502 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000503 _exit(-1);
504 }
505 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
506 LOGERR("Error setting pthread_attr_setscope\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500507 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000508 _exit(-1);
509 }
510 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
511 LOGERR("Error setting pthread_attr_setstacksize\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500512 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000513 _exit(-1);
514 }*/
515 for (i = start_thread_id; i < 9; i++) {
516 sprintf(actual_filename, temp.c_str(), i, 0);
517 if (TWFunc::Path_Exists(actual_filename)) {
518 thread_count++;
519 tars[i].basefn = basefn;
Ethan Yonker924a80b2014-04-02 10:54:12 -0500520 tars[i].setpassword(password);
Dees_Troy83bd4832013-05-04 12:39:56 +0000521 tars[i].thread_id = i;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500522 tars[i].progress_pipe_fd = progress_pipe_fd;
Dees_Troy83bd4832013-05-04 12:39:56 +0000523 LOGINFO("Creating extract thread ID %i\n", i);
524 ret = pthread_create(&tar_thread[i], &tattr, extractMulti, (void*)&tars[i]);
525 if (ret) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500526 LOGINFO("Unable to create %i thread for extraction! %i\nContinuing in same thread (restore will be slower).\n", i, ret);
Dees_Troy83bd4832013-05-04 12:39:56 +0000527 if (extractMulti((void*)&tars[i]) != 0) {
528 LOGERR("Error extracting backup in thread %i.\n", i);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500529 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000530 _exit(-1);
531 } else {
532 tars[i].thread_id = i + 1;
533 }
534 }
535 usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason.
536 } else {
537 break;
538 }
539 }
540 for (i = start_thread_id; i < thread_count + start_thread_id; i++) {
541 if (tars[i].thread_id == i) {
542 if (pthread_join(tar_thread[i], &thread_return)) {
543 LOGERR("Error joining thread %i\n", i);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500544 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000545 _exit(-1);
546 } else {
547 LOGINFO("Joined thread %i.\n", i);
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600548 ret = *((int *)thread_return);
Dees_Troy83bd4832013-05-04 12:39:56 +0000549 if (ret != 0) {
550 thread_error = 1;
551 LOGERR("Thread %i returned an error %i.\n", i, ret);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500552 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000553 _exit(-1);
554 }
555 }
556 } else {
557 LOGINFO("Skipping joining thread %i because of pthread failure.\n", i);
558 }
559 }
560 if (thread_error) {
561 LOGERR("Error returned by one or more threads.\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500562 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000563 _exit(-1);
564 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500565 LOGINFO("Finished encrypted restore.\n");
566 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000567 _exit(0);
568 }
569 }
570 else // parent process
571 {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500572 unsigned long long fs, size_backup;
573 double display_percent, progress_percent;
574 char size_progress[1024];
575 size_backup = 0;
576
577 // Parent closes output side
578 close(progress_pipe[1]);
579
580 // Read progress data from children
581 while (read(progress_pipe[0], &fs, sizeof(fs)) > 0) {
582 size_backup += fs;
583 display_percent = (double)(size_backup + *other_backups_size) / (double)(*overall_size) * 100;
584 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (size_backup + *other_backups_size) / 1048576, *overall_size / 1048576, (int)(display_percent));
585 progress_percent = (display_percent / 100);
586#ifndef BUILD_TWRPTAR_MAIN
587 DataManager::SetValue("tw_size_progress", size_progress);
588 DataManager::SetProgress((float)(progress_percent));
589#endif //ndef BUILD_TWRPTAR_MAIN
590 }
591 close(progress_pipe[0]);
592#ifndef BUILD_TWRPTAR_MAIN
593 DataManager::SetValue("tw_file_progress", "");
594#endif //ndef BUILD_TWRPTAR_MAIN
595 *other_backups_size += size_backup;
596
bigbiff7abc5fe2015-01-17 16:53:12 -0500597 if (TWFunc::Wait_For_Child(tar_fork_pid, &status, "extractTarFork()") != 0)
Dees_Troy83bd4832013-05-04 12:39:56 +0000598 return -1;
599 }
600 }
601 else // fork has failed
602 {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500603 close(progress_pipe[0]);
604 close(progress_pipe[1]);
Dees_Troy2673cec2013-04-02 20:22:16 +0000605 LOGINFO("extract tar failed to fork.\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500606 return -1;
607 }
608 return 0;
609}
610
Dees_Troy83bd4832013-05-04 12:39:56 +0000611int twrpTar::Generate_TarList(string Path, std::vector<TarListStruct> *TarList, unsigned long long *Target_Size, unsigned *thread_id) {
612 DIR* d;
613 struct dirent* de;
614 struct stat st;
615 string FileName;
616 struct TarListStruct TarItem;
617 string::size_type i;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500618 int ret, file_count;
619 file_count = 0;
Dees_Troy83bd4832013-05-04 12:39:56 +0000620
Dees_Troy83bd4832013-05-04 12:39:56 +0000621 d = opendir(Path.c_str());
622 if (d == NULL) {
Dees Troye0a433a2013-12-02 04:10:37 +0000623 LOGERR("Error opening '%s' -- error: %s\n", Path.c_str(), strerror(errno));
Dees_Troy83bd4832013-05-04 12:39:56 +0000624 closedir(d);
625 return -1;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500626 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000627 while ((de = readdir(d)) != NULL) {
Matt Mower50248ab2014-03-31 15:58:40 -0500628 FileName = Path + "/" + de->d_name;
Matt Mowerbb81e5d2014-03-20 18:05:41 -0500629
Matt Mower50248ab2014-03-31 15:58:40 -0500630 if (de->d_type == DT_BLK || de->d_type == DT_CHR || du.check_skip_dirs(FileName))
Dees_Troy83bd4832013-05-04 12:39:56 +0000631 continue;
632 TarItem.fn = FileName;
633 TarItem.thread_id = *thread_id;
Matt Mower50248ab2014-03-31 15:58:40 -0500634 if (de->d_type == DT_DIR) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000635 TarList->push_back(TarItem);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500636 ret = Generate_TarList(FileName, TarList, Target_Size, thread_id);
637 if (ret < 0)
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500638 return -1;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500639 file_count += ret;
Dees_Troy83bd4832013-05-04 12:39:56 +0000640 } else if (de->d_type == DT_REG || de->d_type == DT_LNK) {
641 stat(FileName.c_str(), &st);
642 TarList->push_back(TarItem);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500643 if (de->d_type == DT_REG) {
644 file_count++;
Dees_Troy83bd4832013-05-04 12:39:56 +0000645 Archive_Current_Size += st.st_size;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500646 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000647 if (Archive_Current_Size != 0 && *Target_Size != 0 && Archive_Current_Size > *Target_Size) {
648 *thread_id = *thread_id + 1;
649 Archive_Current_Size = 0;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500650 }
651 }
652 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000653 closedir(d);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500654 return file_count;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500655}
656
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500657int twrpTar::extractTar() {
Dees_Troye34c1332013-02-06 19:13:00 +0000658 char* charRootDir = (char*) tardir.c_str();
Dees_Troy83bd4832013-05-04 12:39:56 +0000659 if (openTar() == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500660 return -1;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500661 if (tar_extract_all(t, charRootDir, &progress_pipe_fd) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000662 LOGERR("Unable to extract tar archive '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500663 return -1;
664 }
665 if (tar_close(t) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000666 LOGERR("Unable to close tar file\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500667 return -1;
668 }
669 return 0;
670}
671
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500672int twrpTar::extract() {
Dees_Troy83bd4832013-05-04 12:39:56 +0000673 Archive_Current_Type = TWFunc::Get_File_Type(tarfn);
n0d33b511632013-03-06 21:14:15 +0200674
675 if (Archive_Current_Type == 1) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500676 //if you return the extractTGZ function directly, stack crashes happen
Dees_Troy2673cec2013-04-02 20:22:16 +0000677 LOGINFO("Extracting gzipped tar\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000678 int ret = extractTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500679 return ret;
Dees_Troy83bd4832013-05-04 12:39:56 +0000680 } else if (Archive_Current_Type == 2) {
Ethan Yonker87af5632014-02-10 11:56:35 -0600681 int ret = TWFunc::Try_Decrypting_File(tarfn, password);
Dees_Troy83bd4832013-05-04 12:39:56 +0000682 if (ret < 1) {
683 LOGERR("Failed to decrypt tar file '%s'\n", tarfn.c_str());
684 return -1;
685 }
686 if (ret == 1) {
687 LOGERR("Decrypted file is not in tar format.\n");
688 return -1;
689 }
690 if (ret == 3) {
691 LOGINFO("Extracting encrypted and compressed tar.\n");
692 Archive_Current_Type = 3;
693 } else
694 LOGINFO("Extracting encrypted tar.\n");
695 return extractTar();
696 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000697 LOGINFO("Extracting uncompressed tar\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500698 return extractTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500699 }
700}
701
Dees Troye0a433a2013-12-02 04:10:37 +0000702int twrpTar::tarList(std::vector<TarListStruct> *TarList, unsigned thread_id) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000703 struct stat st;
704 char buf[PATH_MAX];
705 int list_size = TarList->size(), i = 0, archive_count = 0;
706 string temp;
707 char actual_filename[PATH_MAX];
Dees Troye0a433a2013-12-02 04:10:37 +0000708 char *ptr;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500709 unsigned long long fs;
Dees_Troye34c1332013-02-06 19:13:00 +0000710
Dees Troye0a433a2013-12-02 04:10:37 +0000711 if (split_archives) {
712 basefn = tarfn;
713 temp = basefn + "%i%02i";
714 sprintf(actual_filename, temp.c_str(), thread_id, archive_count);
715 tarfn = actual_filename;
716 include_root_dir = true;
717 } else {
718 include_root_dir = false;
719 }
720 LOGINFO("Creating tar file '%s'\n", tarfn.c_str());
Dees_Troy83bd4832013-05-04 12:39:56 +0000721 if (createTar() != 0) {
722 LOGERR("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id);
723 return -2;
724 }
725 Archive_Current_Size = 0;
726
727 while (i < list_size) {
728 if (TarList->at(i).thread_id == thread_id) {
729 strcpy(buf, TarList->at(i).fn.c_str());
bigbiff bigbiffec8fc282014-03-16 20:32:50 -0400730 lstat(buf, &st);
731 if (S_ISREG(st.st_mode)) { // item is a regular file
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500732 fs = (unsigned long long)(st.st_size);
733 if (split_archives && Archive_Current_Size + fs > MAX_ARCHIVE_SIZE) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000734 if (closeTar() != 0) {
735 LOGERR("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id);
736 return -3;
737 }
738 archive_count++;
Dees Troye0a433a2013-12-02 04:10:37 +0000739 gui_print("Splitting thread ID %i into archive %i\n", thread_id, archive_count + 1);
Dees_Troy83bd4832013-05-04 12:39:56 +0000740 if (archive_count > 99) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000741 LOGERR("Too many archives for thread %i\n", thread_id);
742 return -4;
743 }
744 sprintf(actual_filename, temp.c_str(), thread_id, archive_count);
745 tarfn = actual_filename;
746 if (createTar() != 0) {
747 LOGERR("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id);
748 return -2;
749 }
750 Archive_Current_Size = 0;
751 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500752 Archive_Current_Size += fs;
753 write(progress_pipe_fd, &fs, sizeof(fs));
Dees_Troy83bd4832013-05-04 12:39:56 +0000754 }
Dees Troye0a433a2013-12-02 04:10:37 +0000755 LOGINFO("addFile '%s' including root: %i\n", buf, include_root_dir);
756 if (addFile(buf, include_root_dir) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000757 LOGERR("Error adding file '%s' to '%s'\n", buf, tarfn.c_str());
758 return -1;
759 }
760 }
761 i++;
762 }
763 if (closeTar() != 0) {
764 LOGERR("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id);
765 return -3;
766 }
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600767 LOGINFO("Thread id %i tarList done, %i archives.\n", thread_id, archive_count);
Dees_Troye34c1332013-02-06 19:13:00 +0000768 return 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500769}
770
Dees_Troy83bd4832013-05-04 12:39:56 +0000771void* twrpTar::createList(void *cookie) {
772
773 twrpTar* threadTar = (twrpTar*) cookie;
Ethan Yonkereae42092014-03-07 15:33:13 -0600774 if (threadTar->tarList(threadTar->ItemList, threadTar->thread_id) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000775 LOGINFO("ERROR tarList for thread ID %i\n", threadTar->thread_id);
776 return (void*)-2;
777 }
778 LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id);
779 return (void*)0;
780}
781
782void* twrpTar::extractMulti(void *cookie) {
783
784 twrpTar* threadTar = (twrpTar*) cookie;
785 int archive_count = 0;
786 string temp = threadTar->basefn + "%i%02i";
787 char actual_filename[255];
788 sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count);
789 while (TWFunc::Path_Exists(actual_filename)) {
790 threadTar->tarfn = actual_filename;
791 if (threadTar->extract() != 0) {
792 LOGINFO("Error extracting '%s' in thread ID %i\n", actual_filename, threadTar->thread_id);
793 return (void*)-2;
794 }
795 archive_count++;
796 if (archive_count > 99)
797 break;
798 sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count);
799 }
800 LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id);
801 return (void*)0;
802}
803
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500804int twrpTar::addFilesToExistingTar(vector <string> files, string fn) {
805 char* charTarFile = (char*) fn.c_str();
806
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200807 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 -0500808 return -1;
809 removeEOT(charTarFile);
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200810 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 -0500811 return -1;
812 for (unsigned int i = 0; i < files.size(); ++i) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500813 char* file = (char*) files.at(i).c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500814 if (tar_append_file(t, file, file) == -1)
815 return -1;
816 }
817 if (tar_append_eof(t) == -1)
818 return -1;
819 if (tar_close(t) == -1)
820 return -1;
821 return 0;
822}
823
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500824int twrpTar::createTar() {
825 char* charTarFile = (char*) tarfn.c_str();
Dees_Troye34c1332013-02-06 19:13:00 +0000826 char* charRootDir = (char*) tardir.c_str();
Dees_Troye34c1332013-02-06 19:13:00 +0000827 static tartype_t type = { open, close, read, write_tar };
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500828
Dees_Troy83bd4832013-05-04 12:39:56 +0000829 if (use_encryption && use_compression) {
830 // Compressed and encrypted
831 Archive_Current_Type = 3;
832 LOGINFO("Using encryption and compression...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000833 int i, pipes[4];
834
835 if (pipe(pipes) < 0) {
836 LOGERR("Error creating first pipe\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500837 return -1;
838 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000839 if (pipe(pipes + 2) < 0) {
840 LOGERR("Error creating second pipe\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500841 return -1;
Dees_Troy83bd4832013-05-04 12:39:56 +0000842 }
Dees Troy9d723272014-04-07 17:13:10 +0000843 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);
844 if (output_fd < 0) {
845 LOGERR("Failed to open '%s'\n", tarfn.c_str());
846 for (i = 0; i < 4; i++)
847 close(pipes[i]); // close all
848 return -1;
849 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000850 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400851
Dees_Troy83bd4832013-05-04 12:39:56 +0000852 if (pigz_pid < 0) {
853 LOGERR("pigz fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +0000854 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000855 for (i = 0; i < 4; i++)
856 close(pipes[i]); // close all
857 return -1;
858 } else if (pigz_pid == 0) {
859 // pigz Child
860 close(pipes[1]);
861 close(pipes[2]);
862 close(0);
863 dup2(pipes[0], 0);
864 close(1);
865 dup2(pipes[3], 1);
866 if (execlp("pigz", "pigz", "-", NULL) < 0) {
867 LOGERR("execlp pigz ERROR!\n");
Dees Troy9d723272014-04-07 17:13:10 +0000868 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000869 close(pipes[0]);
870 close(pipes[3]);
871 _exit(-1);
872 }
873 } else {
874 // Parent
875 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400876
Dees_Troy83bd4832013-05-04 12:39:56 +0000877 if (oaes_pid < 0) {
878 LOGERR("openaes fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +0000879 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000880 for (i = 0; i < 4; i++)
881 close(pipes[i]); // close all
882 return -1;
883 } else if (oaes_pid == 0) {
884 // openaes Child
Dees_Troy83bd4832013-05-04 12:39:56 +0000885 close(pipes[0]);
886 close(pipes[1]);
887 close(pipes[3]);
888 close(0);
889 dup2(pipes[2], 0);
890 close(1);
891 dup2(output_fd, 1);
Ethan Yonker87af5632014-02-10 11:56:35 -0600892 if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000893 LOGERR("execlp openaes ERROR!\n");
894 close(pipes[2]);
895 close(output_fd);
896 _exit(-1);
897 }
898 } else {
899 // Parent
900 close(pipes[0]);
901 close(pipes[2]);
902 close(pipes[3]);
903 fd = pipes[1];
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200904 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 +0000905 close(fd);
906 LOGERR("tar_fdopen failed\n");
907 return -1;
908 }
909 return 0;
910 }
911 }
912 } else if (use_compression) {
913 // Compressed
914 Archive_Current_Type = 1;
915 LOGINFO("Using compression...\n");
916 int pigzfd[2];
Dees Troy9d723272014-04-07 17:13:10 +0000917 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);
918 if (output_fd < 0) {
919 LOGERR("Failed to open '%s'\n", tarfn.c_str());
920 close(pigzfd[0]);
921 return -1;
922 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000923
924 if (pipe(pigzfd) < 0) {
925 LOGERR("Error creating pipe\n");
Dees Troy9d723272014-04-07 17:13:10 +0000926 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000927 return -1;
928 }
929 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400930
Dees_Troy83bd4832013-05-04 12:39:56 +0000931 if (pigz_pid < 0) {
932 LOGERR("fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +0000933 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000934 close(pigzfd[0]);
935 close(pigzfd[1]);
936 return -1;
937 } else if (pigz_pid == 0) {
938 // Child
939 close(pigzfd[1]); // close unused output pipe
Dees_Troy83bd4832013-05-04 12:39:56 +0000940 dup2(pigzfd[0], 0); // remap stdin
941 dup2(output_fd, 1); // remap stdout to output file
942 if (execlp("pigz", "pigz", "-", NULL) < 0) {
943 LOGERR("execlp pigz ERROR!\n");
944 close(output_fd);
945 close(pigzfd[0]);
946 _exit(-1);
947 }
948 } else {
949 // Parent
950 close(pigzfd[0]); // close parent input
951 fd = pigzfd[1]; // copy parent output
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200952 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 +0000953 close(fd);
954 LOGERR("tar_fdopen failed\n");
955 return -1;
956 }
957 }
958 } else if (use_encryption) {
959 // Encrypted
960 Archive_Current_Type = 2;
961 LOGINFO("Using encryption...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000962 int oaesfd[2];
Dees Troy9d723272014-04-07 17:13:10 +0000963 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);
964 if (output_fd < 0) {
965 LOGERR("Failed to open '%s'\n", tarfn.c_str());
966 return -1;
967 }
968 if (pipe(oaesfd) < 0) {
969 LOGERR("Error creating pipe\n");
970 close(output_fd);
971 return -1;
972 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000973 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400974
Dees_Troy83bd4832013-05-04 12:39:56 +0000975 if (oaes_pid < 0) {
976 LOGERR("fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +0000977 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000978 close(oaesfd[0]);
979 close(oaesfd[1]);
980 return -1;
981 } else if (oaes_pid == 0) {
982 // Child
983 close(oaesfd[1]); // close unused
Dees_Troy83bd4832013-05-04 12:39:56 +0000984 dup2(oaesfd[0], 0); // remap stdin
985 dup2(output_fd, 1); // remap stdout to output file
Ethan Yonker87af5632014-02-10 11:56:35 -0600986 if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000987 LOGERR("execlp openaes ERROR!\n");
988 close(output_fd);
989 close(oaesfd[0]);
990 _exit(-1);
991 }
992 } else {
993 // Parent
994 close(oaesfd[0]); // close parent input
995 fd = oaesfd[1]; // copy parent output
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200996 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 +0000997 close(fd);
998 LOGERR("tar_fdopen failed\n");
999 return -1;
1000 }
1001 return 0;
1002 }
1003 } else {
1004 // Not compressed or encrypted
1005 init_libtar_buffer(0);
Vojtech Bocek25fd68d2013-08-27 03:10:10 +02001006 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 +00001007 LOGERR("tar_open error opening '%s'\n", tarfn.c_str());
1008 return -1;
1009 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001010 }
1011 return 0;
1012}
1013
Dees_Troy83bd4832013-05-04 12:39:56 +00001014int twrpTar::openTar() {
Dees_Troye34c1332013-02-06 19:13:00 +00001015 char* charRootDir = (char*) tardir.c_str();
1016 char* charTarFile = (char*) tarfn.c_str();
Dees_Troy83bd4832013-05-04 12:39:56 +00001017 string Password;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001018
Dees_Troy83bd4832013-05-04 12:39:56 +00001019 if (Archive_Current_Type == 3) {
1020 LOGINFO("Opening encrypted and compressed backup...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +00001021 int i, pipes[4];
Dees Troy9d723272014-04-07 17:13:10 +00001022 int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE);
1023 if (input_fd < 0) {
1024 LOGERR("Failed to open '%s'\n", tarfn.c_str());
1025 return -1;
1026 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001027
1028 if (pipe(pipes) < 0) {
1029 LOGERR("Error creating first pipe\n");
Dees Troy9d723272014-04-07 17:13:10 +00001030 close(input_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001031 return -1;
1032 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001033 if (pipe(pipes + 2) < 0) {
1034 LOGERR("Error creating second pipe\n");
Dees Troy9d723272014-04-07 17:13:10 +00001035 close(pipes[0]);
1036 close(pipes[1]);
1037 close(input_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001038 return -1;
1039 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001040 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001041
Dees_Troy83bd4832013-05-04 12:39:56 +00001042 if (oaes_pid < 0) {
1043 LOGERR("pigz fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +00001044 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001045 for (i = 0; i < 4; i++)
1046 close(pipes[i]); // close all
1047 return -1;
1048 } else if (oaes_pid == 0) {
1049 // openaes Child
1050 close(pipes[0]); // Close pipes that are not used by this child
1051 close(pipes[2]);
1052 close(pipes[3]);
Dees_Troy83bd4832013-05-04 12:39:56 +00001053 close(0);
1054 dup2(input_fd, 0);
1055 close(1);
1056 dup2(pipes[1], 1);
Ethan Yonker87af5632014-02-10 11:56:35 -06001057 if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +00001058 LOGERR("execlp openaes ERROR!\n");
1059 close(input_fd);
1060 close(pipes[1]);
1061 _exit(-1);
1062 }
1063 } else {
1064 // Parent
1065 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001066
Dees_Troy83bd4832013-05-04 12:39:56 +00001067 if (pigz_pid < 0) {
1068 LOGERR("openaes fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +00001069 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001070 for (i = 0; i < 4; i++)
1071 close(pipes[i]); // close all
1072 return -1;
1073 } else if (pigz_pid == 0) {
1074 // pigz Child
1075 close(pipes[1]); // Close pipes not used by this child
1076 close(pipes[2]);
1077 close(0);
1078 dup2(pipes[0], 0);
1079 close(1);
1080 dup2(pipes[3], 1);
1081 if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) {
1082 LOGERR("execlp pigz ERROR!\n");
Dees Troy9d723272014-04-07 17:13:10 +00001083 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001084 close(pipes[0]);
1085 close(pipes[3]);
1086 _exit(-1);
1087 }
1088 } else {
1089 // Parent
1090 close(pipes[0]); // Close pipes not used by parent
1091 close(pipes[1]);
1092 close(pipes[3]);
1093 fd = pipes[2];
Vojtech Bocek25fd68d2013-08-27 03:10:10 +02001094 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 +00001095 close(fd);
1096 LOGERR("tar_fdopen failed\n");
1097 return -1;
1098 }
1099 }
1100 }
1101 } else if (Archive_Current_Type == 2) {
1102 LOGINFO("Opening encrypted backup...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +00001103 int oaesfd[2];
Dees Troy9d723272014-04-07 17:13:10 +00001104 int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE);
1105 if (input_fd < 0) {
1106 LOGERR("Failed to open '%s'\n", tarfn.c_str());
1107 return -1;
1108 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001109
Dees Troy9d723272014-04-07 17:13:10 +00001110 if (pipe(oaesfd) < 0) {
1111 LOGERR("Error creating pipe\n");
1112 close(input_fd);
1113 return -1;
1114 }
1115
Dees_Troy83bd4832013-05-04 12:39:56 +00001116 oaes_pid = fork();
1117 if (oaes_pid < 0) {
1118 LOGERR("fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +00001119 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001120 close(oaesfd[0]);
1121 close(oaesfd[1]);
1122 return -1;
1123 } else if (oaes_pid == 0) {
1124 // Child
1125 close(oaesfd[0]); // Close unused pipe
Dees_Troy83bd4832013-05-04 12:39:56 +00001126 close(0); // close stdin
1127 dup2(oaesfd[1], 1); // remap stdout
1128 dup2(input_fd, 0); // remap input fd to stdin
Ethan Yonker87af5632014-02-10 11:56:35 -06001129 if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +00001130 LOGERR("execlp openaes ERROR!\n");
1131 close(input_fd);
1132 close(oaesfd[1]);
1133 _exit(-1);
1134 }
1135 } else {
1136 // Parent
1137 close(oaesfd[1]); // close parent output
1138 fd = oaesfd[0]; // copy parent input
Vojtech Bocek25fd68d2013-08-27 03:10:10 +02001139 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 +00001140 close(fd);
1141 LOGERR("tar_fdopen failed\n");
1142 return -1;
1143 }
1144 }
1145 } else if (Archive_Current_Type == 1) {
1146 LOGINFO("Opening as a gzip...\n");
1147 int pigzfd[2];
Dees Troy9d723272014-04-07 17:13:10 +00001148 int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE);
1149 if (input_fd < 0) {
1150 LOGERR("Failed to open '%s'\n", tarfn.c_str());
1151 return -1;
1152 }
1153 if (pipe(pigzfd) < 0) {
1154 LOGERR("Error creating pipe\n");
1155 close(input_fd);
1156 return -1;
1157 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001158
1159 pigz_pid = fork();
1160 if (pigz_pid < 0) {
1161 LOGERR("fork() failed\n");
Dees Troy9d723272014-04-07 17:13:10 +00001162 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001163 close(pigzfd[0]);
1164 close(pigzfd[1]);
1165 return -1;
1166 } else if (pigz_pid == 0) {
1167 // Child
1168 close(pigzfd[0]);
Dees_Troy83bd4832013-05-04 12:39:56 +00001169 dup2(input_fd, 0); // remap input fd to stdin
1170 dup2(pigzfd[1], 1); // remap stdout
1171 if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) {
1172 close(pigzfd[1]);
1173 close(input_fd);
1174 LOGERR("execlp openaes ERROR!\n");
1175 _exit(-1);
1176 }
1177 } else {
1178 // Parent
1179 close(pigzfd[1]); // close parent output
1180 fd = pigzfd[0]; // copy parent input
Vojtech Bocek25fd68d2013-08-27 03:10:10 +02001181 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 +00001182 close(fd);
1183 LOGERR("tar_fdopen failed\n");
1184 return -1;
1185 }
1186 }
Vojtech Bocek25fd68d2013-08-27 03:10:10 +02001187 } 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 +00001188 LOGERR("Unable to open tar archive '%s'\n", charTarFile);
1189 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001190 }
1191 return 0;
1192}
1193
1194string twrpTar::Strip_Root_Dir(string Path) {
1195 string temp;
1196 size_t slash;
1197
1198 if (Path.substr(0, 1) == "/")
1199 temp = Path.substr(1, Path.size() - 1);
1200 else
1201 temp = Path;
1202 slash = temp.find("/");
1203 if (slash == string::npos)
1204 return temp;
1205 else {
1206 string stripped;
1207
1208 stripped = temp.substr(slash, temp.size() - slash);
1209 return stripped;
1210 }
1211 return temp;
1212}
1213
1214int twrpTar::addFile(string fn, bool include_root) {
1215 char* charTarFile = (char*) fn.c_str();
1216 if (include_root) {
1217 if (tar_append_file(t, charTarFile, NULL) == -1)
1218 return -1;
1219 } else {
1220 string temp = Strip_Root_Dir(fn);
1221 char* charTarPath = (char*) temp.c_str();
1222 if (tar_append_file(t, charTarFile, charTarPath) == -1)
1223 return -1;
1224 }
1225 return 0;
1226}
1227
Dees_Troy83bd4832013-05-04 12:39:56 +00001228int twrpTar::closeTar() {
Dees_Troye34c1332013-02-06 19:13:00 +00001229 flush_libtar_buffer(t->fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001230 if (tar_append_eof(t) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001231 LOGERR("tar_append_eof(): %s\n", strerror(errno));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001232 tar_close(t);
1233 return -1;
1234 }
1235 if (tar_close(t) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001236 LOGERR("Unable to close tar archive: '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001237 return -1;
1238 }
Dees_Troy2727b992013-08-14 20:09:30 +00001239 if (Archive_Current_Type > 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001240 close(fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001241 int status;
1242 if (pigz_pid > 0 && TWFunc::Wait_For_Child(pigz_pid, &status, "pigz") != 0)
1243 return -1;
1244 if (oaes_pid > 0 && TWFunc::Wait_For_Child(oaes_pid, &status, "openaes") != 0)
1245 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001246 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001247 free_libtar_buffer();
Dees Troye0a433a2013-12-02 04:10:37 +00001248 if (use_compression && !use_encryption) {
1249 string gzname = tarfn + ".gz";
1250 if (TWFunc::Path_Exists(gzname)) {
1251 rename(gzname.c_str(), tarfn.c_str());
1252 }
1253 }
1254 if (TWFunc::Get_File_Size(tarfn) == 0) {
1255 LOGERR("Backup file size for '%s' is 0 bytes.\n", tarfn.c_str());
1256 return -1;
1257 }
Ethan Yonker960f0302014-12-21 21:54:00 -06001258#ifndef BUILD_TWRPTAR_MAIN
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001259 tw_set_default_metadata(tarfn.c_str());
Ethan Yonker960f0302014-12-21 21:54:00 -06001260#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001261 return 0;
1262}
1263
1264int twrpTar::removeEOT(string tarFile) {
1265 char* charTarFile = (char*) tarFile.c_str();
1266 off_t tarFileEnd;
1267 while (th_read(t) == 0) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001268 if (TH_ISREG(t))
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001269 tar_skip_regfile(t);
1270 tarFileEnd = lseek(t->fd, 0, SEEK_CUR);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001271 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001272 if (tar_close(t) == -1)
1273 return -1;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001274 if (truncate(charTarFile, tarFileEnd) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001275 return -1;
1276 return 0;
1277}
1278
n0d33b511632013-03-06 21:14:15 +02001279int twrpTar::entryExists(string entry) {
1280 char* searchstr = (char*)entry.c_str();
1281 int ret;
1282
Dees_Troy83bd4832013-05-04 12:39:56 +00001283 Archive_Current_Type = TWFunc::Get_File_Type(tarfn);
n0d33b511632013-03-06 21:14:15 +02001284
Dees_Troy83bd4832013-05-04 12:39:56 +00001285 if (openTar() == -1)
n0d33b511632013-03-06 21:14:15 +02001286 ret = 0;
1287 else
1288 ret = tar_find(t, searchstr);
1289
Dees_Troy83bd4832013-05-04 12:39:56 +00001290 if (closeTar() != 0)
1291 LOGINFO("Unable to close tar after searching for entry.\n");
n0d33b511632013-03-06 21:14:15 +02001292
1293 return ret;
1294}
1295
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001296unsigned long long twrpTar::get_size() {
1297 if (TWFunc::Path_Exists(tarfn)) {
1298 LOGINFO("Single archive\n");
1299 int type = 0;
1300 return uncompressedSize(tarfn, &type);
1301 } else {
1302 LOGINFO("Multiple archives\n");
1303 string temp;
1304 char actual_filename[255];
1305 int archive_count = 0, i, type = 0, temp_type = 0;
1306 unsigned long long total_restore_size = 0;
1307
1308 basefn = tarfn;
1309 temp = basefn + "%i%02i";
1310 tarfn += "000";
1311 thread_id = 0;
1312 sprintf(actual_filename, temp.c_str(), thread_id, archive_count);
1313 if (!TWFunc::Path_Exists(actual_filename)) {
1314 LOGERR("Unable to locate '%s' or '%s'\n", basefn.c_str(), tarfn.c_str());
1315 return 0;
1316 }
1317 for (i = 0; i < 9; i++) {
1318 archive_count = 0;
1319 sprintf(actual_filename, temp.c_str(), i, archive_count);
1320 while (TWFunc::Path_Exists(actual_filename)) {
1321 total_restore_size += uncompressedSize(actual_filename, &temp_type);
1322 if (temp_type > type)
1323 type = temp_type;
1324 archive_count++;
1325 if (archive_count > 99)
1326 break;
1327 sprintf(actual_filename, temp.c_str(), i, archive_count);
1328 }
1329 }
1330#ifndef BUILD_TWRPTAR_MAIN
1331 InfoManager backup_info(backup_folder + "/" + partition_name + ".info");
1332 backup_info.SetValue("backup_size", total_restore_size);
1333 backup_info.SetValue("backup_type", type);
1334 backup_info.SaveValues();
1335#endif //ndef BUILD_TWRPTAR_MAIN
1336 return total_restore_size;
1337 }
1338 return 0;
1339}
1340
1341unsigned long long twrpTar::uncompressedSize(string filename, int *archive_type) {
1342 int type = 0;
1343 unsigned long long total_size = 0;
1344 string Tar, Command, result;
1345 vector<string> split;
1346
1347 Tar = TWFunc::Get_Filename(filename);
1348 type = TWFunc::Get_File_Type(filename);
1349 if (type == 0) {
1350 total_size = TWFunc::Get_File_Size(filename);
1351 *archive_type = 0;
1352 } else if (type == 1) {
1353 // Compressed
1354 Command = "pigz -l '" + filename + "'";
1355 /* if we set Command = "pigz -l " + tarfn + " | sed '1d' | cut -f5 -d' '";
1356 we get the uncompressed size at once. */
1357 TWFunc::Exec_Cmd(Command, result);
1358 if (!result.empty()) {
1359 /* Expected output:
1360 compressed original reduced name
1361 95855838 179403776 -1.3% data.yaffs2.win
1362 ^
1363 split[5]
1364 */
1365 split = TWFunc::split_string(result, ' ', true);
1366 if (split.size() > 4)
1367 total_size = atoi(split[5].c_str());
1368 }
1369 *archive_type = 1;
1370 } else if (type == 2) {
1371 // File is encrypted and may be compressed
1372 int ret = TWFunc::Try_Decrypting_File(filename, password);
1373 *archive_type = 2;
1374 if (ret < 1) {
1375 LOGERR("Failed to decrypt tar file '%s'\n", filename.c_str());
1376 total_size = TWFunc::Get_File_Size(filename);
1377 } else if (ret == 1) {
1378 LOGERR("Decrypted file is not in tar format.\n");
1379 total_size = TWFunc::Get_File_Size(filename);
1380 } else if (ret == 3) {
1381 *archive_type = 3;
1382 Command = "openaes dec --key \"" + password + "\" --in '" + filename + "' | pigz -l";
1383 /* if we set Command = "pigz -l " + tarfn + " | sed '1d' | cut -f5 -d' '";
1384 we get the uncompressed size at once. */
1385 TWFunc::Exec_Cmd(Command, result);
1386 if (!result.empty()) {
1387 LOGINFO("result was: '%s'\n", result.c_str());
1388 /* Expected output:
1389 compressed original reduced name
1390 95855838 179403776 -1.3% data.yaffs2.win
1391 ^
1392 split[5]
1393 */
1394 split = TWFunc::split_string(result, ' ', true);
1395 if (split.size() > 4)
1396 total_size = atoi(split[5].c_str());
1397 }
1398 } else {
1399 total_size = TWFunc::Get_File_Size(filename);
1400 }
1401 }
1402
1403 return total_size;
1404}
1405
Dees_Troye34c1332013-02-06 19:13:00 +00001406extern "C" ssize_t write_tar(int fd, const void *buffer, size_t size) {
1407 return (ssize_t) write_libtar_buffer(fd, buffer, size);
Dees_Troy40bbcf82013-02-12 15:01:53 +00001408}