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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
|
/* libarchive plugin for Tux Commander
* version 0.1.5, designed for libarchive 2.5.5
* Copyright (C) 2008 Tomas Bzatek <tbzatek@users.sourceforge.net>
* Check for updates on tuxcmd.sourceforge.net
*
* Uses libarchive library
* Copyright (c) 2003-2007 Tim Kientzle
*
*
* 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 2 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <fnmatch.h>
#include <unistd.h>
#include <glib.h>
#include "vfs_types.h"
#include "vfsutils.h"
#include "strutils.h"
#include "treepathutils.h"
#include "treepath_vfs.h"
#include <archive.h>
#include <archive_entry.h>
#define VERSION "0.1.5"
#define BUILD_DATE "2009-10-25"
#define DEFAULT_BLOCK_SIZE 65536
/******************************************************************************************************/
/** Auxiliary classes */
/************** ****************/
// Declaration of the global plugin object
struct TVFSGlobs {
TVFSLogFunc log_func;
char *curr_dir;
char *archive_path;
unsigned long block_size;
struct PathTree *files;
struct VfsFilelistData *vfs_filelist;
u_int64_t total_size;
TVFSAskQuestionCallback callback_ask_question;
TVFSAskPasswordCallback callback_ask_password;
TVFSProgressCallback callback_progress;
void *callback_data;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////
// Basic initialization functions
struct TVFSGlobs *
VFSNew (TVFSLogFunc log_func)
{
struct TVFSGlobs * globs;
globs = (struct TVFSGlobs *) malloc (sizeof (struct TVFSGlobs));
memset (globs, 0, sizeof (struct TVFSGlobs));
globs->block_size = DEFAULT_BLOCK_SIZE;
globs->callback_data = NULL;
globs->callback_ask_question = NULL;
globs->callback_ask_password = NULL;
globs->callback_progress = NULL;
globs->log_func = log_func;
if (globs->log_func != NULL) globs->log_func("libarchive plugin: VFSInit");
return globs;
}
void
VFSSetCallbacks (struct TVFSGlobs *globs,
TVFSAskQuestionCallback ask_question_callback,
TVFSAskPasswordCallback ask_password_callback,
TVFSProgressCallback progress_func,
void *data)
{
globs->callback_ask_question = ask_question_callback;
globs->callback_ask_password = ask_password_callback;
globs->callback_progress = progress_func;
globs->callback_data = data;
}
void
VFSFree (struct TVFSGlobs *globs)
{
if (globs->log_func != NULL) globs->log_func("libarchive plugin: VFSDestroy");
free (globs);
}
int VFSVersion()
{
return cVFSVersion;
}
struct TVFSInfo *
VFSGetInfo()
{
struct TVFSInfo *module_info = g_malloc0 (sizeof (struct TVFSInfo));
module_info->ID = g_strdup ("libarchive_plugin");
module_info->Name = g_strdup ("libarchive plugin");
module_info->About = g_strdup_printf ("version %s, build date: %s\nusing %s\n", VERSION, BUILD_DATE, ARCHIVE_VERSION_STRING);
module_info->Copyright = g_strdup ("Plugin Copyright (C) 2008-2009 Tomáš Bžatek\nlibarchive sources Copyright (c) 2003-2007 Tim Kientzle");
return module_info;
}
char *
VFSGetArchiveExts ()
{
return g_strdup ("tar;tar.gz;tar.bz2;tgz;tbz2;cpio;iso;a;deb");
}
/**************************************************************************************************************************************/
/**************************************************************************************************************************************/
static TVFSResult libarchive_open(struct archive **a, const char *filename, size_t block_size)
{
int r;
*a = archive_read_new();
// Register supported formats
archive_read_support_compression_all(*a);
archive_read_support_format_all(*a);
/*
archive_read_support_compression_bzip2(*a);
archive_read_support_compression_gzip(*a);
archive_read_support_compression_compress(*a);
archive_read_support_compression_none(*a);
archive_read_support_format_tar(*a);
archive_read_support_format_cpio(*a);
archive_read_support_format_ar(*a);
archive_read_support_format_empty(*a);
archive_read_support_format_gnutar(*a);
archive_read_support_format_iso9660(*a);
archive_read_support_format_cpio(*a);
*/
r = archive_read_open_file(*a, filename, block_size);
if (r) {
fprintf(stderr, "(EE) libarchive_open: error occured when opening archive: %s\n", archive_error_string(*a));
return cVFS_Failed;
}
return cVFS_OK;
}
TVFSResult VFSOpenArchive(struct TVFSGlobs *globs, char *sName)
{
globs->files = filelist_tree_new();
globs->vfs_filelist = vfs_filelist_new(globs->files);
globs->curr_dir = NULL;
globs->archive_path = strdup(sName);
globs->total_size = 0;
fprintf(stderr, "(--) VFSOpenArchive: trying to open archive '%s'...\n", globs->archive_path);
TVFSResult Result;
struct archive *a;
struct archive_entry *entry;
int r;
Result = libarchive_open(&a, globs->archive_path, globs->block_size);
if (Result == cVFS_OK)
{
for (;;) {
entry = NULL;
r = archive_read_next_header(a, &entry);
if (r == ARCHIVE_EOF) {
break;
} else
if (r == ARCHIVE_WARN) {
log("(WW) VFSOpenArchive: file '%s' - libarchive warning: '%s'\n", archive_entry_pathname(entry), archive_error_string(a));
} else
if (r != ARCHIVE_OK) {
fprintf(stderr, "(EE) VFSOpenArchive: error occured while reading archive: '%s'\n", archive_error_string(a));
Result = cVFS_Failed;
break;
}
log("found file: %s, mode = %x\n", archive_entry_pathname(entry), archive_entry_mode(entry));
// Create a TVFSItem entry and fill all info
struct TVFSItem *item = (struct TVFSItem*)malloc(sizeof(struct TVFSItem));
memset(item, 0, sizeof(struct TVFSItem));
item->iSize = (int64_t)archive_entry_size(entry);
item->iPackedSize = -1; /* no support */
globs->total_size += item->iSize;
mode_t mode = archive_entry_mode(entry);
item->iMode = archive_entry_mode(entry);
if (S_ISREG(mode)) item->ItemType = vRegular;
else if (S_ISDIR(mode)) item->ItemType = vDirectory;
else if (S_ISCHR(mode)) item->ItemType = vChardev;
else if (S_ISBLK(mode)) item->ItemType = vBlockdev;
else if (S_ISFIFO(mode)) item->ItemType = vFifo;
else if (S_ISLNK(mode)) item->ItemType = vSymlink;
else if (S_ISSOCK(mode)) item->ItemType = vSock;
if (item->ItemType == vSymlink)
item->sLinkTo = strdup(archive_entry_symlink(entry));
item->iUID = geteuid();
item->iGID = getegid();
item->m_time = archive_entry_mtime(entry);
item->c_time = archive_entry_ctime(entry);
item->a_time = archive_entry_atime(entry);
char *s;
if (g_utf8_validate (archive_entry_pathname(entry), -1, NULL))
s = g_strdup (archive_entry_pathname(entry));
else {
if (archive_entry_pathname_w(entry))
s = wide_to_utf8 (archive_entry_pathname_w(entry));
else
s = g_filename_display_name (archive_entry_pathname(entry));
}
// g_print("file = '%s', wide = '%ls'\n", archive_entry_pathname(entry), archive_entry_pathname_w(entry));
// Add item to the global list and continue with next file
filelist_tree_add_item(globs->files, s, archive_entry_pathname(entry), item, 0);
g_free (s);
}
archive_read_close(a);
}
archive_read_finish(a);
fprintf(stderr, "(II) VFSOpenArchive: done. \n");
/* FIXME: temporarily disabled */
// filelist_tree_resolve_symlinks(globs->files);
printf("\n\nList of items:\n");
filelist_tree_print(globs->files);
return Result;
}
TVFSResult VFSClose(struct TVFSGlobs *globs)
{
if (globs) {
fprintf(stderr, "(II) VFSClose: Freeing objects...\n");
if (globs->vfs_filelist) vfs_filelist_free(globs->vfs_filelist);
if (globs->files) filelist_tree_free(globs->files);
if (globs->archive_path) free(globs->archive_path);
if (globs->curr_dir) free(globs->curr_dir);
}
return cVFS_OK;
}
char *
VFSGetPath (struct TVFSGlobs *globs)
{
return include_trailing_path_sep (globs->curr_dir);
}
u_int64_t VFSGetFileSystemFree(struct TVFSGlobs *globs, char *APath)
{
return 0;
}
u_int64_t VFSGetFileSystemSize(struct TVFSGlobs *globs, char *APath)
{
return globs->total_size;
}
/******************************************************************************************************/
TVFSResult VFSChangeDir(struct TVFSGlobs *globs, char *NewPath)
{
if (NewPath == NULL) {
printf("(EE) VFSChangeDir: NewPath is NULL!\n");
return cVFS_Failed;
}
globs->curr_dir = vfs_filelist_change_dir(globs->vfs_filelist, NewPath);
if (globs->curr_dir) return cVFS_OK;
else return cVFS_Failed;
}
int VFSGetPasswordRequired(struct TVFSGlobs *globs)
{
return FALSE;
}
/******************************************************************************************************/
TVFSResult VFSListFirst(struct TVFSGlobs *globs, char *sDir, struct TVFSItem *Item)
{
if (sDir == NULL) {
printf("(EE) VFSListFirst: sDir is NULL!\n");
return cVFS_Failed;
}
printf ("(--) VFSListFirst: Going to list all items in '%s'\n", sDir);
return vfs_filelist_list_first(globs->vfs_filelist, sDir, Item);
}
TVFSResult VFSListNext(struct TVFSGlobs *globs, char *sDir, struct TVFSItem *Item)
{
return vfs_filelist_list_next(globs->vfs_filelist, sDir, Item);
}
TVFSResult VFSListClose(struct TVFSGlobs *globs)
{
return vfs_filelist_list_close(globs->vfs_filelist);
}
/******************************************************************************************************/
long VFSFileExists(struct TVFSGlobs *globs, const char *FileName, const long Use_lstat)
{
if (! globs) return FALSE;
return vfs_filelist_file_exists(globs->vfs_filelist, FileName, Use_lstat);
}
TVFSResult VFSFileInfo(struct TVFSGlobs *globs, char *AFileName, struct TVFSItem *Item)
{
printf("(--) VFSFileInfo: requested info for object '%s'\n", AFileName);
if (!globs) return cVFS_Failed;
return vfs_filelist_file_info(globs->vfs_filelist, AFileName, Item);
}
/******************************************************************************************************/
/** Recursive tree size counting */
/************** ****************/
u_int64_t VFSGetDirSize(struct TVFSGlobs *globs, char *APath)
{
if (! globs) return 0;
return vfs_filelist_get_dir_size(globs->vfs_filelist, APath);
}
void VFSBreakGetDirSize(struct TVFSGlobs *globs)
{
printf("(WW) VFSBreakGetDirSize: calling break\n");
if (globs) vfs_filelist_get_dir_size_break(globs->vfs_filelist);
}
/******************************************************************************************************/
/** Methods modifying the archive */
/************** ****************/
TVFSResult VFSMkDir(struct TVFSGlobs *globs, const char *sDirName)
{
printf("(WW) VFSMkDir: Not supported in libarchive plugin.\n");
return cVFS_Not_Supported;
}
TVFSResult VFSRemove(struct TVFSGlobs *globs, const char *APath)
{
printf("(WW) VFSRemove: Not supported in libarchive plugin.\n");
return cVFS_Not_Supported;
}
TVFSResult VFSRename(struct TVFSGlobs *globs, const char *sSrcName, const char *sDstName)
{
printf("(WW) VFSRename: Not supported in libarchive plugin.\n");
return cVFS_Not_Supported;
}
TVFSResult VFSMakeSymLink(struct TVFSGlobs *globs, const char *NewFileName, const char *PointTo)
{
printf("(WW) VFSMakeSymLink: Not supported in libarchive plugin.\n");
return cVFS_Not_Supported;
}
TVFSResult VFSChmod(struct TVFSGlobs *globs, const char *FileName, const uint Mode)
{
printf("(WW) VFSChmod: Not supported in libarchive plugin.\n");
return cVFS_Not_Supported;
}
TVFSResult VFSChown(struct TVFSGlobs *globs, const char *FileName, const uint UID, const uint GID)
{
printf("(WW) VFSChown: Not supported in libarchive plugin.\n");
return cVFS_Not_Supported;
}
TVFSResult VFSChangeTimes(struct TVFSGlobs *globs, char *APath, long mtime, long atime)
{
printf("(WW) VFSChangeTimes: Not supported in libarchive plugin.\n");
return cVFS_Not_Supported;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////
TVFSFileDes VFSOpenFile(struct TVFSGlobs *globs, const char *APath, int Mode, int *Error)
{
*Error = cVFS_Not_Supported;
return (TVFSFileDes)0;
}
TVFSResult VFSCloseFile(struct TVFSGlobs *globs, TVFSFileDes FileDescriptor)
{
return cVFS_Not_Supported;
}
u_int64_t VFSFileSeek(struct TVFSGlobs *globs, TVFSFileDes FileDescriptor, u_int64_t AbsoluteOffset, int *Error)
{
*Error = cVFS_Not_Supported;
return 0;
}
int VFSReadFile(struct TVFSGlobs *globs, TVFSFileDes FileDescriptor, void *Buffer, int ABlockSize, int *Error)
{
*Error = cVFS_Not_Supported;
return 0;
}
int VFSWriteFile(struct TVFSGlobs *globs, TVFSFileDes FileDescriptor, void *Buffer, int BytesCount, int *Error)
{
*Error = cVFS_Not_Supported;
return 0;
}
void VFSSetBlockSize(struct TVFSGlobs *globs, int Value)
{
globs->block_size = Value;
}
int VFSIsOnSameFS(struct TVFSGlobs *globs, const char *Path1, const char *Path2)
{
printf("(WW) VFSIsOnSameFS: Not supported in libarchive plugin.\n");
return TRUE;
}
int VFSTwoSameFiles(struct TVFSGlobs *globs, const char *Path1, const char *Path2)
{
printf("(WW) VFSTwoSameFiles: Not supported in libarchive, comparing by paths.\n");
return compare_two_same_files(Path1, Path2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////
/**
* The following code has been stolen from archive_read_data_into_fd.c (libarchive sources) and modified to allow progress callbacks
* Quote: "This implementation minimizes copying of data and is sparse-file aware."
**/
static TVFSResult
my_archive_read_data_into_fd(struct TVFSGlobs *globs, struct archive *a, struct archive_entry *entry, const char *sDstName, size_t max_block, int Append)
{
int r;
int fd;
const void *buff;
size_t size, bytes_to_write;
ssize_t bytes_written, total_written;
off_t offset;
off_t output_offset;
u_int64_t file_size;
gboolean cancel = FALSE;
printf("(II) my_archive_read_data_into_fd: extracting to '%s', Append = %d\n", sDstName, Append);
if (Append) fd = open(sDstName, O_APPEND | O_WRONLY);
else fd = open(sDstName, O_CREAT | O_WRONLY | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0) {
fprintf(stderr, "(EE) my_archive_read_data_into_fd: error occured while extracting data: %s\n", strerror(errno));
return cVFS_Failed;
}
total_written = 0;
output_offset = 0;
file_size = (u_int64_t)archive_entry_size(entry);
while ((r = archive_read_data_block(a, &buff, &size, &offset)) == ARCHIVE_OK) {
const char *p = buff;
if (offset > output_offset) {
lseek(fd, offset - output_offset, SEEK_CUR);
output_offset = offset;
}
while (size > 0) {
if (cancel) break;
bytes_to_write = size;
if (bytes_to_write > max_block)
bytes_to_write = max_block;
bytes_written = write(fd, p, bytes_to_write);
if (bytes_written < 0) {
fprintf(stderr, "(EE) my_archive_read_data_into_fd: error occured while extracting data: %s\n", strerror(errno));
return cVFS_Failed;
}
output_offset += bytes_written;
total_written += bytes_written;
p += bytes_written;
size -= bytes_written;
log(" (II) my_archive_read_data_into_fd: bytes_written = %zd, total_written = %zd\n", bytes_written, total_written);
if (globs->callback_progress) {
if (! globs->callback_progress(total_written, file_size, globs->callback_data)) {
cancel = TRUE;
break;
}
// usleep(100000);
}
}
}
if ((close(fd)) || ((r != ARCHIVE_OK) && (r != ARCHIVE_EOF))) {
fprintf(stderr, "(EE) my_archive_read_data_into_fd: error closing extracted file: %s\n", strerror(errno));
return cVFS_WriteErr;
}
if (cancel) {
if (unlink(sDstName))
fprintf(stderr, "(EE) my_archive_read_data_into_fd: error unlinking cancelled extraction: %s\n", strerror(errno));
return cVFS_Cancelled;
}
printf("(II) my_archive_read_data_into_fd: done.\n");
return cVFS_OK;
}
TVFSResult VFSCopyToLocal(struct TVFSGlobs *globs, const char *sSrcName, const char *sDstName, int Append)
{
if ((sSrcName == NULL) || (sDstName == NULL) || (strlen(sSrcName) < 1) || (strlen(sDstName) < 1)) {
printf("(EE) VFSCopyToLocal: The value of 'sSrcName' or 'sDstName' is NULL or empty\n");
return cVFS_Failed;
}
printf("(II) VFSCopyToLocal: copying file '%s' out to '%s'\n", sSrcName, sDstName);
struct PathTree *node = filelist_tree_find_node_by_path(globs->files, sSrcName);
if (! node) {
fprintf(stderr, "(EE) VFSCopyToLocal: cannot find file '%s'\n", sSrcName);
return cVFS_ReadErr;
}
const char *src = node->original_pathstr;
if (! src) {
fprintf(stderr, "(WW) VFSCopyToLocal: cannot determine original filename\n");
src = sSrcName;
}
printf("(II) VFSCopyToLocal: new src path: '%s'\n", src);
TVFSResult Result;
struct archive *a;
struct archive_entry *entry;
int r;
gboolean found = FALSE;
Result = libarchive_open(&a, globs->archive_path, globs->block_size);
if (Result == cVFS_OK)
{
for (;;) {
entry = NULL;
r = archive_read_next_header(a, &entry);
if (r == ARCHIVE_EOF) {
break;
} else
if (r == ARCHIVE_WARN) {
log("(WW) VFSOpen: file '%s' - libarchive warning: '%s'\n", archive_entry_pathname(entry), archive_error_string(a));
} else
if (r != ARCHIVE_OK) {
fprintf(stderr, "(EE) VFSCopyToLocal: error occured while reading archive: '%s'\n", archive_error_string(a));
Result = cVFS_Failed;
break;
}
// printf ("found file: %s, mode = %x\n", archive_entry_pathname(entry), archive_entry_mode(entry));
const char *ssrc = src;
const char *asrc = archive_entry_pathname(entry);
if (IS_DIR_SEP(*ssrc)) ssrc++;
if (IS_DIR_SEP(*asrc)) asrc++;
// printf ("strcmp: '%s' vs. '%s'\n", ssrc, asrc);
if (strcmp(ssrc, asrc) == 0) {
// printf("--> found file, extracting\n");
fprintf(stderr, "(II) VFSCopyToLocal: extract_file_path(sDstName) = '%s', extract_file_name(sDstName) = '%s' \n", extract_file_path(sDstName), extract_file_name(sDstName));
found = TRUE;
Result = my_archive_read_data_into_fd(globs, a, entry, sDstName, globs->block_size, Append);
break;
}
}
archive_read_close(a);
}
archive_read_finish(a);
if ((! found) && Result == cVFS_OK) {
fprintf(stderr, "(EE) VFSCopyToLocal: file not found in archive.\n");
Result = cVFS_ReadErr;
}
fprintf(stderr, "(II) VFSCopyToLocal: finished. \n");
return Result;
}
TVFSResult VFSCopyFromLocal(struct TVFSGlobs *globs, const char *sSrcName, const char *sDstName, int Append)
{
printf("(WW) VFSCopyFromLocal: Not supported in libarchive plugin.\n");
return cVFS_Not_Supported;
}
/**********
* TODO:
*
* - UTF-8, FName/FDisplayName and absolute/relative paths revision needed!
* - archive testing (needs new VFS API)
* - write support
* - support creating new archives (needs new VFS API)
*
***/
|