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