ObjFW  Check-in [370e91ddce]

Overview
Comment:ofarc: Add --iri option
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 370e91ddce5d2af90469e97d5cde1c28dde5417dce75431e6b657844bd2c4b0b
User & Date: js on 2024-03-03 23:33:01
Other Links: manifest | tags
Context
2024-03-03
23:34
OFFileIRIHandler: Correctly transform exception check-in: b66e682fc6 user: js tags: trunk
23:33
ofarc: Add --iri option check-in: 370e91ddce user: js tags: trunk
20:45
Fix build for AmigaOS check-in: 75a8f74494 user: js tags: trunk
Changes

Modified utils/ofarc/Archive.h from [7732f848c0] to [b06ac0d4c3].

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
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







-
+
-



+
+
+
+

-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+








 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFObject.h"
#import "OFFile.h"
#import "OFString.h"
#import "OFArray.h"

OF_ASSUME_NONNULL_BEGIN

@class OFArray OF_GENERIC(ObjectType);
@class OFIRI;
@class OFStream;

@protocol Archive <OFObject>
+ (instancetype)archiveWithPath: (nullable OFString *)path
			 stream: (OF_KINDOF(OFStream *))stream
			   mode: (OFString *)mode
		       encoding: (OFStringEncoding)encoding;
- (instancetype)initWithPath: (nullable OFString *)path
		      stream: (OF_KINDOF(OFStream *))stream
			mode: (OFString *)mode
		    encoding: (OFStringEncoding)encoding;
+ (instancetype)archiveWithIRI: (nullable OFIRI *)IRI
			stream: (OF_KINDOF(OFStream *))stream
			  mode: (OFString *)mode
		      encoding: (OFStringEncoding)encoding;
- (instancetype)initWithIRI: (nullable OFIRI *)IRI
		     stream: (OF_KINDOF(OFStream *))stream
		       mode: (OFString *)mode
		   encoding: (OFStringEncoding)encoding;
- (void)listFiles;
- (void)extractFiles: (OFArray OF_GENERIC(OFString *) *)files;
- (void)printFiles: (OFArray OF_GENERIC(OFString *) *)files;
@optional
- (void)addFiles: (OFArray OF_GENERIC(OFString *) *)files;
@end

OF_ASSUME_NONNULL_END

Modified utils/ofarc/GZIPArchive.h from [d84167dde2] to [f6a342f9a7].

16
17
18
19
20
21
22

23
24
16
17
18
19
20
21
22
23
24
25







+


#import "OFGZIPStream.h"

#import "Archive.h"

@interface GZIPArchive: OFObject <Archive>
{
	OFGZIPStream *_stream;
	OFIRI *_archiveIRI;
}
@end

Modified utils/ofarc/GZIPArchive.m from [ae6bbc1f46] to [8abaeb7cdd].

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
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







+
+

-
+

+







-
+




-
+







 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFApplication.h"
#import "OFArray.h"
#import "OFFile.h"
#import "OFFileManager.h"
#import "OFStdIOStream.h"
#import "OFIRI.h"
#import "OFLocale.h"
#import "OFStdIOStream.h"

#import "GZIPArchive.h"
#import "OFArc.h"

static OFArc *app;

static void
setPermissions(OFString *destination, OFString *source)
setPermissions(OFString *destination, OFIRI *source)
{
#ifdef OF_FILE_MANAGER_SUPPORTS_PERMISSIONS
	OFFileManager *fileManager = [OFFileManager defaultManager];
	OFFileAttributes attributes = [fileManager
	    attributesOfItemAtPath: source];
	    attributesOfItemAtIRI: source];
	OFFileAttributeKey key = OFFilePOSIXPermissions;
	OFFileAttributes destinationAttributes = [OFDictionary
	    dictionaryWithObject: [attributes objectForKey: key]
			  forKey: key];

	[fileManager setAttributes: destinationAttributes
		      ofItemAtPath: destination];
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
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







-
-
-
-
+
+
+
+

-
-
-
-
+
+
+
+


-
-
-
-
+
+
+
+






+











+







@implementation GZIPArchive
+ (void)initialize
{
	if (self == [GZIPArchive class])
		app = (OFArc *)[OFApplication sharedApplication].delegate;
}

+ (instancetype)archiveWithPath: (OFString *)path
			 stream: (OF_KINDOF(OFStream *))stream
			   mode: (OFString *)mode
		       encoding: (OFStringEncoding)encoding
+ (instancetype)archiveWithIRI: (OFIRI *)IRI
			stream: (OF_KINDOF(OFStream *))stream
			  mode: (OFString *)mode
		      encoding: (OFStringEncoding)encoding
{
	return [[[self alloc] initWithPath: path
				    stream: stream
				      mode: mode
				  encoding: encoding] autorelease];
	return [[[self alloc] initWithIRI: IRI
				   stream: stream
				     mode: mode
				 encoding: encoding] autorelease];
}

