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
|
/*
* Copyright (c) 2008-2026 Jonathan Schleifer <js@nil.im>
*
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3.0 only,
* as published by the Free Software Foundation.
*
* 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 Lesser General Public License
* version 3.0 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3.0 along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <errno.h>
#import "OFApplication.h"
#import "OFArray.h"
#import "OFDate.h"
#import "OFFile.h"
#import "OFFileManager.h"
#import "OFIRI.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFPair.h"
#import "OFSet.h"
#import "OFStdIOStream.h"
#import "OFString.h"
#import "LHAArchive.h"
#import "OFArc.h"
#import "OFSetItemAttributesFailedException.h"
static OFArc *app;
static OFString *
indent(OFString *string)
{
return [string stringByReplacingOccurrencesOfString: @"\n"
withString: @"\n\t"];
}
static void
setPermissions(OFString *path, OFLHAArchiveEntry *entry)
{
[app quarantineFile: path];
#ifdef OF_FILE_MANAGER_SUPPORTS_PERMISSIONS
OFNumber *POSIXPermissions = entry.POSIXPermissions;
if (POSIXPermissions != nil) {
OFFileAttributes attributes;
POSIXPermissions = [OFNumber numberWithUnsignedShort:
POSIXPermissions.unsignedShortValue & 0777];
attributes = [OFDictionary
dictionaryWithObject: POSIXPermissions
forKey: OFFilePOSIXPermissions];
[[OFFileManager defaultManager] setAttributes: attributes
ofItemAtPath: path];
}
#endif
}
static void
setModificationDate(OFString *path, OFLHAArchiveEntry *entry)
{
OFFileAttributes attributes = [OFDictionary
dictionaryWithObject: entry.modificationDate
forKey: OFFileModificationDate];
@try {
[[OFFileManager defaultManager] setAttributes: attributes
ofItemAtPath: path];
} @catch (OFSetItemAttributesFailedException *e) {
if (e.errNo != EISDIR)
@throw e;
}
}
@implementation LHAArchive
+ (void)initialize
{
if (self == [LHAArchive class])
app = (OFArc *)[OFApplication sharedApplication].delegate;
}
+ (instancetype)archiveWithIRI: (OFIRI *)IRI
stream: (OF_KINDOF(OFStream *))stream
mode: (OFString *)mode
encoding: (OFStringEncoding)encoding
{
return objc_autoreleaseReturnValue(
[[self alloc] initWithIRI: IRI
stream: stream
mode: mode
encoding: encoding]);
}
- (instancetype)initWithIRI: (OFIRI *)IRI
stream: (OF_KINDOF(OFStream *))stream
mode: (OFString *)mode
encoding: (OFStringEncoding)encoding
{
self = [super init];
@try {
_archive = [[OFLHAArchive alloc] initWithStream: stream
mode: mode];
if (encoding != OFStringEncodingAutodetect)
_archive.encoding = encoding;
} @catch (id e) {
objc_release(self);
@throw e;
}
return self;
}
- (void)dealloc
{
objc_release(_archive);
[super dealloc];
}
- (void)listFiles
{
OFLHAArchiveEntry *entry;
while ((entry = [_archive nextEntry]) != nil) {
void *pool = objc_autoreleasePoolPush();
[app checkForCancellation];
[OFStdOut writeLine: entry.fileName];
if (app->_outputLevel >= 1) {
OFString *modificationDate = [entry.modificationDate
localDateStringWithFormat: @"%Y-%m-%d %H:%M:%S"];
OFString *compressedSize = [OFString stringWithFormat:
@"%llu", entry.compressedSize];
OFString *uncompressedSize = [OFString stringWithFormat:
@"%llu", entry.uncompressedSize];
OFString *CRC16 = [OFString stringWithFormat:
@"%04" PRIX16, entry.CRC16];
[OFStdOut writeString: @"\t"];
[OFStdOut writeLine: OF_LOCALIZED(
@"list_compressed_size",
@"["
@" 'Compressed: ',"
@" ["
@" {'size == 1': '1 byte'},"
@" {'': '%[size] bytes'}"
@" ]"
@"]".objectByParsingJSON,
@"size", compressedSize)];
[OFStdOut writeString: @"\t"];
[OFStdOut writeLine: OF_LOCALIZED(
@"list_uncompressed_size",
@"["
@" 'Uncompressed: ',"
@" ["
@" {'size == 1': '1 byte'},"
@" {'': '%[size] bytes'}"
@" ]"
@"]".objectByParsingJSON,
@"size", uncompressedSize)];
[OFStdOut writeString: @"\t"];
[OFStdOut writeLine: OF_LOCALIZED(
@"list_compression_method",
@"Compression method: %[method]",
@"method", entry.compressionMethod)];
[OFStdOut writeString: @"\t"];
[OFStdOut writeLine: OF_LOCALIZED(@"list_crc16",
@"CRC16: %[crc16]",
@"crc16", CRC16)];
[OFStdOut writeString: @"\t"];
[OFStdOut writeLine: OF_LOCALIZED(
@"list_modification_date",
@"Modification date: %[date]",
@"date", modificationDate)];
if (entry.POSIXPermissions != nil) {
OFString *permissionsString = [OFString
stringWithFormat: @"%llo",
entry.POSIXPermissions
.unsignedLongLongValue];
[OFStdOut writeString: @"\t"];
[OFStdOut writeLine: OF_LOCALIZED(
@"list_posix_permissions",
@"POSIX permissions: %[perm]",
@"perm", permissionsString)];
}
if (entry.ownerAccountID != nil) {
[OFStdOut writeString: @"\t"];
[OFStdOut writeLine: OF_LOCALIZED(
@"list_owner_account_id",
@"Owner account ID: %[id]",
@"id", entry.ownerAccountID)];
}
if (entry.groupOwnerAccountID != nil) {
[OFStdOut writeString: @"\t"];
[OFStdOut writeLine: OF_LOCALIZED(@"list_gid",
@"Group owner account ID: %[id]",
@"id", entry.groupOwnerAccountID)];
}
if (entry.ownerAccountName != nil) {
[OFStdOut writeString: @"\t"];
[OFStdOut writeLine: OF_LOCALIZED(
@"list_owner_account_name",
@"Owner account name: %[name]",
@"name", entry.ownerAccountName)];
}
if (entry.groupOwnerAccountName != nil) {
[OFStdOut writeString: @"\t"];
[OFStdOut writeLine: OF_LOCALIZED(
@"list_group_owner_account_name",
@"Group: %[name]",
@"name", entry.groupOwnerAccountName)];
}
if (app->_outputLevel >= 2) {
OFString *headerLevel = [OFString
stringWithFormat: @"%" PRIu8,
entry.headerLevel];
[OFStdOut writeString: @"\t"];
[OFStdOut writeLine: OF_LOCALIZED(
@"list_header_level",
@"Header level: %[level]",
@"level", headerLevel)];
if (entry.operatingSystemIdentifier != '\0') {
OFString *OSID =
[OFString stringWithFormat: @"%c",
entry.operatingSystemIdentifier];
[OFStdOut writeString: @"\t"];
[OFStdOut writeLine: OF_LOCALIZED(
@"list_osid",
@"Operating system identifier: "
@"%[osid]",
@"osid", OSID)];
}
}
if (app->_outputLevel >= 3) {
OFString *extensions =
indent(entry.extensions.description);
[OFStdOut writeString: @"\t"];
[OFStdOut writeLine: OF_LOCALIZED(
@"list_extensions",
@"Extensions: %[extensions]",
@"extensions", extensions)];
}
}
objc_autoreleasePoolPop(pool);
}
}
- (void)extractFiles: (OFArray OF_GENERIC(OFString *) *)files
{
OFFileManager *fileManager = [OFFileManager defaultManager];
bool all = (files.count == 0);
OFMutableArray *delayed = [OFMutableArray array];
OFMutableSet OF_GENERIC(OFString *) *missing =
[OFMutableSet setWithArray: files];
OFLHAArchiveEntry *entry;
while ((entry = [_archive nextEntry]) != nil) {
void *pool = objc_autoreleasePoolPush();
OFString *fileName = entry.fileName;
OFString *outFileName, *directory;
OFFile *output;
OFStream *stream;
unsigned long long written = 0, size = entry.uncompressedSize;
int8_t percent = -1, newPercent;
[app checkForCancellation];
if (!all && ![files containsObject: fileName])
continue;
[missing removeObject: fileName];
outFileName = [app safeLocalPathForPath: fileName];
if (outFileName == nil) {
[OFStdErr writeLine: OF_LOCALIZED(
@"refusing_to_extract_file",
@"Refusing to extract %[file]!",
@"file", fileName)];
app->_exitStatus = 1;
goto outer_loop_end;
}
if (app->_outputLevel >= 0)
[OFStdErr writeString: OF_LOCALIZED(@"extracting_file",
@"Extracting %[file]...",
@"file", fileName)];
if ([fileName hasSuffix: @"/"]) {
[fileManager createDirectoryAtPath: outFileName
createParents: true];
/*
* As creating a new file in a directory changes its
* modification date, we can only set it once all files
* have been created. Also, restricting permissions
* (e.g. removing write permissions) before all files
* in a directory have been written would also fail.
*/
[delayed addObject:
[OFPair pairWithFirstObject: outFileName
secondObject: entry]];
if (app->_outputLevel >= 0) {
[OFStdErr writeString: @"\r"];
[OFStdErr writeLine: OF_LOCALIZED(
@"extracting_file_done",
@"Extracting %[file]... done",
@"file", fileName)];
}
goto outer_loop_end;
}
directory = outFileName.stringByDeletingLastPathComponent;
if (![fileManager directoryExistsAtPath: directory])
[fileManager createDirectoryAtPath: directory
createParents: true];
if (![app shouldExtractFile: fileName outFileName: outFileName])
goto outer_loop_end;
stream = [_archive streamForReadingCurrentEntry];
output = [OFFile fileWithPath: outFileName mode: @"w"];
setPermissions(outFileName, entry);
while (!stream.atEndOfStream) {
ssize_t length;
[app checkForCancellation];
length = [app copyBlockFromStream: stream
toStream: output
fileName: fileName];
if (length < 0) {
app->_exitStatus = 1;
goto outer_loop_end;
}
written += length;
newPercent = (written == size
? 100 : (int8_t)(written * 100 / size));
if (app->_outputLevel >= 0 && percent != newPercent) {
OFString *percentString;
percent = newPercent;
percentString = [OFString stringWithFormat:
@"%3u", percent];
[OFStdErr writeString: @"\r"];
[OFStdErr writeString: OF_LOCALIZED(
@"extracting_file_percent",
@"Extracting %[file]... %[percent]%",
@"file", fileName,
@"percent", percentString)];
}
}
[output close];
setModificationDate(outFileName, entry);
if (app->_outputLevel >= 0) {
[OFStdErr writeString: @"\r"];
[OFStdErr writeLine: OF_LOCALIZED(
@"extracting_file_done",
@"Extracting %[file]... done",
@"file", fileName)];
}
outer_loop_end:
objc_autoreleasePoolPop(pool);
}
for (OFPair *pair in delayed) {
setModificationDate(pair.firstObject, pair.secondObject);
setPermissions(pair.firstObject, pair.secondObject);
}
if (missing.count > 0) {
for (OFString *file in missing)
[OFStdErr writeLine: OF_LOCALIZED(
@"file_not_in_archive",
@"File %[file] is not in the archive!",
@"file", file)];
app->_exitStatus = 1;
}
}
- (void)printFiles: (OFArray OF_GENERIC(OFString *) *)files_
{
OFMutableSet *files;
OFLHAArchiveEntry *entry;
if (files_.count < 1) {
[OFStdErr writeLine: OF_LOCALIZED(@"print_no_file_specified",
@"Need one or more files to print!")];
app->_exitStatus = 1;
return;
}
files = [OFMutableSet setWithArray: files_];
while ((entry = [_archive nextEntry]) != nil) {
OFString *fileName = entry.fileName;
OFStream *stream;
[app checkForCancellation];
if (![files containsObject: fileName])
continue;
stream = [_archive streamForReadingCurrentEntry];
while (!stream.atEndOfStream) {
ssize_t length;
[app checkForCancellation];
length = [app copyBlockFromStream: stream
toStream: OFStdOut
fileName: fileName];
if (length < 0) {
app->_exitStatus = 1;
return;
}
}
[files removeObject: fileName];
[stream close];
if (files.count == 0)
break;
}
for (OFString *file in files) {
[OFStdErr writeLine: OF_LOCALIZED(@"file_not_in_archive",
@"File %[file] is not in the archive!",
@"file", file)];
app->_exitStatus = 1;
}
}
- (void)addFiles: (OFArray OF_GENERIC(OFString *) *)files
archiveComment: (OFString *)archiveComment
{
OFFileManager *fileManager = [OFFileManager defaultManager];
for (OFString *fileName in files) {
void *pool = objc_autoreleasePoolPush();
OFFileAttributes attributes;
OFFileAttributeType type;
OFMutableLHAArchiveEntry *entry;
OFStream *output;
[app checkForCancellation];
if (app->_outputLevel >= 0)
[OFStdErr writeString: OF_LOCALIZED(@"adding_file",
@"Adding %[file]...",
@"file", fileName)];
attributes = [fileManager attributesOfItemAtPath: fileName];
type = attributes.fileType;
entry = [OFMutableLHAArchiveEntry entryWithFileName: fileName];
#ifdef OF_FILE_MANAGER_SUPPORTS_PERMISSIONS
entry.POSIXPermissions =
[attributes objectForKey: OFFilePOSIXPermissions];
#endif
entry.modificationDate = attributes.fileModificationDate;
#ifdef OF_FILE_MANAGER_SUPPORTS_OWNER
entry.ownerAccountID =
[attributes objectForKey: OFFileOwnerAccountID];
entry.groupOwnerAccountID =
[attributes objectForKey: OFFileGroupOwnerAccountID];
entry.ownerAccountName =
[attributes objectForKey: OFFileOwnerAccountName];
entry.groupOwnerAccountName =
[attributes objectForKey: OFFileGroupOwnerAccountName];
#endif
if ([type isEqual: OFFileTypeDirectory]) {
entry.compressionMethod = @"-lhd-";
if (![entry.fileName hasSuffix: @"/"])
entry.fileName = [entry.fileName
stringByAppendingString: @"/"];
}
output = [_archive streamForWritingEntry: entry];
if ([type isEqual: OFFileTypeRegular]) {
unsigned long long written = 0;
unsigned long long size = attributes.fileSize;
int8_t percent = -1, newPercent;
OFFile *input = [OFFile fileWithPath: fileName
mode: @"r"];
while (!input.atEndOfStream) {
ssize_t length;
[app checkForCancellation];
length = [app copyBlockFromStream: input
toStream: output
fileName: fileName];
if (length < 0) {
app->_exitStatus = 1;
goto outer_loop_end;
}
written += length;
newPercent = (written == size
? 100 : (int8_t)(written * 100 / size));
if (app->_outputLevel >= 0 &&
percent != newPercent) {
OFString *percentString;
percent = newPercent;
percentString = [OFString
stringWithFormat: @"%3u", percent];
[OFStdErr writeString: @"\r"];
[OFStdErr writeString: OF_LOCALIZED(
@"adding_file_percent",
@"Adding %[file]... %[percent]%",
@"file", fileName,
@"percent", percentString)];
}
}
}
if (app->_outputLevel >= 0) {
[OFStdErr writeString: @"\r"];
[OFStdErr writeLine: OF_LOCALIZED(
@"adding_file_done",
@"Adding %[file]... done",
@"file", fileName)];
}
[output close];
outer_loop_end:
objc_autoreleasePoolPop(pool);
}
[_archive close];
}
@end
|