Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 1 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2 | /* |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 3 | Copyright 2013 to 2020 TeamWin |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 4 | This file is part of TWRP/TeamWin Recovery Project. |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 5 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 6 | 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 bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 10 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 11 | 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 bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 15 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 16 | 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 bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | extern "C" { |
| 21 | #include "libtar/libtar.h" |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 22 | #include "twrpTar.h" |
| 23 | #include "tarWrite.h" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 24 | } |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 27 | #include <sys/wait.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <fstream> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 32 | #include <iostream> |
| 33 | #include <string> |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 34 | #include <sstream> |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 35 | #include <vector> |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 36 | #include <csignal> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 37 | #include <dirent.h> |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 38 | #include <libgen.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 39 | #include <sys/mman.h> |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 40 | #include <sys/ioctl.h> |
| 41 | #include <zlib.h> |
| 42 | #include <semaphore.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 43 | #include "twrpTar.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 44 | #include "twcommon.h" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 45 | #include "variables.h" |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 46 | #include "adbbu/libtwadbbu.hpp" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 47 | #include "twrp-functions.hpp" |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 48 | #include "gui/gui.hpp" |
| 49 | #include "progresstracking.hpp" |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 50 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 51 | #ifndef BUILD_TWRPTAR_MAIN |
| 52 | #include "data.hpp" |
| 53 | #include "infomanager.hpp" |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 54 | #include "set_metadata.h" |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 55 | #endif //ndef BUILD_TWRPTAR_MAIN |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 56 | |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 57 | #ifdef TW_INCLUDE_FBE |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 58 | #ifdef USE_FSCRYPT |
| 59 | #include "fscrypt_policy.h" |
| 60 | #else |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 61 | #include "crypto/ext4crypt/ext4crypt_tar.h" |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 62 | #endif |
| 63 | #endif |
| 64 | |
| 65 | #ifdef TW_INCLUDE_FBE |
| 66 | #ifdef USE_FSCRYPT |
| 67 | #define TWTAR_FLAGS TAR_GNU | TAR_STORE_SELINUX | TAR_STORE_POSIX_CAP | TAR_STORE_ANDROID_USER_XATTR | TAR_STORE_FSCRYPT_POL |
| 68 | #else |
| 69 | #define TWTAR_FLAGS TAR_GNU | TAR_STORE_SELINUX | TAR_STORE_POSIX_CAP | TAR_STORE_ANDROID_USER_XATTR | TAR_STORE_EXT4_POL |
| 70 | #endif |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 71 | #else |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 72 | #define TWTAR_FLAGS TAR_GNU | TAR_STORE_SELINUX | TAR_STORE_POSIX_CAP | TAR_STORE_ANDROID_USER_XATTR |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 73 | #endif |
| 74 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 75 | using namespace std; |
| 76 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 77 | twrpTar::twrpTar(void) { |
| 78 | use_encryption = 0; |
| 79 | userdata_encryption = 0; |
| 80 | use_compression = 0; |
| 81 | split_archives = 0; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 82 | pigz_pid = 0; |
| 83 | oaes_pid = 0; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 84 | Total_Backup_Size = 0; |
Ethan Yonker | 1a147a4 | 2015-02-27 13:14:44 -0600 | [diff] [blame] | 85 | Archive_Current_Size = 0; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 86 | include_root_dir = true; |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 87 | tar_type.openfunc = open; |
| 88 | tar_type.closefunc = close; |
| 89 | tar_type.readfunc = read; |
Ethan Yonker | ea2fcf0 | 2016-09-12 16:07:19 -0500 | [diff] [blame] | 90 | input_fd = -1; |
| 91 | output_fd = -1; |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 92 | backup_exclusions = NULL; |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 93 | #ifdef TW_INCLUDE_FBE |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 94 | #ifdef USE_FSCRYPT |
| 95 | fscrypt_set_mode(); |
| 96 | #else |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 97 | e4crypt_set_mode(); |
| 98 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 99 | #endif |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | twrpTar::~twrpTar(void) { |
| 103 | // Do nothing |
| 104 | } |
| 105 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 106 | void twrpTar::setfn(string fn) { |
| 107 | tarfn = fn; |
| 108 | } |
| 109 | |
| 110 | void twrpTar::setdir(string dir) { |
| 111 | tardir = dir; |
| 112 | } |
| 113 | |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 114 | void twrpTar::setsize(unsigned long long backup_size) { |
| 115 | Total_Backup_Size = backup_size; |
| 116 | } |
| 117 | |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 118 | void twrpTar::setpassword(string pass) { |
| 119 | password = pass; |
| 120 | } |
| 121 | |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 122 | void twrpTar::Signal_Kill(int signum) { |
| 123 | _exit(255); |
| 124 | } |
| 125 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 126 | void twrpTar::Set_Archive_Type(Archive_Type archive_type) { |
| 127 | current_archive_type = archive_type; |
| 128 | } |
| 129 | |
| 130 | int twrpTar::createTarFork(pid_t *tar_fork_pid) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 131 | int status = 0; |
Matt Mower | 23d8aae | 2017-01-06 14:30:33 -0600 | [diff] [blame] | 132 | int progress_pipe[2]; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 133 | |
| 134 | file_count = 0; |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 135 | if (backup_exclusions == NULL) { |
| 136 | LOGINFO("backup_exclusions is NULL\n"); |
| 137 | return -1; |
| 138 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 139 | |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 140 | #ifndef BUILD_TWRPTAR_MAIN |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 141 | if (part_settings->adbbackup) { |
| 142 | std::string Backup_FileName(tarfn); |
| 143 | if (!twadbbu::Write_TWFN(Backup_FileName, Total_Backup_Size, use_compression)) |
| 144 | return -1; |
| 145 | } |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 146 | #endif |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 147 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 148 | if (pipe(progress_pipe) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 149 | LOGINFO("Error creating progress tracking pipe\n"); |
| 150 | gui_err("backup_error=Error creating backup."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 151 | return -1; |
| 152 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 153 | if ((*tar_fork_pid = fork()) == -1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 154 | LOGINFO("create tar failed to fork.\n"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 155 | gui_err("backup_error=Error creating backup."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 156 | close(progress_pipe[0]); |
| 157 | close(progress_pipe[1]); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 158 | return -1; |
| 159 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 160 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 161 | if (*tar_fork_pid == 0) { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 162 | // Child process |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 163 | // Child closes input side of progress pipe |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 164 | signal(SIGUSR2, twrpTar::Signal_Kill); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 165 | close(progress_pipe[0]); |
| 166 | progress_pipe_fd = progress_pipe[1]; |
| 167 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 168 | if (use_encryption || userdata_encryption) { |
| 169 | LOGINFO("Using encryption\n"); |
| 170 | DIR* d; |
| 171 | struct dirent* de; |
that | 2252d24 | 2015-04-03 22:33:04 +0200 | [diff] [blame] | 172 | unsigned long long regular_size = 0, encrypt_size = 0, target_size = 0, total_size; |
| 173 | unsigned enc_thread_id = 1, regular_thread_id = 0, i, start_thread_id = 1, core_count = 1; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 174 | int item_len, ret, thread_error = 0; |
| 175 | std::vector<TarListStruct> RegularList; |
| 176 | std::vector<TarListStruct> EncryptList; |
| 177 | string FileName; |
| 178 | struct TarListStruct TarItem; |
| 179 | twrpTar reg, enc[9]; |
| 180 | struct stat st; |
| 181 | pthread_t enc_thread[9]; |
| 182 | pthread_attr_t tattr; |
| 183 | void *thread_return; |
| 184 | |
| 185 | core_count = sysconf(_SC_NPROCESSORS_CONF); |
| 186 | if (core_count > 8) |
| 187 | core_count = 8; |
that | 2252d24 | 2015-04-03 22:33:04 +0200 | [diff] [blame] | 188 | LOGINFO(" Core Count : %u\n", core_count); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 189 | Archive_Current_Size = 0; |
| 190 | |
| 191 | d = opendir(tardir.c_str()); |
| 192 | if (d == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 193 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tardir)(strerror(errno))); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 194 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 195 | _exit(-1); |
| 196 | } |
| 197 | // Figure out the size of all data to be encrypted and create a list of unencrypted files |
| 198 | while ((de = readdir(d)) != NULL) { |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 199 | FileName = tardir + "/" + de->d_name; |
| 200 | |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 201 | if (de->d_type == DT_BLK || de->d_type == DT_CHR || backup_exclusions->check_skip_dirs(FileName)) |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 202 | continue; |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 203 | if (de->d_type == DT_DIR) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 204 | 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))) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 206 | ret = Generate_TarList(FileName, &RegularList, &target_size, ®ular_thread_id); |
| 207 | if (ret < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 208 | LOGINFO("Error in Generate_TarList with regular list!\n"); |
| 209 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 210 | closedir(d); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 211 | close(progress_pipe_fd); |
| 212 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 213 | _exit(-1); |
| 214 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 215 | file_count = (unsigned long long)(ret); |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 216 | regular_size += backup_exclusions->Get_Folder_Size(FileName); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 217 | } else { |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 218 | encrypt_size += backup_exclusions->Get_Folder_Size(FileName); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 219 | } |
| 220 | } else if (de->d_type == DT_REG) { |
| 221 | stat(FileName.c_str(), &st); |
| 222 | encrypt_size += (unsigned long long)(st.st_size); |
| 223 | } |
| 224 | } |
| 225 | closedir(d); |
| 226 | |
| 227 | target_size = encrypt_size / core_count; |
| 228 | target_size++; |
| 229 | LOGINFO(" Unencrypted size: %llu\n", regular_size); |
| 230 | LOGINFO(" Encrypted size : %llu\n", encrypt_size); |
| 231 | LOGINFO(" Target size : %llu\n", target_size); |
| 232 | if (!userdata_encryption) { |
| 233 | enc_thread_id = 0; |
| 234 | start_thread_id = 0; |
| 235 | core_count--; |
| 236 | } |
| 237 | Archive_Current_Size = 0; |
| 238 | |
| 239 | d = opendir(tardir.c_str()); |
| 240 | if (d == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 241 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tardir)(strerror(errno))); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 242 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 243 | _exit(-1); |
| 244 | } |
| 245 | // Divide up the encrypted file list for threading |
| 246 | while ((de = readdir(d)) != NULL) { |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 247 | FileName = tardir + "/" + de->d_name; |
| 248 | |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 249 | if (de->d_type == DT_BLK || de->d_type == DT_CHR || backup_exclusions->check_skip_dirs(FileName)) |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 250 | continue; |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 251 | if (de->d_type == DT_DIR) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 252 | item_len = strlen(de->d_name); |
| 253 | if (userdata_encryption && ((item_len >= 3 && strncmp(de->d_name, "app", 3) == 0) || (item_len >= 6 && strncmp(de->d_name, "dalvik", 6) == 0))) { |
| 254 | // Do nothing, we added these to RegularList earlier |
| 255 | } else { |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 256 | FileName = tardir + "/" + de->d_name; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 257 | ret = Generate_TarList(FileName, &EncryptList, &target_size, &enc_thread_id); |
| 258 | if (ret < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 259 | LOGINFO("Error in Generate_TarList with encrypted list!\n"); |
| 260 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 261 | closedir(d); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 262 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 263 | _exit(-1); |
| 264 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 265 | file_count += (unsigned long long)(ret); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 266 | } |
| 267 | } else if (de->d_type == DT_REG || de->d_type == DT_LNK) { |
| 268 | stat(FileName.c_str(), &st); |
| 269 | if (de->d_type == DT_REG) |
| 270 | Archive_Current_Size += (unsigned long long)(st.st_size); |
| 271 | TarItem.fn = FileName; |
| 272 | TarItem.thread_id = enc_thread_id; |
| 273 | EncryptList.push_back(TarItem); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 274 | file_count++; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | closedir(d); |
| 278 | if (enc_thread_id != core_count) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 279 | LOGINFO("Error dividing up threads for encryption, %u threads for %u cores!\n", enc_thread_id, core_count); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 280 | if (enc_thread_id > core_count) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 281 | gui_err("backup_error=Error creating backup."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 282 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 283 | _exit(-1); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 284 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 285 | LOGINFO("Continuining anyway."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 286 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 289 | // Send file count to parent |
| 290 | write(progress_pipe_fd, &file_count, sizeof(file_count)); |
| 291 | // Send backup size to parent |
| 292 | total_size = regular_size + encrypt_size; |
| 293 | write(progress_pipe_fd, &total_size, sizeof(total_size)); |
| 294 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 295 | if (userdata_encryption) { |
| 296 | // Create a backup of unencrypted data |
| 297 | reg.setfn(tarfn); |
| 298 | reg.ItemList = &RegularList; |
| 299 | reg.thread_id = 0; |
| 300 | reg.use_encryption = 0; |
| 301 | reg.use_compression = use_compression; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 302 | reg.split_archives = 1; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 303 | reg.progress_pipe_fd = progress_pipe_fd; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 304 | reg.part_settings = part_settings; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 305 | LOGINFO("Creating unencrypted backup...\n"); |
| 306 | if (createList((void*)®) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 307 | LOGINFO("Error creating unencrypted backup.\n"); |
| 308 | gui_err("backup_error=Error creating backup."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 309 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 310 | _exit(-1); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (pthread_attr_init(&tattr)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 315 | LOGINFO("Unable to pthread_attr_init\n"); |
| 316 | gui_err("backup_error=Error creating backup."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 317 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 318 | _exit(-1); |
| 319 | } |
| 320 | if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 321 | LOGINFO("Error setting pthread_attr_setdetachstate\n"); |
| 322 | gui_err("backup_error=Error creating backup."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 323 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 324 | _exit(-1); |
| 325 | } |
| 326 | if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 327 | LOGINFO("Error setting pthread_attr_setscope\n"); |
| 328 | gui_err("backup_error=Error creating backup."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 329 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 330 | _exit(-1); |
| 331 | } |
| 332 | /*if (pthread_attr_setstacksize(&tattr, 524288)) { |
| 333 | LOGERR("Error setting pthread_attr_setstacksize\n"); |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 334 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 335 | _exit(-1); |
| 336 | }*/ |
| 337 | |
| 338 | // Create threads for the divided up encryption lists |
| 339 | for (i = start_thread_id; i <= core_count; i++) { |
| 340 | enc[i].setdir(tardir); |
| 341 | enc[i].setfn(tarfn); |
| 342 | enc[i].ItemList = &EncryptList; |
| 343 | enc[i].thread_id = i; |
| 344 | enc[i].use_encryption = use_encryption; |
Ethan Yonker | 924a80b | 2014-04-02 10:54:12 -0500 | [diff] [blame] | 345 | enc[i].setpassword(password); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 346 | enc[i].use_compression = use_compression; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 347 | enc[i].split_archives = 1; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 348 | enc[i].progress_pipe_fd = progress_pipe_fd; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 349 | enc[i].part_settings = part_settings; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 350 | LOGINFO("Start encryption thread %i\n", i); |
| 351 | ret = pthread_create(&enc_thread[i], &tattr, createList, (void*)&enc[i]); |
| 352 | if (ret) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 353 | LOGINFO("Unable to create %i thread for encryption! %i\nContinuing in same thread (backup will be slower).\n", i, ret); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 354 | if (createList((void*)&enc[i]) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 355 | LOGINFO("Error creating encrypted backup %i.\n", i); |
| 356 | gui_err("backup_error=Error creating backup."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 357 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 358 | _exit(-1); |
| 359 | } else { |
| 360 | enc[i].thread_id = i + 1; |
| 361 | } |
| 362 | } |
| 363 | usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason. |
| 364 | } |
| 365 | if (pthread_attr_destroy(&tattr)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 366 | LOGINFO("Failed to pthread_attr_destroy\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 367 | } |
| 368 | for (i = start_thread_id; i <= core_count; i++) { |
| 369 | if (enc[i].thread_id == i) { |
| 370 | if (pthread_join(enc_thread[i], &thread_return)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 371 | LOGINFO("Error joining thread %i\n", i); |
| 372 | gui_err("backup_error=Error creating backup."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 373 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 374 | _exit(-1); |
| 375 | } else { |
| 376 | LOGINFO("Joined thread %i.\n", i); |
that | 2252d24 | 2015-04-03 22:33:04 +0200 | [diff] [blame] | 377 | ret = (int)(intptr_t)thread_return; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 378 | if (ret != 0) { |
| 379 | thread_error = 1; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 380 | LOGINFO("Thread %i returned an error %i.\n", i, ret); |
| 381 | gui_err("backup_error=Error creating backup."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 382 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 383 | _exit(-1); |
| 384 | } |
| 385 | } |
| 386 | } else { |
| 387 | LOGINFO("Skipping joining thread %i because of pthread failure.\n", i); |
| 388 | } |
| 389 | } |
| 390 | if (thread_error) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 391 | LOGINFO("Error returned by one or more threads.\n"); |
| 392 | gui_err("backup_error=Error creating backup."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 393 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 394 | _exit(-1); |
| 395 | } |
| 396 | LOGINFO("Finished encrypted backup.\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 397 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 398 | _exit(0); |
| 399 | } else { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 400 | // Not encrypted |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 401 | std::vector<TarListStruct> FileList; |
| 402 | unsigned thread_id = 0; |
| 403 | unsigned long long target_size = 0; |
| 404 | twrpTar reg; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 405 | int ret; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 406 | |
| 407 | // Generate list of files to back up |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 408 | ret = Generate_TarList(tardir, &FileList, &target_size, &thread_id); |
| 409 | if (ret < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 410 | LOGINFO("Error in Generate_TarList!\n"); |
| 411 | gui_err("backup_error=Error creating backup."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 412 | close(progress_pipe[1]); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 413 | _exit(-1); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 414 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 415 | file_count = (unsigned long long)(ret); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 416 | // Create a backup |
| 417 | reg.setfn(tarfn); |
| 418 | reg.ItemList = &FileList; |
| 419 | reg.thread_id = 0; |
| 420 | reg.use_encryption = 0; |
| 421 | reg.use_compression = use_compression; |
| 422 | reg.setsize(Total_Backup_Size); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 423 | reg.progress_pipe_fd = progress_pipe_fd; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 424 | reg.part_settings = part_settings; |
| 425 | if (Total_Backup_Size > MAX_ARCHIVE_SIZE && !part_settings->adbbackup) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 426 | gui_msg("split_backup=Breaking backup file into multiple archives..."); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 427 | reg.split_archives = 1; |
| 428 | } else { |
| 429 | reg.split_archives = 0; |
| 430 | } |
| 431 | LOGINFO("Creating backup...\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 432 | write(progress_pipe_fd, &file_count, sizeof(file_count)); |
| 433 | write(progress_pipe_fd, &Total_Backup_Size, sizeof(Total_Backup_Size)); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 434 | if (createList((void*)®) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 435 | gui_err("backup_error=Error creating backup."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 436 | close(progress_pipe[1]); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 437 | _exit(-1); |
| 438 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 439 | close(progress_pipe[1]); |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 440 | _exit(0); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 441 | } |
| 442 | } else { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 443 | // Parent side |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 444 | unsigned long long fs, size_backup = 0, files_backup = 0, file_count = 0; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 445 | int first_data = 0; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 446 | |
| 447 | // Parent closes output side |
| 448 | close(progress_pipe[1]); |
| 449 | |
| 450 | // Read progress data from children |
| 451 | while (read(progress_pipe[0], &fs, sizeof(fs)) > 0) { |
| 452 | if (first_data == 0) { |
| 453 | // First incoming data is the file count |
| 454 | file_count = fs; |
| 455 | if (file_count == 0) file_count = 1; // prevent division by 0 below |
| 456 | first_data = 1; |
| 457 | } else if (first_data == 1) { |
| 458 | // Second incoming data is total size |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 459 | first_data = 2; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 460 | part_settings->progress->SetSizeCount(fs, file_count); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 461 | } else { |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 462 | if (fs > 0) { |
| 463 | size_backup += fs; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 464 | part_settings->progress->UpdateSize(size_backup); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 465 | } else { // fs == 0 increments the file counter |
| 466 | files_backup++; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 467 | part_settings->progress->UpdateSizeCount(size_backup, files_backup); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 468 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | close(progress_pipe[0]); |
| 472 | #ifndef BUILD_TWRPTAR_MAIN |
| 473 | DataManager::SetValue("tw_file_progress", ""); |
| 474 | DataManager::SetValue("tw_size_progress", ""); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 475 | part_settings->progress->DisplayFileCount(false); |
| 476 | part_settings->progress->UpdateDisplayDetails(true); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 477 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 478 | if (!part_settings->adbbackup) { |
James Christopher Adduono | 9059499 | 2016-10-25 00:51:27 -0400 | [diff] [blame] | 479 | InfoManager backup_info(backup_folder + "/" + partition_name + ".info"); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 480 | backup_info.SetValue("backup_size", size_backup); |
| 481 | if (use_compression && use_encryption) |
| 482 | backup_info.SetValue("backup_type", COMPRESSED_ENCRYPTED); |
| 483 | else if (use_encryption) |
| 484 | backup_info.SetValue("backup_type", ENCRYPTED); |
| 485 | else if (use_compression) |
| 486 | backup_info.SetValue("backup_type", COMPRESSED); |
| 487 | else |
| 488 | backup_info.SetValue("backup_type", UNCOMPRESSED); |
| 489 | backup_info.SetValue("file_count", files_backup); |
| 490 | backup_info.SaveValues(); |
| 491 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 492 | #endif //ndef BUILD_TWRPTAR_MAIN |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 493 | if (TWFunc::Wait_For_Child(*tar_fork_pid, &status, "createTarFork()") != 0) |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 494 | return -1; |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 495 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 499 | int twrpTar::extractTarFork() { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 500 | int status = 0; |
Matt Mower | 23d8aae | 2017-01-06 14:30:33 -0600 | [diff] [blame] | 501 | pid_t tar_fork_pid; |
| 502 | int progress_pipe[2]; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 503 | |
| 504 | if (pipe(progress_pipe) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 505 | LOGINFO("Error creating progress tracking pipe\n"); |
| 506 | gui_err("restore_error=Error during restore process."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 507 | return -1; |
| 508 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 509 | |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 510 | tar_fork_pid = fork(); |
| 511 | if (tar_fork_pid >= 0) // fork was successful |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 512 | { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 513 | if (tar_fork_pid == 0) // child process |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 514 | { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 515 | close(progress_pipe[0]); |
| 516 | progress_pipe_fd = progress_pipe[1]; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 517 | if (TWFunc::Path_Exists(tarfn) || part_settings->adbbackup) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 518 | LOGINFO("Single archive\n"); |
| 519 | if (extract() != 0) |
| 520 | _exit(-1); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 521 | else { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 522 | _exit(0); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 523 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 524 | } else { |
| 525 | LOGINFO("Multiple archives\n"); |
| 526 | string temp; |
| 527 | char actual_filename[255]; |
| 528 | twrpTar tars[9]; |
| 529 | pthread_t tar_thread[9]; |
| 530 | pthread_attr_t tattr; |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 531 | unsigned thread_count = 0, i, start_thread_id = 1; |
| 532 | int ret, thread_error = 0; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 533 | void *thread_return; |
| 534 | |
| 535 | basefn = tarfn; |
| 536 | temp = basefn + "%i%02i"; |
| 537 | tarfn += "000"; |
| 538 | if (!TWFunc::Path_Exists(tarfn)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 539 | LOGINFO("Unable to locate '%s' or '%s'\n", basefn.c_str(), tarfn.c_str()); |
| 540 | gui_err("restore_error=Error during restore process."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 541 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 542 | _exit(-1); |
| 543 | } |
| 544 | if (TWFunc::Get_File_Type(tarfn) != 2) { |
| 545 | LOGINFO("First tar file '%s' not encrypted\n", tarfn.c_str()); |
| 546 | tars[0].basefn = basefn; |
| 547 | tars[0].thread_id = 0; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 548 | tars[0].progress_pipe_fd = progress_pipe_fd; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 549 | tars[0].part_settings = part_settings; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 550 | if (extractMulti((void*)&tars[0]) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 551 | LOGINFO("Error extracting split archive.\n"); |
| 552 | gui_err("restore_error=Error during restore process."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 553 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 554 | _exit(-1); |
| 555 | } |
| 556 | } else { |
| 557 | start_thread_id = 0; |
| 558 | } |
| 559 | // Start threading encrypted restores |
| 560 | if (pthread_attr_init(&tattr)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 561 | LOGINFO("Unable to pthread_attr_init\n"); |
| 562 | gui_err("restore_error=Error during restore process."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 563 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 564 | _exit(-1); |
| 565 | } |
| 566 | if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 567 | LOGINFO("Error setting pthread_attr_setdetachstate\n"); |
| 568 | gui_err("restore_error=Error during restore process."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 569 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 570 | _exit(-1); |
| 571 | } |
| 572 | if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 573 | LOGINFO("Error setting pthread_attr_setscope\n"); |
| 574 | gui_err("restore_error=Error during restore process."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 575 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 576 | _exit(-1); |
| 577 | } |
| 578 | /*if (pthread_attr_setstacksize(&tattr, 524288)) { |
| 579 | LOGERR("Error setting pthread_attr_setstacksize\n"); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 580 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 581 | _exit(-1); |
| 582 | }*/ |
| 583 | for (i = start_thread_id; i < 9; i++) { |
| 584 | sprintf(actual_filename, temp.c_str(), i, 0); |
| 585 | if (TWFunc::Path_Exists(actual_filename)) { |
| 586 | thread_count++; |
| 587 | tars[i].basefn = basefn; |
Ethan Yonker | 924a80b | 2014-04-02 10:54:12 -0500 | [diff] [blame] | 588 | tars[i].setpassword(password); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 589 | tars[i].thread_id = i; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 590 | tars[i].progress_pipe_fd = progress_pipe_fd; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 591 | tars[i].part_settings = part_settings; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 592 | LOGINFO("Creating extract thread ID %i\n", i); |
| 593 | ret = pthread_create(&tar_thread[i], &tattr, extractMulti, (void*)&tars[i]); |
| 594 | if (ret) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 595 | LOGINFO("Unable to create %i thread for extraction! %i\nContinuing in same thread (restore will be slower).\n", i, ret); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 596 | if (extractMulti((void*)&tars[i]) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 597 | LOGINFO("Error extracting backup in thread %i.\n", i); |
| 598 | gui_err("restore_error=Error during restore process."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 599 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 600 | _exit(-1); |
| 601 | } else { |
| 602 | tars[i].thread_id = i + 1; |
| 603 | } |
| 604 | } |
| 605 | usleep(100000); // Need a short delay before starting the next thread or the threads will never finish for some reason. |
| 606 | } else { |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | for (i = start_thread_id; i < thread_count + start_thread_id; i++) { |
| 611 | if (tars[i].thread_id == i) { |
| 612 | if (pthread_join(tar_thread[i], &thread_return)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 613 | LOGINFO("Error joining thread %i\n", i); |
| 614 | gui_err("restore_error=Error during restore process."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 615 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 616 | _exit(-1); |
| 617 | } else { |
| 618 | LOGINFO("Joined thread %i.\n", i); |
that | 2252d24 | 2015-04-03 22:33:04 +0200 | [diff] [blame] | 619 | ret = (int)(intptr_t)thread_return; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 620 | if (ret != 0) { |
| 621 | thread_error = 1; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 622 | LOGINFO("Thread %i returned an error %i.\n", i, ret); |
| 623 | gui_err("restore_error=Error during restore process."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 624 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 625 | _exit(-1); |
| 626 | } |
| 627 | } |
| 628 | } else { |
| 629 | LOGINFO("Skipping joining thread %i because of pthread failure.\n", i); |
| 630 | } |
| 631 | } |
| 632 | if (thread_error) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 633 | LOGINFO("Error returned by one or more threads.\n"); |
| 634 | gui_err("restore_error=Error during restore process."); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 635 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 636 | _exit(-1); |
| 637 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 638 | LOGINFO("Finished encrypted restore.\n"); |
| 639 | close(progress_pipe_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 640 | _exit(0); |
| 641 | } |
| 642 | } |
| 643 | else // parent process |
| 644 | { |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 645 | unsigned long long fs, size_backup = 0; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 646 | |
| 647 | // Parent closes output side |
| 648 | close(progress_pipe[1]); |
| 649 | |
| 650 | // Read progress data from children |
| 651 | while (read(progress_pipe[0], &fs, sizeof(fs)) > 0) { |
| 652 | size_backup += fs; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 653 | part_settings->progress->UpdateSize(size_backup); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 654 | } |
| 655 | close(progress_pipe[0]); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 656 | part_settings->progress->UpdateDisplayDetails(true); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 657 | |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 658 | if (TWFunc::Wait_For_Child(tar_fork_pid, &status, "extractTarFork()") != 0) |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 659 | return -1; |
| 660 | } |
| 661 | } |
| 662 | else // fork has failed |
| 663 | { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 664 | close(progress_pipe[0]); |
| 665 | close(progress_pipe[1]); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 666 | LOGINFO("extract tar failed to fork.\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 667 | return -1; |
| 668 | } |
| 669 | return 0; |
| 670 | } |
| 671 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 672 | int twrpTar::Generate_TarList(string Path, std::vector<TarListStruct> *TarList, unsigned long long *Target_Size, unsigned *thread_id) { |
| 673 | DIR* d; |
| 674 | struct dirent* de; |
| 675 | struct stat st; |
| 676 | string FileName; |
| 677 | struct TarListStruct TarItem; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 678 | int ret, file_count; |
| 679 | file_count = 0; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 680 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 681 | d = opendir(Path.c_str()); |
| 682 | if (d == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 683 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Path)(strerror(errno))); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 684 | closedir(d); |
| 685 | return -1; |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 686 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 687 | while ((de = readdir(d)) != NULL) { |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 688 | FileName = Path + "/" + de->d_name; |
Matt Mower | bb81e5d | 2014-03-20 18:05:41 -0500 | [diff] [blame] | 689 | |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 690 | if (de->d_type == DT_BLK || de->d_type == DT_CHR || backup_exclusions->check_skip_dirs(FileName)) |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 691 | continue; |
| 692 | TarItem.fn = FileName; |
| 693 | TarItem.thread_id = *thread_id; |
Matt Mower | 50248ab | 2014-03-31 15:58:40 -0500 | [diff] [blame] | 694 | if (de->d_type == DT_DIR) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 695 | TarList->push_back(TarItem); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 696 | ret = Generate_TarList(FileName, TarList, Target_Size, thread_id); |
| 697 | if (ret < 0) |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 698 | return -1; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 699 | file_count += ret; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 700 | } else if (de->d_type == DT_REG || de->d_type == DT_LNK) { |
| 701 | stat(FileName.c_str(), &st); |
| 702 | TarList->push_back(TarItem); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 703 | if (de->d_type == DT_REG) { |
| 704 | file_count++; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 705 | Archive_Current_Size += st.st_size; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 706 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 707 | if (Archive_Current_Size != 0 && *Target_Size != 0 && Archive_Current_Size > *Target_Size) { |
| 708 | *thread_id = *thread_id + 1; |
| 709 | Archive_Current_Size = 0; |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 713 | closedir(d); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 714 | return file_count; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 715 | } |
| 716 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 717 | int twrpTar::extractTar() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 718 | char* charRootDir = (char*) tardir.c_str(); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 719 | if (openTar() == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 720 | return -1; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 721 | if (tar_extract_all(t, charRootDir, &progress_pipe_fd) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 722 | LOGINFO("Unable to extract tar archive '%s'\n", tarfn.c_str()); |
| 723 | gui_err("restore_error=Error during restore process."); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 724 | return -1; |
| 725 | } |
| 726 | if (tar_close(t) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 727 | LOGINFO("Unable to close tar file\n"); |
| 728 | gui_err("restore_error=Error during restore process."); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 729 | return -1; |
| 730 | } |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 731 | #ifndef BUILD_TWRPTAR_MAIN |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 732 | if (part_settings->adbbackup) { |
| 733 | if (!twadbbu::Write_TWEOF()) |
| 734 | return -1; |
| 735 | } |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 736 | #endif |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 737 | return 0; |
| 738 | } |
| 739 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 740 | int twrpTar::extract() { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 741 | if (!part_settings->adbbackup) { |
| 742 | LOGINFO("Setting archive type\n"); |
| 743 | Set_Archive_Type(TWFunc::Get_File_Type(tarfn)); |
| 744 | } |
| 745 | else { |
| 746 | if (part_settings->adb_compression == 1) |
| 747 | current_archive_type = COMPRESSED; |
| 748 | else |
| 749 | current_archive_type = UNCOMPRESSED; |
| 750 | } |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 751 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 752 | if (current_archive_type == COMPRESSED) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 753 | //if you return the extractTGZ function directly, stack crashes happen |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 754 | LOGINFO("Extracting gzipped tar\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 755 | int ret = extractTar(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 756 | return ret; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 757 | } else if (current_archive_type == ENCRYPTED) { |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 758 | int ret = TWFunc::Try_Decrypting_File(tarfn, password); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 759 | if (ret < 1) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 760 | gui_msg(Msg(msg::kError, "fail_decrypt_tar=Failed to decrypt tar file '{1}'")(tarfn)); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 761 | return -1; |
| 762 | } |
| 763 | if (ret == 1) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 764 | LOGINFO("Decrypted file is not in tar format.\n"); |
| 765 | gui_err("restore_error=Error during restore process."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 766 | return -1; |
| 767 | } |
| 768 | if (ret == 3) { |
| 769 | LOGINFO("Extracting encrypted and compressed tar.\n"); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 770 | current_archive_type = COMPRESSED_ENCRYPTED; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 771 | } else |
| 772 | LOGINFO("Extracting encrypted tar.\n"); |
| 773 | return extractTar(); |
| 774 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 775 | LOGINFO("Extracting uncompressed tar\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 776 | return extractTar(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 780 | int twrpTar::tarList(std::vector<TarListStruct> *TarList, unsigned thread_id) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 781 | struct stat st; |
| 782 | char buf[PATH_MAX]; |
| 783 | int list_size = TarList->size(), i = 0, archive_count = 0; |
| 784 | string temp; |
| 785 | char actual_filename[PATH_MAX]; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 786 | unsigned long long fs; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 787 | |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 788 | if (split_archives) { |
| 789 | basefn = tarfn; |
| 790 | temp = basefn + "%i%02i"; |
| 791 | sprintf(actual_filename, temp.c_str(), thread_id, archive_count); |
| 792 | tarfn = actual_filename; |
| 793 | include_root_dir = true; |
| 794 | } else { |
| 795 | include_root_dir = false; |
| 796 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 797 | |
| 798 | if (part_settings->adbbackup) |
| 799 | LOGINFO("Writing tar file '%s' to adb backup\n", tarfn.c_str()); |
| 800 | else |
| 801 | LOGINFO("Creating tar file '%s'\n", tarfn.c_str()); |
| 802 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 803 | if (createTar() != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 804 | LOGINFO("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id); |
| 805 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 806 | return -2; |
| 807 | } |
| 808 | Archive_Current_Size = 0; |
| 809 | |
| 810 | while (i < list_size) { |
| 811 | if (TarList->at(i).thread_id == thread_id) { |
| 812 | strcpy(buf, TarList->at(i).fn.c_str()); |
bigbiff bigbiff | ec8fc28 | 2014-03-16 20:32:50 -0400 | [diff] [blame] | 813 | lstat(buf, &st); |
| 814 | if (S_ISREG(st.st_mode)) { // item is a regular file |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 815 | fs = (unsigned long long)(st.st_size); |
| 816 | if (split_archives && Archive_Current_Size + fs > MAX_ARCHIVE_SIZE) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 817 | if (closeTar() != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 818 | LOGINFO("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id); |
| 819 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 820 | return -3; |
| 821 | } |
| 822 | archive_count++; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 823 | gui_msg(Msg("split_thread=Splitting thread ID {1} into archive {2}")(thread_id)(archive_count + 1)); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 824 | if (archive_count > 99) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 825 | LOGINFO("Too many archives for thread %i\n", thread_id); |
| 826 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 827 | return -4; |
| 828 | } |
| 829 | sprintf(actual_filename, temp.c_str(), thread_id, archive_count); |
| 830 | tarfn = actual_filename; |
| 831 | if (createTar() != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 832 | LOGINFO("Error creating tar '%s' for thread %i\n", tarfn.c_str(), thread_id); |
| 833 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 834 | return -2; |
| 835 | } |
| 836 | Archive_Current_Size = 0; |
| 837 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 838 | Archive_Current_Size += fs; |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 839 | fs = 0; // Sending a 0 size to the pipe tells it to increment the file counter |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 840 | write(progress_pipe_fd, &fs, sizeof(fs)); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 841 | } |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 842 | LOGINFO("addFile '%s' including root: %i\n", buf, include_root_dir); |
| 843 | if (addFile(buf, include_root_dir) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 844 | LOGINFO("Error adding file '%s' to '%s'\n", buf, tarfn.c_str()); |
| 845 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 846 | return -1; |
| 847 | } |
| 848 | } |
| 849 | i++; |
| 850 | } |
| 851 | if (closeTar() != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 852 | LOGINFO("Error closing '%s' on thread %i\n", tarfn.c_str(), thread_id); |
| 853 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 854 | return -3; |
| 855 | } |
Ethan Yonker | bcc502c | 2014-11-10 11:22:10 -0600 | [diff] [blame] | 856 | LOGINFO("Thread id %i tarList done, %i archives.\n", thread_id, archive_count); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 857 | return 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 858 | } |
| 859 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 860 | void* twrpTar::createList(void *cookie) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 861 | twrpTar* threadTar = (twrpTar*) cookie; |
Ethan Yonker | eae4209 | 2014-03-07 15:33:13 -0600 | [diff] [blame] | 862 | if (threadTar->tarList(threadTar->ItemList, threadTar->thread_id) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 863 | LOGINFO("ERROR tarList for thread ID %i\n", threadTar->thread_id); |
| 864 | return (void*)-2; |
| 865 | } |
| 866 | LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id); |
| 867 | return (void*)0; |
| 868 | } |
| 869 | |
| 870 | void* twrpTar::extractMulti(void *cookie) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 871 | twrpTar* threadTar = (twrpTar*) cookie; |
| 872 | int archive_count = 0; |
| 873 | string temp = threadTar->basefn + "%i%02i"; |
| 874 | char actual_filename[255]; |
| 875 | sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count); |
| 876 | while (TWFunc::Path_Exists(actual_filename)) { |
| 877 | threadTar->tarfn = actual_filename; |
| 878 | if (threadTar->extract() != 0) { |
| 879 | LOGINFO("Error extracting '%s' in thread ID %i\n", actual_filename, threadTar->thread_id); |
| 880 | return (void*)-2; |
| 881 | } |
| 882 | archive_count++; |
| 883 | if (archive_count > 99) |
| 884 | break; |
| 885 | sprintf(actual_filename, temp.c_str(), threadTar->thread_id, archive_count); |
| 886 | } |
| 887 | LOGINFO("Thread ID %i finished successfully.\n", threadTar->thread_id); |
| 888 | return (void*)0; |
| 889 | } |
| 890 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 891 | int twrpTar::addFilesToExistingTar(vector <string> files, string fn) { |
| 892 | char* charTarFile = (char*) fn.c_str(); |
| 893 | |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 894 | if (tar_open(&t, charTarFile, NULL, O_CLOEXEC | O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 895 | return -1; |
| 896 | removeEOT(charTarFile); |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 897 | if (tar_open(&t, charTarFile, NULL, O_CLOEXEC | O_WRONLY | O_APPEND | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 898 | return -1; |
| 899 | for (unsigned int i = 0; i < files.size(); ++i) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 900 | char* file = (char*) files.at(i).c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 901 | if (tar_append_file(t, file, file) == -1) |
| 902 | return -1; |
| 903 | } |
| 904 | if (tar_append_eof(t) == -1) |
| 905 | return -1; |
| 906 | if (tar_close(t) == -1) |
| 907 | return -1; |
| 908 | return 0; |
| 909 | } |
| 910 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 911 | int twrpTar::createTar() { |
| 912 | char* charTarFile = (char*) tarfn.c_str(); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 913 | char* charRootDir = (char*) tardir.c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 914 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 915 | if (use_encryption && use_compression) { |
| 916 | // Compressed and encrypted |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 917 | current_archive_type = COMPRESSED_ENCRYPTED; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 918 | LOGINFO("Using encryption and compression...\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 919 | int i, pipes[4]; |
| 920 | |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 921 | if (pipe2(pipes, O_CLOEXEC) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 922 | LOGINFO("Error creating first pipe\n"); |
| 923 | gui_err("backup_error=Error creating backup."); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 924 | return -1; |
| 925 | } |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 926 | if (pipe2(pipes + 2, O_CLOEXEC) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 927 | LOGINFO("Error creating second pipe\n"); |
| 928 | gui_err("backup_error=Error creating backup."); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 929 | return -1; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 930 | } |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 931 | output_fd = open(tarfn.c_str(), O_CLOEXEC | O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 932 | if (output_fd < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 933 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno))); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 934 | for (i = 0; i < 4; i++) |
| 935 | close(pipes[i]); // close all |
| 936 | return -1; |
| 937 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 938 | pigz_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 939 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 940 | if (pigz_pid < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 941 | LOGINFO("pigz fork() failed\n"); |
| 942 | gui_err("backup_error=Error creating backup."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 943 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 944 | for (i = 0; i < 4; i++) |
| 945 | close(pipes[i]); // close all |
| 946 | return -1; |
| 947 | } else if (pigz_pid == 0) { |
| 948 | // pigz Child |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 949 | dup2(pipes[0], STDIN_FILENO); |
| 950 | dup2(pipes[3], STDOUT_FILENO); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 951 | if (execlp("pigz", "pigz", "-", NULL) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 952 | LOGINFO("execlp pigz ERROR!\n"); |
| 953 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 954 | _exit(-1); |
| 955 | } |
| 956 | } else { |
| 957 | // Parent |
| 958 | oaes_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 959 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 960 | if (oaes_pid < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 961 | LOGINFO("openaes fork() failed\n"); |
| 962 | gui_err("backup_error=Error creating backup."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 963 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 964 | for (i = 0; i < 4; i++) |
| 965 | close(pipes[i]); // close all |
| 966 | return -1; |
| 967 | } else if (oaes_pid == 0) { |
| 968 | // openaes Child |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 969 | dup2(pipes[2], STDIN_FILENO); |
| 970 | dup2(output_fd, STDOUT_FILENO); |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 971 | if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 972 | LOGINFO("execlp openaes ERROR!\n"); |
| 973 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 974 | _exit(-1); |
| 975 | } |
| 976 | } else { |
| 977 | // Parent |
| 978 | close(pipes[0]); |
| 979 | close(pipes[2]); |
| 980 | close(pipes[3]); |
| 981 | fd = pipes[1]; |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 982 | init_libtar_no_buffer(progress_pipe_fd); |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 983 | tar_type.writefunc = write_tar_no_buffer; |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 984 | if (tar_fdopen(&t, fd, charRootDir, &tar_type, O_CLOEXEC | O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 985 | close(fd); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 986 | LOGINFO("tar_fdopen failed\n"); |
| 987 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 988 | return -1; |
| 989 | } |
| 990 | return 0; |
| 991 | } |
| 992 | } |
| 993 | } else if (use_compression) { |
| 994 | // Compressed |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 995 | current_archive_type = COMPRESSED; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 996 | LOGINFO("Using compression...\n"); |
| 997 | int pigzfd[2]; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 998 | if (part_settings->adbbackup) { |
| 999 | LOGINFO("opening TW_ADB_BACKUP compressed stream\n"); |
| 1000 | output_fd = open(TW_ADB_BACKUP, O_WRONLY); |
| 1001 | } |
| 1002 | else { |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1003 | output_fd = open(tarfn.c_str(), O_CLOEXEC | O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1004 | } |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1005 | if (output_fd < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1006 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno))); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1007 | return -1; |
| 1008 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1009 | |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1010 | if (pipe2(pigzfd, O_CLOEXEC) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1011 | LOGINFO("Error creating pipe\n"); |
| 1012 | gui_err("backup_error=Error creating backup."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1013 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1014 | return -1; |
| 1015 | } |
| 1016 | pigz_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 1017 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1018 | if (pigz_pid < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1019 | LOGINFO("fork() failed\n"); |
| 1020 | gui_err("backup_error=Error creating backup."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1021 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1022 | close(pigzfd[0]); |
| 1023 | close(pigzfd[1]); |
| 1024 | return -1; |
| 1025 | } else if (pigz_pid == 0) { |
| 1026 | // Child |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1027 | dup2(pigzfd[0], STDIN_FILENO); // remap stdin |
| 1028 | dup2(output_fd, STDOUT_FILENO); // remap stdout to output file |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1029 | if (execlp("pigz", "pigz", "-", NULL) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1030 | LOGINFO("execlp pigz ERROR!\n"); |
| 1031 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1032 | _exit(-1); |
| 1033 | } |
| 1034 | } else { |
| 1035 | // Parent |
| 1036 | close(pigzfd[0]); // close parent input |
| 1037 | fd = pigzfd[1]; // copy parent output |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 1038 | init_libtar_no_buffer(progress_pipe_fd); |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 1039 | tar_type.writefunc = write_tar_no_buffer; |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1040 | if (tar_fdopen(&t, fd, charRootDir, &tar_type, O_CLOEXEC | O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1041 | close(fd); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1042 | LOGINFO("tar_fdopen failed\n"); |
| 1043 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1044 | return -1; |
| 1045 | } |
| 1046 | } |
| 1047 | } else if (use_encryption) { |
| 1048 | // Encrypted |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1049 | current_archive_type = ENCRYPTED; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1050 | LOGINFO("Using encryption...\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1051 | int oaesfd[2]; |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1052 | output_fd = open(tarfn.c_str(), O_CLOEXEC | O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1053 | if (output_fd < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1054 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno))); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1055 | return -1; |
| 1056 | } |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1057 | if (pipe2(oaesfd, O_CLOEXEC) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1058 | LOGINFO("Error creating pipe\n"); |
| 1059 | gui_err("backup_error=Error creating backup."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1060 | close(output_fd); |
| 1061 | return -1; |
| 1062 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1063 | oaes_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 1064 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1065 | if (oaes_pid < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1066 | LOGINFO("fork() failed\n"); |
| 1067 | gui_err("backup_error=Error creating backup."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1068 | close(output_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1069 | close(oaesfd[0]); |
| 1070 | close(oaesfd[1]); |
| 1071 | return -1; |
| 1072 | } else if (oaes_pid == 0) { |
| 1073 | // Child |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1074 | dup2(oaesfd[0], STDIN_FILENO); // remap stdin |
| 1075 | dup2(output_fd, STDOUT_FILENO); // remap stdout to output file |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 1076 | if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1077 | LOGINFO("execlp openaes ERROR!\n"); |
| 1078 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1079 | _exit(-1); |
| 1080 | } |
| 1081 | } else { |
| 1082 | // Parent |
| 1083 | close(oaesfd[0]); // close parent input |
| 1084 | fd = oaesfd[1]; // copy parent output |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 1085 | init_libtar_no_buffer(progress_pipe_fd); |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 1086 | tar_type.writefunc = write_tar_no_buffer; |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1087 | if (tar_fdopen(&t, fd, charRootDir, &tar_type, O_CLOEXEC | O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1088 | close(fd); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1089 | LOGINFO("tar_fdopen failed\n"); |
| 1090 | gui_err("backup_error=Error creating backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1091 | return -1; |
| 1092 | } |
| 1093 | return 0; |
| 1094 | } |
| 1095 | } else { |
| 1096 | // Not compressed or encrypted |
nkk71 | d59d02e | 2017-06-29 15:16:23 +0300 | [diff] [blame] | 1097 | current_archive_type = UNCOMPRESSED; |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 1098 | init_libtar_buffer(0, progress_pipe_fd); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1099 | if (part_settings->adbbackup) { |
| 1100 | LOGINFO("Opening TW_ADB_BACKUP uncompressed stream\n"); |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 1101 | tar_type.writefunc = write_tar_no_buffer; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1102 | output_fd = open(TW_ADB_BACKUP, O_WRONLY); |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1103 | if(tar_fdopen(&t, output_fd, charRootDir, &tar_type, O_CLOEXEC | O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1104 | close(output_fd); |
| 1105 | LOGERR("tar_fdopen failed\n"); |
| 1106 | return -1; |
| 1107 | } |
| 1108 | } |
| 1109 | else { |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 1110 | tar_type.writefunc = write_tar; |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1111 | if (tar_open(&t, charTarFile, &tar_type, O_CLOEXEC | O_WRONLY | O_CREAT | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) == -1) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1112 | LOGERR("tar_open error opening '%s'\n", tarfn.c_str()); |
| 1113 | gui_err("backup_error=Error creating backup."); |
| 1114 | return -1; |
| 1115 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1116 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1117 | } |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1121 | int twrpTar::openTar() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 1122 | char* charRootDir = (char*) tardir.c_str(); |
| 1123 | char* charTarFile = (char*) tarfn.c_str(); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1124 | string Password; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1125 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1126 | if (current_archive_type == COMPRESSED_ENCRYPTED) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1127 | LOGINFO("Opening encrypted and compressed backup...\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1128 | int i, pipes[4]; |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1129 | input_fd = open(tarfn.c_str(), O_CLOEXEC | O_RDONLY | O_LARGEFILE); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1130 | if (input_fd < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1131 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno))); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1132 | return -1; |
| 1133 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1134 | |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1135 | if (pipe2(pipes, O_CLOEXEC) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1136 | LOGINFO("Error creating first pipe\n"); |
| 1137 | gui_err("restore_error=Error during restore process."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1138 | close(input_fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1139 | return -1; |
| 1140 | } |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1141 | if (pipe2(pipes + 2, O_CLOEXEC) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1142 | LOGINFO("Error creating second pipe\n"); |
| 1143 | gui_err("restore_error=Error during restore process."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1144 | close(pipes[0]); |
| 1145 | close(pipes[1]); |
| 1146 | close(input_fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1147 | return -1; |
| 1148 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1149 | oaes_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 1150 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1151 | if (oaes_pid < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1152 | LOGINFO("pigz fork() failed\n"); |
| 1153 | gui_err("restore_error=Error during restore process."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1154 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1155 | for (i = 0; i < 4; i++) |
| 1156 | close(pipes[i]); // close all |
| 1157 | return -1; |
| 1158 | } else if (oaes_pid == 0) { |
| 1159 | // openaes Child |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1160 | dup2(input_fd, STDIN_FILENO); |
| 1161 | dup2(pipes[1], STDOUT_FILENO); |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 1162 | if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1163 | LOGINFO("execlp openaes ERROR!\n"); |
| 1164 | gui_err("restore_error=Error during restore process."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1165 | _exit(-1); |
| 1166 | } |
| 1167 | } else { |
| 1168 | // Parent |
| 1169 | pigz_pid = fork(); |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 1170 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1171 | if (pigz_pid < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1172 | LOGINFO("openaes fork() failed\n"); |
| 1173 | gui_err("restore_error=Error during restore process."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1174 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1175 | for (i = 0; i < 4; i++) |
| 1176 | close(pipes[i]); // close all |
| 1177 | return -1; |
| 1178 | } else if (pigz_pid == 0) { |
| 1179 | // pigz Child |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1180 | dup2(pipes[0], STDIN_FILENO); |
| 1181 | dup2(pipes[3], STDOUT_FILENO); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1182 | if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1183 | LOGINFO("execlp pigz ERROR!\n"); |
| 1184 | gui_err("restore_error=Error during restore process."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1185 | _exit(-1); |
| 1186 | } |
| 1187 | } else { |
| 1188 | // Parent |
| 1189 | close(pipes[0]); // Close pipes not used by parent |
| 1190 | close(pipes[1]); |
| 1191 | close(pipes[3]); |
| 1192 | fd = pipes[2]; |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1193 | if (tar_fdopen(&t, fd, charRootDir, NULL, O_CLOEXEC | O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1194 | close(fd); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1195 | LOGINFO("tar_fdopen failed\n"); |
| 1196 | gui_err("restore_error=Error during restore process."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1197 | return -1; |
| 1198 | } |
| 1199 | } |
| 1200 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1201 | } else if (current_archive_type == ENCRYPTED) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1202 | LOGINFO("Opening encrypted backup...\n"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1203 | int oaesfd[2]; |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1204 | input_fd = open(tarfn.c_str(), O_CLOEXEC | O_RDONLY | O_LARGEFILE); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1205 | if (input_fd < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1206 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno))); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1207 | return -1; |
| 1208 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1209 | |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1210 | if (pipe2(oaesfd, O_CLOEXEC) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1211 | LOGINFO("Error creating pipe\n"); |
| 1212 | gui_err("restore_error=Error during restore process."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1213 | close(input_fd); |
| 1214 | return -1; |
| 1215 | } |
| 1216 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1217 | oaes_pid = fork(); |
| 1218 | if (oaes_pid < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1219 | LOGINFO("fork() failed\n"); |
| 1220 | gui_err("restore_error=Error during restore process."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1221 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1222 | close(oaesfd[0]); |
| 1223 | close(oaesfd[1]); |
| 1224 | return -1; |
| 1225 | } else if (oaes_pid == 0) { |
| 1226 | // Child |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1227 | dup2(oaesfd[1], STDOUT_FILENO); // remap stdout |
| 1228 | dup2(input_fd, STDIN_FILENO); // remap input fd to stdin |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 1229 | if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1230 | LOGINFO("execlp openaes ERROR!\n"); |
| 1231 | gui_err("restore_error=Error during restore process."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1232 | _exit(-1); |
| 1233 | } |
| 1234 | } else { |
| 1235 | // Parent |
| 1236 | close(oaesfd[1]); // close parent output |
| 1237 | fd = oaesfd[0]; // copy parent input |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1238 | if (tar_fdopen(&t, fd, charRootDir, NULL, O_CLOEXEC | O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1239 | close(fd); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1240 | LOGINFO("tar_fdopen failed\n"); |
| 1241 | gui_err("restore_error=Error during restore process."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1242 | return -1; |
| 1243 | } |
| 1244 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1245 | } else if (current_archive_type == COMPRESSED) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1246 | int pigzfd[2]; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1247 | |
bigbiff bigbiff | adcb4d8 | 2017-09-25 10:51:56 -0400 | [diff] [blame] | 1248 | LOGINFO("Opening gzip compressed tar...\n"); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1249 | if (part_settings->adbbackup) { |
| 1250 | LOGINFO("opening TW_ADB_RESTORE compressed stream\n"); |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1251 | input_fd = open(TW_ADB_RESTORE, O_CLOEXEC | O_RDONLY | O_LARGEFILE); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1252 | } |
| 1253 | else |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1254 | input_fd = open(tarfn.c_str(), O_CLOEXEC | O_RDONLY | O_LARGEFILE); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1255 | |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1256 | if (input_fd < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1257 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(tarfn)(strerror(errno))); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1258 | return -1; |
| 1259 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1260 | |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1261 | if (pipe2(pigzfd, O_CLOEXEC) < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1262 | LOGINFO("Error creating pipe\n"); |
| 1263 | gui_err("restore_error=Error during restore process."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1264 | close(input_fd); |
| 1265 | return -1; |
| 1266 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1267 | |
| 1268 | pigz_pid = fork(); |
| 1269 | if (pigz_pid < 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1270 | LOGINFO("fork() failed\n"); |
| 1271 | gui_err("restore_error=Error during restore process."); |
Dees Troy | 9d72327 | 2014-04-07 17:13:10 +0000 | [diff] [blame] | 1272 | close(input_fd); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1273 | close(pigzfd[0]); |
| 1274 | close(pigzfd[1]); |
| 1275 | return -1; |
| 1276 | } else if (pigz_pid == 0) { |
| 1277 | // Child |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1278 | dup2(pigzfd[1], STDOUT_FILENO); // remap stdout |
| 1279 | dup2(input_fd, STDIN_FILENO); // remap input fd to stdin |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1280 | if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) { |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1281 | LOGINFO("execlp pigz ERROR!\n"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1282 | gui_err("restore_error=Error during restore process."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1283 | _exit(-1); |
| 1284 | } |
| 1285 | } else { |
| 1286 | // Parent |
| 1287 | close(pigzfd[1]); // close parent output |
| 1288 | fd = pigzfd[0]; // copy parent input |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1289 | if (tar_fdopen(&t, fd, charRootDir, NULL, O_CLOEXEC | O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1290 | close(fd); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1291 | LOGINFO("tar_fdopen failed\n"); |
| 1292 | gui_err("restore_error=Error during restore process."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1293 | return -1; |
| 1294 | } |
| 1295 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1296 | } else { |
| 1297 | if (part_settings->adbbackup) { |
| 1298 | LOGINFO("Opening TW_ADB_RESTORE uncompressed stream\n"); |
| 1299 | input_fd = open(TW_ADB_RESTORE, O_RDONLY); |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1300 | if (tar_fdopen(&t, input_fd, charRootDir, NULL, O_CLOEXEC | O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1301 | LOGERR("Unable to open tar archive '%s'\n", charTarFile); |
| 1302 | gui_err("restore_error=Error during restore process."); |
| 1303 | return -1; |
| 1304 | } |
| 1305 | } |
| 1306 | else { |
Fabrice Bellet | 72bcc20 | 2019-12-15 17:49:18 +0100 | [diff] [blame] | 1307 | if (tar_open(&t, charTarFile, NULL, O_CLOEXEC | O_RDONLY | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TWTAR_FLAGS) != 0) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1308 | LOGERR("Unable to open tar archive '%s'\n", charTarFile); |
| 1309 | gui_err("restore_error=Error during restore process."); |
| 1310 | return -1; |
| 1311 | } |
| 1312 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1313 | } |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
| 1317 | string twrpTar::Strip_Root_Dir(string Path) { |
| 1318 | string temp; |
| 1319 | size_t slash; |
| 1320 | |
| 1321 | if (Path.substr(0, 1) == "/") |
| 1322 | temp = Path.substr(1, Path.size() - 1); |
| 1323 | else |
| 1324 | temp = Path; |
| 1325 | slash = temp.find("/"); |
| 1326 | if (slash == string::npos) |
| 1327 | return temp; |
| 1328 | else { |
| 1329 | string stripped; |
| 1330 | |
| 1331 | stripped = temp.substr(slash, temp.size() - slash); |
| 1332 | return stripped; |
| 1333 | } |
| 1334 | return temp; |
| 1335 | } |
| 1336 | |
| 1337 | int twrpTar::addFile(string fn, bool include_root) { |
| 1338 | char* charTarFile = (char*) fn.c_str(); |
| 1339 | if (include_root) { |
| 1340 | if (tar_append_file(t, charTarFile, NULL) == -1) |
| 1341 | return -1; |
| 1342 | } else { |
| 1343 | string temp = Strip_Root_Dir(fn); |
| 1344 | char* charTarPath = (char*) temp.c_str(); |
| 1345 | if (tar_append_file(t, charTarFile, charTarPath) == -1) |
| 1346 | return -1; |
| 1347 | } |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1351 | int twrpTar::closeTar() { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1352 | LOGINFO("Closing tar\n"); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 1353 | flush_libtar_buffer(t->fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1354 | if (tar_append_eof(t) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1355 | LOGINFO("tar_append_eof(): %s\n", strerror(errno)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1356 | tar_close(t); |
| 1357 | return -1; |
| 1358 | } |
| 1359 | if (tar_close(t) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1360 | LOGINFO("Unable to close tar archive: '%s'\n", tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1361 | return -1; |
| 1362 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1363 | if (current_archive_type > 0) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1364 | int status; |
| 1365 | if (pigz_pid > 0 && TWFunc::Wait_For_Child(pigz_pid, &status, "pigz") != 0) |
| 1366 | return -1; |
| 1367 | if (oaes_pid > 0 && TWFunc::Wait_For_Child(oaes_pid, &status, "openaes") != 0) |
| 1368 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1369 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1370 | free_libtar_buffer(); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1371 | if (!part_settings->adbbackup) { |
| 1372 | if (use_compression && !use_encryption) { |
| 1373 | string gzname = tarfn + ".gz"; |
| 1374 | if (TWFunc::Path_Exists(gzname)) { |
| 1375 | rename(gzname.c_str(), tarfn.c_str()); |
| 1376 | } |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 1377 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1378 | if (TWFunc::Get_File_Size(tarfn) == 0) { |
| 1379 | gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(tarfn)); |
| 1380 | return -1; |
| 1381 | } |
Ethan Yonker | 960f030 | 2014-12-21 21:54:00 -0600 | [diff] [blame] | 1382 | #ifndef BUILD_TWRPTAR_MAIN |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1383 | tw_set_default_metadata(tarfn.c_str()); |
Ethan Yonker | 960f030 | 2014-12-21 21:54:00 -0600 | [diff] [blame] | 1384 | #endif |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1385 | } |
| 1386 | else { |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 1387 | #ifndef BUILD_TWRPTAR_MAIN |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1388 | if (!twadbbu::Write_TWEOF()) |
| 1389 | return -1; |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 1390 | #endif |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1391 | } |
Ethan Yonker | ea2fcf0 | 2016-09-12 16:07:19 -0500 | [diff] [blame] | 1392 | if (input_fd >= 0) |
| 1393 | close(input_fd); |
| 1394 | if (output_fd >= 0) |
| 1395 | close(output_fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | int twrpTar::removeEOT(string tarFile) { |
| 1400 | char* charTarFile = (char*) tarFile.c_str(); |
Matt Mower | 2b18a53 | 2015-02-20 16:58:05 -0600 | [diff] [blame] | 1401 | off_t tarFileEnd = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1402 | while (th_read(t) == 0) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1403 | if (TH_ISREG(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1404 | tar_skip_regfile(t); |
| 1405 | tarFileEnd = lseek(t->fd, 0, SEEK_CUR); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1406 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1407 | if (tar_close(t) == -1) |
| 1408 | return -1; |
Matt Mower | 2b18a53 | 2015-02-20 16:58:05 -0600 | [diff] [blame] | 1409 | if (tarFileEnd > 0 && truncate(charTarFile, tarFileEnd) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1410 | return -1; |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 1414 | int twrpTar::entryExists(string entry) { |
| 1415 | char* searchstr = (char*)entry.c_str(); |
| 1416 | int ret; |
| 1417 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1418 | Set_Archive_Type(TWFunc::Get_File_Type(tarfn)); |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 1419 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1420 | if (openTar() == -1) |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 1421 | ret = 0; |
| 1422 | else |
| 1423 | ret = tar_find(t, searchstr); |
| 1424 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1425 | if (closeTar() != 0) |
| 1426 | LOGINFO("Unable to close tar after searching for entry.\n"); |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 1427 | |
| 1428 | return ret; |
| 1429 | } |
| 1430 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1431 | unsigned long long twrpTar::get_size() { |
bigbiff bigbiff | b5ecaad | 2017-03-20 18:53:53 -0400 | [diff] [blame] | 1432 | if (part_settings->adbbackup || TWFunc::Path_Exists(tarfn)) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1433 | LOGINFO("Single archive\n"); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1434 | return uncompressedSize(tarfn); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1435 | } else { |
| 1436 | LOGINFO("Multiple archives\n"); |
| 1437 | string temp; |
| 1438 | char actual_filename[255]; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1439 | int archive_count = 0; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1440 | unsigned long long total_restore_size = 0; |
| 1441 | |
| 1442 | basefn = tarfn; |
| 1443 | temp = basefn + "%i%02i"; |
| 1444 | tarfn += "000"; |
| 1445 | thread_id = 0; |
| 1446 | sprintf(actual_filename, temp.c_str(), thread_id, archive_count); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1447 | if (!part_settings->adbbackup) { |
| 1448 | if (!TWFunc::Path_Exists(actual_filename)) { |
| 1449 | LOGERR("Unable to locate '%s' or '%s'\n", basefn.c_str(), tarfn.c_str()); |
| 1450 | return 0; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1451 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1452 | for (int i = 0; i < 9; i++) { |
| 1453 | archive_count = 0; |
| 1454 | sprintf(actual_filename, temp.c_str(), i, archive_count); |
| 1455 | while (TWFunc::Path_Exists(actual_filename)) { |
| 1456 | total_restore_size += uncompressedSize(actual_filename); |
| 1457 | archive_count++; |
| 1458 | if (archive_count > 99) |
| 1459 | break; |
| 1460 | sprintf(actual_filename, temp.c_str(), i, archive_count); |
| 1461 | } |
| 1462 | } |
| 1463 | #ifndef BUILD_TWRPTAR_MAIN |
| 1464 | if (!part_settings->adbbackup) { |
| 1465 | InfoManager backup_info(tarfn + ".info"); |
| 1466 | backup_info.SetValue("backup_size", total_restore_size); |
| 1467 | backup_info.SetValue("backup_type", current_archive_type); |
| 1468 | backup_info.SaveValues(); |
| 1469 | } |
| 1470 | #endif //ndef BUILD_TWRPTAR_MAIN |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1471 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1472 | return total_restore_size; |
| 1473 | } |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1477 | unsigned long long twrpTar::uncompressedSize(string filename) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1478 | unsigned long long total_size = 0; |
| 1479 | string Tar, Command, result; |
| 1480 | vector<string> split; |
| 1481 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1482 | Set_Archive_Type(TWFunc::Get_File_Type(tarfn)); |
| 1483 | if (current_archive_type == UNCOMPRESSED) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1484 | total_size = TWFunc::Get_File_Size(filename); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1485 | } else if (current_archive_type == COMPRESSED) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1486 | // Compressed |
| 1487 | Command = "pigz -l '" + filename + "'"; |
| 1488 | /* if we set Command = "pigz -l " + tarfn + " | sed '1d' | cut -f5 -d' '"; |
| 1489 | we get the uncompressed size at once. */ |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 1490 | TWFunc::Exec_Cmd(Command, result, false); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1491 | if (!result.empty()) { |
| 1492 | /* Expected output: |
| 1493 | compressed original reduced name |
| 1494 | 95855838 179403776 -1.3% data.yaffs2.win |
| 1495 | ^ |
| 1496 | split[5] |
| 1497 | */ |
| 1498 | split = TWFunc::split_string(result, ' ', true); |
| 1499 | if (split.size() > 4) |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1500 | total_size = atoi(split[5].c_str()); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1501 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1502 | } else if (current_archive_type == COMPRESSED_ENCRYPTED) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1503 | // File is encrypted and may be compressed |
| 1504 | int ret = TWFunc::Try_Decrypting_File(filename, password); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1505 | if (ret < 1) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1506 | gui_msg(Msg(msg::kError, "fail_decrypt_tar=Failed to decrypt tar file '{1}'")(tarfn)); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1507 | total_size = TWFunc::Get_File_Size(filename); |
| 1508 | } else if (ret == 1) { |
| 1509 | LOGERR("Decrypted file is not in tar format.\n"); |
| 1510 | total_size = TWFunc::Get_File_Size(filename); |
| 1511 | } else if (ret == 3) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1512 | Command = "openaes dec --key \"" + password + "\" --in '" + filename + "' | pigz -l"; |
| 1513 | /* if we set Command = "pigz -l " + tarfn + " | sed '1d' | cut -f5 -d' '"; |
| 1514 | we get the uncompressed size at once. */ |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 1515 | TWFunc::Exec_Cmd(Command, result, false); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1516 | if (!result.empty()) { |
| 1517 | LOGINFO("result was: '%s'\n", result.c_str()); |
| 1518 | /* Expected output: |
| 1519 | compressed original reduced name |
| 1520 | 95855838 179403776 -1.3% data.yaffs2.win |
| 1521 | ^ |
| 1522 | split[5] |
| 1523 | */ |
| 1524 | split = TWFunc::split_string(result, ' ', true); |
| 1525 | if (split.size() > 4) |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1526 | total_size = atoi(split[5].c_str()); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1527 | } |
| 1528 | } else { |
| 1529 | total_size = TWFunc::Get_File_Size(filename); |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | return total_size; |
| 1534 | } |
| 1535 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 1536 | extern "C" ssize_t write_tar(int fd, const void *buffer, size_t size) { |
| 1537 | return (ssize_t) write_libtar_buffer(fd, buffer, size); |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame] | 1538 | } |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 1539 | |
| 1540 | extern "C" ssize_t write_tar_no_buffer(int fd, const void *buffer, size_t size) { |
| 1541 | return (ssize_t) write_libtar_no_buffer(fd, buffer, size); |
| 1542 | } |