- (instancetype)initWithPath: (OFString *)path
		      stream: (OF_KINDOF(OFStream *))stream
			mode: (OFString *)mode
		    encoding: (OFStringEncoding)encoding
- (instancetype)initWithIRI: (OFIRI *)IRI
		     stream: (OF_KINDOF(OFStream *))stream
		       mode: (OFString *)mode
		   encoding: (OFStringEncoding)encoding
{
	self = [super init];

	@try {
		_stream = [[OFGZIPStream alloc] initWithStream: stream
							  mode: mode];
		_archiveIRI = [IRI copy];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_stream release];
	[_archiveIRI release];

	[super dealloc];
}

- (void)listFiles
{
	[OFStdErr writeLine: OF_LOCALIZED(@"cannot_list_gz",
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
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







-
-
+
+










-
+







		[OFStdErr writeLine: OF_LOCALIZED(
		    @"cannot_extract_specific_file_from_gz",
		    @"Cannot extract a specific file of a .gz archive!")];
		app->_exitStatus = 1;
		return;
	}

	fileName = app->_archivePath.lastPathComponent
	    .stringByDeletingPathExtension;
	/* FIXME: Should use IRI-specific path extension deletion. */
	fileName = _archiveIRI.lastPathComponent.stringByDeletingPathExtension;

	if (app->_outputLevel >= 0)
		[OFStdOut writeString: OF_LOCALIZED(@"extracting_file",
		    @"Extracting %[file]...",
		    @"file", fileName)];

	if (![app shouldExtractFile: fileName outFileName: fileName])
		return;

	output = [OFFile fileWithPath: fileName mode: @"w"];
	setPermissions(fileName, app->_archivePath);
	setPermissions(fileName, _archiveIRI);

	while (!_stream.atEndOfStream) {
		ssize_t length = [app copyBlockFromStream: _stream
						 toStream: output
						 fileName: fileName];

		if (length < 0) {
155
156
157
158
159
160
161

162

163
164
165
166
167
168
169
160
161
162
163
164
165
166
167

168
169
170
171
172
173
174
175







+
-
+







		    @"Extracting %[file]... done",
		    @"file", fileName)];
	}
}

- (void)printFiles: (OFArray OF_GENERIC(OFString *) *)files
{
	/* FIXME: Should use IRI-specific path extension deletion. */
	OFString *fileName = app->_archivePath.lastPathComponent
	OFString *fileName = _archiveIRI.lastPathComponent
	    .stringByDeletingPathExtension;

	if (files.count > 0) {
		[OFStdErr writeLine: OF_LOCALIZED(
		    @"cannot_print_specific_file_from_gz",
		    @"Cannot print a specific file of a .gz archive!")];
		app->_exitStatus = 1;

Modified utils/ofarc/LHAArchive.m from [86289cab8e] to [5d3bfe8dcb].

16
17
18
19
20
21
22

23

24
25
26
27
28
29
30
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32







+

+







#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"

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
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







-
-
-
-
+
+
+
+

-
-
-
-
+
+
+
+


-
-
-
-
+
+
+
+







@implementation LHAArchive
+ (void)initialize
{
	if (self == [LHAArchive class])
		app = (OFArc *)[OFApplication sharedApplication].delegate;
}

+ (instancetype)archiveWithPath: (OFString *)path
			 stream: (OF_KINDOF(OFStream *))stream
			   mode: (OFString *)mode
		       encoding: (OFStringEncoding)encoding
+ (instancetype)archiveWithIRI: (OFIRI *)IRI
			stream: (OF_KINDOF(OFStream *))stream
			  mode: (OFString *)mode
		      encoding: (OFStringEncoding)encoding
{
	return [[[self alloc] initWithPath: path
				    stream: stream
				      mode: mode
				  encoding: encoding] autorelease];
	return [[[self alloc] initWithIRI: IRI
				   stream: stream
				     mode: mode
				 encoding: encoding] autorelease];
}

- (instancetype)initWithPath: (OFString *)path
		      stream: (OF_KINDOF(OFStream *))stream
			mode: (OFString *)mode
		    encoding: (OFStringEncoding)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];

Modified utils/ofarc/OFArc.h from [919560b647] to [3694789001].

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
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







+
+













-



-
-
-
-
+
+
+
+










#import "OFObject.h"
#import "OFString.h"

#import "Archive.h"

OF_ASSUME_NONNULL_BEGIN

@class OFIRI;

#ifndef S_IRWXG
# define S_IRWXG 0
#endif
#ifndef S_IRWXO
# define S_IRWXO 0
#endif

@interface OFArc: OFObject <OFApplicationDelegate>
{
	int8_t _overwrite;
@public
	int8_t _outputLevel;
	OFString *_archivePath;
	int _exitStatus;
}

- (id <Archive>)openArchiveWithPath: (OFString *)path
			       type: (OFString *)type
			       mode: (char)mode
			   encoding: (OFStringEncoding)encoding;
- (id <Archive>)openArchiveWithIRI: (nullable OFIRI *)IRI
			      type: (OFString *)type
			      mode: (char)mode
			  encoding: (OFStringEncoding)encoding;
- (bool)shouldExtractFile: (OFString *)fileName
	      outFileName: (OFString *)outFileName;
- (ssize_t)copyBlockFromStream: (OFStream *)input
		      toStream: (OFStream *)output
		      fileName: (OFString *)fileName;
- (nullable OFString *)safeLocalPathForPath: (OFString *)path;
@end

OF_ASSUME_NONNULL_END

Modified utils/ofarc/OFArc.m from [4801a33bf2] to [487eb6d138].

18
19
20
21
22
23
24

25
26
27
28
29
30
31
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32







+







#include <string.h>

#import "OFApplication.h"
#import "OFArray.h"
#import "OFFile.h"
#import "OFFileManager.h"
#import "OFIRI.h"
#import "OFIRIHandler.h"
#import "OFLocale.h"
#import "OFOptionsParser.h"
#import "OFSandbox.h"
#import "OFStdIOStream.h"

#import "OFArc.h"
#import "GZIPArchive.h"
62
63
64
65
66
67
68

69
70
71
72
73
74
75
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77







+







		    @"    -c  --create      Create archive\n"
		    @"    -C  --directory=  Extract into the specified "
		    @"directory\n"
		    @"    -E  --encoding=   The encoding used by the archive\n"
		    @"                      (only tar, lha and zoo files)\n"
		    @"    -f  --force       Force / overwrite files\n"
		    @"    -h  --help        Show this help\n"
		    @"        --iri         Use an IRI to access the archive\n"
		    @"    -l  --list        List all files in the archive\n"
		    @"    -n  --no-clobber  Never overwrite files\n"
		    @"    -p  --print       Print one or more files from the "
		    @"archive\n"
		    @"    -q  --quiet       Quiet mode (no output, except "
		    @"errors)\n"
		    @"    -t  --type=       Archive type (gz, lha, tar, tgz, "
96
97
98
99
100
101
102












103
104
105
106
107
108
109
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







+
+
+
+
+
+
+
+
+
+
+
+







	    @"are mutually exclusive!",
	    @"shortopt1", shortOption1Str,
	    @"longopt1", longOption1,
	    @"shortopt2", shortOption2Str,
	    @"longopt2", longOption2)];
	[OFApplication terminateWithStatus: 1];
}

