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