1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
/*
Copyright (©) 2003-2025 Teus Benschop.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <filter/archive.h>
#include <filter/url.h>
#include <filter/shell.h>
#include <filter/string.h>
#include <database/logs.h>
#include <microtar/microtar.h>
#include <miniz/miniz.h>
// Work around old Microsoft macro definitions.
#undef max
#undef min
// Compresses a $folder into zip format.
// Returns the path to the compressed archive it created.
std::string filter_archive_zip_folder (std::string folder)
{
#ifdef HAVE_CLOUD
return filter_archive_zip_folder_shell_internal (folder);
#endif
#ifdef HAVE_CLIENT
return filter_archive_zip_folder_miniz_internal (folder);
#endif
}
// Compresses a $folder into zip format.
// Returns the path to the compressed archive it created.
std::string filter_archive_zip_folder_shell_internal (std::string folder)
{
if (!file_or_dir_exists (folder)) return std::string();
std::string zippedfile = filter_url_tempfile () + ".zip";
#ifdef HAVE_CLOUD
std::string logfile = filter_url_tempfile () + ".log";
folder = filter_url_escape_shell_argument (folder);
std::string command = "cd " + folder + " && " + filter::shell::get_executable(filter::shell::Executable::zip) + " -r " + zippedfile + " * > " + logfile + " 2>&1";\
int return_var;
// Run the command.
return_var = system (command.c_str());
if (return_var != 0) {
filter_url_unlink (zippedfile);
zippedfile.clear();
std::string errors = filter_url_file_get_contents (logfile);
Database_Logs::log (errors);
}
#endif
return zippedfile;
}
// Compresses a $folder into zip format.
// Returns the path to the compressed archive it created.
std::string filter_archive_zip_folder_miniz_internal (std::string folder)
{
if (!file_or_dir_exists (folder)) {
return std::string();
}
std::string zippedfile = filter_url_tempfile () + ".zip";
std::vector <std::string> paths;
filter_url_recursive_scandir (folder, paths);
for (auto path : paths) {
bool is_dir = filter_url_is_dir (path);
std::string file = path.substr (folder.size () + 1);
#ifdef HAVE_WINDOWS
// The file names in Windows will be backslashes (\) at this point.
// But the mzip library, in its current configuration, works with forward slashes (/).
// So the code below, in case of Windows, updates the type of slashes.
file = filter::strings::replace (DIRECTORY_SEPARATOR, "/", file);
#endif
mz_bool status;
if (is_dir) {
file.append ("/");
status = mz_zip_add_mem_to_archive_file_in_place(zippedfile.c_str(), file.c_str(), nullptr, 0, "", 0, MZ_DEFAULT_LEVEL);
} else {
std::string contents = filter_url_file_get_contents (path);
status = mz_zip_add_mem_to_archive_file_in_place (zippedfile.c_str(), file.c_str(), contents.c_str(), contents.size(), "", 0, MZ_DEFAULT_LEVEL);
}
if (!status) {
Database_Logs::log ("mz_zip_add_mem_to_archive_file_in_place failed for " + path);
return std::string();
}
}
return zippedfile;
}
// Uncompresses a zip archive identified by $file.
// Returns the path to the folder it created.
std::string filter_archive_unzip (std::string file)
{
#ifdef HAVE_CLOUD
return filter_archive_unzip_shell_internal (file);
#endif
#ifdef HAVE_CLIENT
return filter_archive_unzip_miniz_internal (file);
#endif
}
// Uncompresses a zip archive identified by $file.
// Returns the path to the folder it created.
std::string filter_archive_unzip_shell_internal ([[maybe_unused]] std::string file)
{
std::string folder = filter_url_tempfile ();
#ifdef HAVE_CLOUD
filter_url_mkdir (folder);
folder.append (DIRECTORY_SEPARATOR);
const std::string logfile = filter_url_tempfile () + ".log";
file = filter_url_escape_shell_argument (file);
std::string command = std::string(filter::shell::get_executable(filter::shell::Executable::unzip)) + " -o -d " + folder + " " + file + " > " + logfile + " 2>&1";
// Run the command.
int return_var = system (command.c_str());
if (return_var != 0) {
filter_url_rmdir (folder);
folder.clear();
std::string errors = filter_url_file_get_contents (logfile);
Database_Logs::log (errors);
} else {
// Set free permissions after unzipping.
command = std::string(filter::shell::get_executable(filter::shell::Executable::chmod)) + " -R 0777 " + folder;
[[maybe_unused]] int result = system (command.c_str ());
}
#endif
return folder;
}
// Uncompresses the $zipfile.
// Returns the path to the folder it created.
std::string filter_archive_unzip_miniz_internal (std::string zipfile)
{
// Directory where to unzip the archive.
std::string folder = filter_url_tempfile ();
filter_url_mkdir (folder);
// Covers the entire process.
bool error = false;
// Open the zip archive.
mz_bool status;
mz_zip_archive zip_archive;
memset (&zip_archive, 0, sizeof (zip_archive));
status = mz_zip_reader_init_file(&zip_archive, zipfile.c_str(), 0);
if (status) {
// Iterate over the files in the archive.
unsigned filecount = mz_zip_reader_get_num_files (&zip_archive);
for (unsigned i = 0; i < filecount; i++) {
// If there was an error, skip processing further files.
if (error) continue;
// Get information about this file.
mz_zip_archive_file_stat file_stat;
status = mz_zip_reader_file_stat (&zip_archive, i, &file_stat);
if (status) {
std::string filename = filter_url_create_path ({folder, file_stat.m_filename});
// The miniz library returns Unix directory separators above.
// So in case of Windows, convert them to Windows ones.
std::string fixed_filename = filter_url_update_directory_separator_if_windows (filename);
if (mz_zip_reader_is_file_a_directory (&zip_archive, i)) {
// Create this directory.
if (!file_or_dir_exists (fixed_filename)) filter_url_mkdir (fixed_filename);
} else {
/* Code that extracts file contents memory, if needed.
size_t filesize = file_stat.m_uncomp_size;
std::cout << filename << " " << filesize << std::endl;
void * buff = operator new (filesize);
if (buff) {
status = mz_zip_reader_extract_to_mem (&zip_archive, i, buff, filesize, 0);
if (status) {
std::string contents (static_cast<const char*>(buff), filesize);
} else {
// "mz_zip_reader_extract_to_mem failure for " + filename + " in " + zipfile;
error = true;
}
operator delete (buff);
} else {
// "failure to allocate buffer for file extraction";
error = true;
}
*/
// Ensure this file's folder exists.
std::string dirname = filter_url_dirname (fixed_filename);
if (!file_or_dir_exists (dirname)) filter_url_mkdir (dirname);
// Extract this file.
status = mz_zip_reader_extract_to_file (&zip_archive, i, fixed_filename.c_str(), 0);
if (!status) {
Database_Logs::log ("mz_zip_reader_extract_to_file failure for file " + filename + " in " + zipfile);
error = true;
}
}
} else {
Database_Logs::log ("mz_zip_reader_file_stat failed for " + zipfile);
error = true;
}
}
// Close the archive, freeing any resources it was using.
mz_zip_reader_end (&zip_archive);
} else {
Database_Logs::log ("mz_zip_reader_init_file failed for " + zipfile);
error = true;
}
// If there was an error, return nothing, to indicate that uncompression has failed.
if (error) folder.clear ();
// The folder where the files were unpacked.
return folder;
}
// Compresses a file identified by $filename into gzipped tar format.
// Returns the path to the compressed archive it created.
std::string filter_archive_tar_gzip_file (std::string filename)
{
std::string tarball = filter_url_tempfile () + ".tar.gz";
const std::string dirname = filter_url_escape_shell_argument (filter_url_dirname (filename));
const std::string basename = filter_url_escape_shell_argument (filter_url_basename (filename));
const std::string logfile = filter_url_tempfile () + ".log";
const std::string command = "cd " + dirname + " && " + std::string(filter::shell::get_executable(filter::shell::Executable::tar)) + " -czf " + tarball + " " + basename + " > " + logfile + " 2>&1";
int return_var;
#ifdef HAVE_IOS
// Crashes on iOS.
return_var = 1;
#else
// Run the command.
return_var = system (command.c_str());
#endif
if (return_var != 0) {
filter_url_unlink (tarball);
tarball.clear();
std::string errors = filter_url_file_get_contents (logfile);
Database_Logs::log (errors);
}
return tarball;
}
// Compresses a $folder into gzipped tar format.
// Returns the path to the compressed archive it created.
std::string filter_archive_tar_gzip_folder (std::string folder)
{
std::string tarball = filter_url_tempfile () + ".tar.gz";
folder = filter_url_escape_shell_argument (folder);
std::string logfile = filter_url_tempfile () + ".log";
std::string command = "cd " + folder + " && " + std::string(filter::shell::get_executable(filter::shell::Executable::tar)) + " -czf " + tarball + " . > " + logfile + " 2>&1";
int return_var;
#ifdef HAVE_IOS
// Crashes on iOS.
return_var = 1;
#else
// Run the command.
return_var = system (command.c_str());
#endif
if (return_var != 0) {
filter_url_unlink (tarball);
tarball.clear();
std::string errors = filter_url_file_get_contents (logfile);
Database_Logs::log (errors);
}
return tarball;
}
// Uncompresses a .tar.gz archive identified by $file.
// Returns the path to the folder it created.
std::string filter_archive_untar_gzip (std::string file)
{
file = filter_url_escape_shell_argument (file);
std::string folder = filter_url_tempfile ();
filter_url_mkdir (folder);
folder.append (DIRECTORY_SEPARATOR);
std::string logfile = filter_url_tempfile () + ".log";
std::string command = "cd " + folder + " && " + std::string(filter::shell::get_executable(filter::shell::Executable::tar)) + " zxf " + file + " > " + logfile + " 2>&1";
int return_var;
#ifdef HAVE_IOS
// Crashes on iOS.
return_var = 1;
#else
// Run the command.
return_var = system (command.c_str());
#endif
if (return_var != 0) {
filter_url_rmdir (folder);
folder.clear();
std::string errors = filter_url_file_get_contents (logfile);
Database_Logs::log (errors);
}
return folder;
}
// Uncompresses a known archive identified by $file.
// Returns the path to the folder it created.
std::string filter_archive_uncompress (std::string file)
{
const int type = filter_archive_is_archive(file);
if (type == 1) {
return filter_archive_untar_gzip (file);
}
if (type == 2) {
return filter_archive_unzip (file);
}
return std::string();
}
// Returns 0 if it is not an archive that Bibledit supports.
// Else returns 1, 2, 3... depending on the type of archive.
int filter_archive_is_archive (std::string file)
{
// Tar (.tar) archives, including those compressed with gzip (.tar.gz, .tgz), bzip (.tar.bz, .tbz), bzip2 (.tar.bz2, .tbz2), compress (.tar.Z, .taz), lzop (.tar.lzo, .tzo) and lzma (.tar.lzma)
// Zip archives (.zip)
// Jar archives (.jar, .ear, .war)
// 7z archives (.7z)
// iso9660 CD images (.iso)
// Lha archives (.lzh)
// Single files compressed with gzip (.gz), bzip (.bz), bzip2 (.bz2), compress (.Z), lzop (.lzo) and lzma (.lzma)
std::string suffix = filter_url_get_extension (file);
if ((suffix == "tar.gz") || (suffix == "gz") || (suffix == "tgz")) {
return 1;
}
if ((suffix == "zip")) {
return 2;
}
if ((suffix == "tar.bz") || (suffix == "tbz") || (suffix == "tar.bz2") || (suffix == "tbz2")) {
return 0;
}
return 0;
}
// Create a tarball at $tarpath with input $files from $directory.
std::string filter_archive_microtar_pack (std::string tarpath, std::string directory, std::vector <std::string> files)
{
mtar_t tar;
int res;
// Open archive for writing.
res = mtar_open (&tar, tarpath.c_str(), "w");
if (res != MTAR_ESUCCESS) return mtar_strerror (res);
// Iterate over the files.
for (auto file : files) {
// Full path.
std::string path = filter_url_create_path ({directory, file});
// Skip directories.
if (filter_url_is_dir (path)) continue;
// Read the file's data.
std::string data = filter_url_file_get_contents (path);
// Write the file's name to the tarball.
res = mtar_write_file_header(&tar, file.c_str(), static_cast<unsigned> (data.length ()));
if (res != MTAR_ESUCCESS) return mtar_strerror (res);
// Write the file's data to the tarball.
res = mtar_write_data(&tar, data.c_str(), static_cast<unsigned> (data.length ()));
if (res != MTAR_ESUCCESS) return mtar_strerror (res);
}
// Finalize: This needs to be the last thing done before closing.
res = mtar_finalize(&tar);
if (res != MTAR_ESUCCESS) return mtar_strerror (res);
// Close archive.
res = mtar_close(&tar);
if (res != MTAR_ESUCCESS) return mtar_strerror (res);
// OK, done :)
return std::string();
}
// Unpack the tarball at $tarpath and store the individual files at $outputpath.
std::string filter_archive_microtar_unpack (std::string tarball, std::string directory)
{
mtar_t tar;
mtar_header_t h;
int res;
// Open archive for reading.
res = mtar_open (&tar, tarball.c_str(), "r");
if (res != MTAR_ESUCCESS) return mtar_strerror (res);
// Read all file names.
std::vector <std::string> files;
while ((mtar_read_header(&tar, &h)) != MTAR_ENULLRECORD) {
files.push_back (h.name);
mtar_next(&tar);
}
// Create directory if needed.
if (!file_or_dir_exists (directory)) filter_url_mkdir (directory);
// Unpack all files and save them.
for (auto file : files) {
// Find the file's information.
res = mtar_find (&tar, file.c_str(), &h);
if (res != MTAR_ESUCCESS) return mtar_strerror (res);
// Read the file's data.
char *p = static_cast<char *> (calloc(1, h.size + 1));
res = mtar_read_data(&tar, p, h.size);
if (res != MTAR_ESUCCESS) return mtar_strerror (res);
std::string data (p, h.size);
free(p);
// If the file contains a directory, ensure that directory exists.
std::string dirname = filter_url_dirname (file);
if (dirname != ".") {
dirname = filter_url_create_path ({directory, dirname});
if (!file_or_dir_exists (dirname)) filter_url_mkdir (dirname);
}
// Write the file's data.
filter_url_file_put_contents (filter_url_create_path ({directory, file}), data);
}
// Close archive.
res = mtar_close(&tar);
if (res != MTAR_ESUCCESS) return mtar_strerror (res);
// Done, hallelujah :)
return std::string();
}
|