static OFIRI *
argumentToIRI(OFString *argument, bool isIRI)
{
	if (isIRI)
		return [OFIRI IRIWithString: argument];

	if ([argument isEqual: @"-"])
		return nil;

	return [OFIRI fileIRIWithPath: argument];
}

static void
mutuallyExclusiveError5(OFUnichar shortOption1, OFString *longOption1,
    OFUnichar shortOption2, OFString *longOption2,
    OFUnichar shortOption3, OFString *longOption3,
    OFUnichar shortOption4, OFString *longOption4,
    OFUnichar shortOption5, OFString *longOption5)
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
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







+







+













+







	[archive addFiles: expandedFiles];
}

@implementation OFArc
- (void)applicationDidFinishLaunching: (OFNotification *)notification
{
	OFString *outputDir, *encodingString, *type;
	bool isIRI;
	const OFOptionsParserOption options[] = {
		{ 'a', @"append", 0, NULL, NULL },
		{ 'c', @"create", 0, NULL, NULL },
		{ 'C', @"directory", 1, NULL, &outputDir },
		{ 'E', @"encoding", 1, NULL, &encodingString },
		{ 'f', @"force", 0, NULL, NULL },
		{ 'h', @"help", 0, NULL, NULL },
		{ 0,   @"iri", 0, &isIRI, NULL },
		{ 'l', @"list", 0, NULL, NULL },
		{ 'n', @"no-clobber", 0, NULL, NULL },
		{ 'p', @"print", 0, NULL, NULL },
		{ 'q', @"quiet", 0, NULL, NULL },
		{ 't', @"type", 1, NULL, &type },
		{ 'v', @"verbose", 0, NULL, NULL },
		{ 'x', @"extract", 0, NULL, NULL },
		{ '\0', nil, 0, NULL, NULL }
	};
	OFUnichar option, mode = '\0';
	OFStringEncoding encoding = OFStringEncodingAutodetect;
	OFOptionsParser *optionsParser;
	OFArray OF_GENERIC(OFString *) *remainingArguments, *files;
	OFIRI *IRI;
	id <Archive> archive;

#ifdef OF_HAVE_SANDBOX
	OFSandbox *sandbox = [OFSandbox sandbox];
	sandbox.allowsStdIO = true;
	sandbox.allowsReadingFiles = true;
	sandbox.allowsWritingFiles = true;
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
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







+




-
-
+
+









-
+
-
-
-
-
+
+
+







+
+

-
-
+
+






-
+
-
-
-
-
+
+
+







+
+
+
+

-
-
+
+






-
-
-
-
+
-
-
-
-
+
+
+







+




-
-
+
+




















-
+
-
-
-
-
+
+
+















-
-
-





-
+


-
-
-





-
+












-
-
-
-
+
+
+
+


-




-
-
-
-
-
-


















-
+













-
-


+
-
+

-
-
-




-
-
+
+





-
+








	switch (mode) {
	case 'a':
	case 'c':
		if (remainingArguments.count < 1)
			help(OFStdErr, false, 1);

		IRI = argumentToIRI(remainingArguments.firstObject, isIRI);
		files = [remainingArguments objectsInRange:
		    OFMakeRange(1, remainingArguments.count - 1)];

#ifdef OF_HAVE_SANDBOX
		if (![remainingArguments.firstObject isEqual: @"-"])
			[sandbox unveilPath: remainingArguments.firstObject
		if ([IRI.scheme isEqual: @"file"])
			[sandbox unveilPath: IRI.path
				permissions: (mode == 'a' ? @"rwc" : @"wc")];

		for (OFString *path in files)
			[sandbox unveilPath: path permissions: @"r"];

		sandbox.allowsUnveil = false;
		[OFApplication of_activateSandbox: sandbox];
#endif

		archive = [self
		archive = [self openArchiveWithIRI: IRI
		    openArchiveWithPath: remainingArguments.firstObject
				   type: type
				   mode: mode
			       encoding: encoding];
					      type: type
					      mode: mode
					  encoding: encoding];

		addFiles(archive, files);
		break;
	case 'l':
		if (remainingArguments.count != 1)
			help(OFStdErr, false, 1);

		IRI = argumentToIRI(remainingArguments.firstObject, isIRI);

#ifdef OF_HAVE_SANDBOX
		if (![remainingArguments.firstObject isEqual: @"-"])
			[sandbox unveilPath: remainingArguments.firstObject
		if ([IRI.scheme isEqual: @"file"])
			[sandbox unveilPath: IRI.path
				permissions: @"r"];

		sandbox.allowsUnveil = false;
		[OFApplication of_activateSandbox: sandbox];
#endif

		archive = [self
		archive = [self openArchiveWithIRI: IRI
		    openArchiveWithPath: remainingArguments.firstObject
				   type: type
				   mode: mode
			       encoding: encoding];
					      type: type
					      mode: mode
					  encoding: encoding];

		[archive listFiles];
		break;
	case 'p':
		if (remainingArguments.count < 1)
			help(OFStdErr, false, 1);

		IRI = argumentToIRI(remainingArguments.firstObject, isIRI);
		files = [remainingArguments objectsInRange:
		    OFMakeRange(1, remainingArguments.count - 1)];

#ifdef OF_HAVE_SANDBOX
		if (![remainingArguments.firstObject isEqual: @"-"])
			[sandbox unveilPath: remainingArguments.firstObject
		if ([IRI.scheme isEqual: @"file"])
			[sandbox unveilPath: IRI.path
				permissions: @"r"];

		sandbox.allowsUnveil = false;
		[OFApplication of_activateSandbox: sandbox];
#endif

		files = [remainingArguments objectsInRange:
		    OFMakeRange(1, remainingArguments.count - 1)];

		archive = [self
		archive = [self openArchiveWithIRI: IRI
		    openArchiveWithPath: remainingArguments.firstObject
				   type: type
				   mode: mode
			       encoding: encoding];
					      type: type
					      mode: mode
					  encoding: encoding];

		[archive printFiles: files];
		break;
	case 'x':
		if (remainingArguments.count < 1)
			help(OFStdErr, false, 1);

		IRI = argumentToIRI(remainingArguments.firstObject, isIRI);
		files = [remainingArguments objectsInRange:
		    OFMakeRange(1, remainingArguments.count - 1)];

#ifdef OF_HAVE_SANDBOX
		if (![remainingArguments.firstObject isEqual: @"-"])
			[sandbox unveilPath: remainingArguments.firstObject
		if ([IRI.scheme isEqual: @"file"])
			[sandbox unveilPath: IRI.path
				permissions: @"r"];

		if (files.count > 0)
			for (OFString *path in files)
				[sandbox unveilPath: path permissions: @"wc"];
		else {
			OFString *path = outputDir;

			if (path == nil)
				path = [[OFFileManager defaultManager]
				    currentDirectoryPath];

			/* We need 'r' to change the directory to it. */
			[sandbox unveilPath: path permissions: @"rwc"];
		}

		sandbox.allowsUnveil = false;
		[OFApplication of_activateSandbox: sandbox];
#endif

		archive = [self
		archive = [self openArchiveWithIRI: IRI
		    openArchiveWithPath: remainingArguments.firstObject
				   type: type
				   mode: mode
			       encoding: encoding];
					      type: type
					      mode: mode
					  encoding: encoding];

		if (outputDir != nil) {
			OFFileManager *fileManager =
			    [OFFileManager defaultManager];

			if (![fileManager directoryExistsAtPath: outputDir])
				[fileManager createDirectoryAtPath: outputDir
						     createParents: true];

			[fileManager changeCurrentDirectoryPath: outputDir];
		}

		@try {
			[archive extractFiles: files];
		} @catch (OFCreateDirectoryFailedException *e) {
			OFString *error = [OFString
			    stringWithCString: strerror(e.errNo)
				     encoding: [OFLocale encoding]];
			[OFStdErr writeString: @"\r"];
			[OFStdErr writeLine: OF_LOCALIZED(
			    @"failed_to_create_directory",
			    @"Failed to create directory %[dir]: %[error]",
			    @"dir", e.IRI.fileSystemRepresentation,
			    @"error", error)];
			    @"error", OFStrError(e.errNo))];
			_exitStatus = 1;
		} @catch (OFOpenItemFailedException *e) {
			OFString *error = [OFString
			    stringWithCString: strerror(e.errNo)
				     encoding: [OFLocale encoding]];
			[OFStdErr writeString: @"\r"];
			[OFStdErr writeLine: OF_LOCALIZED(
			    @"failed_to_open_file",
			    @"Failed to open file %[file]: %[error]",
			    @"file", e.path,
			    @"error", error)];
			    @"error", OFStrError(e.errNo))];
			_exitStatus = 1;
		}

		break;
	default:
		help(OFStdErr, true, 1);
		break;
	}

	[OFApplication terminateWithStatus: _exitStatus];
}

