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> |
| 27 | #include <string.h> |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <fstream> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 31 | #include <iostream> |
| 32 | #include <string> |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 33 | #include <sstream> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 34 | #include <dirent.h> |
| 35 | #include <sys/mman.h> |
| 36 | #include "twrpTar.hpp" |
| 37 | #include "common.h" |
| 38 | #include "data.hpp" |
| 39 | #include "variables.h" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 40 | #include "twrp-functions.hpp" |
| 41 | |
| 42 | using namespace std; |
| 43 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 44 | void twrpTar::setfn(string fn) { |
| 45 | tarfn = fn; |
| 46 | } |
| 47 | |
| 48 | void twrpTar::setdir(string dir) { |
| 49 | tardir = dir; |
| 50 | } |
| 51 | |
| 52 | int twrpTar::createTarGZThread() { |
| 53 | pthread_t thread; |
| 54 | ThreadPtr tarptr = &twrpTar::createTGZ; |
| 55 | PThreadPtr p = *(PThreadPtr*)&tarptr; |
| 56 | pthread_create(&thread, NULL, p, this); |
| 57 | if(pthread_join(thread, NULL)) { |
| 58 | return -1; |
| 59 | } |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int twrpTar::createTarThread() { |
| 64 | pthread_t thread; |
| 65 | ThreadPtr tarptr = &twrpTar::create; |
| 66 | PThreadPtr p = *(PThreadPtr*)&tarptr; |
| 67 | pthread_create(&thread, NULL, p, this); |
| 68 | if(pthread_join(thread, NULL)) { |
| 69 | return -1; |
| 70 | } |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | int twrpTar::extractTarThread() { |
| 75 | pthread_t thread; |
| 76 | ThreadPtr tarptr = &twrpTar::extract; |
| 77 | PThreadPtr p = *(PThreadPtr*)&tarptr; |
| 78 | pthread_create(&thread, NULL, p, this); |
| 79 | if(pthread_join(thread, NULL)) { |
| 80 | return -1; |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | int twrpTar::splitArchiveThread() { |
| 86 | pthread_t thread; |
| 87 | ThreadPtr tarptr = &twrpTar::Split_Archive; |
| 88 | PThreadPtr p = *(PThreadPtr*)&tarptr; |
| 89 | pthread_create(&thread, NULL, p, this); |
| 90 | if(pthread_join(thread, NULL)) { |
| 91 | return -1; |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | int twrpTar::Generate_Multiple_Archives(string Path) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 97 | DIR* d; |
| 98 | struct dirent* de; |
| 99 | struct stat st; |
| 100 | string FileName; |
| 101 | char actual_filename[255]; |
| 102 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 103 | if (has_data_media == 1 && Path.size() >= 11 && strncmp(Path.c_str(), "/data/media", 11) == 0) |
| 104 | return 0; // Skip /data/media |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 105 | 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] | 106 | |
| 107 | d = opendir(Path.c_str()); |
| 108 | if (d == NULL) |
| 109 | { |
| 110 | LOGE("error opening '%s' -- error: %s\n", Path.c_str(), strerror(errno)); |
| 111 | closedir(d); |
| 112 | return -1; |
| 113 | } |
| 114 | while ((de = readdir(d)) != NULL) |
| 115 | { |
| 116 | FileName = Path + "/"; |
| 117 | FileName += de->d_name; |
| 118 | if (has_data_media == 1 && FileName.size() >= 11 && strncmp(FileName.c_str(), "/data/media", 11) == 0) |
| 119 | continue; // Skip /data/media |
| 120 | if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) |
| 121 | { |
| 122 | unsigned long long folder_size = TWFunc::Get_Folder_Size(FileName, false); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 123 | tardir = FileName; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 124 | if (Archive_Current_Size + folder_size > MAX_ARCHIVE_SIZE) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 125 | LOGI("Calling Generate_Multiple_Archives\n"); |
| 126 | if (Generate_Multiple_Archives(FileName) < 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 127 | return -1; |
| 128 | } else { |
| 129 | //FileName += "/"; |
| 130 | LOGI("Adding folder '%s'\n", FileName.c_str()); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 131 | tardir = FileName; |
| 132 | if (tarDirs(true) < 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 133 | return -1; |
| 134 | Archive_Current_Size += folder_size; |
| 135 | } |
| 136 | } |
| 137 | else if (de->d_type == DT_REG || de->d_type == DT_LNK) |
| 138 | { |
| 139 | stat(FileName.c_str(), &st); |
| 140 | |
| 141 | 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] | 142 | LOGI("Closing tar '%s', ", tarfn.c_str()); |
| 143 | closeTar(false); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 144 | reinit_libtar_buffer(); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 145 | if (TWFunc::Get_File_Size(tarfn) == 0) { |
| 146 | LOGE("Backup file size for '%s' is 0 bytes.\n", tarfn.c_str()); |
| 147 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 148 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 149 | Archive_File_Count++; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 150 | if (Archive_File_Count > 999) { |
| 151 | LOGE("Archive count is too large!\n"); |
| 152 | return -1; |
| 153 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 154 | string temp = basefn + "%03i"; |
| 155 | sprintf(actual_filename, temp.c_str(), Archive_File_Count); |
| 156 | tarfn = actual_filename; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 157 | Archive_Current_Size = 0; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 158 | LOGI("Creating tar '%s'\n", tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 159 | ui_print("Creating archive %i...\n", Archive_File_Count + 1); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 160 | if (createTar() != 0) |
| 161 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 162 | } |
| 163 | LOGI("Adding file: '%s'... ", FileName.c_str()); |
| 164 | if (addFile(FileName, true) < 0) |
| 165 | return -1; |
| 166 | Archive_Current_Size += st.st_size; |
| 167 | LOGI("added successfully, archive size: %llu\n", Archive_Current_Size); |
| 168 | if (st.st_size > 2147483648LL) |
| 169 | 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()); |
| 170 | } |
| 171 | } |
| 172 | closedir(d); |
| 173 | return 0; |
| 174 | } |
| 175 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 176 | int twrpTar::Split_Archive() |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 177 | { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 178 | string temp = tarfn + "%03i"; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 179 | char actual_filename[255]; |
| 180 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 181 | basefn = tarfn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 182 | Archive_File_Count = 0; |
| 183 | Archive_Current_Size = 0; |
| 184 | sprintf(actual_filename, temp.c_str(), Archive_File_Count); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 185 | tarfn = actual_filename; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 186 | init_libtar_buffer(0); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 187 | createTar(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 188 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media); |
| 189 | ui_print("Creating archive 1...\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 190 | if (Generate_Multiple_Archives(tardir) < 0) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 191 | LOGE("Error generating multiple archives\n"); |
| 192 | free_libtar_buffer(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 193 | return -1; |
| 194 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 195 | closeTar(false); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 196 | free_libtar_buffer(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 197 | LOGI("Done, created %i archives.\n", (Archive_File_Count++)); |
| 198 | return (Archive_File_Count); |
| 199 | } |
| 200 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 201 | int twrpTar::extractTar() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 202 | char* charRootDir = (char*) tardir.c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 203 | bool gzip = false; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 204 | if (openTar(gzip) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 205 | return -1; |
| 206 | if (tar_extract_all(t, charRootDir) != 0) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 207 | LOGE("Unable to extract tar archive '%s'\n", tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 208 | return -1; |
| 209 | } |
| 210 | if (tar_close(t) != 0) { |
| 211 | LOGE("Unable to close tar file\n"); |
| 212 | return -1; |
| 213 | } |
| 214 | return 0; |
| 215 | } |
| 216 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 217 | int twrpTar::extract() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 218 | int len = 3; |
| 219 | char header[len]; |
| 220 | string::size_type i = 0; |
| 221 | int firstbyte = 0; |
| 222 | int secondbyte = 0; |
| 223 | int ret; |
| 224 | ifstream f; |
| 225 | f.open(tarfn.c_str(), ios::in | ios::binary); |
| 226 | f.get(header, len); |
| 227 | firstbyte = header[i] & 0xff; |
| 228 | secondbyte = header[++i] & 0xff; |
| 229 | f.close(); |
| 230 | if (firstbyte == 0x1f && secondbyte == 0x8b) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 231 | //if you return the extractTGZ function directly, stack crashes happen |
| 232 | LOGI("Extracting gzipped tar\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 233 | ret = extractTGZ(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 234 | return ret; |
| 235 | } |
| 236 | else { |
| 237 | LOGI("Extracting uncompressed tar\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 238 | return extractTar(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 242 | int twrpTar::tarDirs(bool include_root) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 243 | DIR* d; |
| 244 | string mainfolder = tardir + "/", subfolder; |
| 245 | char buf[1024]; |
| 246 | char* charTarFile = (char*) tarfn.c_str(); |
| 247 | d = opendir(tardir.c_str()); |
| 248 | if (d != NULL) { |
| 249 | struct dirent* de; |
| 250 | while ((de = readdir(d)) != NULL) { |
| 251 | LOGI("adding %s\n", de->d_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 252 | #ifdef RECOVERY_SDCARD_ON_DATA |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 253 | if ((tardir == "/data" || tardir == "/data/") && strcmp(de->d_name, "media") == 0) continue; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 254 | #endif |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 255 | if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 256 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 257 | subfolder = mainfolder; |
| 258 | subfolder += de->d_name; |
| 259 | strcpy(buf, subfolder.c_str()); |
| 260 | if (de->d_type == DT_DIR) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 261 | if (include_root) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 262 | if (tar_append_tree(t, buf, NULL) != 0) { |
| 263 | LOGE("Error appending '%s' to tar archive '%s'\n", buf, charTarFile); |
| 264 | return -1; |
| 265 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 266 | } else { |
| 267 | string temp = Strip_Root_Dir(buf); |
| 268 | char* charTarPath = (char*) temp.c_str(); |
| 269 | if (tar_append_tree(t, buf, charTarPath) != 0) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 270 | LOGE("Error appending '%s' to tar archive '%s'\n", buf, charTarFile); |
| 271 | return -1; |
| 272 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 273 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 274 | } 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] | 275 | if (addFile(buf, include_root) != 0) |
| 276 | return -1; |
| 277 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 278 | fflush(NULL); |
| 279 | } |
| 280 | closedir(d); |
| 281 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 285 | int twrpTar::createTGZ() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 286 | bool gzip = true; |
| 287 | |
| 288 | init_libtar_buffer(0); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 289 | if (createTar() == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 290 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 291 | if (tarDirs(false) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 292 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 293 | if (closeTar(gzip) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 294 | return -1; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 295 | free_libtar_buffer(); |
| 296 | return 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 297 | } |
| 298 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 299 | int twrpTar::create() { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 300 | bool gzip = false; |
| 301 | |
| 302 | init_libtar_buffer(0); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 303 | if (createTar() == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 304 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 305 | if (tarDirs(false) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 306 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 307 | if (closeTar(gzip) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 308 | return -1; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 309 | free_libtar_buffer(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | int twrpTar::addFilesToExistingTar(vector <string> files, string fn) { |
| 314 | char* charTarFile = (char*) fn.c_str(); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 315 | static tartype_t type = { open, close, read, write_tar }; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 316 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 317 | init_libtar_buffer(0); |
| 318 | 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] | 319 | return -1; |
| 320 | removeEOT(charTarFile); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 321 | 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] | 322 | return -1; |
| 323 | for (unsigned int i = 0; i < files.size(); ++i) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 324 | char* file = (char*) files.at(i).c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 325 | if (tar_append_file(t, file, file) == -1) |
| 326 | return -1; |
| 327 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 328 | flush_libtar_buffer(t->fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 329 | if (tar_append_eof(t) == -1) |
| 330 | return -1; |
| 331 | if (tar_close(t) == -1) |
| 332 | return -1; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 333 | free_libtar_buffer(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 337 | int twrpTar::createTar() { |
| 338 | char* charTarFile = (char*) tarfn.c_str(); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 339 | char* charRootDir = (char*) tardir.c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 340 | int use_compression = 0; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 341 | static tartype_t type = { open, close, read, write_tar }; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 342 | |
| 343 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 344 | if (use_compression) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 345 | string cmd = "pigz - > '" + tarfn + "'"; |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame^] | 346 | p = __popen(cmd.c_str(), "w"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 347 | fd = fileno(p); |
| 348 | if (!p) return -1; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 349 | 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] | 350 | pclose(p); |
| 351 | return -1; |
| 352 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 353 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 354 | else { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 355 | 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] | 356 | return -1; |
| 357 | } |
| 358 | return 0; |
| 359 | } |
| 360 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 361 | int twrpTar::openTar(bool gzip) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 362 | char* charRootDir = (char*) tardir.c_str(); |
| 363 | char* charTarFile = (char*) tarfn.c_str(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 364 | |
| 365 | if (gzip) { |
| 366 | LOGI("Opening as a gzip\n"); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 367 | string cmd = "pigz -d -c '" + tarfn + "'"; |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame^] | 368 | FILE* pipe = __popen(cmd.c_str(), "r"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 369 | int fd = fileno(pipe); |
| 370 | if (!pipe) return -1; |
| 371 | if(tar_fdopen(&t, fd, charRootDir, NULL, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) { |
| 372 | LOGI("tar_fdopen returned error\n"); |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame^] | 373 | __pclose(pipe); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 374 | return -1; |
| 375 | } |
| 376 | } |
| 377 | else { |
| 378 | if (tar_open(&t, charTarFile, NULL, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) { |
| 379 | LOGE("Unable to open tar archive '%s'\n", charTarFile); |
| 380 | return -1; |
| 381 | } |
| 382 | } |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | string twrpTar::Strip_Root_Dir(string Path) { |
| 387 | string temp; |
| 388 | size_t slash; |
| 389 | |
| 390 | if (Path.substr(0, 1) == "/") |
| 391 | temp = Path.substr(1, Path.size() - 1); |
| 392 | else |
| 393 | temp = Path; |
| 394 | slash = temp.find("/"); |
| 395 | if (slash == string::npos) |
| 396 | return temp; |
| 397 | else { |
| 398 | string stripped; |
| 399 | |
| 400 | stripped = temp.substr(slash, temp.size() - slash); |
| 401 | return stripped; |
| 402 | } |
| 403 | return temp; |
| 404 | } |
| 405 | |
| 406 | int twrpTar::addFile(string fn, bool include_root) { |
| 407 | char* charTarFile = (char*) fn.c_str(); |
| 408 | if (include_root) { |
| 409 | if (tar_append_file(t, charTarFile, NULL) == -1) |
| 410 | return -1; |
| 411 | } else { |
| 412 | string temp = Strip_Root_Dir(fn); |
| 413 | char* charTarPath = (char*) temp.c_str(); |
| 414 | if (tar_append_file(t, charTarFile, charTarPath) == -1) |
| 415 | return -1; |
| 416 | } |
| 417 | return 0; |
| 418 | } |
| 419 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 420 | int twrpTar::closeTar(bool gzip) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 421 | int use_compression; |
| 422 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); |
| 423 | |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 424 | flush_libtar_buffer(t->fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 425 | if (tar_append_eof(t) != 0) { |
| 426 | LOGE("tar_append_eof(): %s\n", strerror(errno)); |
| 427 | tar_close(t); |
| 428 | return -1; |
| 429 | } |
| 430 | if (tar_close(t) != 0) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 431 | LOGE("Unable to close tar archive: '%s'\n", tarfn.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 432 | return -1; |
| 433 | } |
| 434 | if (use_compression || gzip) { |
| 435 | LOGI("Closing popen and fd\n"); |
| 436 | pclose(p); |
| 437 | close(fd); |
| 438 | } |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | int twrpTar::removeEOT(string tarFile) { |
| 443 | char* charTarFile = (char*) tarFile.c_str(); |
| 444 | off_t tarFileEnd; |
| 445 | while (th_read(t) == 0) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 446 | if (TH_ISREG(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 447 | tar_skip_regfile(t); |
| 448 | tarFileEnd = lseek(t->fd, 0, SEEK_CUR); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 449 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 450 | if (tar_close(t) == -1) |
| 451 | return -1; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 452 | if (truncate(charTarFile, tarFileEnd) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 453 | return -1; |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | int twrpTar::compress(string fn) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 458 | string cmd = "pigz " + fn; |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame^] | 459 | p = __popen(cmd.c_str(), "r"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 460 | if (!p) return -1; |
| 461 | char buffer[128]; |
| 462 | string result = ""; |
| 463 | while(!feof(p)) { |
| 464 | if(fgets(buffer, 128, p) != NULL) |
| 465 | result += buffer; |
| 466 | } |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame^] | 467 | __pclose(p); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 471 | int twrpTar::extractTGZ() { |
| 472 | string splatrootdir(tardir); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 473 | bool gzip = true; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 474 | char* splatCharRootDir = (char*) splatrootdir.c_str(); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 475 | if (openTar(gzip) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 476 | return -1; |
| 477 | int ret = tar_extract_all(t, splatCharRootDir); |
| 478 | if (tar_close(t) != 0) { |
| 479 | LOGE("Unable to close tar file\n"); |
| 480 | return -1; |
| 481 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 482 | return 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 483 | } |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 484 | |
| 485 | extern "C" ssize_t write_tar(int fd, const void *buffer, size_t size) { |
| 486 | return (ssize_t) write_libtar_buffer(fd, buffer, size); |
Dees_Troy | 40bbcf8 | 2013-02-12 15:01:53 +0000 | [diff] [blame^] | 487 | } |