bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 2 | Copyright 2012 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 4 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 9 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 14 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | extern "C" { |
| 20 | #include "libtar/libtar.h" |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 21 | #include "twrpTar.h" |
| 22 | #include "tarWrite.h" |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame] | 23 | #include "libcrecovery/common.h" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 24 | } |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 27 | #include <sys/wait.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <fstream> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 32 | #include <iostream> |
| 33 | #include <string> |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 34 | #include <sstream> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 35 | #include <dirent.h> |
| 36 | #include <sys/mman.h> |
| 37 | #include "twrpTar.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 38 | #include "twcommon.h" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 39 | #include "data.hpp" |
| 40 | #include "variables.h" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 41 | #include "twrp-functions.hpp" |
| 42 | |
| 43 | using namespace std; |
| 44 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 45 | void twrpTar::setfn(string fn) { |
| 46 | tarfn = fn; |
| 47 | } |
| 48 | |
| 49 | void twrpTar::setdir(string dir) { |
| 50 | tardir = dir; |
| 51 | } |
| 52 | |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 53 | int twrpTar::createTarGZFork() { |
| 54 | int status; |
| 55 | pid_t pid; |
| 56 | if ((pid = fork()) == -1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 57 | LOGINFO("create tar failed to fork.\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 58 | return -1; |
| 59 | } |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 60 | if (pid == 0) { |
| 61 | if (createTGZ() != 0) |
| 62 | exit(-1); |
| 63 | else |
| 64 | exit(0); |
| 65 | } |
| 66 | else { |
| 67 | if ((pid = wait(&status)) == -1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 68 | LOGINFO("Tar creation failed\n"); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | else { |
| 72 | if (WIFSIGNALED(status) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 73 | LOGINFO("Child process ended with signal: %d\n", WTERMSIG(status)); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | else if (WIFEXITED(status) != 0) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 77 | LOGINFO("Tar creation successful\n"); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 78 | else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 79 | LOGINFO("Tar creation failed\n"); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 80 | return -1; |
| 81 | } |
| 82 | } |
| 83 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 87 | int twrpTar::createTarFork() { |
| 88 | int status; |
| 89 | pid_t pid; |
| 90 | if ((pid = fork()) == -1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 91 | LOGINFO("create tar failed to fork.\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 92 | return -1; |
| 93 | } |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 94 | if (pid == 0) { |
| 95 | if (create() != 0) |
| 96 | exit(-1); |
| 97 | else |
| 98 | exit(0); |
| 99 | } |
| 100 | else { |
| 101 | if ((pid = wait(&status)) == -1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 102 | LOGINFO("Tar creation failed\n"); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 103 | return -1; |
| 104 | } |
| 105 | else { |
| 106 | if (WIFSIGNALED(status) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 107 | LOGINFO("Child process ended with signal: %d\n", WTERMSIG(status)); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 108 | return -1; |
| 109 | } |
Dees_Troy | 50d63be | 2013-03-02 08:49:30 -0600 | [diff] [blame] | 110 | else if (WEXITSTATUS(status) == 0) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 111 | LOGINFO("Tar creation successful\n"); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 112 | else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 113 | LOGINFO("Tar creation failed\n"); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 114 | return -1; |
| 115 | } |
| 116 | } |
| 117 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 121 | int twrpTar::extractTarFork() { |
| 122 | int status; |
| 123 | pid_t pid; |
| 124 | if ((pid = fork()) == -1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 125 | LOGINFO("extract tar failed to fork.\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 126 | return -1; |
| 127 | } |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 128 | if (pid == 0) { |
| 129 | if (extract() != 0) |
| 130 | exit(-1); |
| 131 | else |
| 132 | exit(0); |
| 133 | } |
| 134 | else { |
| 135 | if ((pid = wait(&status)) == -1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 136 | LOGINFO("Tar extraction failed\n"); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 137 | return -1; |
| 138 | } |
| 139 | else { |
| 140 | if (WIFSIGNALED(status) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 141 | LOGINFO("Child process ended with signal: %d\n", WTERMSIG(status)); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 142 | return -1; |
| 143 | } |
Dees_Troy | 50d63be | 2013-03-02 08:49:30 -0600 | [diff] [blame] | 144 | else if (WEXITSTATUS(status) == 0) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 145 | LOGINFO("Tar extraction successful\n"); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 146 | else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 147 | LOGINFO("Tar extraction failed\n"); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 148 | return -1; |
| 149 | } |
| 150 | } |
| 151 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 155 | int twrpTar::splitArchiveFork() { |
| 156 | int status; |
| 157 | pid_t pid; |
| 158 | if ((pid = fork()) == -1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 159 | LOGINFO("create tar failed to fork.\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 160 | return -1; |
| 161 | } |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 162 | if (pid == 0) { |
| 163 | if (Split_Archive() != 0) |
| 164 | exit(-1); |
| 165 | else |
| 166 | exit(0); |
| 167 | } |
| 168 | else { |
| 169 | if ((pid = wait(&status)) == -1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 170 | LOGINFO("Tar creation failed\n"); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 171 | return -1; |
| 172 | } |
| 173 | else { |
| 174 | if (WIFSIGNALED(status) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 175 | LOGINFO("Child process ended with signal: %d\n", WTERMSIG(status)); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 176 | return -1; |
| 177 | } |
| 178 | else if (WIFEXITED(status) != 0) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 179 | LOGINFO("Tar creation successful\n"); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 180 | else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 181 | LOGINFO("Tar creation failed\n"); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 182 | return -1; |
| 183 | } |
| 184 | } |
| 185 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | int twrpTar::Generate_Multiple_Archives(string Path) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 190 | DIR* d; |
| 191 | struct dirent* de; |
| 192 | struct stat st; |
| 193 | string FileName; |
| 194 | char actual_filename[255]; |
| 195 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 196 | if (has_data_media == 1 && Path.size() >= 11 && strncmp(Path.c_str(), "/data/media", 11) == 0) |
| 197 | return 0; // Skip /data/media |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 198 | LOGINFO("Path: '%s', archive filename: '%s'\n", Path.c_str(), tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 199 | |
| 200 | d = opendir(Path.c_str()); |
| 201 | if (d == NULL) |
| 202 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 203 | LOGERR("error opening '%s' -- error: %s\n", Path.c_str(), strerror(errno)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 204 | closedir(d); |
| 205 | return -1; |
| 206 | } |
| 207 | while ((de = readdir(d)) != NULL) |
| 208 | { |
| 209 | FileName = Path + "/"; |
| 210 | FileName += de->d_name; |
| 211 | if (has_data_media == 1 && FileName.size() >= 11 && strncmp(FileName.c_str(), "/data/media", 11) == 0) |
| 212 | continue; // Skip /data/media |
bigbiff bigbiff | 71e5aa4 | 2013-02-26 20:10:16 -0500 | [diff] [blame] | 213 | if (de->d_type == DT_BLK || de->d_type == DT_CHR) |
| 214 | continue; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 215 | if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) |
| 216 | { |
| 217 | unsigned long long folder_size = TWFunc::Get_Folder_Size(FileName, false); |
| 218 | if (Archive_Current_Size + folder_size > MAX_ARCHIVE_SIZE) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 219 | LOGINFO("Calling Generate_Multiple_Archives\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 220 | if (Generate_Multiple_Archives(FileName) < 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 221 | return -1; |
| 222 | } else { |
| 223 | //FileName += "/"; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 224 | LOGINFO("Adding folder '%s'\n", FileName.c_str()); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 225 | tardir = FileName; |
| 226 | if (tarDirs(true) < 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 227 | return -1; |
| 228 | Archive_Current_Size += folder_size; |
| 229 | } |
| 230 | } |
| 231 | else if (de->d_type == DT_REG || de->d_type == DT_LNK) |
| 232 | { |
| 233 | stat(FileName.c_str(), &st); |
| 234 | |
| 235 | if (Archive_Current_Size != 0 && Archive_Current_Size + st.st_size > MAX_ARCHIVE_SIZE) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 236 | LOGINFO("Closing tar '%s', ", tarfn.c_str()); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 237 | closeTar(false); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 238 | reinit_libtar_buffer(); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 239 | if (TWFunc::Get_File_Size(tarfn) == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 240 | LOGERR("Backup file size for '%s' is 0 bytes.\n", tarfn.c_str()); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 241 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 242 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 243 | Archive_File_Count++; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 244 | if (Archive_File_Count > 999) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 245 | LOGERR("Archive count is too large!\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 246 | return -1; |
| 247 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 248 | string temp = basefn + "%03i"; |
| 249 | sprintf(actual_filename, temp.c_str(), Archive_File_Count); |
| 250 | tarfn = actual_filename; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 251 | Archive_Current_Size = 0; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 252 | LOGINFO("Creating tar '%s'\n", tarfn.c_str()); |
| 253 | gui_print("Creating archive %i...\n", Archive_File_Count + 1); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 254 | if (createTar() != 0) |
| 255 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 256 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 257 | LOGINFO("Adding file: '%s'... ", FileName.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 258 | if (addFile(FileName, true) < 0) |
| 259 | return -1; |
| 260 | Archive_Current_Size += st.st_size; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 261 | LOGINFO("added successfully, archive size: %llu\n", Archive_Current_Size); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 262 | if (st.st_size > 2147483648LL) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 263 | LOGERR("There is a file that is larger than 2GB in the file system\n'%s'\nThis file may not restore properly\n", FileName.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | closedir(d); |
| 267 | return 0; |
| 268 | } |
| 269 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 270 | int twrpTar::Split_Archive() |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 271 | { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 272 | string temp = tarfn + "%03i"; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 273 | char actual_filename[255]; |
| 274 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 275 | basefn = tarfn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 276 | Archive_File_Count = 0; |
| 277 | Archive_Current_Size = 0; |
| 278 | sprintf(actual_filename, temp.c_str(), Archive_File_Count); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 279 | tarfn = actual_filename; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 280 | init_libtar_buffer(0); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 281 | createTar(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 282 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 283 | gui_print("Creating archive 1...\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 284 | if (Generate_Multiple_Archives(tardir) < 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 285 | LOGERR("Error generating multiple archives\n"); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 286 | free_libtar_buffer(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 287 | return -1; |
| 288 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 289 | closeTar(false); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 290 | free_libtar_buffer(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 291 | LOGINFO("Done, created %i archives.\n", (Archive_File_Count++)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 292 | return (Archive_File_Count); |
| 293 | } |
| 294 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 295 | int twrpTar::extractTar() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 296 | char* charRootDir = (char*) tardir.c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 297 | bool gzip = false; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 298 | if (openTar(gzip) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 299 | return -1; |
| 300 | if (tar_extract_all(t, charRootDir) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 301 | LOGERR("Unable to extract tar archive '%s'\n", tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 302 | return -1; |
| 303 | } |
| 304 | if (tar_close(t) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 305 | LOGERR("Unable to close tar file\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 306 | return -1; |
| 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 311 | int twrpTar::getArchiveType() { |
| 312 | int type = 0; |
| 313 | string::size_type i = 0; |
| 314 | int firstbyte = 0, secondbyte = 0; |
| 315 | char header[3]; |
| 316 | |
| 317 | ifstream f; |
| 318 | f.open(tarfn.c_str(), ios::in | ios::binary); |
| 319 | f.get(header, 3); |
| 320 | f.close(); |
| 321 | firstbyte = header[i] & 0xff; |
| 322 | secondbyte = header[++i] & 0xff; |
| 323 | |
| 324 | if (firstbyte == 0x1f && secondbyte == 0x8b) |
| 325 | type = 1; // Compressed |
| 326 | else |
| 327 | type = 0; // Uncompressed |
| 328 | |
| 329 | return type; |
| 330 | } |
| 331 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 332 | int twrpTar::extract() { |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 333 | int Archive_Current_Type = getArchiveType(); |
| 334 | |
| 335 | if (Archive_Current_Type == 1) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 336 | //if you return the extractTGZ function directly, stack crashes happen |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 337 | LOGINFO("Extracting gzipped tar\n"); |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 338 | int ret = extractTGZ(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 339 | return ret; |
| 340 | } |
| 341 | else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 342 | LOGINFO("Extracting uncompressed tar\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 343 | return extractTar(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 347 | int twrpTar::tarDirs(bool include_root) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 348 | DIR* d; |
| 349 | string mainfolder = tardir + "/", subfolder; |
Dees_Troy | 3263e92 | 2013-03-15 11:42:57 -0500 | [diff] [blame] | 350 | char buf[PATH_MAX]; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 351 | d = opendir(tardir.c_str()); |
| 352 | if (d != NULL) { |
| 353 | struct dirent* de; |
| 354 | while ((de = readdir(d)) != NULL) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 355 | #ifdef RECOVERY_SDCARD_ON_DATA |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 356 | if ((tardir == "/data" || tardir == "/data/") && strcmp(de->d_name, "media") == 0) continue; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 357 | #endif |
Dees_Troy | 3263e92 | 2013-03-15 11:42:57 -0500 | [diff] [blame] | 358 | if (de->d_type == DT_BLK || de->d_type == DT_CHR || strcmp(de->d_name, "..") == 0) |
| 359 | continue; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 360 | subfolder = mainfolder; |
Dees_Troy | 3263e92 | 2013-03-15 11:42:57 -0500 | [diff] [blame] | 361 | if (strcmp(de->d_name, ".") != 0) { |
| 362 | subfolder += de->d_name; |
| 363 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 364 | LOGINFO("adding '%s'\n", subfolder.c_str()); |
Dees_Troy | 3263e92 | 2013-03-15 11:42:57 -0500 | [diff] [blame] | 365 | if (addFile(subfolder, include_root) != 0) |
| 366 | return -1; |
| 367 | continue; |
| 368 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 369 | LOGINFO("adding '%s'\n", subfolder.c_str()); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 370 | strcpy(buf, subfolder.c_str()); |
| 371 | if (de->d_type == DT_DIR) { |
Dees_Troy | 3263e92 | 2013-03-15 11:42:57 -0500 | [diff] [blame] | 372 | char* charTarPath; |
| 373 | if (include_root) { |
| 374 | charTarPath = NULL; |
| 375 | } else { |
| 376 | string temp = Strip_Root_Dir(buf); |
| 377 | charTarPath = (char*) temp.c_str(); |
| 378 | } |
| 379 | if (tar_append_tree(t, buf, charTarPath) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 380 | LOGERR("Error appending '%s' to tar archive '%s'\n", buf, tarfn.c_str()); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 381 | return -1; |
| 382 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 383 | } else if (tardir != "/" && (de->d_type == DT_REG || de->d_type == DT_LNK)) { |
Dees_Troy | 3263e92 | 2013-03-15 11:42:57 -0500 | [diff] [blame] | 384 | if (addFile(buf, include_root) != 0) |
| 385 | return -1; |
| 386 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 387 | fflush(NULL); |
| 388 | } |
| 389 | closedir(d); |
| 390 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 394 | int twrpTar::createTGZ() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 395 | bool gzip = true; |
| 396 | |
| 397 | init_libtar_buffer(0); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 398 | if (createTar() == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 399 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 400 | if (tarDirs(false) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 401 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 402 | if (closeTar(gzip) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 403 | return -1; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 404 | free_libtar_buffer(); |
| 405 | return 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 406 | } |
| 407 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 408 | int twrpTar::create() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 409 | bool gzip = false; |
| 410 | |
| 411 | init_libtar_buffer(0); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 412 | if (createTar() == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 413 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 414 | if (tarDirs(false) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 415 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 416 | if (closeTar(gzip) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 417 | return -1; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 418 | free_libtar_buffer(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | int twrpTar::addFilesToExistingTar(vector <string> files, string fn) { |
| 423 | char* charTarFile = (char*) fn.c_str(); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 424 | static tartype_t type = { open, close, read, write_tar }; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 425 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 426 | init_libtar_buffer(0); |
| 427 | if (tar_open(&t, charTarFile, &type, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 428 | return -1; |
| 429 | removeEOT(charTarFile); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 430 | if (tar_open(&t, charTarFile, &type, O_WRONLY | O_APPEND | O_LARGEFILE, 0644, TAR_GNU) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 431 | return -1; |
| 432 | for (unsigned int i = 0; i < files.size(); ++i) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 433 | char* file = (char*) files.at(i).c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 434 | if (tar_append_file(t, file, file) == -1) |
| 435 | return -1; |
| 436 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 437 | flush_libtar_buffer(t->fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 438 | if (tar_append_eof(t) == -1) |
| 439 | return -1; |
| 440 | if (tar_close(t) == -1) |
| 441 | return -1; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 442 | free_libtar_buffer(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 446 | int twrpTar::createTar() { |
| 447 | char* charTarFile = (char*) tarfn.c_str(); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 448 | char* charRootDir = (char*) tardir.c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 449 | int use_compression = 0; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 450 | static tartype_t type = { open, close, read, write_tar }; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 451 | |
| 452 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 453 | if (use_compression) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 454 | string cmd = "pigz - > '" + tarfn + "'"; |
bigbiff bigbiff | 23aa819 | 2013-02-22 10:43:59 -0500 | [diff] [blame] | 455 | p = popen(cmd.c_str(), "w"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 456 | fd = fileno(p); |
| 457 | if (!p) return -1; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 458 | if(tar_fdopen(&t, fd, charRootDir, &type, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 459 | pclose(p); |
| 460 | return -1; |
| 461 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 462 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 463 | else { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 464 | if (tar_open(&t, charTarFile, &type, O_WRONLY | O_CREAT | O_LARGEFILE, 0644, TAR_GNU) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 465 | return -1; |
| 466 | } |
| 467 | return 0; |
| 468 | } |
| 469 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 470 | int twrpTar::openTar(bool gzip) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 471 | char* charRootDir = (char*) tardir.c_str(); |
| 472 | char* charTarFile = (char*) tarfn.c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 473 | |
| 474 | if (gzip) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 475 | LOGINFO("Opening as a gzip\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 476 | string cmd = "pigz -d -c '" + tarfn + "'"; |
bigbiff bigbiff | 23aa819 | 2013-02-22 10:43:59 -0500 | [diff] [blame] | 477 | FILE* pipe = popen(cmd.c_str(), "r"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 478 | int fd = fileno(pipe); |
| 479 | if (!pipe) return -1; |
| 480 | if(tar_fdopen(&t, fd, charRootDir, NULL, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 481 | LOGINFO("tar_fdopen returned error\n"); |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame] | 482 | __pclose(pipe); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 483 | return -1; |
| 484 | } |
| 485 | } |
| 486 | else { |
| 487 | if (tar_open(&t, charTarFile, NULL, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 488 | LOGERR("Unable to open tar archive '%s'\n", charTarFile); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 489 | return -1; |
| 490 | } |
| 491 | } |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | string twrpTar::Strip_Root_Dir(string Path) { |
| 496 | string temp; |
| 497 | size_t slash; |
| 498 | |
| 499 | if (Path.substr(0, 1) == "/") |
| 500 | temp = Path.substr(1, Path.size() - 1); |
| 501 | else |
| 502 | temp = Path; |
| 503 | slash = temp.find("/"); |
| 504 | if (slash == string::npos) |
| 505 | return temp; |
| 506 | else { |
| 507 | string stripped; |
| 508 | |
| 509 | stripped = temp.substr(slash, temp.size() - slash); |
| 510 | return stripped; |
| 511 | } |
| 512 | return temp; |
| 513 | } |
| 514 | |
| 515 | int twrpTar::addFile(string fn, bool include_root) { |
| 516 | char* charTarFile = (char*) fn.c_str(); |
| 517 | if (include_root) { |
| 518 | if (tar_append_file(t, charTarFile, NULL) == -1) |
| 519 | return -1; |
| 520 | } else { |
| 521 | string temp = Strip_Root_Dir(fn); |
| 522 | char* charTarPath = (char*) temp.c_str(); |
| 523 | if (tar_append_file(t, charTarFile, charTarPath) == -1) |
| 524 | return -1; |
| 525 | } |
| 526 | return 0; |
| 527 | } |
| 528 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 529 | int twrpTar::closeTar(bool gzip) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 530 | int use_compression; |
| 531 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); |
| 532 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 533 | flush_libtar_buffer(t->fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 534 | if (tar_append_eof(t) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 535 | LOGERR("tar_append_eof(): %s\n", strerror(errno)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 536 | tar_close(t); |
| 537 | return -1; |
| 538 | } |
| 539 | if (tar_close(t) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 540 | LOGERR("Unable to close tar archive: '%s'\n", tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 541 | return -1; |
| 542 | } |
| 543 | if (use_compression || gzip) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 544 | LOGINFO("Closing popen and fd\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 545 | pclose(p); |
| 546 | close(fd); |
| 547 | } |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | int twrpTar::removeEOT(string tarFile) { |
| 552 | char* charTarFile = (char*) tarFile.c_str(); |
| 553 | off_t tarFileEnd; |
| 554 | while (th_read(t) == 0) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 555 | if (TH_ISREG(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 556 | tar_skip_regfile(t); |
| 557 | tarFileEnd = lseek(t->fd, 0, SEEK_CUR); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 558 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 559 | if (tar_close(t) == -1) |
| 560 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 561 | if (truncate(charTarFile, tarFileEnd) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 562 | return -1; |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | int twrpTar::compress(string fn) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 567 | string cmd = "pigz " + fn; |
bigbiff bigbiff | 23aa819 | 2013-02-22 10:43:59 -0500 | [diff] [blame] | 568 | p = popen(cmd.c_str(), "r"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 569 | if (!p) return -1; |
| 570 | char buffer[128]; |
| 571 | string result = ""; |
| 572 | while(!feof(p)) { |
| 573 | if(fgets(buffer, 128, p) != NULL) |
| 574 | result += buffer; |
| 575 | } |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame] | 576 | __pclose(p); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 580 | int twrpTar::extractTGZ() { |
| 581 | string splatrootdir(tardir); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 582 | bool gzip = true; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 583 | char* splatCharRootDir = (char*) splatrootdir.c_str(); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 584 | if (openTar(gzip) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 585 | return -1; |
| 586 | int ret = tar_extract_all(t, splatCharRootDir); |
| 587 | if (tar_close(t) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 588 | LOGERR("Unable to close tar file\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 589 | return -1; |
| 590 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 591 | return 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 592 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 593 | |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 594 | int twrpTar::entryExists(string entry) { |
| 595 | char* searchstr = (char*)entry.c_str(); |
| 596 | int ret; |
| 597 | |
| 598 | int Archive_Current_Type = getArchiveType(); |
| 599 | |
| 600 | if (openTar(Archive_Current_Type) == -1) |
| 601 | ret = 0; |
| 602 | else |
| 603 | ret = tar_find(t, searchstr); |
| 604 | |
| 605 | if (tar_close(t) != 0) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 606 | LOGINFO("Unable to close tar file after searching for entry '%s'.\n", entry.c_str()); |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 607 | |
| 608 | return ret; |
| 609 | } |
| 610 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 611 | extern "C" ssize_t write_tar(int fd, const void *buffer, size_t size) { |
| 612 | return (ssize_t) write_libtar_buffer(fd, buffer, size); |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame] | 613 | } |