- (id <Archive>)openArchiveWithPath: (OFString *)path_
			       type: (OFString *)type
			       mode: (char)mode
			   encoding: (OFStringEncoding)encoding
- (id <Archive>)openArchiveWithIRI: (OFIRI *)IRI
			      type: (OFString *)type
			      mode: (char)mode
			  encoding: (OFStringEncoding)encoding
{
	/* To make clang-analyzer happy about assigning nil to path later. */
	OFString *path = path_;
	OFString *modeString, *fileModeString;
	OFStream *file = nil;
	id <Archive> archive = nil;

	[_archivePath release];
	_archivePath = [path copy];

	if (path == nil)
		return nil;

	switch (mode) {
	case 'a':
		modeString = @"a";
		fileModeString = @"r+";
		break;
	case 'c':
		modeString = @"w";
		fileModeString = @"w+";
		break;
	case 'l':
	case 'p':
	case 'x':
		modeString = fileModeString = @"r";
		break;
	default:
		@throw [OFInvalidArgumentException exception];
	}

	if ([path isEqual: @"-"]) {
	if (IRI == nil) {
		switch (mode) {
		case 'a':
		case 'c':
			file = OFStdOut;
			break;
		case 'l':
		case 'p':
		case 'x':
			file = OFStdIn;
			break;
		default:
			@throw [OFInvalidArgumentException exception];
		}

		path = nil;
	} else {
		@try {
			file = [OFIRIHandler openItemAtIRI: IRI
			file = [OFFile fileWithPath: path mode: fileModeString];
						      mode: fileModeString];
		} @catch (OFOpenItemFailedException *e) {
			OFString *error = [OFString
			    stringWithCString: strerror(e.errNo)
				     encoding: [OFLocale encoding]];
			[OFStdErr writeString: @"\r"];
			[OFStdErr writeLine: OF_LOCALIZED(
			    @"failed_to_open_file",
			    @"Failed to open file %[file]: %[error]",
			    @"file", e.path,
			    @"error", error)];
			    @"file", e.IRI.string,
			    @"error", OFStrError(e.errNo))];
			[OFApplication terminateWithStatus: 1];
		}
	}

	if (type == nil || [type isEqual: @"auto"]) {
		OFString *lowercasePath = path.lowercaseString;
		OFString *lowercasePath = IRI.path.lowercaseString;

		/* This one has to be first for obvious reasons */
		if ([lowercasePath hasSuffix: @".tar.gz"] ||
		    [lowercasePath hasSuffix: @".tgz"])
			type = @"tgz";
		else if ([lowercasePath hasSuffix: @".gz"])
			type = @"gz";
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
669
670
671
672
673
674
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
669







-
+
-
-
-
-
-
-
-
-
-
-



+
+
+
+
+
+
+
+
+
+




-
-
-
-
+
+
+
+

-
-
-
-
+
+
+
+

-
-
-
-
+
+
+
+
















-
-
-


-
-
+
+


-
-
-


-
-
+
+





-
+












-
-
+
+







			type = @"zoo";
		else
			type = @"zip";
	}

	@try {
		if ([type isEqual: @"gz"])
			archive = [GZIPArchive archiveWithPath: path
			archive = [GZIPArchive archiveWithIRI: IRI
							stream: file
							  mode: modeString
						      encoding: encoding];
		else if ([type isEqual: @"lha"])
			 archive = [LHAArchive archiveWithPath: path
							stream: file
							  mode: modeString
						      encoding: encoding];
		else if ([type isEqual: @"tar"])
			archive = [TarArchive archiveWithPath: path
						       stream: file
							 mode: modeString
						     encoding: encoding];
		else if ([type isEqual: @"lha"])
			 archive = [LHAArchive archiveWithIRI: IRI
						       stream: file
							 mode: modeString
						      encoding: encoding];
		else if ([type isEqual: @"tar"])
			archive = [TarArchive archiveWithIRI: IRI
						      stream: file
							mode: modeString
						    encoding: encoding];
		else if ([type isEqual: @"tgz"]) {
			OFStream *GZIPStream = [OFGZIPStream
			    streamWithStream: file
					mode: modeString];
			archive = [TarArchive archiveWithPath: path
						       stream: GZIPStream
							 mode: modeString
						     encoding: encoding];
			archive = [TarArchive archiveWithIRI: IRI
						      stream: GZIPStream
							mode: modeString
						    encoding: encoding];
		} else if ([type isEqual: @"zip"])
			archive = [ZIPArchive archiveWithPath: path
						       stream: file
							 mode: modeString
						     encoding: encoding];
			archive = [ZIPArchive archiveWithIRI: IRI
						      stream: file
							mode: modeString
						    encoding: encoding];
		else if ([type isEqual: @"zoo"])
			archive = [ZooArchive archiveWithPath: path
						       stream: file
							 mode: modeString
						     encoding: encoding];
			archive = [ZooArchive archiveWithIRI: IRI
						      stream: file
							mode: modeString
						    encoding: encoding];
		else {
			[OFStdErr writeLine: OF_LOCALIZED(
			    @"unknown_archive_type",
			    @"Unknown archive type: %[type]",
			    @"type", type)];
			goto error;
		}
	} @catch (OFNotImplementedException *e) {
		if ((mode == 'a' || mode == 'c') && sel_isEqual(e.selector,
		    @selector(initWithStream:mode:))) {
			writingNotSupported(type);
			goto error;
		}

		@throw e;
	} @catch (OFReadFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror(e.errNo)
			     encoding: [OFLocale encoding]];
		[OFStdErr writeLine: OF_LOCALIZED(@"failed_to_read_file",
		    @"Failed to read file %[file]: %[error]",
		    @"file", path,
		    @"error", error)];
		    @"file", IRI.string,
		    @"error", OFStrError(e.errNo))];
		goto error;
	} @catch (OFSeekFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror(e.errNo)
			     encoding: [OFLocale encoding]];
		[OFStdErr writeLine: OF_LOCALIZED(@"failed_to_seek_in_file",
		    @"Failed to seek in file %[file]: %[error]",
		    @"file", path,
		    @"error", error)];
		    @"file", IRI.string,
		    @"error", OFStrError(e.errNo))];
		goto error;
	} @catch (OFInvalidFormatException *e) {
		[OFStdErr writeLine: OF_LOCALIZED(
		    @"file_is_not_a_valid_archive",
		    @"File %[file] is not a valid archive!",
		    @"file", path)];
		    @"file", IRI.string)];
		goto error;
	}

	if ((mode == 'a' || mode == 'c') &&
	    ![archive respondsToSelector: @selector(addFiles:)]) {
		writingNotSupported(type);
		goto error;
	}

	return archive;

