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