blob: 76c8920ae24911cfb1197047f65aa417fda51c34 [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 Yonker74db1572015-10-28 12:44:49 -050048#include "gui/gui.hpp"
Ethan Yonker960f0302014-12-21 21:54:00 -060049extern "C" {
50 #include "set_metadata.h"
51}
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050052#endif //ndef BUILD_TWRPTAR_MAIN
bigbiff bigbiff9c754052013-01-09 09:09:08 -050053
54using namespace std;
55
Dees_Troy83bd4832013-05-04 12:39:56 +000056twrpTar::twrpTar(void) {
57 use_encryption = 0;
58 userdata_encryption = 0;
59 use_compression = 0;
60 split_archives = 0;
61 has_data_media = 0;
62 pigz_pid = 0;
63 oaes_pid = 0;
Dees Troye0a433a2013-12-02 04:10:37 +000064 Total_Backup_Size = 0;
Ethan Yonker1a147a42015-02-27 13:14:44 -060065 Archive_Current_Size = 0;
Dees Troye0a433a2013-12-02 04:10:37 +000066 include_root_dir = true;
Dees_Troy83bd4832013-05-04 12:39:56 +000067}
68
69twrpTar::~twrpTar(void) {
70 // Do nothing
71}
72
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -050073void twrpTar::setfn(string fn) {
74 tarfn = fn;
75}
76
77void twrpTar::setdir(string dir) {
78 tardir = dir;
79}
80
Dees Troye0a433a2013-12-02 04:10:37 +000081void twrpTar::setsize(unsigned long long backup_size) {
82 Total_Backup_Size = backup_size;
83}
84
Ethan Yonker87af5632014-02-10 11:56:35 -060085void twrpTar::setpassword(string pass) {
86 password = pass;
87}
88
bigbiff7abc5fe2015-01-17 16:53:12 -050089void twrpTar::Signal_Kill(int signum) {
90 _exit(255);
91}
92
93int 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 +000094 int status = 0;
bigbiff7abc5fe2015-01-17 16:53:12 -050095 pid_t rc_pid, tar_fork_pid;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050096 int progress_pipe[2], ret;
97
98 file_count = 0;
99
100 if (pipe(progress_pipe) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500101 LOGINFO("Error creating progress tracking pipe\n");
102 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500103 return -1;
104 }
bigbiff7abc5fe2015-01-17 16:53:12 -0500105 if ((tar_fork_pid = fork()) == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000106 LOGINFO("create tar failed to fork.\n");
Ethan Yonker74db1572015-10-28 12:44:49 -0500107 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500108 close(progress_pipe[0]);
109 close(progress_pipe[1]);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500110 return -1;
111 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500112
bigbiff7abc5fe2015-01-17 16:53:12 -0500113 if (tar_fork_pid == 0) {
114 // Child process
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500115 // Child closes input side of progress pipe
bigbiff7abc5fe2015-01-17 16:53:12 -0500116 signal(SIGUSR2, twrpTar::Signal_Kill);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500117 close(progress_pipe[0]);
118 progress_pipe_fd = progress_pipe[1];
119
Dees_Troy83bd4832013-05-04 12:39:56 +0000120 if (use_encryption || userdata_encryption) {
121 LOGINFO("Using encryption\n");
122 DIR* d;
123 struct dirent* de;
that2252d242015-04-03 22:33:04 +0200124 unsigned long long regular_size = 0, encrypt_size = 0, target_size = 0, total_size;
125 unsigned enc_thread_id = 1, regular_thread_id = 0, i, start_thread_id = 1, core_count = 1;
Dees_Troy83bd4832013-05-04 12:39:56 +0000126 int item_len, ret, thread_error = 0;
127 std::vector<TarListStruct> RegularList;
128 std::vector<TarListStruct> EncryptList;
129 string FileName;
130 struct TarListStruct TarItem;
131 twrpTar reg, enc[9];
132 struct stat st;
133 pthread_t enc_thread[9];
134 pthread_attr_t tattr;
135 void *thread_return;
136
137 core_count = sysconf(_SC_NPROCESSORS_CONF);
138 if (core_count > 8)
139 core_count = 8;
that2252d242015-04-03 22:33:04 +0200140 LOGINFO(" Core Count : %u\n", core_count);
Dees_Troy83bd4832013-05-04 12:39:56 +0000141 Archive_Current_Size = 0;
142
143 d = opendir(tardir.c_str());
144 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500145 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tardir)(strerror(errno)));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500146 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000147 _exit(-1);
148 }
149 // Figure out the size of all data to be encrypted and create a list of unencrypted files
150 while ((de = readdir(d)) != NULL) {
Matt Mower50248ab2014-03-31 15:58:40 -0500151 FileName = tardir + "/" + de->d_name;
152
153 if (de->d_type == DT_BLK || de->d_type == DT_CHR || du.check_skip_dirs(FileName))
Dees_Troy83bd4832013-05-04 12:39:56 +0000154 continue;
Matt Mower50248ab2014-03-31 15:58:40 -0500155 if (de->d_type == DT_DIR) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000156 item_len = strlen(de->d_name);
157 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 -0500158 ret = Generate_TarList(FileName, &RegularList, &target_size, &regular_thread_id);
159 if (ret < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500160 LOGINFO("Error in Generate_TarList with regular list!\n");
161 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000162 closedir(d);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500163 close(progress_pipe_fd);
164 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000165 _exit(-1);
166 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500167 file_count = (unsigned long long)(ret);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500168 regular_size += du.Get_Folder_Size(FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +0000169 } else {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500170 encrypt_size += du.Get_Folder_Size(FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +0000171 }
172 } else if (de->d_type == DT_REG) {
173 stat(FileName.c_str(), &st);
174 encrypt_size += (unsigned long long)(st.st_size);
175 }
176 }
177 closedir(d);
178
179 target_size = encrypt_size / core_count;
180 target_size++;
181 LOGINFO(" Unencrypted size: %llu\n", regular_size);
182 LOGINFO(" Encrypted size : %llu\n", encrypt_size);
183 LOGINFO(" Target size : %llu\n", target_size);
184 if (!userdata_encryption) {
185 enc_thread_id = 0;
186 start_thread_id = 0;
187 core_count--;
188 }
189 Archive_Current_Size = 0;
190
191 d = opendir(tardir.c_str());
192 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500193 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tardir)(strerror(errno)));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500194 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000195 _exit(-1);
196 }
197 // Divide up the encrypted file list for threading
198 while ((de = readdir(d)) != NULL) {
Matt Mower50248ab2014-03-31 15:58:40 -0500199 FileName = tardir + "/" + de->d_name;
200
201 if (de->d_type == DT_BLK || de->d_type == DT_CHR || du.check_skip_dirs(FileName))
Dees_Troy83bd4832013-05-04 12:39:56 +0000202 continue;
Matt Mower50248ab2014-03-31 15:58:40 -0500203 if (de->d_type == DT_DIR) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000204 item_len = strlen(de->d_name);
205 if (userdata_encryption && ((item_len >= 3 && strncmp(de->d_name, "app", 3) == 0) || (item_len >= 6 && strncmp(de->d_name, "dalvik", 6) == 0))) {
206 // Do nothing, we added these to RegularList earlier
207 } else {
Matt Mower50248ab2014-03-31 15:58:40 -0500208 FileName = tardir + "/" + de->d_name;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500209 ret = Generate_TarList(FileName, &EncryptList, &target_size, &enc_thread_id);
210 if (ret < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500211 LOGINFO("Error in Generate_TarList with encrypted list!\n");
212 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000213 closedir(d);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500214 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000215 _exit(-1);
216 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500217 file_count += (unsigned long long)(ret);
Dees_Troy83bd4832013-05-04 12:39:56 +0000218 }
219 } else if (de->d_type == DT_REG || de->d_type == DT_LNK) {
220 stat(FileName.c_str(), &st);
221 if (de->d_type == DT_REG)
222 Archive_Current_Size += (unsigned long long)(st.st_size);
223 TarItem.fn = FileName;
224 TarItem.thread_id = enc_thread_id;
225 EncryptList.push_back(TarItem);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500226 file_count++;
Dees_Troy83bd4832013-05-04 12:39:56 +0000227 }
228 }
229 closedir(d);
230 if (enc_thread_id != core_count) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500231 LOGINFO("Error dividing up threads for encryption, %u threads for %u cores!\n", enc_thread_id, core_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500232 if (enc_thread_id > core_count) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500233 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500234 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000235 _exit(-1);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500236 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500237 LOGINFO("Continuining anyway.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500238 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000239 }
240
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500241 // Send file count to parent
242 write(progress_pipe_fd, &file_count, sizeof(file_count));
243 // Send backup size to parent
244 total_size = regular_size + encrypt_size;
245 write(progress_pipe_fd, &total_size, sizeof(total_size));
246
Dees_Troy83bd4832013-05-04 12:39:56 +0000247 if (userdata_encryption) {
248 // Create a backup of unencrypted data
249 reg.setfn(tarfn);
250 reg.ItemList = &RegularList;
251 reg.thread_id = 0;
252 reg.use_encryption = 0;
253 reg.use_compression = use_compression;
Dees Troye0a433a2013-12-02 04:10:37 +0000254 reg.split_archives = 1;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500255 reg.progress_pipe_fd = progress_pipe_fd;
Dees_Troy83bd4832013-05-04 12:39:56 +0000256 LOGINFO("Creating unencrypted backup...\n");
257 if (createList((void*)&reg) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500258 LOGINFO("Error creating unencrypted backup.\n");
259 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500260 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000261 _exit(-1);
262 }
263 }
264
265 if (pthread_attr_init(&tattr)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500266 LOGINFO("Unable to pthread_attr_init\n");
267 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500268 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000269 _exit(-1);
270 }
271 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500272 LOGINFO("Error setting pthread_attr_setdetachstate\n");
273 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500274 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000275 _exit(-1);
276 }
277 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500278 LOGINFO("Error setting pthread_attr_setscope\n");
279 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500280 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000281 _exit(-1);
282 }
283 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
284 LOGERR("Error setting pthread_attr_setstacksize\n");
285 _exit(-1);
286 }*/
287
288 // Create threads for the divided up encryption lists
289 for (i = start_thread_id; i <= core_count; i++) {
290 enc[i].setdir(tardir);
291 enc[i].setfn(tarfn);
292 enc[i].ItemList = &EncryptList;
293 enc[i].thread_id = i;
294 enc[i].use_encryption = use_encryption;
Ethan Yonker924a80b2014-04-02 10:54:12 -0500295 enc[i].setpassword(password);
Dees_Troy83bd4832013-05-04 12:39:56 +0000296 enc[i].use_compression = use_compression;
Dees Troye0a433a2013-12-02 04:10:37 +0000297 enc[i].split_archives = 1;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500298 enc[i].progress_pipe_fd = progress_pipe_fd;
Dees_Troy83bd4832013-05-04 12:39:56 +0000299 LOGINFO("Start encryption thread %i\n", i);
300 ret = pthread_create(&enc_thread[i], &tattr, createList, (void*)&enc[i]);
301 if (ret) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500302 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 +0000303 if (createList((void*)&enc[i]) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500304 LOGINFO("Error creating encrypted backup %i.\n", i);
305 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500306 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000307 _exit(-1);
308 } else {
309 enc[i].thread_id = i + 1;
310 }
311 }
312 usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason.
313 }
314 if (pthread_attr_destroy(&tattr)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500315 LOGINFO("Failed to pthread_attr_destroy\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000316 }
317 for (i = start_thread_id; i <= core_count; i++) {
318 if (enc[i].thread_id == i) {
319 if (pthread_join(enc_thread[i], &thread_return)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500320 LOGINFO("Error joining thread %i\n", i);
321 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500322 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000323 _exit(-1);
324 } else {
325 LOGINFO("Joined thread %i.\n", i);
that2252d242015-04-03 22:33:04 +0200326 ret = (int)(intptr_t)thread_return;
Dees_Troy83bd4832013-05-04 12:39:56 +0000327 if (ret != 0) {
328 thread_error = 1;
Ethan Yonker74db1572015-10-28 12:44:49 -0500329 LOGINFO("Thread %i returned an error %i.\n", i, ret);
330 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500331 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000332 _exit(-1);
333 }
334 }
335 } else {
336 LOGINFO("Skipping joining thread %i because of pthread failure.\n", i);
337 }
338 }
339 if (thread_error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500340 LOGINFO("Error returned by one or more threads.\n");
341 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500342 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000343 _exit(-1);
344 }
345 LOGINFO("Finished encrypted backup.\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500346 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000347 _exit(0);
348 } else {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500349 // Not encrypted
Dees Troye0a433a2013-12-02 04:10:37 +0000350 std::vector<TarListStruct> FileList;
351 unsigned thread_id = 0;
352 unsigned long long target_size = 0;
353 twrpTar reg;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500354 int ret;
Dees Troye0a433a2013-12-02 04:10:37 +0000355
356 // Generate list of files to back up
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500357 ret = Generate_TarList(tardir, &FileList, &target_size, &thread_id);
358 if (ret < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500359 LOGINFO("Error in Generate_TarList!\n");
360 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500361 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000362 _exit(-1);
Dees Troye0a433a2013-12-02 04:10:37 +0000363 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500364 file_count = (unsigned long long)(ret);
Dees Troye0a433a2013-12-02 04:10:37 +0000365 // Create a backup
366 reg.setfn(tarfn);
367 reg.ItemList = &FileList;
368 reg.thread_id = 0;
369 reg.use_encryption = 0;
370 reg.use_compression = use_compression;
371 reg.setsize(Total_Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500372 reg.progress_pipe_fd = progress_pipe_fd;
Dees Troye0a433a2013-12-02 04:10:37 +0000373 if (Total_Backup_Size > MAX_ARCHIVE_SIZE) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500374 gui_msg("split_backup=Breaking backup file into multiple archives...");
Dees Troye0a433a2013-12-02 04:10:37 +0000375 reg.split_archives = 1;
376 } else {
377 reg.split_archives = 0;
378 }
379 LOGINFO("Creating backup...\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500380 write(progress_pipe_fd, &file_count, sizeof(file_count));
381 write(progress_pipe_fd, &Total_Backup_Size, sizeof(Total_Backup_Size));
Dees Troye0a433a2013-12-02 04:10:37 +0000382 if (createList((void*)&reg) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500383 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500384 close(progress_pipe[1]);
Dees Troye0a433a2013-12-02 04:10:37 +0000385 _exit(-1);
386 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500387 close(progress_pipe[1]);
Dees Troye0a433a2013-12-02 04:10:37 +0000388 _exit(0);
Dees_Troy83bd4832013-05-04 12:39:56 +0000389 }
390 } else {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500391 // Parent side
392 unsigned long long fs, size_backup, files_backup, total_backup_size;
393 int first_data = 0;
394 double display_percent, progress_percent;
395 char file_progress[1024];
396 char size_progress[1024];
397 files_backup = 0;
398 size_backup = 0;
Ethan Yonker74db1572015-10-28 12:44:49 -0500399 string file_prog = gui_lookup("file_progress", "%llu of %llu files, %i%%");
400 string size_prog = gui_lookup("size_progress", "%lluMB of %lluMB, %i%%");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500401
bigbiff7abc5fe2015-01-17 16:53:12 -0500402 fork_pid = tar_fork_pid;
403
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500404 // Parent closes output side
405 close(progress_pipe[1]);
406
407 // Read progress data from children
408 while (read(progress_pipe[0], &fs, sizeof(fs)) > 0) {
409 if (first_data == 0) {
410 // First incoming data is the file count
411 file_count = fs;
412 if (file_count == 0) file_count = 1; // prevent division by 0 below
413 first_data = 1;
414 } else if (first_data == 1) {
415 // Second incoming data is total size
416 total_backup_size = fs;
417 first_data = 2;
418 } else {
419 files_backup++;
420 size_backup += fs;
421 display_percent = (double)(files_backup) / (double)(file_count) * 100;
Ethan Yonker74db1572015-10-28 12:44:49 -0500422 sprintf(file_progress, file_prog.c_str(), files_backup, file_count, (int)(display_percent));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500423#ifndef BUILD_TWRPTAR_MAIN
424 DataManager::SetValue("tw_file_progress", file_progress);
425 display_percent = (double)(size_backup + *other_backups_size) / (double)(*overall_size) * 100;
Ethan Yonker74db1572015-10-28 12:44:49 -0500426 sprintf(size_progress, size_prog.c_str(), (size_backup + *other_backups_size) / 1048576, *overall_size / 1048576, (int)(display_percent));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500427 DataManager::SetValue("tw_size_progress", size_progress);
428 progress_percent = (display_percent / 100);
429 DataManager::SetProgress((float)(progress_percent));
430#endif //ndef BUILD_TWRPTAR_MAIN
431 }
432 }
433 close(progress_pipe[0]);
434#ifndef BUILD_TWRPTAR_MAIN
435 DataManager::SetValue("tw_file_progress", "");
436 DataManager::SetValue("tw_size_progress", "");
437
438 InfoManager backup_info(backup_folder + partition_name + ".info");
439 backup_info.SetValue("backup_size", size_backup);
440 if (use_compression && use_encryption)
441 backup_info.SetValue("backup_type", 3);
442 else if (use_encryption)
443 backup_info.SetValue("backup_type", 2);
444 else if (use_compression)
445 backup_info.SetValue("backup_type", 1);
446 else
447 backup_info.SetValue("backup_type", 0);
448 backup_info.SetValue("file_count", files_backup);
449 backup_info.SaveValues();
450#endif //ndef BUILD_TWRPTAR_MAIN
bigbiff7abc5fe2015-01-17 16:53:12 -0500451 if (TWFunc::Wait_For_Child(tar_fork_pid, &status, "createTarFork()") != 0)
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500452 return -1;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500453 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500454 return 0;
455}
456
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500457int twrpTar::extractTarFork(const unsigned long long *overall_size, unsigned long long *other_backups_size) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000458 int status = 0;
bigbiff7abc5fe2015-01-17 16:53:12 -0500459 pid_t rc_pid, tar_fork_pid;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500460 int progress_pipe[2], ret;
461
462 if (pipe(progress_pipe) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500463 LOGINFO("Error creating progress tracking pipe\n");
464 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500465 return -1;
466 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000467
bigbiff7abc5fe2015-01-17 16:53:12 -0500468 tar_fork_pid = fork();
469 if (tar_fork_pid >= 0) // fork was successful
Dees_Troy83bd4832013-05-04 12:39:56 +0000470 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500471 if (tar_fork_pid == 0) // child process
Dees_Troy83bd4832013-05-04 12:39:56 +0000472 {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500473 close(progress_pipe[0]);
474 progress_pipe_fd = progress_pipe[1];
Dees_Troy83bd4832013-05-04 12:39:56 +0000475 if (TWFunc::Path_Exists(tarfn)) {
476 LOGINFO("Single archive\n");
477 if (extract() != 0)
478 _exit(-1);
479 else
480 _exit(0);
481 } else {
482 LOGINFO("Multiple archives\n");
483 string temp;
484 char actual_filename[255];
485 twrpTar tars[9];
486 pthread_t tar_thread[9];
487 pthread_attr_t tattr;
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500488 unsigned thread_count = 0, i, start_thread_id = 1;
489 int ret, thread_error = 0;
Dees_Troy83bd4832013-05-04 12:39:56 +0000490 void *thread_return;
491
492 basefn = tarfn;
493 temp = basefn + "%i%02i";
494 tarfn += "000";
495 if (!TWFunc::Path_Exists(tarfn)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500496 LOGINFO("Unable to locate '%s' or '%s'\n", basefn.c_str(), tarfn.c_str());
497 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500498 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000499 _exit(-1);
500 }
501 if (TWFunc::Get_File_Type(tarfn) != 2) {
502 LOGINFO("First tar file '%s' not encrypted\n", tarfn.c_str());
503 tars[0].basefn = basefn;
504 tars[0].thread_id = 0;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500505 tars[0].progress_pipe_fd = progress_pipe_fd;
Dees_Troy83bd4832013-05-04 12:39:56 +0000506 if (extractMulti((void*)&tars[0]) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500507 LOGINFO("Error extracting split archive.\n");
508 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500509 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000510 _exit(-1);
511 }
512 } else {
513 start_thread_id = 0;
514 }
515 // Start threading encrypted restores
516 if (pthread_attr_init(&tattr)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500517 LOGINFO("Unable to pthread_attr_init\n");
518 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500519 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000520 _exit(-1);
521 }
522 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500523 LOGINFO("Error setting pthread_attr_setdetachstate\n");
524 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500525 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000526 _exit(-1);
527 }
528 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500529 LOGINFO("Error setting pthread_attr_setscope\n");
530 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500531 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000532 _exit(-1);
533 }
534 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
535 LOGERR("Error setting pthread_attr_setstacksize\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500536 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000537 _exit(-1);
538 }*/
539 for (i = start_thread_id; i < 9; i++) {
540 sprintf(actual_filename, temp.c_str(), i, 0);
541 if (TWFunc::Path_Exists(actual_filename)) {
542 thread_count++;
543 tars[i].basefn = basefn;
Ethan Yonker924a80b2014-04-02 10:54:12 -0500544 tars[i].setpassword(password);
Dees_Troy83bd4832013-05-04 12:39:56 +0000545 tars[i].thread_id = i;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500546 tars[i].progress_pipe_fd = progress_pipe_fd;
Dees_Troy83bd4832013-05-04 12:39:56 +0000547 LOGINFO("Creating extract thread ID %i\n", i);
548 ret = pthread_create(&tar_thread[i], &tattr, extractMulti, (void*)&tars[i]);
549 if (ret) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500550 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 +0000551 if (extractMulti((void*)&tars[i]) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500552 LOGINFO("Error extracting backup in thread %i.\n", i);
553 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500554 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000555 _exit(-1);
556 } else {
557 tars[i].thread_id = i + 1;
558 }
559 }
560 usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason.
561 } else {
562 break;
563 }
564 }
565 for (i = start_thread_id; i < thread_count + start_thread_id; i++) {
566 if (tars[i].thread_id == i) {
567 if (pthread_join(tar_thread[i], &thread_return)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500568 LOGINFO("Error joining thread %i\n", i);
569 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500570 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000571 _exit(-1);
572 } else {
573 LOGINFO("Joined thread %i.\n", i);
that2252d242015-04-03 22:33:04 +0200574 ret = (int)(intptr_t)thread_return;
Dees_Troy83bd4832013-05-04 12:39:56 +0000575 if (ret != 0) {
576 thread_error = 1;
Ethan Yonker74db1572015-10-28 12:44:49 -0500577 LOGINFO("Thread %i returned an error %i.\n", i, ret);
578 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500579 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000580 _exit(-1);
581 }
582 }
583 } else {
584 LOGINFO("Skipping joining thread %i because of pthread failure.\n", i);
585 }
586 }
587 if (thread_error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500588 LOGINFO("Error returned by one or more threads.\n");
589 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500590 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000591 _exit(-1);
592 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500593 LOGINFO("Finished encrypted restore.\n");
594 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000595 _exit(0);
596 }
597 }
598 else // parent process
599 {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500600 unsigned long long fs, size_backup;
601 double display_percent, progress_percent;
602 char size_progress[1024];
603 size_backup = 0;
Ethan Yonker74db1572015-10-28 12:44:49 -0500604 string size_prog = gui_lookup("size_progress", "%lluMB of %lluMB, %i%%");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500605
606 // Parent closes output side
607 close(progress_pipe[1]);
608
609 // Read progress data from children
610 while (read(progress_pipe[0], &fs, sizeof(fs)) > 0) {
611 size_backup += fs;
612 display_percent = (double)(size_backup + *other_backups_size) / (double)(*overall_size) * 100;
Ethan Yonker74db1572015-10-28 12:44:49 -0500613 sprintf(size_progress, size_prog.c_str(), (size_backup + *other_backups_size) / 1048576, *overall_size / 1048576, (int)(display_percent));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500614 progress_percent = (display_percent / 100);
615#ifndef BUILD_TWRPTAR_MAIN
616 DataManager::SetValue("tw_size_progress", size_progress);
617 DataManager::SetProgress((float)(progress_percent));
618#endif //ndef BUILD_TWRPTAR_MAIN
619 }
620 close(progress_pipe[0]);
621#ifndef BUILD_TWRPTAR_MAIN
622 DataManager::SetValue("tw_file_progress", "");
623#endif //ndef BUILD_TWRPTAR_MAIN
624 *other_backups_size += size_backup;
625
bigbiff7abc5fe2015-01-17 16:53:12 -0500626 if (TWFunc::Wait_For_Child(tar_fork_pid, &status, "extractTarFork()") != 0)
Dees_Troy83bd4832013-05-04 12:39:56 +0000627 return -1;
628 }
629 }
630 else // fork has failed
631 {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500632 close(progress_pipe[0]);
633 close(progress_pipe[1]);
Dees_Troy2673cec2013-04-02 20:22:16 +0000634 LOGINFO("extract tar failed to fork.\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500635 return -1;
636 }
637 return 0;
638}
639
Dees_Troy83bd4832013-05-04 12:39:56 +0000640int twrpTar::Generate_TarList(string Path, std::vector<TarListStruct> *TarList, unsigned long long *Target_Size, unsigned *thread_id) {
641 DIR* d;
642 struct dirent* de;
643 struct stat st;
644 string FileName;
645 struct TarListStruct TarItem;
646 string::size_type i;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500647 int ret, file_count;
648 file_count = 0;
Dees_Troy83bd4832013-05-04 12:39:56 +0000649
Dees_Troy83bd4832013-05-04 12:39:56 +0000650 d = opendir(Path.c_str());
651 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500652 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Path)(strerror(errno)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000653 closedir(d);
654 return -1;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500655 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000656 while ((de = readdir(d)) != NULL) {
Matt Mower50248ab2014-03-31 15:58:40 -0500657 FileName = Path + "/" + de->d_name;
Matt Mowerbb81e5d2014-03-20 18:05:41 -0500658
Matt Mower50248ab2014-03-31 15:58:40 -0500659 if (de->d_type == DT_BLK || de->d_type == DT_CHR || du.check_skip_dirs(FileName))
Dees_Troy83bd4832013-05-04 12:39:56 +0000660 continue;
661 TarItem.fn = FileName;
662 TarItem.thread_id = *thread_id;
Matt Mower50248ab2014-03-31 15:58:40 -0500663 if (de->d_type == DT_DIR) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000664 TarList->push_back(TarItem);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500665 ret = Generate_TarList(FileName, TarList, Target_Size, thread_id);
666 if (ret < 0)
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500667 return -1;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500668 file_count += ret;
Dees_Troy83bd4832013-05-04 12:39:56 +0000669 } else if (de->d_type == DT_REG || de->d_type == DT_LNK) {
670 stat(FileName.c_str(), &st);
671 TarList->push_back(TarItem);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500672 if (de->d_type == DT_REG) {
673 file_count++;
Dees_Troy83bd4832013-05-04 12:39:56 +0000674 Archive_Current_Size += st.st_size;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500675 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000676 if (Archive_Current_Size != 0 && *Target_Size != 0 && Archive_Current_Size > *Target_Size) {
677 *thread_id = *thread_id + 1;
678 Archive_Current_Size = 0;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500679 }
680 }
681 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000682 closedir(d);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500683 return file_count;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500684}
685
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500686int twrpTar::extractTar() {
Dees_Troye34c1332013-02-06 19:13:00 +0000687 char* charRootDir = (char*) tardir.c_str();
Dees_Troy83bd4832013-05-04 12:39:56 +0000688 if (openTar() == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500689 return -1;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500690 if (tar_extract_all(t, charRootDir, &progress_pipe_fd) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500691 LOGINFO("Unable to extract tar archive '%s'\n", tarfn.c_str());
692 gui_err("restore_error=Error during restore process.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500693 return -1;
694 }
695 if (tar_close(t) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500696 LOGINFO("Unable to close tar file\n");
697 gui_err("restore_error=Error during restore process.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500698 return -1;
699 }
700 return 0;
701}
702
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500703int twrpTar::extract() {
Dees_Troy83bd4832013-05-04 12:39:56 +0000704 Archive_Current_Type = TWFunc::Get_File_Type(tarfn);
n0d33b511632013-03-06 21:14:15 +0200705
706 if (Archive_Current_Type == 1) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500707 //if you return the extractTGZ function directly, stack crashes happen
Dees_Troy2673cec2013-04-02 20:22:16 +0000708 LOGINFO("Extracting gzipped tar\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000709 int ret = extractTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500710 return ret;
Dees_Troy83bd4832013-05-04 12:39:56 +0000711 } else if (Archive_Current_Type == 2) {
Ethan Yonker87af5632014-02-10 11:56:35 -0600712 int ret = TWFunc::Try_Decrypting_File(tarfn, password);
Dees_Troy83bd4832013-05-04 12:39:56 +0000713 if (ret < 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500714 gui_msg(Msg(msg::kError, "fail_decrypt_tar=Failed to decrypt tar file '{1}'")(tarfn));
Dees_Troy83bd4832013-05-04 12:39:56 +0000715 return -1;
716 }
717 if (ret == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500718 LOGINFO("Decrypted file is not in tar format.\n");
719 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000720 return -1;
721 }
722 if (ret == 3) {
723 LOGINFO("Extracting encrypted and compressed tar.\n");
724 Archive_Current_Type = 3;
725 } else
726 LOGINFO("Extracting encrypted tar.\n");
727 return extractTar();
728 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000729 LOGINFO("Extracting uncompressed tar\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500730 return extractTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500731 }
732}
733
Dees Troye0a433a2013-12-02 04:10:37 +0000734int twrpTar::tarList(std::vector<TarListStruct> *TarList, unsigned thread_id) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000735 struct stat st;
736 char buf[PATH_MAX];
737 int list_size = TarList->size(), i = 0, archive_count = 0;
738 string temp;
739 char actual_filename[PATH_MAX];
Dees Troye0a433a2013-12-02 04:10:37 +0000740 char *ptr;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500741 unsigned long long fs;
Dees_Troye34c1332013-02-06 19:13:00 +0000742
Dees Troye0a433a2013-12-02 04:10:37 +0000743 if (split_archives) {
744 basefn = tarfn;
745 temp = basefn + "%i%02i";
746 sprintf(actual_filename, temp.c_str(), thread_id, archive_count);
747 tarfn = actual_filename;
748 include_root_dir = true;
749 } else {
750 include_root_dir = false;
751 }
752 LOGINFO("Creating tar file '%s'\n", tarfn.c_str());
Dees_Troy83bd4832013-05-04 12:39:56 +0000753 if (createTar() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500754 LOGINFO("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id);
755 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000756 return -2;
757 }
758 Archive_Current_Size = 0;
759
760 while (i < list_size) {
761 if (TarList->at(i).thread_id == thread_id) {
762 strcpy(buf, TarList->at(i).fn.c_str());
bigbiff bigbiffec8fc282014-03-16 20:32:50 -0400763 lstat(buf, &st);
764 if (S_ISREG(st.st_mode)) { // item is a regular file
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500765 fs = (unsigned long long)(st.st_size);
766 if (split_archives && Archive_Current_Size + fs > MAX_ARCHIVE_SIZE) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000767 if (closeTar() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500768 LOGINFO("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id);
769 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000770 return -3;
771 }
772 archive_count++;
Ethan Yonker74db1572015-10-28 12:44:49 -0500773 gui_msg(Msg("split_thread=Splitting thread ID {1} into archive {2}")(thread_id)(archive_count + 1));
Dees_Troy83bd4832013-05-04 12:39:56 +0000774 if (archive_count > 99) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500775 LOGINFO("Too many archives for thread %i\n", thread_id);
776 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000777 return -4;
778 }
779 sprintf(actual_filename, temp.c_str(), thread_id, archive_count);
780 tarfn = actual_filename;
781 if (createTar() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500782 LOGINFO("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id);
783 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000784 return -2;
785 }
786 Archive_Current_Size = 0;
787 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500788 Archive_Current_Size += fs;
789 write(progress_pipe_fd, &fs, sizeof(fs));
Dees_Troy83bd4832013-05-04 12:39:56 +0000790 }
Dees Troye0a433a2013-12-02 04:10:37 +0000791 LOGINFO("addFile '%s' including root: %i\n", buf, include_root_dir);
792 if (addFile(buf, include_root_dir) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500793 LOGINFO("Error adding file '%s' to '%s'\n", buf, tarfn.c_str());
794 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000795 return -1;
796 }
797 }
798 i++;
799 }
800 if (closeTar() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500801 LOGINFO("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id);
802 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000803 return -3;
804 }
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600805 LOGINFO("Thread id %i tarList done, %i archives.\n", thread_id, archive_count);
Dees_Troye34c1332013-02-06 19:13:00 +0000806 return 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500807}
808
Dees_Troy83bd4832013-05-04 12:39:56 +0000809void* twrpTar::createList(void *cookie) {
810
811 twrpTar* threadTar = (twrpTar*) cookie;
Ethan Yonkereae42092014-03-07 15:33:13 -0600812 if (threadTar->tarList(threadTar->ItemList, threadTar->thread_id) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000813 LOGINFO("ERROR tarList for thread ID %i\n", threadTar->thread_id);
814 return (void*)-2;
815 }
816 LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id);
817 return (void*)0;
818}
819
820void* twrpTar::extractMulti(void *cookie) {
821
822 twrpTar* threadTar = (twrpTar*) cookie;
823 int archive_count = 0;
824 string temp = threadTar->basefn + "%i%02i";
825 char actual_filename[255];
826 sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count);
827 while (TWFunc::Path_Exists(actual_filename)) {
828 threadTar->tarfn = actual_filename;
829 if (threadTar->extract() != 0) {
830 LOGINFO("Error extracting '%s' in thread ID %i\n", actual_filename, threadTar->thread_id);
831 return (void*)-2;
832 }
833 archive_count++;
834 if (archive_count > 99)
835 break;
836 sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count);
837 }
838 LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id);
839 return (void*)0;
840}
841
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500842int twrpTar::addFilesToExistingTar(vector <string> files, string fn) {
843 char* charTarFile = (char*) fn.c_str();
844
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200845 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 -0500846 return -1;
847 removeEOT(charTarFile);
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200848 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 -0500849 return -1;
850 for (unsigned int i = 0; i < files.size(); ++i) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500851 char* file = (char*) files.at(i).c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500852 if (tar_append_file(t, file, file) == -1)
853 return -1;
854 }
855 if (tar_append_eof(t) == -1)
856 return -1;
857 if (tar_close(t) == -1)
858 return -1;
859 return 0;
860}
861
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500862int twrpTar::createTar() {
863 char* charTarFile = (char*) tarfn.c_str();
Dees_Troye34c1332013-02-06 19:13:00 +0000864 char* charRootDir = (char*) tardir.c_str();
Dees_Troye34c1332013-02-06 19:13:00 +0000865 static tartype_t type = { open, close, read, write_tar };
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500866
Dees_Troy83bd4832013-05-04 12:39:56 +0000867 if (use_encryption && use_compression) {
868 // Compressed and encrypted
869 Archive_Current_Type = 3;
870 LOGINFO("Using encryption and compression...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000871 int i, pipes[4];
872
873 if (pipe(pipes) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500874 LOGINFO("Error creating first pipe\n");
875 gui_err("backup_error=Error creating backup.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500876 return -1;
877 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000878 if (pipe(pipes + 2) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500879 LOGINFO("Error creating second pipe\n");
880 gui_err("backup_error=Error creating backup.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500881 return -1;
Dees_Troy83bd4832013-05-04 12:39:56 +0000882 }
Dees Troy9d723272014-04-07 17:13:10 +0000883 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);
884 if (output_fd < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500885 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno)));
Dees Troy9d723272014-04-07 17:13:10 +0000886 for (i = 0; i < 4; i++)
887 close(pipes[i]); // close all
888 return -1;
889 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000890 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400891
Dees_Troy83bd4832013-05-04 12:39:56 +0000892 if (pigz_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500893 LOGINFO("pigz fork() failed\n");
894 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +0000895 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000896 for (i = 0; i < 4; i++)
897 close(pipes[i]); // close all
898 return -1;
899 } else if (pigz_pid == 0) {
900 // pigz Child
901 close(pipes[1]);
902 close(pipes[2]);
903 close(0);
904 dup2(pipes[0], 0);
905 close(1);
906 dup2(pipes[3], 1);
907 if (execlp("pigz", "pigz", "-", NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500908 LOGINFO("execlp pigz ERROR!\n");
909 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +0000910 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000911 close(pipes[0]);
912 close(pipes[3]);
913 _exit(-1);
914 }
915 } else {
916 // Parent
917 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400918
Dees_Troy83bd4832013-05-04 12:39:56 +0000919 if (oaes_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500920 LOGINFO("openaes fork() failed\n");
921 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +0000922 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000923 for (i = 0; i < 4; i++)
924 close(pipes[i]); // close all
925 return -1;
926 } else if (oaes_pid == 0) {
927 // openaes Child
Dees_Troy83bd4832013-05-04 12:39:56 +0000928 close(pipes[0]);
929 close(pipes[1]);
930 close(pipes[3]);
931 close(0);
932 dup2(pipes[2], 0);
933 close(1);
934 dup2(output_fd, 1);
Ethan Yonker87af5632014-02-10 11:56:35 -0600935 if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500936 LOGINFO("execlp openaes ERROR!\n");
937 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000938 close(pipes[2]);
939 close(output_fd);
940 _exit(-1);
941 }
942 } else {
943 // Parent
944 close(pipes[0]);
945 close(pipes[2]);
946 close(pipes[3]);
947 fd = pipes[1];
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200948 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 +0000949 close(fd);
Ethan Yonker74db1572015-10-28 12:44:49 -0500950 LOGINFO("tar_fdopen failed\n");
951 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000952 return -1;
953 }
954 return 0;
955 }
956 }
957 } else if (use_compression) {
958 // Compressed
959 Archive_Current_Type = 1;
960 LOGINFO("Using compression...\n");
961 int pigzfd[2];
Dees Troy9d723272014-04-07 17:13:10 +0000962 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);
963 if (output_fd < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500964 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno)));
Dees Troy9d723272014-04-07 17:13:10 +0000965 close(pigzfd[0]);
966 return -1;
967 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000968
969 if (pipe(pigzfd) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500970 LOGINFO("Error creating pipe\n");
971 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +0000972 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000973 return -1;
974 }
975 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400976
Dees_Troy83bd4832013-05-04 12:39:56 +0000977 if (pigz_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500978 LOGINFO("fork() failed\n");
979 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +0000980 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000981 close(pigzfd[0]);
982 close(pigzfd[1]);
983 return -1;
984 } else if (pigz_pid == 0) {
985 // Child
986 close(pigzfd[1]); // close unused output pipe
Dees_Troy83bd4832013-05-04 12:39:56 +0000987 dup2(pigzfd[0], 0); // remap stdin
988 dup2(output_fd, 1); // remap stdout to output file
989 if (execlp("pigz", "pigz", "-", NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500990 LOGINFO("execlp pigz ERROR!\n");
991 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000992 close(output_fd);
993 close(pigzfd[0]);
994 _exit(-1);
995 }
996 } else {
997 // Parent
998 close(pigzfd[0]); // close parent input
999 fd = pigzfd[1]; // copy parent output
Vojtech Bocek25fd68d2013-08-27 03:10:10 +02001000 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 +00001001 close(fd);
Ethan Yonker74db1572015-10-28 12:44:49 -05001002 LOGINFO("tar_fdopen failed\n");
1003 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001004 return -1;
1005 }
1006 }
1007 } else if (use_encryption) {
1008 // Encrypted
1009 Archive_Current_Type = 2;
1010 LOGINFO("Using encryption...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +00001011 int oaesfd[2];
Dees Troy9d723272014-04-07 17:13:10 +00001012 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);
1013 if (output_fd < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001014 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno)));
Dees Troy9d723272014-04-07 17:13:10 +00001015 return -1;
1016 }
1017 if (pipe(oaesfd) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001018 LOGINFO("Error creating pipe\n");
1019 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +00001020 close(output_fd);
1021 return -1;
1022 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001023 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001024
Dees_Troy83bd4832013-05-04 12:39:56 +00001025 if (oaes_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001026 LOGINFO("fork() failed\n");
1027 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +00001028 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001029 close(oaesfd[0]);
1030 close(oaesfd[1]);
1031 return -1;
1032 } else if (oaes_pid == 0) {
1033 // Child
1034 close(oaesfd[1]); // close unused
Dees_Troy83bd4832013-05-04 12:39:56 +00001035 dup2(oaesfd[0], 0); // remap stdin
1036 dup2(output_fd, 1); // remap stdout to output file
Ethan Yonker87af5632014-02-10 11:56:35 -06001037 if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001038 LOGINFO("execlp openaes ERROR!\n");
1039 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001040 close(output_fd);
1041 close(oaesfd[0]);
1042 _exit(-1);
1043 }
1044 } else {
1045 // Parent
1046 close(oaesfd[0]); // close parent input
1047 fd = oaesfd[1]; // copy parent output
Vojtech Bocek25fd68d2013-08-27 03:10:10 +02001048 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 +00001049 close(fd);
Ethan Yonker74db1572015-10-28 12:44:49 -05001050 LOGINFO("tar_fdopen failed\n");
1051 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001052 return -1;
1053 }
1054 return 0;
1055 }
1056 } else {
1057 // Not compressed or encrypted
1058 init_libtar_buffer(0);
Vojtech Bocek25fd68d2013-08-27 03:10:10 +02001059 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) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001060 LOGINFO("tar_open error opening '%s'\n", tarfn.c_str());
1061 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001062 return -1;
1063 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001064 }
1065 return 0;
1066}
1067
Dees_Troy83bd4832013-05-04 12:39:56 +00001068int twrpTar::openTar() {
Dees_Troye34c1332013-02-06 19:13:00 +00001069 char* charRootDir = (char*) tardir.c_str();
1070 char* charTarFile = (char*) tarfn.c_str();
Dees_Troy83bd4832013-05-04 12:39:56 +00001071 string Password;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001072
Dees_Troy83bd4832013-05-04 12:39:56 +00001073 if (Archive_Current_Type == 3) {
1074 LOGINFO("Opening encrypted and compressed backup...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +00001075 int i, pipes[4];
Dees Troy9d723272014-04-07 17:13:10 +00001076 int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE);
1077 if (input_fd < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001078 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno)));
Dees Troy9d723272014-04-07 17:13:10 +00001079 return -1;
1080 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001081
1082 if (pipe(pipes) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001083 LOGINFO("Error creating first pipe\n");
1084 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001085 close(input_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001086 return -1;
1087 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001088 if (pipe(pipes + 2) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001089 LOGINFO("Error creating second pipe\n");
1090 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001091 close(pipes[0]);
1092 close(pipes[1]);
1093 close(input_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001094 return -1;
1095 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001096 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001097
Dees_Troy83bd4832013-05-04 12:39:56 +00001098 if (oaes_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001099 LOGINFO("pigz fork() failed\n");
1100 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001101 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001102 for (i = 0; i < 4; i++)
1103 close(pipes[i]); // close all
1104 return -1;
1105 } else if (oaes_pid == 0) {
1106 // openaes Child
1107 close(pipes[0]); // Close pipes that are not used by this child
1108 close(pipes[2]);
1109 close(pipes[3]);
Dees_Troy83bd4832013-05-04 12:39:56 +00001110 close(0);
1111 dup2(input_fd, 0);
1112 close(1);
1113 dup2(pipes[1], 1);
Ethan Yonker87af5632014-02-10 11:56:35 -06001114 if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001115 LOGINFO("execlp openaes ERROR!\n");
1116 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001117 close(input_fd);
1118 close(pipes[1]);
1119 _exit(-1);
1120 }
1121 } else {
1122 // Parent
1123 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001124
Dees_Troy83bd4832013-05-04 12:39:56 +00001125 if (pigz_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001126 LOGINFO("openaes fork() failed\n");
1127 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001128 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001129 for (i = 0; i < 4; i++)
1130 close(pipes[i]); // close all
1131 return -1;
1132 } else if (pigz_pid == 0) {
1133 // pigz Child
1134 close(pipes[1]); // Close pipes not used by this child
1135 close(pipes[2]);
1136 close(0);
1137 dup2(pipes[0], 0);
1138 close(1);
1139 dup2(pipes[3], 1);
1140 if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001141 LOGINFO("execlp pigz ERROR!\n");
1142 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001143 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001144 close(pipes[0]);
1145 close(pipes[3]);
1146 _exit(-1);
1147 }
1148 } else {
1149 // Parent
1150 close(pipes[0]); // Close pipes not used by parent
1151 close(pipes[1]);
1152 close(pipes[3]);
1153 fd = pipes[2];
Vojtech Bocek25fd68d2013-08-27 03:10:10 +02001154 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 +00001155 close(fd);
Ethan Yonker74db1572015-10-28 12:44:49 -05001156 LOGINFO("tar_fdopen failed\n");
1157 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001158 return -1;
1159 }
1160 }
1161 }
1162 } else if (Archive_Current_Type == 2) {
1163 LOGINFO("Opening encrypted backup...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +00001164 int oaesfd[2];
Dees Troy9d723272014-04-07 17:13:10 +00001165 int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE);
1166 if (input_fd < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001167 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno)));
Dees Troy9d723272014-04-07 17:13:10 +00001168 return -1;
1169 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001170
Dees Troy9d723272014-04-07 17:13:10 +00001171 if (pipe(oaesfd) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001172 LOGINFO("Error creating pipe\n");
1173 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001174 close(input_fd);
1175 return -1;
1176 }
1177
Dees_Troy83bd4832013-05-04 12:39:56 +00001178 oaes_pid = fork();
1179 if (oaes_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001180 LOGINFO("fork() failed\n");
1181 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001182 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001183 close(oaesfd[0]);
1184 close(oaesfd[1]);
1185 return -1;
1186 } else if (oaes_pid == 0) {
1187 // Child
1188 close(oaesfd[0]); // Close unused pipe
Dees_Troy83bd4832013-05-04 12:39:56 +00001189 close(0); // close stdin
1190 dup2(oaesfd[1], 1); // remap stdout
1191 dup2(input_fd, 0); // remap input fd to stdin
Ethan Yonker87af5632014-02-10 11:56:35 -06001192 if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001193 LOGINFO("execlp openaes ERROR!\n");
1194 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001195 close(input_fd);
1196 close(oaesfd[1]);
1197 _exit(-1);
1198 }
1199 } else {
1200 // Parent
1201 close(oaesfd[1]); // close parent output
1202 fd = oaesfd[0]; // copy parent input
Vojtech Bocek25fd68d2013-08-27 03:10:10 +02001203 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 +00001204 close(fd);
Ethan Yonker74db1572015-10-28 12:44:49 -05001205 LOGINFO("tar_fdopen failed\n");
1206 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001207 return -1;
1208 }
1209 }
1210 } else if (Archive_Current_Type == 1) {
1211 LOGINFO("Opening as a gzip...\n");
1212 int pigzfd[2];
Dees Troy9d723272014-04-07 17:13:10 +00001213 int input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE);
1214 if (input_fd < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001215 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno)));
Dees Troy9d723272014-04-07 17:13:10 +00001216 return -1;
1217 }
1218 if (pipe(pigzfd) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001219 LOGINFO("Error creating pipe\n");
1220 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001221 close(input_fd);
1222 return -1;
1223 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001224
1225 pigz_pid = fork();
1226 if (pigz_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001227 LOGINFO("fork() failed\n");
1228 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001229 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001230 close(pigzfd[0]);
1231 close(pigzfd[1]);
1232 return -1;
1233 } else if (pigz_pid == 0) {
1234 // Child
1235 close(pigzfd[0]);
Dees_Troy83bd4832013-05-04 12:39:56 +00001236 dup2(input_fd, 0); // remap input fd to stdin
1237 dup2(pigzfd[1], 1); // remap stdout
1238 if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) {
1239 close(pigzfd[1]);
1240 close(input_fd);
Ethan Yonker74db1572015-10-28 12:44:49 -05001241 LOGINFO("execlp openaes ERROR!\n");
1242 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001243 _exit(-1);
1244 }
1245 } else {
1246 // Parent
1247 close(pigzfd[1]); // close parent output
1248 fd = pigzfd[0]; // copy parent input
Vojtech Bocek25fd68d2013-08-27 03:10:10 +02001249 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 +00001250 close(fd);
Ethan Yonker74db1572015-10-28 12:44:49 -05001251 LOGINFO("tar_fdopen failed\n");
1252 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001253 return -1;
1254 }
1255 }
Vojtech Bocek25fd68d2013-08-27 03:10:10 +02001256 } 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) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001257 LOGINFO("Unable to open tar archive '%s'\n", charTarFile);
1258 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001259 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001260 }
1261 return 0;
1262}
1263
1264string twrpTar::Strip_Root_Dir(string Path) {
1265 string temp;
1266 size_t slash;
1267
1268 if (Path.substr(0, 1) == "/")
1269 temp = Path.substr(1, Path.size() - 1);
1270 else
1271 temp = Path;
1272 slash = temp.find("/");
1273 if (slash == string::npos)
1274 return temp;
1275 else {
1276 string stripped;
1277
1278 stripped = temp.substr(slash, temp.size() - slash);
1279 return stripped;
1280 }
1281 return temp;
1282}
1283
1284int twrpTar::addFile(string fn, bool include_root) {
1285 char* charTarFile = (char*) fn.c_str();
1286 if (include_root) {
1287 if (tar_append_file(t, charTarFile, NULL) == -1)
1288 return -1;
1289 } else {
1290 string temp = Strip_Root_Dir(fn);
1291 char* charTarPath = (char*) temp.c_str();
1292 if (tar_append_file(t, charTarFile, charTarPath) == -1)
1293 return -1;
1294 }
1295 return 0;
1296}
1297
Dees_Troy83bd4832013-05-04 12:39:56 +00001298int twrpTar::closeTar() {
Dees_Troye34c1332013-02-06 19:13:00 +00001299 flush_libtar_buffer(t->fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001300 if (tar_append_eof(t) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001301 LOGINFO("tar_append_eof(): %s\n", strerror(errno));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001302 tar_close(t);
1303 return -1;
1304 }
1305 if (tar_close(t) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001306 LOGINFO("Unable to close tar archive: '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001307 return -1;
1308 }
Dees_Troy2727b992013-08-14 20:09:30 +00001309 if (Archive_Current_Type > 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001310 close(fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001311 int status;
1312 if (pigz_pid > 0 && TWFunc::Wait_For_Child(pigz_pid, &status, "pigz") != 0)
1313 return -1;
1314 if (oaes_pid > 0 && TWFunc::Wait_For_Child(oaes_pid, &status, "openaes") != 0)
1315 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001316 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001317 free_libtar_buffer();
Dees Troye0a433a2013-12-02 04:10:37 +00001318 if (use_compression && !use_encryption) {
1319 string gzname = tarfn + ".gz";
1320 if (TWFunc::Path_Exists(gzname)) {
1321 rename(gzname.c_str(), tarfn.c_str());
1322 }
1323 }
1324 if (TWFunc::Get_File_Size(tarfn) == 0) {
Matt Mower3c366972015-12-25 19:28:31 -06001325 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(tarfn));
Dees Troye0a433a2013-12-02 04:10:37 +00001326 return -1;
1327 }
Ethan Yonker960f0302014-12-21 21:54:00 -06001328#ifndef BUILD_TWRPTAR_MAIN
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001329 tw_set_default_metadata(tarfn.c_str());
Ethan Yonker960f0302014-12-21 21:54:00 -06001330#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001331 return 0;
1332}
1333
1334int twrpTar::removeEOT(string tarFile) {
1335 char* charTarFile = (char*) tarFile.c_str();
Matt Mower2b18a532015-02-20 16:58:05 -06001336 off_t tarFileEnd = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001337 while (th_read(t) == 0) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001338 if (TH_ISREG(t))
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001339 tar_skip_regfile(t);
1340 tarFileEnd = lseek(t->fd, 0, SEEK_CUR);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001341 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001342 if (tar_close(t) == -1)
1343 return -1;
Matt Mower2b18a532015-02-20 16:58:05 -06001344 if (tarFileEnd > 0 && truncate(charTarFile, tarFileEnd) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001345 return -1;
1346 return 0;
1347}
1348
n0d33b511632013-03-06 21:14:15 +02001349int twrpTar::entryExists(string entry) {
1350 char* searchstr = (char*)entry.c_str();
1351 int ret;
1352
Dees_Troy83bd4832013-05-04 12:39:56 +00001353 Archive_Current_Type = TWFunc::Get_File_Type(tarfn);
n0d33b511632013-03-06 21:14:15 +02001354
Dees_Troy83bd4832013-05-04 12:39:56 +00001355 if (openTar() == -1)
n0d33b511632013-03-06 21:14:15 +02001356 ret = 0;
1357 else
1358 ret = tar_find(t, searchstr);
1359
Dees_Troy83bd4832013-05-04 12:39:56 +00001360 if (closeTar() != 0)
1361 LOGINFO("Unable to close tar after searching for entry.\n");
n0d33b511632013-03-06 21:14:15 +02001362
1363 return ret;
1364}
1365
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001366unsigned long long twrpTar::get_size() {
1367 if (TWFunc::Path_Exists(tarfn)) {
1368 LOGINFO("Single archive\n");
1369 int type = 0;
1370 return uncompressedSize(tarfn, &type);
1371 } else {
1372 LOGINFO("Multiple archives\n");
1373 string temp;
1374 char actual_filename[255];
1375 int archive_count = 0, i, type = 0, temp_type = 0;
1376 unsigned long long total_restore_size = 0;
1377
1378 basefn = tarfn;
1379 temp = basefn + "%i%02i";
1380 tarfn += "000";
1381 thread_id = 0;
1382 sprintf(actual_filename, temp.c_str(), thread_id, archive_count);
1383 if (!TWFunc::Path_Exists(actual_filename)) {
1384 LOGERR("Unable to locate '%s' or '%s'\n", basefn.c_str(), tarfn.c_str());
1385 return 0;
1386 }
1387 for (i = 0; i < 9; i++) {
1388 archive_count = 0;
1389 sprintf(actual_filename, temp.c_str(), i, archive_count);
1390 while (TWFunc::Path_Exists(actual_filename)) {
1391 total_restore_size += uncompressedSize(actual_filename, &temp_type);
1392 if (temp_type > type)
1393 type = temp_type;
1394 archive_count++;
1395 if (archive_count > 99)
1396 break;
1397 sprintf(actual_filename, temp.c_str(), i, archive_count);
1398 }
1399 }
1400#ifndef BUILD_TWRPTAR_MAIN
1401 InfoManager backup_info(backup_folder + "/" + partition_name + ".info");
1402 backup_info.SetValue("backup_size", total_restore_size);
1403 backup_info.SetValue("backup_type", type);
1404 backup_info.SaveValues();
1405#endif //ndef BUILD_TWRPTAR_MAIN
1406 return total_restore_size;
1407 }
1408 return 0;
1409}
1410
1411unsigned long long twrpTar::uncompressedSize(string filename, int *archive_type) {
1412 int type = 0;
1413 unsigned long long total_size = 0;
1414 string Tar, Command, result;
1415 vector<string> split;
1416
1417 Tar = TWFunc::Get_Filename(filename);
1418 type = TWFunc::Get_File_Type(filename);
1419 if (type == 0) {
1420 total_size = TWFunc::Get_File_Size(filename);
1421 *archive_type = 0;
1422 } else if (type == 1) {
1423 // Compressed
1424 Command = "pigz -l '" + filename + "'";
1425 /* if we set Command = "pigz -l " + tarfn + " | sed '1d' | cut -f5 -d' '";
1426 we get the uncompressed size at once. */
1427 TWFunc::Exec_Cmd(Command, result);
1428 if (!result.empty()) {
1429 /* Expected output:
1430 compressed original reduced name
1431 95855838 179403776 -1.3% data.yaffs2.win
1432 ^
1433 split[5]
1434 */
1435 split = TWFunc::split_string(result, ' ', true);
1436 if (split.size() > 4)
1437 total_size = atoi(split[5].c_str());
1438 }
1439 *archive_type = 1;
1440 } else if (type == 2) {
1441 // File is encrypted and may be compressed
1442 int ret = TWFunc::Try_Decrypting_File(filename, password);
1443 *archive_type = 2;
1444 if (ret < 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001445 gui_msg(Msg(msg::kError, "fail_decrypt_tar=Failed to decrypt tar file '{1}'")(tarfn));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001446 total_size = TWFunc::Get_File_Size(filename);
1447 } else if (ret == 1) {
1448 LOGERR("Decrypted file is not in tar format.\n");
1449 total_size = TWFunc::Get_File_Size(filename);
1450 } else if (ret == 3) {
1451 *archive_type = 3;
1452 Command = "openaes dec --key \"" + password + "\" --in '" + filename + "' | pigz -l";
1453 /* if we set Command = "pigz -l " + tarfn + " | sed '1d' | cut -f5 -d' '";
1454 we get the uncompressed size at once. */
1455 TWFunc::Exec_Cmd(Command, result);
1456 if (!result.empty()) {
1457 LOGINFO("result was: '%s'\n", result.c_str());
1458 /* Expected output:
1459 compressed original reduced name
1460 95855838 179403776 -1.3% data.yaffs2.win
1461 ^
1462 split[5]
1463 */
1464 split = TWFunc::split_string(result, ' ', true);
1465 if (split.size() > 4)
1466 total_size = atoi(split[5].c_str());
1467 }
1468 } else {
1469 total_size = TWFunc::Get_File_Size(filename);
1470 }
1471 }
1472
1473 return total_size;
1474}
1475
Dees_Troye34c1332013-02-06 19:13:00 +00001476extern "C" ssize_t write_tar(int fd, const void *buffer, size_t size) {
1477 return (ssize_t) write_libtar_buffer(fd, buffer, size);
Dees_Troy40bbcf82013-02-12 15:01:53 +00001478}