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