error:
	if (mode == 'c' && path != nil)
		[[OFFileManager defaultManager] removeItemAtPath: path];
	if (mode == 'c' && IRI != nil)
		[[OFFileManager defaultManager] removeItemAtIRI: IRI];

	[OFApplication terminateWithStatus: 1];
	abort();
}

- (bool)shouldExtractFile: (OFString *)fileName
	      outFileName: (OFString *)outFileName
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749

750
751
752
753
754
755
756
757
758
759
760
761
762
763

764
765
766
767
768
769
770
730
731
732
733
734
735
736



737
738
739
740

741
742
743
744
745
746
747



748
749
750
751

752
753
754
755
756
757
758
759







-
-
-




-
+






-
-
-




-
+







{
	char buffer[bufferSize];
	size_t length;

	@try {
		length = [input readIntoBuffer: buffer length: bufferSize];
	} @catch (OFReadFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror(e.errNo)
			     encoding: [OFLocale encoding]];
		[OFStdOut writeString: @"\r"];
		[OFStdErr writeLine: OF_LOCALIZED(@"failed_to_read_file",
		    @"Failed to read file %[file]: %[error]",
		    @"file", fileName,
		    @"error", error)];
		    @"error", OFStrError(e.errNo))];
		return -1;
	}

	@try {
		[output writeBuffer: buffer length: length];
	} @catch (OFWriteFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror(e.errNo)
			     encoding: [OFLocale encoding]];
		[OFStdOut writeString: @"\r"];
		[OFStdErr writeLine: OF_LOCALIZED(@"failed_to_write_file",
		    @"Failed to write file %[file]: %[error]",
		    @"file", fileName,
		    @"error", error)];
		    @"error", OFStrError(e.errNo))];
		return -1;
	}

	return length;
}

