blob: cf225db5859010b18f5d898a63b394f4ed69d2a7 [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);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500218 tardir = FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500219 if (Archive_Current_Size + folder_size > MAX_ARCHIVE_SIZE) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500220 LOGI("Calling Generate_Multiple_Archives\n");
221 if (Generate_Multiple_Archives(FileName) < 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500222 return -1;
223 } else {
224 //FileName += "/";
225 LOGI("Adding folder '%s'\n", FileName.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500226 tardir = FileName;
227 if (tarDirs(true) < 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500228 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 bigbiff3bf2b0e2013-01-21 21:26:43 -0500237 LOGI("Closing tar '%s', ", tarfn.c_str());
238 closeTar(false);
Dees_Troye34c1332013-02-06 19:13:00 +0000239 reinit_libtar_buffer();
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500240 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 bigbiff9c754052013-01-09 09:09:08 -0500243 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500244 Archive_File_Count++;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500245 if (Archive_File_Count > 999) {
246 LOGE("Archive count is too large!\n");
247 return -1;
248 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500249 string temp = basefn + "%03i";
250 sprintf(actual_filename, temp.c_str(), Archive_File_Count);
251 tarfn = actual_filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500252 Archive_Current_Size = 0;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500253 LOGI("Creating tar '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500254 ui_print("Creating archive %i...\n", Archive_File_Count + 1);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500255 if (createTar() != 0)
256 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500257 }
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 bigbiff3bf2b0e2013-01-21 21:26:43 -0500271int twrpTar::Split_Archive()
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500272{
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500273 string temp = tarfn + "%03i";
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500274 char actual_filename[255];
275
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500276 basefn = tarfn;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500277 Archive_File_Count = 0;
278 Archive_Current_Size = 0;
279 sprintf(actual_filename, temp.c_str(), Archive_File_Count);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500280 tarfn = actual_filename;
Dees_Troye34c1332013-02-06 19:13:00 +0000281 init_libtar_buffer(0);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500282 createTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500283 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
284 ui_print("Creating archive 1...\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500285 if (Generate_Multiple_Archives(tardir) < 0) {
Dees_Troye34c1332013-02-06 19:13:00 +0000286 LOGE("Error generating multiple archives\n");
287 free_libtar_buffer();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500288 return -1;
289 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500290 closeTar(false);
Dees_Troye34c1332013-02-06 19:13:00 +0000291 free_libtar_buffer();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500292 LOGI("Done, created %i archives.\n", (Archive_File_Count++));
293 return (Archive_File_Count);
294}
295
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500296int twrpTar::extractTar() {
Dees_Troye34c1332013-02-06 19:13:00 +0000297 char* charRootDir = (char*) tardir.c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500298 bool gzip = false;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500299 if (openTar(gzip) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500300 return -1;
301 if (tar_extract_all(t, charRootDir) != 0) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500302 LOGE("Unable to extract tar archive '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500303 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 bigbiff3bf2b0e2013-01-21 21:26:43 -0500312int twrpTar::extract() {
Dees_Troye34c1332013-02-06 19:13:00 +0000313 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 bigbiff9c754052013-01-09 09:09:08 -0500326 //if you return the extractTGZ function directly, stack crashes happen
327 LOGI("Extracting gzipped tar\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500328 ret = extractTGZ();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500329 return ret;
330 }
331 else {
332 LOGI("Extracting uncompressed tar\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500333 return extractTar();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500334 }
335}
336
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500337int twrpTar::tarDirs(bool include_root) {
Dees_Troye34c1332013-02-06 19:13:00 +0000338 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 bigbiff9c754052013-01-09 09:09:08 -0500347#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troye34c1332013-02-06 19:13:00 +0000348 if ((tardir == "/data" || tardir == "/data/") && strcmp(de->d_name, "media") == 0) continue;
bigbiff bigbiff71e5aa42013-02-26 20:10:16 -0500349 if (de->d_type == DT_BLK || de->d_type == DT_CHR)
350 continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500351#endif
Dees_Troye34c1332013-02-06 19:13:00 +0000352 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500353
Dees_Troye34c1332013-02-06 19:13:00 +0000354 subfolder = mainfolder;
355 subfolder += de->d_name;
356 strcpy(buf, subfolder.c_str());
357 if (de->d_type == DT_DIR) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500358 if (include_root) {
Dees_Troye34c1332013-02-06 19:13:00 +0000359 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 bigbiff9c754052013-01-09 09:09:08 -0500363 } 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_Troye34c1332013-02-06 19:13:00 +0000367 LOGE("Error appending '%s' to tar archive '%s'\n", buf, charTarFile);
368 return -1;
369 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500370 }
Dees_Troye34c1332013-02-06 19:13:00 +0000371 } else if (tardir != "/" && (de->d_type == DT_REG || de->d_type == DT_LNK)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500372 if (addFile(buf, include_root) != 0)
373 return -1;
374 }
Dees_Troye34c1332013-02-06 19:13:00 +0000375 fflush(NULL);
376 }
377 closedir(d);
378 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500379 return 0;
380}
381
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500382int twrpTar::createTGZ() {
Dees_Troye34c1332013-02-06 19:13:00 +0000383 bool gzip = true;
384
385 init_libtar_buffer(0);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500386 if (createTar() == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500387 return -1;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500388 if (tarDirs(false) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500389 return -1;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500390 if (closeTar(gzip) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500391 return -1;
Dees_Troye34c1332013-02-06 19:13:00 +0000392 free_libtar_buffer();
393 return 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500394}
395
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500396int twrpTar::create() {
Dees_Troye34c1332013-02-06 19:13:00 +0000397 bool gzip = false;
398
399 init_libtar_buffer(0);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500400 if (createTar() == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500401 return -1;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500402 if (tarDirs(false) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500403 return -1;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500404 if (closeTar(gzip) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500405 return -1;
Dees_Troye34c1332013-02-06 19:13:00 +0000406 free_libtar_buffer();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500407 return 0;
408}
409
410int twrpTar::addFilesToExistingTar(vector <string> files, string fn) {
411 char* charTarFile = (char*) fn.c_str();
Dees_Troye34c1332013-02-06 19:13:00 +0000412 static tartype_t type = { open, close, read, write_tar };
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500413
Dees_Troye34c1332013-02-06 19:13:00 +0000414 init_libtar_buffer(0);
415 if (tar_open(&t, charTarFile, &type, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500416 return -1;
417 removeEOT(charTarFile);
Dees_Troye34c1332013-02-06 19:13:00 +0000418 if (tar_open(&t, charTarFile, &type, O_WRONLY | O_APPEND | O_LARGEFILE, 0644, TAR_GNU) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500419 return -1;
420 for (unsigned int i = 0; i < files.size(); ++i) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500421 char* file = (char*) files.at(i).c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500422 if (tar_append_file(t, file, file) == -1)
423 return -1;
424 }
Dees_Troye34c1332013-02-06 19:13:00 +0000425 flush_libtar_buffer(t->fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500426 if (tar_append_eof(t) == -1)
427 return -1;
428 if (tar_close(t) == -1)
429 return -1;
Dees_Troye34c1332013-02-06 19:13:00 +0000430 free_libtar_buffer();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500431 return 0;
432}
433
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500434int twrpTar::createTar() {
435 char* charTarFile = (char*) tarfn.c_str();
Dees_Troye34c1332013-02-06 19:13:00 +0000436 char* charRootDir = (char*) tardir.c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500437 int use_compression = 0;
Dees_Troye34c1332013-02-06 19:13:00 +0000438 static tartype_t type = { open, close, read, write_tar };
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500439
440 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500441 if (use_compression) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500442 string cmd = "pigz - > '" + tarfn + "'";
bigbiff bigbiff23aa8192013-02-22 10:43:59 -0500443 p = popen(cmd.c_str(), "w");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500444 fd = fileno(p);
445 if (!p) return -1;
Dees_Troye34c1332013-02-06 19:13:00 +0000446 if(tar_fdopen(&t, fd, charRootDir, &type, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500447 pclose(p);
448 return -1;
449 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500450 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500451 else {
Dees_Troye34c1332013-02-06 19:13:00 +0000452 if (tar_open(&t, charTarFile, &type, O_WRONLY | O_CREAT | O_LARGEFILE, 0644, TAR_GNU) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500453 return -1;
454 }
455 return 0;
456}
457
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500458int twrpTar::openTar(bool gzip) {
Dees_Troye34c1332013-02-06 19:13:00 +0000459 char* charRootDir = (char*) tardir.c_str();
460 char* charTarFile = (char*) tarfn.c_str();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500461
462 if (gzip) {
463 LOGI("Opening as a gzip\n");
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500464 string cmd = "pigz -d -c '" + tarfn + "'";
bigbiff bigbiff23aa8192013-02-22 10:43:59 -0500465 FILE* pipe = popen(cmd.c_str(), "r");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500466 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_Troy40bbcf82013-02-12 15:01:53 +0000470 __pclose(pipe);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500471 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
483string 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
503int 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 bigbiff3bf2b0e2013-01-21 21:26:43 -0500517int twrpTar::closeTar(bool gzip) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500518 int use_compression;
519 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
520
Dees_Troye34c1332013-02-06 19:13:00 +0000521 flush_libtar_buffer(t->fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500522 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 bigbiff3bf2b0e2013-01-21 21:26:43 -0500528 LOGE("Unable to close tar archive: '%s'\n", tarfn.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500529 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
539int twrpTar::removeEOT(string tarFile) {
540 char* charTarFile = (char*) tarFile.c_str();
541 off_t tarFileEnd;
542 while (th_read(t) == 0) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500543 if (TH_ISREG(t))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500544 tar_skip_regfile(t);
545 tarFileEnd = lseek(t->fd, 0, SEEK_CUR);
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500546 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500547 if (tar_close(t) == -1)
548 return -1;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500549 if (truncate(charTarFile, tarFileEnd) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500550 return -1;
551 return 0;
552}
553
554int twrpTar::compress(string fn) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500555 string cmd = "pigz " + fn;
bigbiff bigbiff23aa8192013-02-22 10:43:59 -0500556 p = popen(cmd.c_str(), "r");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500557 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_Troy40bbcf82013-02-12 15:01:53 +0000564 __pclose(p);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500565 return 0;
566}
567
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500568int twrpTar::extractTGZ() {
569 string splatrootdir(tardir);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500570 bool gzip = true;
Dees_Troye34c1332013-02-06 19:13:00 +0000571 char* splatCharRootDir = (char*) splatrootdir.c_str();
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500572 if (openTar(gzip) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500573 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_Troye34c1332013-02-06 19:13:00 +0000579 return 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500580}
Dees_Troye34c1332013-02-06 19:13:00 +0000581
582extern "C" ssize_t write_tar(int fd, const void *buffer, size_t size) {
583 return (ssize_t) write_libtar_buffer(fd, buffer, size);
Dees_Troy40bbcf82013-02-12 15:01:53 +0000584}