blob: 7e4d29a7a881ff2b3ddc044daa8ba688044f9991 [file] [log] [blame]
Dees Troye0a433a2013-12-02 04:10:37 +00001
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002/*
Ethan Yonker472f5062016-02-25 13:47:30 -06003 Copyright 2013 to 2016 bigbiff/Dees_Troy TeamWin
Dees_Troye34c1332013-02-06 19:13:00 +00004 This file is part of TWRP/TeamWin Recovery Project.
bigbiff bigbiff9c754052013-01-09 09:09:08 -05005
Dees_Troye34c1332013-02-06 19:13:00 +00006 TWRP is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
bigbiff bigbiff9c754052013-01-09 09:09:08 -050010
Dees_Troye34c1332013-02-06 19:13:00 +000011 TWRP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
bigbiff bigbiff9c754052013-01-09 09:09:08 -050015
Dees_Troye34c1332013-02-06 19:13:00 +000016 You should have received a copy of the GNU General Public License
17 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
bigbiff bigbiff9c754052013-01-09 09:09:08 -050018*/
19
20extern "C" {
21 #include "libtar/libtar.h"
Dees_Troye34c1332013-02-06 19:13:00 +000022 #include "twrpTar.h"
23 #include "tarWrite.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050024}
25#include <sys/types.h>
26#include <sys/stat.h>
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -050027#include <sys/wait.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050028#include <string.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <fstream>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050032#include <iostream>
33#include <string>
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -050034#include <sstream>
Dees_Troy83bd4832013-05-04 12:39:56 +000035#include <vector>
bigbiff7abc5fe2015-01-17 16:53:12 -050036#include <csignal>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050037#include <dirent.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040038#include <libgen.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050039#include <sys/mman.h>
bigbiffce8f83c2015-12-12 18:30:21 -050040#include <sys/ioctl.h>
41#include <zlib.h>
42#include <semaphore.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050043#include "twrpTar.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000044#include "twcommon.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045#include "variables.h"
bigbiffce8f83c2015-12-12 18:30:21 -050046#include "adbbu/libtwadbbu.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050047#include "twrp-functions.hpp"
Ethan Yonker472f5062016-02-25 13:47:30 -060048#include "gui/gui.hpp"
49#include "progresstracking.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050050#ifndef BUILD_TWRPTAR_MAIN
51#include "data.hpp"
52#include "infomanager.hpp"
Ethan Yonkerf1179622016-08-25 15:32:21 -050053#include "set_metadata.h"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050054#endif //ndef BUILD_TWRPTAR_MAIN
bigbiff bigbiff9c754052013-01-09 09:09:08 -050055
Ethan Yonker79f88bd2016-12-09 14:52:12 -060056#ifdef TW_INCLUDE_FBE
57#include "crypto/ext4crypt/ext4crypt_tar.h"
Ethan Yonker71187742017-01-13 13:30:10 -060058#define TWTAR_FLAGS TAR_GNU | TAR_STORE_SELINUX | TAR_STORE_POSIX_CAP |TAR_STORE_EXT4_POL
Ethan Yonker79f88bd2016-12-09 14:52:12 -060059#else
Ethan Yonker71187742017-01-13 13:30:10 -060060#define TWTAR_FLAGS TAR_GNU | TAR_STORE_SELINUX | TAR_STORE_POSIX_CAP
Ethan Yonker79f88bd2016-12-09 14:52:12 -060061#endif
62
bigbiff bigbiff9c754052013-01-09 09:09:08 -050063using namespace std;
64
Dees_Troy83bd4832013-05-04 12:39:56 +000065twrpTar::twrpTar(void) {
66 use_encryption = 0;
67 userdata_encryption = 0;
68 use_compression = 0;
69 split_archives = 0;
70 has_data_media = 0;
71 pigz_pid = 0;
72 oaes_pid = 0;
Dees Troye0a433a2013-12-02 04:10:37 +000073 Total_Backup_Size = 0;
Ethan Yonker1a147a42015-02-27 13:14:44 -060074 Archive_Current_Size = 0;
Dees Troye0a433a2013-12-02 04:10:37 +000075 include_root_dir = true;
Ethan Yonkerf1179622016-08-25 15:32:21 -050076 tar_type.openfunc = open;
77 tar_type.closefunc = close;
78 tar_type.readfunc = read;
Ethan Yonkerea2fcf02016-09-12 16:07:19 -050079 input_fd = -1;
80 output_fd = -1;
Ethan Yonker3fdcda42016-11-30 12:29:37 -060081 backup_exclusions = NULL;
Ethan Yonker79f88bd2016-12-09 14:52:12 -060082#ifdef TW_INCLUDE_FBE
83 e4crypt_set_mode();
84#endif
Dees_Troy83bd4832013-05-04 12:39:56 +000085}
86
87twrpTar::~twrpTar(void) {
88 // Do nothing
89}
90
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -050091void twrpTar::setfn(string fn) {
92 tarfn = fn;
93}
94
95void twrpTar::setdir(string dir) {
96 tardir = dir;
97}
98
Dees Troye0a433a2013-12-02 04:10:37 +000099void twrpTar::setsize(unsigned long long backup_size) {
100 Total_Backup_Size = backup_size;
101}
102
Ethan Yonker87af5632014-02-10 11:56:35 -0600103void twrpTar::setpassword(string pass) {
104 password = pass;
105}
106
bigbiff7abc5fe2015-01-17 16:53:12 -0500107void twrpTar::Signal_Kill(int signum) {
108 _exit(255);
109}
110
bigbiffce8f83c2015-12-12 18:30:21 -0500111void twrpTar::Set_Archive_Type(Archive_Type archive_type) {
112 current_archive_type = archive_type;
113}
114
115int twrpTar::createTarFork(pid_t *tar_fork_pid) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000116 int status = 0;
Matt Mower23d8aae2017-01-06 14:30:33 -0600117 int progress_pipe[2];
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500118
119 file_count = 0;
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600120 if (backup_exclusions == NULL) {
121 LOGINFO("backup_exclusions is NULL\n");
122 return -1;
123 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500124
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600125#ifndef BUILD_TWRPTAR_MAIN
bigbiffce8f83c2015-12-12 18:30:21 -0500126 if (part_settings->adbbackup) {
127 std::string Backup_FileName(tarfn);
128 if (!twadbbu::Write_TWFN(Backup_FileName, Total_Backup_Size, use_compression))
129 return -1;
130 }
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600131#endif
bigbiffce8f83c2015-12-12 18:30:21 -0500132
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500133 if (pipe(progress_pipe) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500134 LOGINFO("Error creating progress tracking pipe\n");
135 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500136 return -1;
137 }
bigbiffce8f83c2015-12-12 18:30:21 -0500138 if ((*tar_fork_pid = fork()) == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000139 LOGINFO("create tar failed to fork.\n");
Ethan Yonker74db1572015-10-28 12:44:49 -0500140 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500141 close(progress_pipe[0]);
142 close(progress_pipe[1]);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500143 return -1;
144 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500145
bigbiffce8f83c2015-12-12 18:30:21 -0500146 if (*tar_fork_pid == 0) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500147 // Child process
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500148 // Child closes input side of progress pipe
bigbiff7abc5fe2015-01-17 16:53:12 -0500149 signal(SIGUSR2, twrpTar::Signal_Kill);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500150 close(progress_pipe[0]);
151 progress_pipe_fd = progress_pipe[1];
152
Dees_Troy83bd4832013-05-04 12:39:56 +0000153 if (use_encryption || userdata_encryption) {
154 LOGINFO("Using encryption\n");
155 DIR* d;
156 struct dirent* de;
that2252d242015-04-03 22:33:04 +0200157 unsigned long long regular_size = 0, encrypt_size = 0, target_size = 0, total_size;
158 unsigned enc_thread_id = 1, regular_thread_id = 0, i, start_thread_id = 1, core_count = 1;
Dees_Troy83bd4832013-05-04 12:39:56 +0000159 int item_len, ret, thread_error = 0;
160 std::vector<TarListStruct> RegularList;
161 std::vector<TarListStruct> EncryptList;
162 string FileName;
163 struct TarListStruct TarItem;
164 twrpTar reg, enc[9];
165 struct stat st;
166 pthread_t enc_thread[9];
167 pthread_attr_t tattr;
168 void *thread_return;
169
170 core_count = sysconf(_SC_NPROCESSORS_CONF);
171 if (core_count > 8)
172 core_count = 8;
that2252d242015-04-03 22:33:04 +0200173 LOGINFO(" Core Count : %u\n", core_count);
Dees_Troy83bd4832013-05-04 12:39:56 +0000174 Archive_Current_Size = 0;
175
176 d = opendir(tardir.c_str());
177 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500178 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tardir)(strerror(errno)));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500179 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000180 _exit(-1);
181 }
182 // Figure out the size of all data to be encrypted and create a list of unencrypted files
183 while ((de = readdir(d)) != NULL) {
Matt Mower50248ab2014-03-31 15:58:40 -0500184 FileName = tardir + "/" + de->d_name;
185
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600186 if (de->d_type == DT_BLK || de->d_type == DT_CHR || backup_exclusions->check_skip_dirs(FileName))
Dees_Troy83bd4832013-05-04 12:39:56 +0000187 continue;
Matt Mower50248ab2014-03-31 15:58:40 -0500188 if (de->d_type == DT_DIR) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000189 item_len = strlen(de->d_name);
190 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 -0500191 ret = Generate_TarList(FileName, &RegularList, &target_size, &regular_thread_id);
192 if (ret < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500193 LOGINFO("Error in Generate_TarList with regular list!\n");
194 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000195 closedir(d);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500196 close(progress_pipe_fd);
197 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000198 _exit(-1);
199 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500200 file_count = (unsigned long long)(ret);
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600201 regular_size += backup_exclusions->Get_Folder_Size(FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +0000202 } else {
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600203 encrypt_size += backup_exclusions->Get_Folder_Size(FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +0000204 }
205 } else if (de->d_type == DT_REG) {
206 stat(FileName.c_str(), &st);
207 encrypt_size += (unsigned long long)(st.st_size);
208 }
209 }
210 closedir(d);
211
212 target_size = encrypt_size / core_count;
213 target_size++;
214 LOGINFO(" Unencrypted size: %llu\n", regular_size);
215 LOGINFO(" Encrypted size : %llu\n", encrypt_size);
216 LOGINFO(" Target size : %llu\n", target_size);
217 if (!userdata_encryption) {
218 enc_thread_id = 0;
219 start_thread_id = 0;
220 core_count--;
221 }
222 Archive_Current_Size = 0;
223
224 d = opendir(tardir.c_str());
225 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500226 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tardir)(strerror(errno)));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500227 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000228 _exit(-1);
229 }
230 // Divide up the encrypted file list for threading
231 while ((de = readdir(d)) != NULL) {
Matt Mower50248ab2014-03-31 15:58:40 -0500232 FileName = tardir + "/" + de->d_name;
233
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600234 if (de->d_type == DT_BLK || de->d_type == DT_CHR || backup_exclusions->check_skip_dirs(FileName))
Dees_Troy83bd4832013-05-04 12:39:56 +0000235 continue;
Matt Mower50248ab2014-03-31 15:58:40 -0500236 if (de->d_type == DT_DIR) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000237 item_len = strlen(de->d_name);
238 if (userdata_encryption && ((item_len >= 3 && strncmp(de->d_name, "app", 3) == 0) || (item_len >= 6 && strncmp(de->d_name, "dalvik", 6) == 0))) {
239 // Do nothing, we added these to RegularList earlier
240 } else {
Matt Mower50248ab2014-03-31 15:58:40 -0500241 FileName = tardir + "/" + de->d_name;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500242 ret = Generate_TarList(FileName, &EncryptList, &target_size, &enc_thread_id);
243 if (ret < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500244 LOGINFO("Error in Generate_TarList with encrypted list!\n");
245 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000246 closedir(d);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500247 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000248 _exit(-1);
249 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500250 file_count += (unsigned long long)(ret);
Dees_Troy83bd4832013-05-04 12:39:56 +0000251 }
252 } else if (de->d_type == DT_REG || de->d_type == DT_LNK) {
253 stat(FileName.c_str(), &st);
254 if (de->d_type == DT_REG)
255 Archive_Current_Size += (unsigned long long)(st.st_size);
256 TarItem.fn = FileName;
257 TarItem.thread_id = enc_thread_id;
258 EncryptList.push_back(TarItem);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500259 file_count++;
Dees_Troy83bd4832013-05-04 12:39:56 +0000260 }
261 }
262 closedir(d);
263 if (enc_thread_id != core_count) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500264 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 -0500265 if (enc_thread_id > core_count) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500266 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500267 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000268 _exit(-1);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500269 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500270 LOGINFO("Continuining anyway.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500271 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000272 }
273
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500274 // Send file count to parent
275 write(progress_pipe_fd, &file_count, sizeof(file_count));
276 // Send backup size to parent
277 total_size = regular_size + encrypt_size;
278 write(progress_pipe_fd, &total_size, sizeof(total_size));
279
Dees_Troy83bd4832013-05-04 12:39:56 +0000280 if (userdata_encryption) {
281 // Create a backup of unencrypted data
282 reg.setfn(tarfn);
283 reg.ItemList = &RegularList;
284 reg.thread_id = 0;
285 reg.use_encryption = 0;
286 reg.use_compression = use_compression;
Dees Troye0a433a2013-12-02 04:10:37 +0000287 reg.split_archives = 1;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500288 reg.progress_pipe_fd = progress_pipe_fd;
bigbiffce8f83c2015-12-12 18:30:21 -0500289 reg.part_settings = part_settings;
Dees_Troy83bd4832013-05-04 12:39:56 +0000290 LOGINFO("Creating unencrypted backup...\n");
291 if (createList((void*)&reg) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500292 LOGINFO("Error creating unencrypted backup.\n");
293 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500294 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000295 _exit(-1);
296 }
297 }
298
299 if (pthread_attr_init(&tattr)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500300 LOGINFO("Unable to pthread_attr_init\n");
301 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500302 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000303 _exit(-1);
304 }
305 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500306 LOGINFO("Error setting pthread_attr_setdetachstate\n");
307 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500308 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000309 _exit(-1);
310 }
311 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500312 LOGINFO("Error setting pthread_attr_setscope\n");
313 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500314 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000315 _exit(-1);
316 }
317 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
318 LOGERR("Error setting pthread_attr_setstacksize\n");
319 _exit(-1);
320 }*/
321
322 // Create threads for the divided up encryption lists
323 for (i = start_thread_id; i <= core_count; i++) {
324 enc[i].setdir(tardir);
325 enc[i].setfn(tarfn);
326 enc[i].ItemList = &EncryptList;
327 enc[i].thread_id = i;
328 enc[i].use_encryption = use_encryption;
Ethan Yonker924a80b2014-04-02 10:54:12 -0500329 enc[i].setpassword(password);
Dees_Troy83bd4832013-05-04 12:39:56 +0000330 enc[i].use_compression = use_compression;
Dees Troye0a433a2013-12-02 04:10:37 +0000331 enc[i].split_archives = 1;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500332 enc[i].progress_pipe_fd = progress_pipe_fd;
bigbiffce8f83c2015-12-12 18:30:21 -0500333 enc[i].part_settings = part_settings;
Dees_Troy83bd4832013-05-04 12:39:56 +0000334 LOGINFO("Start encryption thread %i\n", i);
335 ret = pthread_create(&enc_thread[i], &tattr, createList, (void*)&enc[i]);
336 if (ret) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500337 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 +0000338 if (createList((void*)&enc[i]) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500339 LOGINFO("Error creating encrypted backup %i.\n", i);
340 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500341 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000342 _exit(-1);
343 } else {
344 enc[i].thread_id = i + 1;
345 }
346 }
347 usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason.
348 }
349 if (pthread_attr_destroy(&tattr)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500350 LOGINFO("Failed to pthread_attr_destroy\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000351 }
352 for (i = start_thread_id; i <= core_count; i++) {
353 if (enc[i].thread_id == i) {
354 if (pthread_join(enc_thread[i], &thread_return)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500355 LOGINFO("Error joining thread %i\n", i);
356 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500357 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000358 _exit(-1);
359 } else {
360 LOGINFO("Joined thread %i.\n", i);
that2252d242015-04-03 22:33:04 +0200361 ret = (int)(intptr_t)thread_return;
Dees_Troy83bd4832013-05-04 12:39:56 +0000362 if (ret != 0) {
363 thread_error = 1;
Ethan Yonker74db1572015-10-28 12:44:49 -0500364 LOGINFO("Thread %i returned an error %i.\n", i, ret);
365 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500366 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000367 _exit(-1);
368 }
369 }
370 } else {
371 LOGINFO("Skipping joining thread %i because of pthread failure.\n", i);
372 }
373 }
374 if (thread_error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500375 LOGINFO("Error returned by one or more threads.\n");
376 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500377 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000378 _exit(-1);
379 }
380 LOGINFO("Finished encrypted backup.\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500381 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000382 _exit(0);
383 } else {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500384 // Not encrypted
Dees Troye0a433a2013-12-02 04:10:37 +0000385 std::vector<TarListStruct> FileList;
386 unsigned thread_id = 0;
387 unsigned long long target_size = 0;
388 twrpTar reg;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500389 int ret;
Dees Troye0a433a2013-12-02 04:10:37 +0000390
391 // Generate list of files to back up
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500392 ret = Generate_TarList(tardir, &FileList, &target_size, &thread_id);
393 if (ret < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500394 LOGINFO("Error in Generate_TarList!\n");
395 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500396 close(progress_pipe[1]);
Dees_Troy83bd4832013-05-04 12:39:56 +0000397 _exit(-1);
Dees Troye0a433a2013-12-02 04:10:37 +0000398 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500399 file_count = (unsigned long long)(ret);
Dees Troye0a433a2013-12-02 04:10:37 +0000400 // Create a backup
401 reg.setfn(tarfn);
402 reg.ItemList = &FileList;
403 reg.thread_id = 0;
404 reg.use_encryption = 0;
405 reg.use_compression = use_compression;
406 reg.setsize(Total_Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500407 reg.progress_pipe_fd = progress_pipe_fd;
bigbiffce8f83c2015-12-12 18:30:21 -0500408 reg.part_settings = part_settings;
409 if (Total_Backup_Size > MAX_ARCHIVE_SIZE && !part_settings->adbbackup) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500410 gui_msg("split_backup=Breaking backup file into multiple archives...");
Dees Troye0a433a2013-12-02 04:10:37 +0000411 reg.split_archives = 1;
412 } else {
413 reg.split_archives = 0;
414 }
415 LOGINFO("Creating backup...\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500416 write(progress_pipe_fd, &file_count, sizeof(file_count));
417 write(progress_pipe_fd, &Total_Backup_Size, sizeof(Total_Backup_Size));
Dees Troye0a433a2013-12-02 04:10:37 +0000418 if (createList((void*)&reg) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500419 gui_err("backup_error=Error creating backup.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500420 close(progress_pipe[1]);
Dees Troye0a433a2013-12-02 04:10:37 +0000421 _exit(-1);
422 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500423 close(progress_pipe[1]);
Dees Troye0a433a2013-12-02 04:10:37 +0000424 _exit(0);
Dees_Troy83bd4832013-05-04 12:39:56 +0000425 }
426 } else {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500427 // Parent side
Ethan Yonker472f5062016-02-25 13:47:30 -0600428 unsigned long long fs, size_backup = 0, files_backup = 0, file_count = 0;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500429 int first_data = 0;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500430
431 // Parent closes output side
432 close(progress_pipe[1]);
433
434 // Read progress data from children
435 while (read(progress_pipe[0], &fs, sizeof(fs)) > 0) {
436 if (first_data == 0) {
437 // First incoming data is the file count
438 file_count = fs;
439 if (file_count == 0) file_count = 1; // prevent division by 0 below
440 first_data = 1;
441 } else if (first_data == 1) {
442 // Second incoming data is total size
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500443 first_data = 2;
bigbiffce8f83c2015-12-12 18:30:21 -0500444 part_settings->progress->SetSizeCount(fs, file_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500445 } else {
Ethan Yonker472f5062016-02-25 13:47:30 -0600446 if (fs > 0) {
447 size_backup += fs;
bigbiffce8f83c2015-12-12 18:30:21 -0500448 part_settings->progress->UpdateSize(size_backup);
Ethan Yonker472f5062016-02-25 13:47:30 -0600449 } else { // fs == 0 increments the file counter
450 files_backup++;
bigbiffce8f83c2015-12-12 18:30:21 -0500451 part_settings->progress->UpdateSizeCount(size_backup, files_backup);
Ethan Yonker472f5062016-02-25 13:47:30 -0600452 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500453 }
454 }
455 close(progress_pipe[0]);
456#ifndef BUILD_TWRPTAR_MAIN
457 DataManager::SetValue("tw_file_progress", "");
458 DataManager::SetValue("tw_size_progress", "");
bigbiffce8f83c2015-12-12 18:30:21 -0500459 part_settings->progress->DisplayFileCount(false);
460 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500461
bigbiffce8f83c2015-12-12 18:30:21 -0500462 if (!part_settings->adbbackup) {
James Christopher Adduono90594992016-10-25 00:51:27 -0400463 InfoManager backup_info(backup_folder + "/" + partition_name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -0500464 backup_info.SetValue("backup_size", size_backup);
465 if (use_compression && use_encryption)
466 backup_info.SetValue("backup_type", COMPRESSED_ENCRYPTED);
467 else if (use_encryption)
468 backup_info.SetValue("backup_type", ENCRYPTED);
469 else if (use_compression)
470 backup_info.SetValue("backup_type", COMPRESSED);
471 else
472 backup_info.SetValue("backup_type", UNCOMPRESSED);
473 backup_info.SetValue("file_count", files_backup);
474 backup_info.SaveValues();
475 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500476#endif //ndef BUILD_TWRPTAR_MAIN
bigbiffce8f83c2015-12-12 18:30:21 -0500477 if (TWFunc::Wait_For_Child(*tar_fork_pid, &status, "createTarFork()") != 0)
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500478 return -1;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500479 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500480 return 0;
481}
482
bigbiffce8f83c2015-12-12 18:30:21 -0500483int twrpTar::extractTarFork() {
Dees_Troy83bd4832013-05-04 12:39:56 +0000484 int status = 0;
Matt Mower23d8aae2017-01-06 14:30:33 -0600485 pid_t tar_fork_pid;
486 int progress_pipe[2];
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500487
488 if (pipe(progress_pipe) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500489 LOGINFO("Error creating progress tracking pipe\n");
490 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500491 return -1;
492 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000493
bigbiff7abc5fe2015-01-17 16:53:12 -0500494 tar_fork_pid = fork();
495 if (tar_fork_pid >= 0) // fork was successful
Dees_Troy83bd4832013-05-04 12:39:56 +0000496 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500497 if (tar_fork_pid == 0) // child process
Dees_Troy83bd4832013-05-04 12:39:56 +0000498 {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500499 close(progress_pipe[0]);
500 progress_pipe_fd = progress_pipe[1];
bigbiffce8f83c2015-12-12 18:30:21 -0500501 if (TWFunc::Path_Exists(tarfn) || part_settings->adbbackup) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000502 LOGINFO("Single archive\n");
503 if (extract() != 0)
504 _exit(-1);
bigbiffce8f83c2015-12-12 18:30:21 -0500505 else {
Dees_Troy83bd4832013-05-04 12:39:56 +0000506 _exit(0);
bigbiffce8f83c2015-12-12 18:30:21 -0500507 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000508 } else {
509 LOGINFO("Multiple archives\n");
510 string temp;
511 char actual_filename[255];
512 twrpTar tars[9];
513 pthread_t tar_thread[9];
514 pthread_attr_t tattr;
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500515 unsigned thread_count = 0, i, start_thread_id = 1;
516 int ret, thread_error = 0;
Dees_Troy83bd4832013-05-04 12:39:56 +0000517 void *thread_return;
518
519 basefn = tarfn;
520 temp = basefn + "%i%02i";
521 tarfn += "000";
522 if (!TWFunc::Path_Exists(tarfn)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500523 LOGINFO("Unable to locate '%s' or '%s'\n", basefn.c_str(), tarfn.c_str());
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 (TWFunc::Get_File_Type(tarfn) != 2) {
529 LOGINFO("First tar file '%s' not encrypted\n", tarfn.c_str());
530 tars[0].basefn = basefn;
531 tars[0].thread_id = 0;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500532 tars[0].progress_pipe_fd = progress_pipe_fd;
bigbiffce8f83c2015-12-12 18:30:21 -0500533 tars[0].part_settings = part_settings;
Dees_Troy83bd4832013-05-04 12:39:56 +0000534 if (extractMulti((void*)&tars[0]) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500535 LOGINFO("Error extracting split archive.\n");
536 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500537 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000538 _exit(-1);
539 }
540 } else {
541 start_thread_id = 0;
542 }
543 // Start threading encrypted restores
544 if (pthread_attr_init(&tattr)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500545 LOGINFO("Unable to pthread_attr_init\n");
546 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500547 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000548 _exit(-1);
549 }
550 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500551 LOGINFO("Error setting pthread_attr_setdetachstate\n");
552 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500553 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000554 _exit(-1);
555 }
556 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500557 LOGINFO("Error setting pthread_attr_setscope\n");
558 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500559 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000560 _exit(-1);
561 }
562 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
563 LOGERR("Error setting pthread_attr_setstacksize\n");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500564 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000565 _exit(-1);
566 }*/
567 for (i = start_thread_id; i < 9; i++) {
568 sprintf(actual_filename, temp.c_str(), i, 0);
569 if (TWFunc::Path_Exists(actual_filename)) {
570 thread_count++;
571 tars[i].basefn = basefn;
Ethan Yonker924a80b2014-04-02 10:54:12 -0500572 tars[i].setpassword(password);
Dees_Troy83bd4832013-05-04 12:39:56 +0000573 tars[i].thread_id = i;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500574 tars[i].progress_pipe_fd = progress_pipe_fd;
bigbiffce8f83c2015-12-12 18:30:21 -0500575 tars[i].part_settings = part_settings;
Dees_Troy83bd4832013-05-04 12:39:56 +0000576 LOGINFO("Creating extract thread ID %i\n", i);
577 ret = pthread_create(&tar_thread[i], &tattr, extractMulti, (void*)&tars[i]);
578 if (ret) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500579 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 +0000580 if (extractMulti((void*)&tars[i]) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500581 LOGINFO("Error extracting backup in thread %i.\n", i);
582 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500583 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000584 _exit(-1);
585 } else {
586 tars[i].thread_id = i + 1;
587 }
588 }
589 usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason.
590 } else {
591 break;
592 }
593 }
594 for (i = start_thread_id; i < thread_count + start_thread_id; i++) {
595 if (tars[i].thread_id == i) {
596 if (pthread_join(tar_thread[i], &thread_return)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500597 LOGINFO("Error joining thread %i\n", i);
598 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500599 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000600 _exit(-1);
601 } else {
602 LOGINFO("Joined thread %i.\n", i);
that2252d242015-04-03 22:33:04 +0200603 ret = (int)(intptr_t)thread_return;
Dees_Troy83bd4832013-05-04 12:39:56 +0000604 if (ret != 0) {
605 thread_error = 1;
Ethan Yonker74db1572015-10-28 12:44:49 -0500606 LOGINFO("Thread %i returned an error %i.\n", i, ret);
607 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500608 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000609 _exit(-1);
610 }
611 }
612 } else {
613 LOGINFO("Skipping joining thread %i because of pthread failure.\n", i);
614 }
615 }
616 if (thread_error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500617 LOGINFO("Error returned by one or more threads.\n");
618 gui_err("restore_error=Error during restore process.");
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500619 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000620 _exit(-1);
621 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500622 LOGINFO("Finished encrypted restore.\n");
623 close(progress_pipe_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000624 _exit(0);
625 }
626 }
627 else // parent process
628 {
Ethan Yonker472f5062016-02-25 13:47:30 -0600629 unsigned long long fs, size_backup = 0;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500630
631 // Parent closes output side
632 close(progress_pipe[1]);
633
634 // Read progress data from children
635 while (read(progress_pipe[0], &fs, sizeof(fs)) > 0) {
636 size_backup += fs;
bigbiffce8f83c2015-12-12 18:30:21 -0500637 part_settings->progress->UpdateSize(size_backup);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500638 }
639 close(progress_pipe[0]);
bigbiffce8f83c2015-12-12 18:30:21 -0500640 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500641
bigbiff7abc5fe2015-01-17 16:53:12 -0500642 if (TWFunc::Wait_For_Child(tar_fork_pid, &status, "extractTarFork()") != 0)
Dees_Troy83bd4832013-05-04 12:39:56 +0000643 return -1;
644 }
645 }
646 else // fork has failed
647 {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500648 close(progress_pipe[0]);
649 close(progress_pipe[1]);
Dees_Troy2673cec2013-04-02 20:22:16 +0000650 LOGINFO("extract tar failed to fork.\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500651 return -1;
652 }
653 return 0;
654}
655
Dees_Troy83bd4832013-05-04 12:39:56 +0000656int twrpTar::Generate_TarList(string Path, std::vector<TarListStruct> *TarList, unsigned long long *Target_Size, unsigned *thread_id) {
657 DIR* d;
658 struct dirent* de;
659 struct stat st;
660 string FileName;
661 struct TarListStruct TarItem;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500662 int ret, file_count;
663 file_count = 0;
Dees_Troy83bd4832013-05-04 12:39:56 +0000664
Dees_Troy83bd4832013-05-04 12:39:56 +0000665 d = opendir(Path.c_str());
666 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500667 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Path)(strerror(errno)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000668 closedir(d);
669 return -1;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500670 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000671 while ((de = readdir(d)) != NULL) {
Matt Mower50248ab2014-03-31 15:58:40 -0500672 FileName = Path + "/" + de->d_name;
Matt Mowerbb81e5d2014-03-20 18:05:41 -0500673
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600674 if (de->d_type == DT_BLK || de->d_type == DT_CHR || backup_exclusions->check_skip_dirs(FileName))
Dees_Troy83bd4832013-05-04 12:39:56 +0000675 continue;
676 TarItem.fn = FileName;
677 TarItem.thread_id = *thread_id;
Matt Mower50248ab2014-03-31 15:58:40 -0500678 if (de->d_type == DT_DIR) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000679 TarList->push_back(TarItem);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500680 ret = Generate_TarList(FileName, TarList, Target_Size, thread_id);
681 if (ret < 0)
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500682 return -1;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500683 file_count += ret;
Dees_Troy83bd4832013-05-04 12:39:56 +0000684 } else if (de->d_type == DT_REG || de->d_type == DT_LNK) {
685 stat(FileName.c_str(), &st);
686 TarList->push_back(TarItem);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500687 if (de->d_type == DT_REG) {
688 file_count++;
Dees_Troy83bd4832013-05-04 12:39:56 +0000689 Archive_Current_Size += st.st_size;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500690 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000691 if (Archive_Current_Size != 0 && *Target_Size != 0 && Archive_Current_Size > *Target_Size) {
692 *thread_id = *thread_id + 1;
693 Archive_Current_Size = 0;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500694 }
695 }
696 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000697 closedir(d);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500698 return file_count;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500699}
700
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500701int twrpTar::extractTar() {
Dees_Troye34c1332013-02-06 19:13:00 +0000702 char* charRootDir = (char*) tardir.c_str();
Dees_Troy83bd4832013-05-04 12:39:56 +0000703 if (openTar() == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500704 return -1;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500705 if (tar_extract_all(t, charRootDir, &progress_pipe_fd) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500706 LOGINFO("Unable to extract tar archive '%s'\n", tarfn.c_str());
707 gui_err("restore_error=Error during restore process.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500708 return -1;
709 }
710 if (tar_close(t) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500711 LOGINFO("Unable to close tar file\n");
712 gui_err("restore_error=Error during restore process.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500713 return -1;
714 }
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600715#ifndef BUILD_TWRPTAR_MAIN
bigbiffce8f83c2015-12-12 18:30:21 -0500716 if (part_settings->adbbackup) {
717 if (!twadbbu::Write_TWEOF())
718 return -1;
719 }
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600720#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500721 return 0;
722}
723
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500724int twrpTar::extract() {
bigbiffce8f83c2015-12-12 18:30:21 -0500725 if (!part_settings->adbbackup) {
726 LOGINFO("Setting archive type\n");
727 Set_Archive_Type(TWFunc::Get_File_Type(tarfn));
728 }
729 else {
730 if (part_settings->adb_compression == 1)
731 current_archive_type = COMPRESSED;
732 else
733 current_archive_type = UNCOMPRESSED;
734 }
n0d33b511632013-03-06 21:14:15 +0200735
bigbiffce8f83c2015-12-12 18:30:21 -0500736 if (current_archive_type == COMPRESSED) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500737 //if you return the extractTGZ function directly, stack crashes happen
Dees_Troy2673cec2013-04-02 20:22:16 +0000738 LOGINFO("Extracting gzipped tar\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000739 int ret = extractTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500740 return ret;
bigbiffce8f83c2015-12-12 18:30:21 -0500741 } else if (current_archive_type == ENCRYPTED) {
Ethan Yonker87af5632014-02-10 11:56:35 -0600742 int ret = TWFunc::Try_Decrypting_File(tarfn, password);
Dees_Troy83bd4832013-05-04 12:39:56 +0000743 if (ret < 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500744 gui_msg(Msg(msg::kError, "fail_decrypt_tar=Failed to decrypt tar file '{1}'")(tarfn));
Dees_Troy83bd4832013-05-04 12:39:56 +0000745 return -1;
746 }
747 if (ret == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500748 LOGINFO("Decrypted file is not in tar format.\n");
749 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000750 return -1;
751 }
752 if (ret == 3) {
753 LOGINFO("Extracting encrypted and compressed tar.\n");
bigbiffce8f83c2015-12-12 18:30:21 -0500754 current_archive_type = COMPRESSED_ENCRYPTED;
Dees_Troy83bd4832013-05-04 12:39:56 +0000755 } else
756 LOGINFO("Extracting encrypted tar.\n");
757 return extractTar();
758 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000759 LOGINFO("Extracting uncompressed tar\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500760 return extractTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500761 }
762}
763
Dees Troye0a433a2013-12-02 04:10:37 +0000764int twrpTar::tarList(std::vector<TarListStruct> *TarList, unsigned thread_id) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000765 struct stat st;
766 char buf[PATH_MAX];
767 int list_size = TarList->size(), i = 0, archive_count = 0;
768 string temp;
769 char actual_filename[PATH_MAX];
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500770 unsigned long long fs;
Dees_Troye34c1332013-02-06 19:13:00 +0000771
Dees Troye0a433a2013-12-02 04:10:37 +0000772 if (split_archives) {
773 basefn = tarfn;
774 temp = basefn + "%i%02i";
775 sprintf(actual_filename, temp.c_str(), thread_id, archive_count);
776 tarfn = actual_filename;
777 include_root_dir = true;
778 } else {
779 include_root_dir = false;
780 }
bigbiffce8f83c2015-12-12 18:30:21 -0500781
782 if (part_settings->adbbackup)
783 LOGINFO("Writing tar file '%s' to adb backup\n", tarfn.c_str());
784 else
785 LOGINFO("Creating tar file '%s'\n", tarfn.c_str());
786
Dees_Troy83bd4832013-05-04 12:39:56 +0000787 if (createTar() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500788 LOGINFO("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id);
789 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000790 return -2;
791 }
792 Archive_Current_Size = 0;
793
794 while (i < list_size) {
795 if (TarList->at(i).thread_id == thread_id) {
796 strcpy(buf, TarList->at(i).fn.c_str());
bigbiff bigbiffec8fc282014-03-16 20:32:50 -0400797 lstat(buf, &st);
798 if (S_ISREG(st.st_mode)) { // item is a regular file
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500799 fs = (unsigned long long)(st.st_size);
800 if (split_archives && Archive_Current_Size + fs > MAX_ARCHIVE_SIZE) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000801 if (closeTar() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500802 LOGINFO("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id);
803 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000804 return -3;
805 }
806 archive_count++;
Ethan Yonker74db1572015-10-28 12:44:49 -0500807 gui_msg(Msg("split_thread=Splitting thread ID {1} into archive {2}")(thread_id)(archive_count + 1));
Dees_Troy83bd4832013-05-04 12:39:56 +0000808 if (archive_count > 99) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500809 LOGINFO("Too many archives for thread %i\n", thread_id);
810 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000811 return -4;
812 }
813 sprintf(actual_filename, temp.c_str(), thread_id, archive_count);
814 tarfn = actual_filename;
815 if (createTar() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500816 LOGINFO("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id);
817 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000818 return -2;
819 }
820 Archive_Current_Size = 0;
821 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500822 Archive_Current_Size += fs;
Ethan Yonker472f5062016-02-25 13:47:30 -0600823 fs = 0; // Sending a 0 size to the pipe tells it to increment the file counter
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500824 write(progress_pipe_fd, &fs, sizeof(fs));
Dees_Troy83bd4832013-05-04 12:39:56 +0000825 }
Dees Troye0a433a2013-12-02 04:10:37 +0000826 LOGINFO("addFile '%s' including root: %i\n", buf, include_root_dir);
827 if (addFile(buf, include_root_dir) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500828 LOGINFO("Error adding file '%s' to '%s'\n", buf, tarfn.c_str());
829 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000830 return -1;
831 }
832 }
833 i++;
834 }
835 if (closeTar() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500836 LOGINFO("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id);
837 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000838 return -3;
839 }
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600840 LOGINFO("Thread id %i tarList done, %i archives.\n", thread_id, archive_count);
Dees_Troye34c1332013-02-06 19:13:00 +0000841 return 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500842}
843
Dees_Troy83bd4832013-05-04 12:39:56 +0000844void* twrpTar::createList(void *cookie) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000845 twrpTar* threadTar = (twrpTar*) cookie;
Ethan Yonkereae42092014-03-07 15:33:13 -0600846 if (threadTar->tarList(threadTar->ItemList, threadTar->thread_id) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000847 LOGINFO("ERROR tarList for thread ID %i\n", threadTar->thread_id);
848 return (void*)-2;
849 }
850 LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id);
851 return (void*)0;
852}
853
854void* twrpTar::extractMulti(void *cookie) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000855 twrpTar* threadTar = (twrpTar*) cookie;
856 int archive_count = 0;
857 string temp = threadTar->basefn + "%i%02i";
858 char actual_filename[255];
859 sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count);
860 while (TWFunc::Path_Exists(actual_filename)) {
861 threadTar->tarfn = actual_filename;
862 if (threadTar->extract() != 0) {
863 LOGINFO("Error extracting '%s' in thread ID %i\n", actual_filename, threadTar->thread_id);
864 return (void*)-2;
865 }
866 archive_count++;
867 if (archive_count > 99)
868 break;
869 sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count);
870 }
871 LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id);
872 return (void*)0;
873}
874
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500875int twrpTar::addFilesToExistingTar(vector <string> files, string fn) {
876 char* charTarFile = (char*) fn.c_str();
877
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600878 if (tar_open(&t, charTarFile, NULL, O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500879 return -1;
880 removeEOT(charTarFile);
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600881 if (tar_open(&t, charTarFile, NULL, O_WRONLY | O_APPEND | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500882 return -1;
883 for (unsigned int i = 0; i < files.size(); ++i) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500884 char* file = (char*) files.at(i).c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500885 if (tar_append_file(t, file, file) == -1)
886 return -1;
887 }
888 if (tar_append_eof(t) == -1)
889 return -1;
890 if (tar_close(t) == -1)
891 return -1;
892 return 0;
893}
894
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500895int twrpTar::createTar() {
896 char* charTarFile = (char*) tarfn.c_str();
Dees_Troye34c1332013-02-06 19:13:00 +0000897 char* charRootDir = (char*) tardir.c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500898
Dees_Troy83bd4832013-05-04 12:39:56 +0000899 if (use_encryption && use_compression) {
900 // Compressed and encrypted
bigbiffce8f83c2015-12-12 18:30:21 -0500901 current_archive_type = COMPRESSED_ENCRYPTED;
Dees_Troy83bd4832013-05-04 12:39:56 +0000902 LOGINFO("Using encryption and compression...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +0000903 int i, pipes[4];
904
905 if (pipe(pipes) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500906 LOGINFO("Error creating first pipe\n");
907 gui_err("backup_error=Error creating backup.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500908 return -1;
909 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000910 if (pipe(pipes + 2) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500911 LOGINFO("Error creating second pipe\n");
912 gui_err("backup_error=Error creating backup.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500913 return -1;
Dees_Troy83bd4832013-05-04 12:39:56 +0000914 }
bigbiffce8f83c2015-12-12 18:30:21 -0500915 output_fd = open(tarfn.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
Dees Troy9d723272014-04-07 17:13:10 +0000916 if (output_fd < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500917 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno)));
Dees Troy9d723272014-04-07 17:13:10 +0000918 for (i = 0; i < 4; i++)
919 close(pipes[i]); // close all
920 return -1;
921 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000922 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400923
Dees_Troy83bd4832013-05-04 12:39:56 +0000924 if (pigz_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500925 LOGINFO("pigz fork() failed\n");
926 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +0000927 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000928 for (i = 0; i < 4; i++)
929 close(pipes[i]); // close all
930 return -1;
931 } else if (pigz_pid == 0) {
932 // pigz Child
933 close(pipes[1]);
934 close(pipes[2]);
Ethan Yonkerea2fcf02016-09-12 16:07:19 -0500935 int stdinfd = fileno(stdin);
936 int stdoutfd = fileno(stdout);
937 close(stdinfd);
938 dup2(pipes[0], stdinfd);
939 close(stdoutfd);
940 dup2(pipes[3], stdoutfd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000941 if (execlp("pigz", "pigz", "-", NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500942 LOGINFO("execlp pigz ERROR!\n");
943 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +0000944 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000945 close(pipes[0]);
946 close(pipes[3]);
947 _exit(-1);
948 }
949 } else {
950 // Parent
951 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -0400952
Dees_Troy83bd4832013-05-04 12:39:56 +0000953 if (oaes_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500954 LOGINFO("openaes fork() failed\n");
955 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +0000956 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +0000957 for (i = 0; i < 4; i++)
958 close(pipes[i]); // close all
959 return -1;
960 } else if (oaes_pid == 0) {
961 // openaes Child
Dees_Troy83bd4832013-05-04 12:39:56 +0000962 close(pipes[0]);
963 close(pipes[1]);
964 close(pipes[3]);
Ethan Yonkerea2fcf02016-09-12 16:07:19 -0500965 int stdinfd = fileno(stdin);
966 int stdoutfd = fileno(stdout);
967 close(stdinfd);
968 dup2(pipes[2], stdinfd);
969 close(stdoutfd);
970 dup2(output_fd, stdoutfd);
Ethan Yonker87af5632014-02-10 11:56:35 -0600971 if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500972 LOGINFO("execlp openaes ERROR!\n");
973 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000974 close(pipes[2]);
975 close(output_fd);
976 _exit(-1);
977 }
978 } else {
979 // Parent
980 close(pipes[0]);
981 close(pipes[2]);
982 close(pipes[3]);
983 fd = pipes[1];
Ethan Yonker472f5062016-02-25 13:47:30 -0600984 init_libtar_no_buffer(progress_pipe_fd);
Ethan Yonkerf1179622016-08-25 15:32:21 -0500985 tar_type.writefunc = write_tar_no_buffer;
Matt Mowera8a89d12016-12-30 18:10:37 -0600986 if (tar_fdopen(&t, fd, charRootDir, &tar_type, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000987 close(fd);
Ethan Yonker74db1572015-10-28 12:44:49 -0500988 LOGINFO("tar_fdopen failed\n");
989 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000990 return -1;
991 }
992 return 0;
993 }
994 }
995 } else if (use_compression) {
996 // Compressed
bigbiffce8f83c2015-12-12 18:30:21 -0500997 current_archive_type = COMPRESSED;
Dees_Troy83bd4832013-05-04 12:39:56 +0000998 LOGINFO("Using compression...\n");
999 int pigzfd[2];
bigbiffce8f83c2015-12-12 18:30:21 -05001000 if (part_settings->adbbackup) {
1001 LOGINFO("opening TW_ADB_BACKUP compressed stream\n");
1002 output_fd = open(TW_ADB_BACKUP, O_WRONLY);
1003 }
1004 else {
1005 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);
1006 }
Dees Troy9d723272014-04-07 17:13:10 +00001007 if (output_fd < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001008 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno)));
Dees Troy9d723272014-04-07 17:13:10 +00001009 close(pigzfd[0]);
1010 return -1;
1011 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001012
1013 if (pipe(pigzfd) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001014 LOGINFO("Error creating pipe\n");
1015 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +00001016 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001017 return -1;
1018 }
1019 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001020
Dees_Troy83bd4832013-05-04 12:39:56 +00001021 if (pigz_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001022 LOGINFO("fork() failed\n");
1023 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +00001024 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001025 close(pigzfd[0]);
1026 close(pigzfd[1]);
1027 return -1;
1028 } else if (pigz_pid == 0) {
1029 // Child
1030 close(pigzfd[1]); // close unused output pipe
Ethan Yonkerea2fcf02016-09-12 16:07:19 -05001031 dup2(pigzfd[0], fileno(stdin)); // remap stdin
1032 dup2(output_fd, fileno(stdout)); // remap stdout to output file
Dees_Troy83bd4832013-05-04 12:39:56 +00001033 if (execlp("pigz", "pigz", "-", NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001034 LOGINFO("execlp pigz ERROR!\n");
1035 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001036 close(output_fd);
1037 close(pigzfd[0]);
1038 _exit(-1);
1039 }
1040 } else {
1041 // Parent
1042 close(pigzfd[0]); // close parent input
1043 fd = pigzfd[1]; // copy parent output
Ethan Yonker472f5062016-02-25 13:47:30 -06001044 init_libtar_no_buffer(progress_pipe_fd);
Ethan Yonkerf1179622016-08-25 15:32:21 -05001045 tar_type.writefunc = write_tar_no_buffer;
Matt Mowera8a89d12016-12-30 18:10:37 -06001046 if (tar_fdopen(&t, fd, charRootDir, &tar_type, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +00001047 close(fd);
Ethan Yonker74db1572015-10-28 12:44:49 -05001048 LOGINFO("tar_fdopen failed\n");
1049 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001050 return -1;
1051 }
1052 }
1053 } else if (use_encryption) {
1054 // Encrypted
bigbiffce8f83c2015-12-12 18:30:21 -05001055 current_archive_type = ENCRYPTED;
Dees_Troy83bd4832013-05-04 12:39:56 +00001056 LOGINFO("Using encryption...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +00001057 int oaesfd[2];
bigbiffce8f83c2015-12-12 18:30:21 -05001058 output_fd = open(tarfn.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
Dees Troy9d723272014-04-07 17:13:10 +00001059 if (output_fd < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001060 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno)));
Dees Troy9d723272014-04-07 17:13:10 +00001061 return -1;
1062 }
1063 if (pipe(oaesfd) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001064 LOGINFO("Error creating pipe\n");
1065 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +00001066 close(output_fd);
1067 return -1;
1068 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001069 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001070
Dees_Troy83bd4832013-05-04 12:39:56 +00001071 if (oaes_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001072 LOGINFO("fork() failed\n");
1073 gui_err("backup_error=Error creating backup.");
Dees Troy9d723272014-04-07 17:13:10 +00001074 close(output_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001075 close(oaesfd[0]);
1076 close(oaesfd[1]);
1077 return -1;
1078 } else if (oaes_pid == 0) {
1079 // Child
1080 close(oaesfd[1]); // close unused
Ethan Yonkerea2fcf02016-09-12 16:07:19 -05001081 dup2(oaesfd[0], fileno(stdin)); // remap stdin
1082 dup2(output_fd, fileno(stdout)); // remap stdout to output file
Ethan Yonker87af5632014-02-10 11:56:35 -06001083 if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001084 LOGINFO("execlp openaes ERROR!\n");
1085 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001086 close(output_fd);
1087 close(oaesfd[0]);
1088 _exit(-1);
1089 }
1090 } else {
1091 // Parent
1092 close(oaesfd[0]); // close parent input
1093 fd = oaesfd[1]; // copy parent output
Ethan Yonker472f5062016-02-25 13:47:30 -06001094 init_libtar_no_buffer(progress_pipe_fd);
Ethan Yonkerf1179622016-08-25 15:32:21 -05001095 tar_type.writefunc = write_tar_no_buffer;
Matt Mowera8a89d12016-12-30 18:10:37 -06001096 if (tar_fdopen(&t, fd, charRootDir, &tar_type, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +00001097 close(fd);
Ethan Yonker74db1572015-10-28 12:44:49 -05001098 LOGINFO("tar_fdopen failed\n");
1099 gui_err("backup_error=Error creating backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001100 return -1;
1101 }
1102 return 0;
1103 }
1104 } else {
1105 // Not compressed or encrypted
Ethan Yonker472f5062016-02-25 13:47:30 -06001106 init_libtar_buffer(0, progress_pipe_fd);
bigbiffce8f83c2015-12-12 18:30:21 -05001107 if (part_settings->adbbackup) {
1108 LOGINFO("Opening TW_ADB_BACKUP uncompressed stream\n");
Ethan Yonkerf1179622016-08-25 15:32:21 -05001109 tar_type.writefunc = write_tar_no_buffer;
bigbiffce8f83c2015-12-12 18:30:21 -05001110 output_fd = open(TW_ADB_BACKUP, O_WRONLY);
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001111 if(tar_fdopen(&t, output_fd, charRootDir, &tar_type, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05001112 close(output_fd);
1113 LOGERR("tar_fdopen failed\n");
1114 return -1;
1115 }
1116 }
1117 else {
Ethan Yonkerf1179622016-08-25 15:32:21 -05001118 tar_type.writefunc = write_tar;
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001119 if (tar_open(&t, charTarFile, &tar_type, O_WRONLY | O_CREAT | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) == -1) {
bigbiffce8f83c2015-12-12 18:30:21 -05001120 LOGERR("tar_open error opening '%s'\n", tarfn.c_str());
1121 gui_err("backup_error=Error creating backup.");
1122 return -1;
1123 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001124 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001125 }
1126 return 0;
1127}
1128
Dees_Troy83bd4832013-05-04 12:39:56 +00001129int twrpTar::openTar() {
Dees_Troye34c1332013-02-06 19:13:00 +00001130 char* charRootDir = (char*) tardir.c_str();
1131 char* charTarFile = (char*) tarfn.c_str();
Dees_Troy83bd4832013-05-04 12:39:56 +00001132 string Password;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001133
bigbiffce8f83c2015-12-12 18:30:21 -05001134 if (current_archive_type == COMPRESSED_ENCRYPTED) {
Dees_Troy83bd4832013-05-04 12:39:56 +00001135 LOGINFO("Opening encrypted and compressed backup...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +00001136 int i, pipes[4];
bigbiffce8f83c2015-12-12 18:30:21 -05001137 input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE);
Dees Troy9d723272014-04-07 17:13:10 +00001138 if (input_fd < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001139 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno)));
Dees Troy9d723272014-04-07 17:13:10 +00001140 return -1;
1141 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001142
1143 if (pipe(pipes) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001144 LOGINFO("Error creating first pipe\n");
1145 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001146 close(input_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001147 return -1;
1148 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001149 if (pipe(pipes + 2) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001150 LOGINFO("Error creating second pipe\n");
1151 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001152 close(pipes[0]);
1153 close(pipes[1]);
1154 close(input_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001155 return -1;
1156 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001157 oaes_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001158
Dees_Troy83bd4832013-05-04 12:39:56 +00001159 if (oaes_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001160 LOGINFO("pigz fork() failed\n");
1161 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001162 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001163 for (i = 0; i < 4; i++)
1164 close(pipes[i]); // close all
1165 return -1;
1166 } else if (oaes_pid == 0) {
1167 // openaes Child
1168 close(pipes[0]); // Close pipes that are not used by this child
1169 close(pipes[2]);
1170 close(pipes[3]);
Ethan Yonkerea2fcf02016-09-12 16:07:19 -05001171 int stdinfd = fileno(stdin);
1172 int stdoutfd = fileno(stdout);
1173 close(stdinfd);
1174 dup2(input_fd, stdinfd);
1175 close(stdoutfd);
1176 dup2(pipes[1], stdoutfd);
Ethan Yonker87af5632014-02-10 11:56:35 -06001177 if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001178 LOGINFO("execlp openaes ERROR!\n");
1179 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001180 close(input_fd);
1181 close(pipes[1]);
1182 _exit(-1);
1183 }
1184 } else {
1185 // Parent
1186 pigz_pid = fork();
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001187
Dees_Troy83bd4832013-05-04 12:39:56 +00001188 if (pigz_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001189 LOGINFO("openaes fork() failed\n");
1190 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001191 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001192 for (i = 0; i < 4; i++)
1193 close(pipes[i]); // close all
1194 return -1;
1195 } else if (pigz_pid == 0) {
1196 // pigz Child
1197 close(pipes[1]); // Close pipes not used by this child
1198 close(pipes[2]);
Ethan Yonkerea2fcf02016-09-12 16:07:19 -05001199 int stdinfd = fileno(stdin);
1200 int stdoutfd = fileno(stdout);
1201 close(stdinfd);
1202 dup2(pipes[0], stdinfd);
1203 close(stdoutfd);
1204 dup2(pipes[3], stdoutfd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001205 if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001206 LOGINFO("execlp pigz ERROR!\n");
1207 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001208 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001209 close(pipes[0]);
1210 close(pipes[3]);
1211 _exit(-1);
1212 }
1213 } else {
1214 // Parent
1215 close(pipes[0]); // Close pipes not used by parent
1216 close(pipes[1]);
1217 close(pipes[3]);
1218 fd = pipes[2];
Matt Mowera8a89d12016-12-30 18:10:37 -06001219 if (tar_fdopen(&t, fd, charRootDir, NULL, O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +00001220 close(fd);
Ethan Yonker74db1572015-10-28 12:44:49 -05001221 LOGINFO("tar_fdopen failed\n");
1222 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001223 return -1;
1224 }
1225 }
1226 }
bigbiffce8f83c2015-12-12 18:30:21 -05001227 } else if (current_archive_type == ENCRYPTED) {
Dees_Troy83bd4832013-05-04 12:39:56 +00001228 LOGINFO("Opening encrypted backup...\n");
Dees_Troy83bd4832013-05-04 12:39:56 +00001229 int oaesfd[2];
bigbiffce8f83c2015-12-12 18:30:21 -05001230 input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE);
Dees Troy9d723272014-04-07 17:13:10 +00001231 if (input_fd < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001232 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno)));
Dees Troy9d723272014-04-07 17:13:10 +00001233 return -1;
1234 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001235
Dees Troy9d723272014-04-07 17:13:10 +00001236 if (pipe(oaesfd) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001237 LOGINFO("Error creating pipe\n");
1238 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001239 close(input_fd);
1240 return -1;
1241 }
1242
Dees_Troy83bd4832013-05-04 12:39:56 +00001243 oaes_pid = fork();
1244 if (oaes_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001245 LOGINFO("fork() failed\n");
1246 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001247 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001248 close(oaesfd[0]);
1249 close(oaesfd[1]);
1250 return -1;
1251 } else if (oaes_pid == 0) {
1252 // Child
1253 close(oaesfd[0]); // Close unused pipe
Ethan Yonkerea2fcf02016-09-12 16:07:19 -05001254 int stdinfd = fileno(stdin);
1255 close(stdinfd); // close stdin
1256 dup2(oaesfd[1], fileno(stdout)); // remap stdout
1257 dup2(input_fd, stdinfd); // remap input fd to stdin
Ethan Yonker87af5632014-02-10 11:56:35 -06001258 if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001259 LOGINFO("execlp openaes ERROR!\n");
1260 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001261 close(input_fd);
1262 close(oaesfd[1]);
1263 _exit(-1);
1264 }
1265 } else {
1266 // Parent
1267 close(oaesfd[1]); // close parent output
1268 fd = oaesfd[0]; // copy parent input
Matt Mowera8a89d12016-12-30 18:10:37 -06001269 if (tar_fdopen(&t, fd, charRootDir, NULL, O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +00001270 close(fd);
Ethan Yonker74db1572015-10-28 12:44:49 -05001271 LOGINFO("tar_fdopen failed\n");
1272 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001273 return -1;
1274 }
1275 }
bigbiffce8f83c2015-12-12 18:30:21 -05001276 } else if (current_archive_type == COMPRESSED) {
Dees_Troy83bd4832013-05-04 12:39:56 +00001277 int pigzfd[2];
bigbiffce8f83c2015-12-12 18:30:21 -05001278
1279 LOGINFO("Opening as a gzip...\n");
1280 if (part_settings->adbbackup) {
1281 LOGINFO("opening TW_ADB_RESTORE compressed stream\n");
1282 input_fd = open(TW_ADB_RESTORE, O_RDONLY | O_LARGEFILE);
1283 }
1284 else
1285 input_fd = open(tarfn.c_str(), O_RDONLY | O_LARGEFILE);
1286
Dees Troy9d723272014-04-07 17:13:10 +00001287 if (input_fd < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001288 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno)));
Dees Troy9d723272014-04-07 17:13:10 +00001289 return -1;
1290 }
bigbiffce8f83c2015-12-12 18:30:21 -05001291
Dees Troy9d723272014-04-07 17:13:10 +00001292 if (pipe(pigzfd) < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001293 LOGINFO("Error creating pipe\n");
1294 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001295 close(input_fd);
1296 return -1;
1297 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001298
1299 pigz_pid = fork();
1300 if (pigz_pid < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001301 LOGINFO("fork() failed\n");
1302 gui_err("restore_error=Error during restore process.");
Dees Troy9d723272014-04-07 17:13:10 +00001303 close(input_fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001304 close(pigzfd[0]);
1305 close(pigzfd[1]);
1306 return -1;
1307 } else if (pigz_pid == 0) {
1308 // Child
1309 close(pigzfd[0]);
Ethan Yonkerea2fcf02016-09-12 16:07:19 -05001310 dup2(pigzfd[1], fileno(stdout)); // remap stdout
1311 dup2(input_fd, fileno(stdin)); // remap input fd to stdin
Dees_Troy83bd4832013-05-04 12:39:56 +00001312 if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) {
1313 close(pigzfd[1]);
1314 close(input_fd);
Ethan Yonker74db1572015-10-28 12:44:49 -05001315 LOGINFO("execlp openaes ERROR!\n");
1316 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001317 _exit(-1);
1318 }
1319 } else {
1320 // Parent
1321 close(pigzfd[1]); // close parent output
1322 fd = pigzfd[0]; // copy parent input
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001323 if (tar_fdopen(&t, fd, charRootDir, NULL, O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) {
Dees_Troy83bd4832013-05-04 12:39:56 +00001324 close(fd);
Ethan Yonker74db1572015-10-28 12:44:49 -05001325 LOGINFO("tar_fdopen failed\n");
1326 gui_err("restore_error=Error during restore process.");
Dees_Troy83bd4832013-05-04 12:39:56 +00001327 return -1;
1328 }
1329 }
bigbiffce8f83c2015-12-12 18:30:21 -05001330 } else {
1331 if (part_settings->adbbackup) {
1332 LOGINFO("Opening TW_ADB_RESTORE uncompressed stream\n");
1333 input_fd = open(TW_ADB_RESTORE, O_RDONLY);
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001334 if (tar_fdopen(&t, input_fd, charRootDir, NULL, O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05001335 LOGERR("Unable to open tar archive '%s'\n", charTarFile);
1336 gui_err("restore_error=Error during restore process.");
1337 return -1;
1338 }
1339 }
1340 else {
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001341 if (tar_open(&t, charTarFile, NULL, O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05001342 LOGERR("Unable to open tar archive '%s'\n", charTarFile);
1343 gui_err("restore_error=Error during restore process.");
1344 return -1;
1345 }
1346 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001347 }
1348 return 0;
1349}
1350
1351string twrpTar::Strip_Root_Dir(string Path) {
1352 string temp;
1353 size_t slash;
1354
1355 if (Path.substr(0, 1) == "/")
1356 temp = Path.substr(1, Path.size() - 1);
1357 else
1358 temp = Path;
1359 slash = temp.find("/");
1360 if (slash == string::npos)
1361 return temp;
1362 else {
1363 string stripped;
1364
1365 stripped = temp.substr(slash, temp.size() - slash);
1366 return stripped;
1367 }
1368 return temp;
1369}
1370
1371int twrpTar::addFile(string fn, bool include_root) {
1372 char* charTarFile = (char*) fn.c_str();
1373 if (include_root) {
1374 if (tar_append_file(t, charTarFile, NULL) == -1)
1375 return -1;
1376 } else {
1377 string temp = Strip_Root_Dir(fn);
1378 char* charTarPath = (char*) temp.c_str();
1379 if (tar_append_file(t, charTarFile, charTarPath) == -1)
1380 return -1;
1381 }
1382 return 0;
1383}
1384
Dees_Troy83bd4832013-05-04 12:39:56 +00001385int twrpTar::closeTar() {
bigbiffce8f83c2015-12-12 18:30:21 -05001386 LOGINFO("Closing tar\n");
Dees_Troye34c1332013-02-06 19:13:00 +00001387 flush_libtar_buffer(t->fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001388 if (tar_append_eof(t) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001389 LOGINFO("tar_append_eof(): %s\n", strerror(errno));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001390 tar_close(t);
1391 return -1;
1392 }
1393 if (tar_close(t) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001394 LOGINFO("Unable to close tar archive: '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001395 return -1;
1396 }
bigbiffce8f83c2015-12-12 18:30:21 -05001397 if (current_archive_type > 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001398 close(fd);
Dees_Troy83bd4832013-05-04 12:39:56 +00001399 int status;
1400 if (pigz_pid > 0 && TWFunc::Wait_For_Child(pigz_pid, &status, "pigz") != 0)
1401 return -1;
1402 if (oaes_pid > 0 && TWFunc::Wait_For_Child(oaes_pid, &status, "openaes") != 0)
1403 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001404 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001405 free_libtar_buffer();
bigbiffce8f83c2015-12-12 18:30:21 -05001406 if (!part_settings->adbbackup) {
1407 if (use_compression && !use_encryption) {
1408 string gzname = tarfn + ".gz";
1409 if (TWFunc::Path_Exists(gzname)) {
1410 rename(gzname.c_str(), tarfn.c_str());
1411 }
Dees Troye0a433a2013-12-02 04:10:37 +00001412 }
bigbiffce8f83c2015-12-12 18:30:21 -05001413 if (TWFunc::Get_File_Size(tarfn) == 0) {
1414 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(tarfn));
1415 return -1;
1416 }
Ethan Yonker960f0302014-12-21 21:54:00 -06001417#ifndef BUILD_TWRPTAR_MAIN
bigbiffce8f83c2015-12-12 18:30:21 -05001418 tw_set_default_metadata(tarfn.c_str());
Ethan Yonker960f0302014-12-21 21:54:00 -06001419#endif
bigbiffce8f83c2015-12-12 18:30:21 -05001420 }
1421 else {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001422#ifndef BUILD_TWRPTAR_MAIN
bigbiffce8f83c2015-12-12 18:30:21 -05001423 if (!twadbbu::Write_TWEOF())
1424 return -1;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001425#endif
bigbiffce8f83c2015-12-12 18:30:21 -05001426 }
Ethan Yonkerea2fcf02016-09-12 16:07:19 -05001427 if (input_fd >= 0)
1428 close(input_fd);
1429 if (output_fd >= 0)
1430 close(output_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001431 return 0;
1432}
1433
1434int twrpTar::removeEOT(string tarFile) {
1435 char* charTarFile = (char*) tarFile.c_str();
Matt Mower2b18a532015-02-20 16:58:05 -06001436 off_t tarFileEnd = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001437 while (th_read(t) == 0) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001438 if (TH_ISREG(t))
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001439 tar_skip_regfile(t);
1440 tarFileEnd = lseek(t->fd, 0, SEEK_CUR);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001441 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001442 if (tar_close(t) == -1)
1443 return -1;
Matt Mower2b18a532015-02-20 16:58:05 -06001444 if (tarFileEnd > 0 && truncate(charTarFile, tarFileEnd) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001445 return -1;
1446 return 0;
1447}
1448
n0d33b511632013-03-06 21:14:15 +02001449int twrpTar::entryExists(string entry) {
1450 char* searchstr = (char*)entry.c_str();
1451 int ret;
1452
bigbiffce8f83c2015-12-12 18:30:21 -05001453 Set_Archive_Type(TWFunc::Get_File_Type(tarfn));
n0d33b511632013-03-06 21:14:15 +02001454
Dees_Troy83bd4832013-05-04 12:39:56 +00001455 if (openTar() == -1)
n0d33b511632013-03-06 21:14:15 +02001456 ret = 0;
1457 else
1458 ret = tar_find(t, searchstr);
1459
Dees_Troy83bd4832013-05-04 12:39:56 +00001460 if (closeTar() != 0)
1461 LOGINFO("Unable to close tar after searching for entry.\n");
n0d33b511632013-03-06 21:14:15 +02001462
1463 return ret;
1464}
1465
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001466unsigned long long twrpTar::get_size() {
bigbiffce8f83c2015-12-12 18:30:21 -05001467 if (TWFunc::Path_Exists(tarfn) && !part_settings->adbbackup) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001468 LOGINFO("Single archive\n");
bigbiffce8f83c2015-12-12 18:30:21 -05001469 return uncompressedSize(tarfn);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001470 } else {
1471 LOGINFO("Multiple archives\n");
1472 string temp;
1473 char actual_filename[255];
bigbiffce8f83c2015-12-12 18:30:21 -05001474 int archive_count = 0;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001475 unsigned long long total_restore_size = 0;
1476
1477 basefn = tarfn;
1478 temp = basefn + "%i%02i";
1479 tarfn += "000";
1480 thread_id = 0;
1481 sprintf(actual_filename, temp.c_str(), thread_id, archive_count);
bigbiffce8f83c2015-12-12 18:30:21 -05001482 if (!part_settings->adbbackup) {
1483 if (!TWFunc::Path_Exists(actual_filename)) {
1484 LOGERR("Unable to locate '%s' or '%s'\n", basefn.c_str(), tarfn.c_str());
1485 return 0;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001486 }
bigbiffce8f83c2015-12-12 18:30:21 -05001487 for (int i = 0; i < 9; i++) {
1488 archive_count = 0;
1489 sprintf(actual_filename, temp.c_str(), i, archive_count);
1490 while (TWFunc::Path_Exists(actual_filename)) {
1491 total_restore_size += uncompressedSize(actual_filename);
1492 archive_count++;
1493 if (archive_count > 99)
1494 break;
1495 sprintf(actual_filename, temp.c_str(), i, archive_count);
1496 }
1497 }
1498 #ifndef BUILD_TWRPTAR_MAIN
1499 if (!part_settings->adbbackup) {
1500 InfoManager backup_info(tarfn + ".info");
1501 backup_info.SetValue("backup_size", total_restore_size);
1502 backup_info.SetValue("backup_type", current_archive_type);
1503 backup_info.SaveValues();
1504 }
1505 #endif //ndef BUILD_TWRPTAR_MAIN
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001506 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001507 return total_restore_size;
1508 }
1509 return 0;
1510}
1511
bigbiffce8f83c2015-12-12 18:30:21 -05001512unsigned long long twrpTar::uncompressedSize(string filename) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001513 unsigned long long total_size = 0;
1514 string Tar, Command, result;
1515 vector<string> split;
1516
bigbiffce8f83c2015-12-12 18:30:21 -05001517 Set_Archive_Type(TWFunc::Get_File_Type(tarfn));
1518 if (current_archive_type == UNCOMPRESSED) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001519 total_size = TWFunc::Get_File_Size(filename);
bigbiffce8f83c2015-12-12 18:30:21 -05001520 } else if (current_archive_type == COMPRESSED) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001521 // Compressed
1522 Command = "pigz -l '" + filename + "'";
1523 /* if we set Command = "pigz -l " + tarfn + " | sed '1d' | cut -f5 -d' '";
1524 we get the uncompressed size at once. */
1525 TWFunc::Exec_Cmd(Command, result);
1526 if (!result.empty()) {
1527 /* Expected output:
1528 compressed original reduced name
1529 95855838 179403776 -1.3% data.yaffs2.win
1530 ^
1531 split[5]
1532 */
1533 split = TWFunc::split_string(result, ' ', true);
1534 if (split.size() > 4)
Matt Mowera8a89d12016-12-30 18:10:37 -06001535 total_size = atoi(split[5].c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001536 }
bigbiffce8f83c2015-12-12 18:30:21 -05001537 } else if (current_archive_type == COMPRESSED_ENCRYPTED) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001538 // File is encrypted and may be compressed
1539 int ret = TWFunc::Try_Decrypting_File(filename, password);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001540 if (ret < 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001541 gui_msg(Msg(msg::kError, "fail_decrypt_tar=Failed to decrypt tar file '{1}'")(tarfn));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001542 total_size = TWFunc::Get_File_Size(filename);
1543 } else if (ret == 1) {
1544 LOGERR("Decrypted file is not in tar format.\n");
1545 total_size = TWFunc::Get_File_Size(filename);
1546 } else if (ret == 3) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001547 Command = "openaes dec --key \"" + password + "\" --in '" + filename + "' | pigz -l";
1548 /* if we set Command = "pigz -l " + tarfn + " | sed '1d' | cut -f5 -d' '";
1549 we get the uncompressed size at once. */
1550 TWFunc::Exec_Cmd(Command, result);
1551 if (!result.empty()) {
1552 LOGINFO("result was: '%s'\n", result.c_str());
1553 /* Expected output:
1554 compressed original reduced name
1555 95855838 179403776 -1.3% data.yaffs2.win
1556 ^
1557 split[5]
1558 */
1559 split = TWFunc::split_string(result, ' ', true);
1560 if (split.size() > 4)
Matt Mowera8a89d12016-12-30 18:10:37 -06001561 total_size = atoi(split[5].c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001562 }
1563 } else {
1564 total_size = TWFunc::Get_File_Size(filename);
1565 }
1566 }
1567
1568 return total_size;
1569}
1570
Dees_Troye34c1332013-02-06 19:13:00 +00001571extern "C" ssize_t write_tar(int fd, const void *buffer, size_t size) {
1572 return (ssize_t) write_libtar_buffer(fd, buffer, size);
Dees_Troy40bbcf82013-02-12 15:01:53 +00001573}
Ethan Yonker472f5062016-02-25 13:47:30 -06001574
1575extern "C" ssize_t write_tar_no_buffer(int fd, const void *buffer, size_t size) {
1576 return (ssize_t) write_libtar_no_buffer(fd, buffer, size);
1577}