- (OFString *)safeLocalPathForPath: (OFString *)path

Modified utils/ofarc/TarArchive.m from [bcc24ae359] to [84b3855c99].

16
17
18
19
20
21
22

23
24
25
26
27
28
29
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30







+







#include "config.h"

#include <errno.h>

#import "OFApplication.h"
#import "OFArray.h"
#import "OFDate.h"
#import "OFFile.h"
#import "OFFileManager.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFPair.h"
#import "OFSet.h"
#import "OFStdIOStream.h"
#import "OFString.h"
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
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







-
-
-
-
+
+
+
+

-
-
-
-
+
+
+
+


-
-
-
-
+
+
+
+







@implementation TarArchive
+ (void)initialize
{
	if (self == [TarArchive class])
		app = (OFArc *)[OFApplication sharedApplication].delegate;
}

+ (instancetype)archiveWithPath: (OFString *)path
			 stream: (OF_KINDOF(OFStream *))stream
			   mode: (OFString *)mode
		       encoding: (OFStringEncoding)encoding
+ (instancetype)archiveWithIRI: (OFIRI *)IRI
			stream: (OF_KINDOF(OFStream *))stream
			  mode: (OFString *)mode
		      encoding: (OFStringEncoding)encoding
{
	return [[[self alloc] initWithPath: path
				    stream: stream
				      mode: mode
				  encoding: encoding] autorelease];
	return [[[self alloc] initWithIRI: IRI
				   stream: stream
				     mode: mode
				 encoding: encoding] autorelease];
}

