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