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" |
| 38 | #include "common.h" |
| 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) { |
| 57 | LOGI("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) { |
| 68 | LOGI("Tar creation failed\n"); |
| 69 | return -1; |
| 70 | } |
| 71 | else { |
| 72 | if (WIFSIGNALED(status) != 0) { |
| 73 | LOGI("Child process ended with signal: %d\n", WTERMSIG(status)); |
| 74 | return -1; |
| 75 | } |
| 76 | else if (WIFEXITED(status) != 0) |
| 77 | LOGI("Tar creation successful\n"); |
| 78 | else { |
| 79 | LOGI("Tar creation failed\n"); |
| 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) { |
| 91 | LOGI("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) { |
| 102 | LOGI("Tar creation failed\n"); |
| 103 | return -1; |
| 104 | } |
| 105 | else { |
| 106 | if (WIFSIGNALED(status) != 0) { |
| 107 | LOGI("Child process ended with signal: %d\n", WTERMSIG(status)); |
| 108 | return -1; |
| 109 | } |
Dees_Troy | 50d63be | 2013-03-02 08:49:30 -0600 | [diff] [blame] | 110 | else if (WEXITSTATUS(status) == 0) |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 111 | LOGI("Tar creation successful\n"); |
| 112 | else { |
| 113 | LOGI("Tar creation failed\n"); |
| 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 | ee6632c | 2013-02-27 18:07:32 +0000 | [diff] [blame] | 125 | LOGI("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) { |
bigbiff bigbiff | 71e5aa4 | 2013-02-26 20:10:16 -0500 | [diff] [blame] | 136 | LOGI("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) { |
| 141 | LOGI("Child process ended with signal: %d\n", WTERMSIG(status)); |
| 142 | return -1; |
| 143 | } |
Dees_Troy | 50d63be | 2013-03-02 08:49:30 -0600 | [diff] [blame] | 144 | else if (WEXITSTATUS(status) == 0) |
bigbiff bigbiff | 71e5aa4 | 2013-02-26 20:10:16 -0500 | [diff] [blame] | 145 | LOGI("Tar extraction successful\n"); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 146 | else { |
Dees_Troy | ee6632c | 2013-02-27 18:07:32 +0000 | [diff] [blame] | 147 | LOGI("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) { |
| 159 | LOGI("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) { |
| 170 | LOGI("Tar creation failed\n"); |
| 171 | return -1; |
| 172 | } |
| 173 | else { |
| 174 | if (WIFSIGNALED(status) != 0) { |
| 175 | LOGI("Child process ended with signal: %d\n", WTERMSIG(status)); |
| 176 | return -1; |
| 177 | } |
| 178 | else if (WIFEXITED(status) != 0) |
| 179 | LOGI("Tar creation successful\n"); |
| 180 | else { |
| 181 | LOGI("Tar creation failed\n"); |
| 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 |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 198 | LOGI("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 | { |
| 203 | LOGE("error opening '%s' -- error: %s\n", Path.c_str(), strerror(errno)); |
| 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); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 218 | tardir = FileName; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 219 | if (Archive_Current_Size + folder_size > MAX_ARCHIVE_SIZE) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 220 | LOGI("Calling Generate_Multiple_Archives\n"); |
| 221 | if (Generate_Multiple_Archives(FileName) < 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 222 | return -1; |
| 223 | } else { |
| 224 | //FileName += "/"; |
| 225 | LOGI("Adding folder '%s'\n", FileName.c_str()); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 226 | tardir = FileName; |
| 227 | if (tarDirs(true) < 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 228 | return -1; |
| 229 | Archive_Current_Size += folder_size; |
| 230 | } |
| 231 | } |
| 232 | else if (de->d_type == DT_REG || de->d_type == DT_LNK) |
| 233 | { |
| 234 | stat(FileName.c_str(), &st); |
| 235 | |
| 236 | if (Archive_Current_Size != 0 && Archive_Current_Size + st.st_size > MAX_ARCHIVE_SIZE) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 237 | LOGI("Closing tar '%s', ", tarfn.c_str()); |
| 238 | closeTar(false); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 239 | reinit_libtar_buffer(); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 240 | if (TWFunc::Get_File_Size(tarfn) == 0) { |
| 241 | LOGE("Backup file size for '%s' is 0 bytes.\n", tarfn.c_str()); |
| 242 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 243 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 244 | Archive_File_Count++; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 245 | if (Archive_File_Count > 999) { |
| 246 | LOGE("Archive count is too large!\n"); |
| 247 | return -1; |
| 248 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 249 | string temp = basefn + "%03i"; |
| 250 | sprintf(actual_filename, temp.c_str(), Archive_File_Count); |
| 251 | tarfn = actual_filename; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 252 | Archive_Current_Size = 0; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 253 | LOGI("Creating tar '%s'\n", tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 254 | ui_print("Creating archive %i...\n", Archive_File_Count + 1); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 255 | if (createTar() != 0) |
| 256 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 257 | } |
| 258 | LOGI("Adding file: '%s'... ", FileName.c_str()); |
| 259 | if (addFile(FileName, true) < 0) |
| 260 | return -1; |
| 261 | Archive_Current_Size += st.st_size; |
| 262 | LOGI("added successfully, archive size: %llu\n", Archive_Current_Size); |
| 263 | if (st.st_size > 2147483648LL) |
| 264 | LOGE("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()); |
| 265 | } |
| 266 | } |
| 267 | closedir(d); |
| 268 | return 0; |
| 269 | } |
| 270 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 271 | int twrpTar::Split_Archive() |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 272 | { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 273 | string temp = tarfn + "%03i"; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 274 | char actual_filename[255]; |
| 275 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 276 | basefn = tarfn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 277 | Archive_File_Count = 0; |
| 278 | Archive_Current_Size = 0; |
| 279 | sprintf(actual_filename, temp.c_str(), Archive_File_Count); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 280 | tarfn = actual_filename; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 281 | init_libtar_buffer(0); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 282 | createTar(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 283 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media); |
| 284 | ui_print("Creating archive 1...\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 285 | if (Generate_Multiple_Archives(tardir) < 0) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 286 | LOGE("Error generating multiple archives\n"); |
| 287 | free_libtar_buffer(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 288 | return -1; |
| 289 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 290 | closeTar(false); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 291 | free_libtar_buffer(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 292 | LOGI("Done, created %i archives.\n", (Archive_File_Count++)); |
| 293 | return (Archive_File_Count); |
| 294 | } |
| 295 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 296 | int twrpTar::extractTar() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 297 | char* charRootDir = (char*) tardir.c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 298 | bool gzip = false; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 299 | if (openTar(gzip) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 300 | return -1; |
| 301 | if (tar_extract_all(t, charRootDir) != 0) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 302 | LOGE("Unable to extract tar archive '%s'\n", tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 303 | return -1; |
| 304 | } |
| 305 | if (tar_close(t) != 0) { |
| 306 | LOGE("Unable to close tar file\n"); |
| 307 | return -1; |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 312 | int twrpTar::extract() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 313 | int len = 3; |
| 314 | char header[len]; |
| 315 | string::size_type i = 0; |
| 316 | int firstbyte = 0; |
| 317 | int secondbyte = 0; |
| 318 | int ret; |
| 319 | ifstream f; |
| 320 | f.open(tarfn.c_str(), ios::in | ios::binary); |
| 321 | f.get(header, len); |
| 322 | firstbyte = header[i] & 0xff; |
| 323 | secondbyte = header[++i] & 0xff; |
| 324 | f.close(); |
| 325 | if (firstbyte == 0x1f && secondbyte == 0x8b) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 326 | //if you return the extractTGZ function directly, stack crashes happen |
| 327 | LOGI("Extracting gzipped tar\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 328 | ret = extractTGZ(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 329 | return ret; |
| 330 | } |
| 331 | else { |
| 332 | LOGI("Extracting uncompressed tar\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 333 | return extractTar(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 337 | int twrpTar::tarDirs(bool include_root) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 338 | DIR* d; |
| 339 | string mainfolder = tardir + "/", subfolder; |
| 340 | char buf[1024]; |
| 341 | char* charTarFile = (char*) tarfn.c_str(); |
| 342 | d = opendir(tardir.c_str()); |
| 343 | if (d != NULL) { |
| 344 | struct dirent* de; |
| 345 | while ((de = readdir(d)) != NULL) { |
| 346 | LOGI("adding %s\n", de->d_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 347 | #ifdef RECOVERY_SDCARD_ON_DATA |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 348 | if ((tardir == "/data" || tardir == "/data/") && strcmp(de->d_name, "media") == 0) continue; |
bigbiff bigbiff | 71e5aa4 | 2013-02-26 20:10:16 -0500 | [diff] [blame] | 349 | if (de->d_type == DT_BLK || de->d_type == DT_CHR) |
| 350 | continue; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 351 | #endif |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 352 | if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 353 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 354 | subfolder = mainfolder; |
| 355 | subfolder += de->d_name; |
| 356 | strcpy(buf, subfolder.c_str()); |
| 357 | if (de->d_type == DT_DIR) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 358 | if (include_root) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 359 | if (tar_append_tree(t, buf, NULL) != 0) { |
| 360 | LOGE("Error appending '%s' to tar archive '%s'\n", buf, charTarFile); |
| 361 | return -1; |
| 362 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 363 | } else { |
| 364 | string temp = Strip_Root_Dir(buf); |
| 365 | char* charTarPath = (char*) temp.c_str(); |
| 366 | if (tar_append_tree(t, buf, charTarPath) != 0) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 367 | LOGE("Error appending '%s' to tar archive '%s'\n", buf, charTarFile); |
| 368 | return -1; |
| 369 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 370 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 371 | } else if (tardir != "/" && (de->d_type == DT_REG || de->d_type == DT_LNK)) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 372 | if (addFile(buf, include_root) != 0) |
| 373 | return -1; |
| 374 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 375 | fflush(NULL); |
| 376 | } |
| 377 | closedir(d); |
| 378 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 382 | int twrpTar::createTGZ() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 383 | bool gzip = true; |
| 384 | |
| 385 | init_libtar_buffer(0); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 386 | if (createTar() == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 387 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 388 | if (tarDirs(false) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 389 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 390 | if (closeTar(gzip) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 391 | return -1; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 392 | free_libtar_buffer(); |
| 393 | return 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 394 | } |
| 395 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 396 | int twrpTar::create() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 397 | bool gzip = false; |
| 398 | |
| 399 | init_libtar_buffer(0); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 400 | if (createTar() == -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 (tarDirs(false) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 403 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 404 | if (closeTar(gzip) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 405 | return -1; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 406 | free_libtar_buffer(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | int twrpTar::addFilesToExistingTar(vector <string> files, string fn) { |
| 411 | char* charTarFile = (char*) fn.c_str(); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 412 | static tartype_t type = { open, close, read, write_tar }; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 413 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 414 | init_libtar_buffer(0); |
| 415 | 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] | 416 | return -1; |
| 417 | removeEOT(charTarFile); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 418 | 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] | 419 | return -1; |
| 420 | for (unsigned int i = 0; i < files.size(); ++i) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 421 | char* file = (char*) files.at(i).c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 422 | if (tar_append_file(t, file, file) == -1) |
| 423 | return -1; |
| 424 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 425 | flush_libtar_buffer(t->fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 426 | if (tar_append_eof(t) == -1) |
| 427 | return -1; |
| 428 | if (tar_close(t) == -1) |
| 429 | return -1; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 430 | free_libtar_buffer(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 434 | int twrpTar::createTar() { |
| 435 | char* charTarFile = (char*) tarfn.c_str(); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 436 | char* charRootDir = (char*) tardir.c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 437 | int use_compression = 0; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 438 | static tartype_t type = { open, close, read, write_tar }; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 439 | |
| 440 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 441 | if (use_compression) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 442 | string cmd = "pigz - > '" + tarfn + "'"; |
bigbiff bigbiff | 23aa819 | 2013-02-22 10:43:59 -0500 | [diff] [blame] | 443 | p = popen(cmd.c_str(), "w"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 444 | fd = fileno(p); |
| 445 | if (!p) return -1; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 446 | 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] | 447 | pclose(p); |
| 448 | return -1; |
| 449 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 450 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 451 | else { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 452 | 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] | 453 | return -1; |
| 454 | } |
| 455 | return 0; |
| 456 | } |
| 457 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 458 | int twrpTar::openTar(bool gzip) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 459 | char* charRootDir = (char*) tardir.c_str(); |
| 460 | char* charTarFile = (char*) tarfn.c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 461 | |
| 462 | if (gzip) { |
| 463 | LOGI("Opening as a gzip\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 464 | string cmd = "pigz -d -c '" + tarfn + "'"; |
bigbiff bigbiff | 23aa819 | 2013-02-22 10:43:59 -0500 | [diff] [blame] | 465 | FILE* pipe = popen(cmd.c_str(), "r"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 466 | int fd = fileno(pipe); |
| 467 | if (!pipe) return -1; |
| 468 | if(tar_fdopen(&t, fd, charRootDir, NULL, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) { |
| 469 | LOGI("tar_fdopen returned error\n"); |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame] | 470 | __pclose(pipe); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 471 | return -1; |
| 472 | } |
| 473 | } |
| 474 | else { |
| 475 | if (tar_open(&t, charTarFile, NULL, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) { |
| 476 | LOGE("Unable to open tar archive '%s'\n", charTarFile); |
| 477 | return -1; |
| 478 | } |
| 479 | } |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | string twrpTar::Strip_Root_Dir(string Path) { |
| 484 | string temp; |
| 485 | size_t slash; |
| 486 | |
| 487 | if (Path.substr(0, 1) == "/") |
| 488 | temp = Path.substr(1, Path.size() - 1); |
| 489 | else |
| 490 | temp = Path; |
| 491 | slash = temp.find("/"); |
| 492 | if (slash == string::npos) |
| 493 | return temp; |
| 494 | else { |
| 495 | string stripped; |
| 496 | |
| 497 | stripped = temp.substr(slash, temp.size() - slash); |
| 498 | return stripped; |
| 499 | } |
| 500 | return temp; |
| 501 | } |
| 502 | |
| 503 | int twrpTar::addFile(string fn, bool include_root) { |
| 504 | char* charTarFile = (char*) fn.c_str(); |
| 505 | if (include_root) { |
| 506 | if (tar_append_file(t, charTarFile, NULL) == -1) |
| 507 | return -1; |
| 508 | } else { |
| 509 | string temp = Strip_Root_Dir(fn); |
| 510 | char* charTarPath = (char*) temp.c_str(); |
| 511 | if (tar_append_file(t, charTarFile, charTarPath) == -1) |
| 512 | return -1; |
| 513 | } |
| 514 | return 0; |
| 515 | } |
| 516 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 517 | int twrpTar::closeTar(bool gzip) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 518 | int use_compression; |
| 519 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); |
| 520 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 521 | flush_libtar_buffer(t->fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 522 | if (tar_append_eof(t) != 0) { |
| 523 | LOGE("tar_append_eof(): %s\n", strerror(errno)); |
| 524 | tar_close(t); |
| 525 | return -1; |
| 526 | } |
| 527 | if (tar_close(t) != 0) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 528 | LOGE("Unable to close tar archive: '%s'\n", tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 529 | return -1; |
| 530 | } |
| 531 | if (use_compression || gzip) { |
| 532 | LOGI("Closing popen and fd\n"); |
| 533 | pclose(p); |
| 534 | close(fd); |
| 535 | } |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | int twrpTar::removeEOT(string tarFile) { |
| 540 | char* charTarFile = (char*) tarFile.c_str(); |
| 541 | off_t tarFileEnd; |
| 542 | while (th_read(t) == 0) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 543 | if (TH_ISREG(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 544 | tar_skip_regfile(t); |
| 545 | tarFileEnd = lseek(t->fd, 0, SEEK_CUR); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 546 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 547 | if (tar_close(t) == -1) |
| 548 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 549 | if (truncate(charTarFile, tarFileEnd) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 550 | return -1; |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | int twrpTar::compress(string fn) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 555 | string cmd = "pigz " + fn; |
bigbiff bigbiff | 23aa819 | 2013-02-22 10:43:59 -0500 | [diff] [blame] | 556 | p = popen(cmd.c_str(), "r"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 557 | if (!p) return -1; |
| 558 | char buffer[128]; |
| 559 | string result = ""; |
| 560 | while(!feof(p)) { |
| 561 | if(fgets(buffer, 128, p) != NULL) |
| 562 | result += buffer; |
| 563 | } |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame] | 564 | __pclose(p); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 568 | int twrpTar::extractTGZ() { |
| 569 | string splatrootdir(tardir); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 570 | bool gzip = true; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 571 | char* splatCharRootDir = (char*) splatrootdir.c_str(); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 572 | if (openTar(gzip) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 573 | return -1; |
| 574 | int ret = tar_extract_all(t, splatCharRootDir); |
| 575 | if (tar_close(t) != 0) { |
| 576 | LOGE("Unable to close tar file\n"); |
| 577 | return -1; |
| 578 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 579 | return 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 580 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 581 | |
| 582 | extern "C" ssize_t write_tar(int fd, const void *buffer, size_t size) { |
| 583 | return (ssize_t) write_libtar_buffer(fd, buffer, size); |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame] | 584 | } |