- (instancetype)initWithPath: (OFString *)path
		      stream: (OF_KINDOF(OFStream *))stream
			mode: (OFString *)mode
		    encoding: (OFStringEncoding)encoding
- (instancetype)initWithIRI: (OFIRI *)IRI
		     stream: (OF_KINDOF(OFStream *))stream
		       mode: (OFString *)mode
		   encoding: (OFStringEncoding)encoding
{
	self = [super init];

	@try {
		_archive = [[OFTarArchive alloc] initWithStream: stream
							   mode: mode];

Modified utils/ofarc/ZIPArchive.h from [5707c10edc] to [60ff5cf688].

15
16
17
18
19
20
21
22

23
24
25
15
16
17
18
19
20
21

22
23
24
25







-
+




#import "OFZIPArchive.h"

#import "Archive.h"

@interface ZIPArchive: OFObject <OFZIPArchiveDelegate, Archive>
{
	OFString *_path;
	OFIRI *_archiveIRI;
	OFZIPArchive *_archive;
}
@end

Modified utils/ofarc/ZIPArchive.m from [9a0280ef0d] to [c2df60b68d].

17
18
19
20
21
22
23

24


25
26
27
28
29
30
31
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34







+

+
+








#include <errno.h>

#import "OFApplication.h"
#import "OFArray.h"
#import "OFData.h"
#import "OFDate.h"
#import "OFFile.h"
#import "OFFileManager.h"
#import "OFIRI.h"
#import "OFIRIHandler.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFPair.h"
#import "OFSet.h"
#import "OFStdIOStream.h"
#import "OFString.h"

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
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







-
-
-
-
+
+
+
+

-
-
-
-
+
+
+
+


-
-
-
-
+
+
+
+




-
+













-
+









-
+

-
+







-
-
-
+
+
+
+

+
+
-
-
+
+
+







@implementation ZIPArchive
+ (void)initialize
{
	if (self == [ZIPArchive class])
		app = (OFArc *)[OFApplication sharedApplication].delegate;
}

+ (instancetype)archiveWithPath: (OFString *)path
			 stream: (OF_KINDOF(OFStream *))stream
			   mode: (OFString *)mode
		       encoding: (OFStringEncoding)encoding
+ (instancetype)archiveWithIRI: (OFIRI *)IRI
			stream: (OF_KINDOF(OFStream *))stream
			  mode: (OFString *)mode
		      encoding: (OFStringEncoding)encoding
{
	return [[[self alloc] initWithPath: path
				    stream: stream
				      mode: mode
				  encoding: encoding] autorelease];
	return [[[self alloc] initWithIRI: IRI
				   stream: stream
				     mode: mode
				 encoding: encoding] autorelease];
}

- (instancetype)initWithPath: (OFString *)path
		      stream: (OF_KINDOF(OFStream *))stream
			mode: (OFString *)mode
		    encoding: (OFStringEncoding)encoding
- (instancetype)initWithIRI: (OFIRI *)IRI
		     stream: (OF_KINDOF(OFStream *))stream
		       mode: (OFString *)mode
		   encoding: (OFStringEncoding)encoding
{
	self = [super init];

	@try {
		_path = [path copy];
		_archiveIRI = [IRI copy];
		_archive = [[OFZIPArchive alloc] initWithStream: stream
							   mode: mode];
		_archive.delegate = self;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_path release];
	[_archiveIRI release];
	[_archive release];

	[super dealloc];
}

- (OFSeekableStream *)archive: (OFZIPArchive *)archive
	    wantsPartNumbered: (unsigned int)partNumber
	       lastPartNumber: (unsigned int)lastPartNumber
{
	OFString *path;
	OFIRI *IRI;

	if ([_path.pathExtension caseInsensitiveCompare: @"zip"] !=
	if ([_archiveIRI.path.pathExtension caseInsensitiveCompare: @"zip"] !=
	    OFOrderedSame)
		return nil;

	if (partNumber > 98)
		return nil;

	if (partNumber == lastPartNumber)
		path = _path;
	else
		path = [_path.stringByDeletingPathExtension
		IRI = _archiveIRI;
	else {
		OFMutableIRI *copy = [[_archiveIRI mutableCopy] autorelease];
		copy.path = [_archiveIRI.path.stringByDeletingPathExtension
		    stringByAppendingFormat: @".z%02u", partNumber + 1];
		[copy makeImmutable];
		IRI = copy;

	return [OFFile fileWithPath: path mode: @"r"];
	}

	return (OFSeekableStream *)[OFIRIHandler openItemAtIRI: IRI mode: @"r"];
}

- (void)listFiles
{
	for (OFZIPArchiveEntry *entry in _archive.entries) {
		void *pool = objc_autoreleasePoolPush();

Modified utils/ofarc/ZooArchive.m from [116b81ed76] to [7cdd686704].

14
15
16
17
18
19
20

21

22
23
24
25
26
27
28
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30







+

+







 */

#include "config.h"

#include <errno.h>

#import "OFApplication.h"
#import "OFArray.h"
#import "OFDate.h"
#import "OFFile.h"
#import "OFFileManager.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFSet.h"
#import "OFStdIOStream.h"
#import "OFString.h"

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
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







-
-
-
-
+
+
+
+

-
-
-
-
+
+
+
+


-
-
-
-
+
+
+
+







@implementation ZooArchive
+ (void)initialize
{
	if (self == [ZooArchive class])
		app = (OFArc *)[OFApplication sharedApplication].delegate;
}

+ (instancetype)archiveWithPath: (OFString *)path
			 stream: (OF_KINDOF(OFStream *))stream
			   mode: (OFString *)mode
		       encoding: (OFStringEncoding)encoding
+ (instancetype)archiveWithIRI: (OFIRI *)IRI
			stream: (OF_KINDOF(OFStream *))stream
			  mode: (OFString *)mode
		      encoding: (OFStringEncoding)encoding
{
	return [[[self alloc] initWithPath: path
				    stream: stream
				      mode: mode
				  encoding: encoding] autorelease];
	return [[[self alloc] initWithIRI: IRI
				   stream: stream
				     mode: mode
				 encoding: encoding] autorelease];
}

- (instancetype)initWithPath: (OFString *)path
		      stream: (OF_KINDOF(OFStream *))stream
			mode: (OFString *)mode
		    encoding: (OFStringEncoding)encoding
- (instancetype)initWithIRI: (OFIRI *)IRI
		     stream: (OF_KINDOF(OFStream *))stream
		       mode: (OFString *)mode
		   encoding: (OFStringEncoding)encoding
{
	self = [super init];

	@try {
		_archive = [[OFZooArchive alloc] initWithStream: stream
							   mode: mode];

Modified utils/ofarc/localization/de.json from [deba1f0d7e] to [306a0a1827].

18
19
20
21
22
23
24


25
26
27
28
29
30
31
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33







+
+







        "    -a  --append      Zu Archiv hinzufügen\n",
        "    -c  --create      Archiv erstellen\n",
        "    -C  --directory=  In angegebenes Verzeichnis entpacken\n",
        "    -E  --encoding=   Das Encoding des Archivs (nur tar-, ",
        "lha- und zoo-Dateien)\n",
        "    -f  --force       Existierende Dateien überschreiben\n",
        "    -h  --help        Diese Hilfe anzeigen\n",
        "        --iri         Eine IRI benutzen um auf das Archiv zuzugreifen",
        "\n",
        "    -l  --list        Alle Dateien im Archiv auflisten\n",
        "    -n  --no-clobber  Dateien niemals überschreiben\n",
        "    -p  --print       Eine oder mehr Dateien aus dem Archiv ausgeben",
        "\n",
        "    -q  --quiet       Ruhiger Modus (keine Ausgabe außer Fehler)\n",
        "    -t  --type=       Archiv-Typ (gz, lha, tar, tgz, zip, zoo)\n",
        "    -v  --verbose     Ausführlicher Modus für Datei-Liste\n",