blob: dd6d5c37b1b041a2a1e85694a19959a2fdeb874d [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
Dees_Troye34c1332013-02-06 19:13:00 +00002 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
bigbiff bigbiff9c754052013-01-09 09:09:08 -05004
Dees_Troye34c1332013-02-06 19:13:00 +00005 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 bigbiff9c754052013-01-09 09:09:08 -05009
Dees_Troye34c1332013-02-06 19:13:00 +000010 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 bigbiff9c754052013-01-09 09:09:08 -050014
Dees_Troye34c1332013-02-06 19:13:00 +000015 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 bigbiff9c754052013-01-09 09:09:08 -050017*/
18
19extern "C" {
20 #include "libtar/libtar.h"
Dees_Troye34c1332013-02-06 19:13:00 +000021 #include "twrpTar.h"
22 #include "tarWrite.h"
Dees_Troy40bbcf82013-02-12 15:01:53 +000023 #include "libcrecovery/common.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050024}
25#include <sys/types.h>
26#include <sys/stat.h>
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -050027#include <sys/wait.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050028#include <string.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <fstream>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050032#include <iostream>
33#include <string>
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -050034#include <sstream>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050035#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 bigbiff9c754052013-01-09 09:09:08 -050041#include "twrp-functions.hpp"
42
43using namespace std;
44
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -050045void twrpTar::setfn(string fn) {
46 tarfn = fn;
47}
48
49void twrpTar::setdir(string dir) {
50 tardir = dir;
51}
52
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -050053int twrpTar::createTarGZFork() {
54 int status;
55 pid_t pid;
56 if ((pid = fork()) == -1) {
57 LOGI("create tar failed to fork.\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -050058 return -1;
59 }
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -050060 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 bigbiff3bf2b0e2013-01-21 21:26:43 -050084 return 0;
85}
86
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -050087int twrpTar::createTarFork() {
88 int status;
89 pid_t pid;
90 if ((pid = fork()) == -1) {
91 LOGI("create tar failed to fork.\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -050092 return -1;
93 }
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -050094 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_Troy50d63be2013-03-02 08:49:30 -0600110 else if (WEXITSTATUS(status) == 0)
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500111 LOGI("Tar creation successful\n");
112 else {
113 LOGI("Tar creation failed\n");
114 return -1;
115 }
116 }
117 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500118 return 0;
119}
120
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500121int twrpTar::extractTarFork() {
122 int status;
123 pid_t pid;
124 if ((pid = fork()) == -1) {
Dees_Troyee6632c2013-02-27 18:07:32 +0000125 LOGI("extract tar failed to fork.\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500126 return -1;
127 }
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500128 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 bigbiff71e5aa42013-02-26 20:10:16 -0500136 LOGI("Tar extraction failed\n");
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500137 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_Troy50d63be2013-03-02 08:49:30 -0600144 else if (WEXITSTATUS(status) == 0)
bigbiff bigbiff71e5aa42013-02-26 20:10:16 -0500145 LOGI("Tar extraction successful\n");
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500146 else {
Dees_Troyee6632c2013-02-27 18:07:32 +0000147 LOGI("Tar extraction failed\n");
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500148 return -1;
149 }
150 }
151 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500152 return 0;
153}
154
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500155int twrpTar::splitArchiveFork() {
156 int status;
157 pid_t pid;
158 if ((pid = fork()) == -1) {
159 LOGI("create tar failed to fork.\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500160 return -1;
161 }
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -0500162 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 bigbiff3bf2b0e2013-01-21 21:26:43 -0500186 return 0;
187}
188
189int twrpTar::Generate_Multiple_Archives(string Path) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500190 DIR* d;
191 struct dirent* de;
192 struct stat st;
193 string FileName;
194 char actual_filename[255];
195
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500196 if (has_data_media == 1 && Path.size() >= 11 && strncmp(Path.c_str(), "/data/media", 11) == 0)
197 return 0; // Skip /data/media
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500198 LOGI("Path: '%s', archive filename: '%s'\n", Path.c_str(), tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500199
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 bigbiff71e5aa42013-02-26 20:10:16 -0500213 if (de->d_type == DT_BLK || de->d_type == DT_CHR)
214 continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500215 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) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500219 LOGI("Calling Generate_Multiple_Archives\n");
220 if (Generate_Multiple_Archives(FileName) < 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500221 return -1;
222 } else {
223 //FileName += "/";
224 LOGI("Adding folder '%s'\n", FileName.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500225 tardir = FileName;
226 if (tarDirs(true) < 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500227 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) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500236 LOGI("Closing tar '%s', ", tarfn.c_str());
237 closeTar(false);
Dees_Troye34c1332013-02-06 19:13:00 +0000238 reinit_libtar_buffer();
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500239 if (TWFunc::Get_File_Size(tarfn) == 0) {
240 LOGE("Backup file size for '%s' is 0 bytes.\n", tarfn.c_str());
241 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500242 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500243 Archive_File_Count++;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500244 if (Archive_File_Count > 999) {
245 LOGE("Archive count is too large!\n");
246 return -1;
247 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500248 string temp = basefn + "%03i";
249 sprintf(actual_filename, temp.c_str(), Archive_File_Count);
250 tarfn = actual_filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500251 Archive_Current_Size = 0;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500252 LOGI("Creating tar '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500253 ui_print("Creating archive %i...\n", Archive_File_Count + 1);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500254 if (createTar() != 0)
255 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500256 }
257 LOGI("Adding file: '%s'... ", FileName.c_str());
258 if (addFile(FileName, true) < 0)
259 return -1;
260 Archive_Current_Size += st.st_size;
261 LOGI("added successfully, archive size: %llu\n", Archive_Current_Size);
262 if (st.st_size > 2147483648LL)
263 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());
264 }
265 }
266 closedir(d);
267 return 0;
268}
269
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500270int twrpTar::Split_Archive()
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500271{
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500272 string temp = tarfn + "%03i";
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500273 char actual_filename[255];
274
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500275 basefn = tarfn;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500276 Archive_File_Count = 0;
277 Archive_Current_Size = 0;
278 sprintf(actual_filename, temp.c_str(), Archive_File_Count);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500279 tarfn = actual_filename;
Dees_Troye34c1332013-02-06 19:13:00 +0000280 init_libtar_buffer(0);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500281 createTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500282 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
283 ui_print("Creating archive 1...\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500284 if (Generate_Multiple_Archives(tardir) < 0) {
Dees_Troye34c1332013-02-06 19:13:00 +0000285 LOGE("Error generating multiple archives\n");
286 free_libtar_buffer();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500287 return -1;
288 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500289 closeTar(false);
Dees_Troye34c1332013-02-06 19:13:00 +0000290 free_libtar_buffer();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500291 LOGI("Done, created %i archives.\n", (Archive_File_Count++));
292 return (Archive_File_Count);
293}
294
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500295int twrpTar::extractTar() {
Dees_Troye34c1332013-02-06 19:13:00 +0000296 char* charRootDir = (char*) tardir.c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500297 bool gzip = false;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500298 if (openTar(gzip) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500299 return -1;
300 if (tar_extract_all(t, charRootDir) != 0) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500301 LOGE("Unable to extract tar archive '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500302 return -1;
303 }
304 if (tar_close(t) != 0) {
305 LOGE("Unable to close tar file\n");
306 return -1;
307 }
308 return 0;
309}
310
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500311int twrpTar::extract() {
Dees_Troye34c1332013-02-06 19:13:00 +0000312 int len = 3;
313 char header[len];
314 string::size_type i = 0;
315 int firstbyte = 0;
316 int secondbyte = 0;
317 int ret;
318 ifstream f;
319 f.open(tarfn.c_str(), ios::in | ios::binary);
320 f.get(header, len);
321 firstbyte = header[i] & 0xff;
322 secondbyte = header[++i] & 0xff;
323 f.close();
324 if (firstbyte == 0x1f && secondbyte == 0x8b) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500325 //if you return the extractTGZ function directly, stack crashes happen
326 LOGI("Extracting gzipped tar\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500327 ret = extractTGZ();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500328 return ret;
329 }
330 else {
331 LOGI("Extracting uncompressed tar\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500332 return extractTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500333 }
334}
335
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500336int twrpTar::tarDirs(bool include_root) {
Dees_Troye34c1332013-02-06 19:13:00 +0000337 DIR* d;
338 string mainfolder = tardir + "/", subfolder;
Dees_Troy3263e922013-03-15 11:42:57 -0500339 char buf[PATH_MAX];
Dees_Troye34c1332013-02-06 19:13:00 +0000340 d = opendir(tardir.c_str());
341 if (d != NULL) {
342 struct dirent* de;
343 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500344#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troye34c1332013-02-06 19:13:00 +0000345 if ((tardir == "/data" || tardir == "/data/") && strcmp(de->d_name, "media") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500346#endif
Dees_Troy3263e922013-03-15 11:42:57 -0500347 if (de->d_type == DT_BLK || de->d_type == DT_CHR || strcmp(de->d_name, "..") == 0)
348 continue;
Dees_Troye34c1332013-02-06 19:13:00 +0000349 subfolder = mainfolder;
Dees_Troy3263e922013-03-15 11:42:57 -0500350 if (strcmp(de->d_name, ".") != 0) {
351 subfolder += de->d_name;
352 } else {
353 LOGI("adding '%s'\n", subfolder.c_str());
354 if (addFile(subfolder, include_root) != 0)
355 return -1;
356 continue;
357 }
358 LOGI("adding '%s'\n", subfolder.c_str());
Dees_Troye34c1332013-02-06 19:13:00 +0000359 strcpy(buf, subfolder.c_str());
360 if (de->d_type == DT_DIR) {
Dees_Troy3263e922013-03-15 11:42:57 -0500361 char* charTarPath;
362 if (include_root) {
363 charTarPath = NULL;
364 } else {
365 string temp = Strip_Root_Dir(buf);
366 charTarPath = (char*) temp.c_str();
367 }
368 if (tar_append_tree(t, buf, charTarPath) != 0) {
369 LOGE("Error appending '%s' to tar archive '%s'\n", buf, tarfn.c_str());
Dees_Troye34c1332013-02-06 19:13:00 +0000370 return -1;
371 }
Dees_Troye34c1332013-02-06 19:13:00 +0000372 } else if (tardir != "/" && (de->d_type == DT_REG || de->d_type == DT_LNK)) {
Dees_Troy3263e922013-03-15 11:42:57 -0500373 if (addFile(buf, include_root) != 0)
374 return -1;
375 }
Dees_Troye34c1332013-02-06 19:13:00 +0000376 fflush(NULL);
377 }
378 closedir(d);
379 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500380 return 0;
381}
382
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500383int twrpTar::createTGZ() {
Dees_Troye34c1332013-02-06 19:13:00 +0000384 bool gzip = true;
385
386 init_libtar_buffer(0);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500387 if (createTar() == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500388 return -1;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500389 if (tarDirs(false) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500390 return -1;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500391 if (closeTar(gzip) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500392 return -1;
Dees_Troye34c1332013-02-06 19:13:00 +0000393 free_libtar_buffer();
394 return 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500395}
396
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500397int twrpTar::create() {
Dees_Troye34c1332013-02-06 19:13:00 +0000398 bool gzip = false;
399
400 init_libtar_buffer(0);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500401 if (createTar() == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500402 return -1;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500403 if (tarDirs(false) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500404 return -1;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500405 if (closeTar(gzip) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500406 return -1;
Dees_Troye34c1332013-02-06 19:13:00 +0000407 free_libtar_buffer();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500408 return 0;
409}
410
411int twrpTar::addFilesToExistingTar(vector <string> files, string fn) {
412 char* charTarFile = (char*) fn.c_str();
Dees_Troye34c1332013-02-06 19:13:00 +0000413 static tartype_t type = { open, close, read, write_tar };
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500414
Dees_Troye34c1332013-02-06 19:13:00 +0000415 init_libtar_buffer(0);
416 if (tar_open(&t, charTarFile, &type, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500417 return -1;
418 removeEOT(charTarFile);
Dees_Troye34c1332013-02-06 19:13:00 +0000419 if (tar_open(&t, charTarFile, &type, O_WRONLY | O_APPEND | O_LARGEFILE, 0644, TAR_GNU) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500420 return -1;
421 for (unsigned int i = 0; i < files.size(); ++i) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500422 char* file = (char*) files.at(i).c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500423 if (tar_append_file(t, file, file) == -1)
424 return -1;
425 }
Dees_Troye34c1332013-02-06 19:13:00 +0000426 flush_libtar_buffer(t->fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500427 if (tar_append_eof(t) == -1)
428 return -1;
429 if (tar_close(t) == -1)
430 return -1;
Dees_Troye34c1332013-02-06 19:13:00 +0000431 free_libtar_buffer();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500432 return 0;
433}
434
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500435int twrpTar::createTar() {
436 char* charTarFile = (char*) tarfn.c_str();
Dees_Troye34c1332013-02-06 19:13:00 +0000437 char* charRootDir = (char*) tardir.c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500438 int use_compression = 0;
Dees_Troye34c1332013-02-06 19:13:00 +0000439 static tartype_t type = { open, close, read, write_tar };
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500440
441 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500442 if (use_compression) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500443 string cmd = "pigz - > '" + tarfn + "'";
bigbiff bigbiff23aa8192013-02-22 10:43:59 -0500444 p = popen(cmd.c_str(), "w");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500445 fd = fileno(p);
446 if (!p) return -1;
Dees_Troye34c1332013-02-06 19:13:00 +0000447 if(tar_fdopen(&t, fd, charRootDir, &type, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500448 pclose(p);
449 return -1;
450 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500451 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500452 else {
Dees_Troye34c1332013-02-06 19:13:00 +0000453 if (tar_open(&t, charTarFile, &type, O_WRONLY | O_CREAT | O_LARGEFILE, 0644, TAR_GNU) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500454 return -1;
455 }
456 return 0;
457}
458
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500459int twrpTar::openTar(bool gzip) {
Dees_Troye34c1332013-02-06 19:13:00 +0000460 char* charRootDir = (char*) tardir.c_str();
461 char* charTarFile = (char*) tarfn.c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500462
463 if (gzip) {
464 LOGI("Opening as a gzip\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500465 string cmd = "pigz -d -c '" + tarfn + "'";
bigbiff bigbiff23aa8192013-02-22 10:43:59 -0500466 FILE* pipe = popen(cmd.c_str(), "r");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500467 int fd = fileno(pipe);
468 if (!pipe) return -1;
469 if(tar_fdopen(&t, fd, charRootDir, NULL, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) {
470 LOGI("tar_fdopen returned error\n");
Dees_Troy40bbcf82013-02-12 15:01:53 +0000471 __pclose(pipe);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500472 return -1;
473 }
474 }
475 else {
476 if (tar_open(&t, charTarFile, NULL, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) {
477 LOGE("Unable to open tar archive '%s'\n", charTarFile);
478 return -1;
479 }
480 }
481 return 0;
482}
483
484string twrpTar::Strip_Root_Dir(string Path) {
485 string temp;
486 size_t slash;
487
488 if (Path.substr(0, 1) == "/")
489 temp = Path.substr(1, Path.size() - 1);
490 else
491 temp = Path;
492 slash = temp.find("/");
493 if (slash == string::npos)
494 return temp;
495 else {
496 string stripped;
497
498 stripped = temp.substr(slash, temp.size() - slash);
499 return stripped;
500 }
501 return temp;
502}
503
504int twrpTar::addFile(string fn, bool include_root) {
505 char* charTarFile = (char*) fn.c_str();
506 if (include_root) {
507 if (tar_append_file(t, charTarFile, NULL) == -1)
508 return -1;
509 } else {
510 string temp = Strip_Root_Dir(fn);
511 char* charTarPath = (char*) temp.c_str();
512 if (tar_append_file(t, charTarFile, charTarPath) == -1)
513 return -1;
514 }
515 return 0;
516}
517
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500518int twrpTar::closeTar(bool gzip) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500519 int use_compression;
520 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
521
Dees_Troye34c1332013-02-06 19:13:00 +0000522 flush_libtar_buffer(t->fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500523 if (tar_append_eof(t) != 0) {
524 LOGE("tar_append_eof(): %s\n", strerror(errno));
525 tar_close(t);
526 return -1;
527 }
528 if (tar_close(t) != 0) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500529 LOGE("Unable to close tar archive: '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500530 return -1;
531 }
532 if (use_compression || gzip) {
533 LOGI("Closing popen and fd\n");
534 pclose(p);
535 close(fd);
536 }
537 return 0;
538}
539
540int twrpTar::removeEOT(string tarFile) {
541 char* charTarFile = (char*) tarFile.c_str();
542 off_t tarFileEnd;
543 while (th_read(t) == 0) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500544 if (TH_ISREG(t))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500545 tar_skip_regfile(t);
546 tarFileEnd = lseek(t->fd, 0, SEEK_CUR);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500547 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500548 if (tar_close(t) == -1)
549 return -1;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500550 if (truncate(charTarFile, tarFileEnd) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500551 return -1;
552 return 0;
553}
554
555int twrpTar::compress(string fn) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500556 string cmd = "pigz " + fn;
bigbiff bigbiff23aa8192013-02-22 10:43:59 -0500557 p = popen(cmd.c_str(), "r");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500558 if (!p) return -1;
559 char buffer[128];
560 string result = "";
561 while(!feof(p)) {
562 if(fgets(buffer, 128, p) != NULL)
563 result += buffer;
564 }
Dees_Troy40bbcf82013-02-12 15:01:53 +0000565 __pclose(p);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500566 return 0;
567}
568
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500569int twrpTar::extractTGZ() {
570 string splatrootdir(tardir);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500571 bool gzip = true;
Dees_Troye34c1332013-02-06 19:13:00 +0000572 char* splatCharRootDir = (char*) splatrootdir.c_str();
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500573 if (openTar(gzip) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500574 return -1;
575 int ret = tar_extract_all(t, splatCharRootDir);
576 if (tar_close(t) != 0) {
577 LOGE("Unable to close tar file\n");
578 return -1;
579 }
Dees_Troye34c1332013-02-06 19:13:00 +0000580 return 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500581}
Dees_Troye34c1332013-02-06 19:13:00 +0000582
583extern "C" ssize_t write_tar(int fd, const void *buffer, size_t size) {
584 return (ssize_t) write_libtar_buffer(fd, buffer, size);
Dees_Troy40bbcf82013-02-12 15:01:53 +0000585}