ObjFW  Diff

Differences From Artifact [772087febb]:

To Artifact [8ac0b34839]:


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







+

+













-
+






+
+








#import "OFZIP.h"
#import "GZIPArchive.h"
#import "TarArchive.h"
#import "ZIPArchive.h"

#import "OFCreateDirectoryFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotImplementedException.h"
#import "OFOpenItemFailedException.h"
#import "OFReadFailedException.h"
#import "OFSeekFailedException.h"
#import "OFWriteFailedException.h"

#define BUFFER_SIZE 4096

OF_APPLICATION_DELEGATE(OFZIP)

static void
help(OFStream *stream, bool full, int status)
{
	[stream writeLine: OF_LOCALIZED(@"usage",
	    @"Usage: %[prog] -[Cfhlnpqtvx] archive.zip [file1 file2 ...]",
	    @"Usage: %[prog] -[acCfhlnpqtvx] archive.zip [file1 file2 ...]",
	    @"prog", [OFApplication programName])];

	if (full) {
		[stream writeString: @"\n"];
		[stream writeLine: OF_LOCALIZED(@"full_usage",
		    @"Options:\n"
		    @"    -a  --append      Append to archive\n"
		    @"    -c  --create      Create archive\n"
		    @"    -C  --directory   Extract into the specified "
		    @"directory\n"
		    @"    -f  --force       Force / overwrite files\n"
		    @"    -h  --help        Show this help\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 "
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
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







-
+

-
+
+
+







+
+
+
+

-
+

-
-
+
+
+





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








+
+







	    @"longopt1", longOption1,
	    @"shortopt2", shortOption2Str,
	    @"longopt2", longOption2)];
	[OFApplication terminateWithStatus: 1];
}

static void
mutuallyExclusiveError3(of_unichar_t shortOption1, OFString *longOption1,
mutuallyExclusiveError5(of_unichar_t shortOption1, OFString *longOption1,
    of_unichar_t shortOption2, OFString *longOption2,
    of_unichar_t shortOption3, OFString *longOption3)
    of_unichar_t shortOption3, OFString *longOption3,
    of_unichar_t shortOption4, OFString *longOption4,
    of_unichar_t shortOption5, OFString *longOption5)
{
	OFString *shortOption1Str = [OFString stringWithFormat: @"%C",
								shortOption1];
	OFString *shortOption2Str = [OFString stringWithFormat: @"%C",
								shortOption2];
	OFString *shortOption3Str = [OFString stringWithFormat: @"%C",
								shortOption3];
	OFString *shortOption4Str = [OFString stringWithFormat: @"%C",
								shortOption4];
	OFString *shortOption5Str = [OFString stringWithFormat: @"%C",
								shortOption5];

	[of_stderr writeLine: OF_LOCALIZED(@"3_options_mutually_exclusive",
	[of_stderr writeLine: OF_LOCALIZED(@"5_options_mutually_exclusive",
	    @"Error: -%[shortopt1] / --%[longopt1], "
	    @"-%[shortopt2] / --%[longopt2] and -%[shortopt3] / --%[longopt3] "
	    @"are mutually exclusive!",
	    @"-%[shortopt2] / --%[longopt2], -%[shortopt3] / --%[longopt3], "
	    @"-%[shortopt4] / --%[longopt4] and\n"
	    @"       -%[shortopt5] / --%[longopt5] are mutually exclusive!",
	    @"shortopt1", shortOption1Str,
	    @"longopt1", longOption1,
	    @"shortopt2", shortOption2Str,
	    @"longopt2", longOption2,
	    @"shortopt3", shortOption3Str,
	    @"longopt3", longOption3)];
	    @"longopt3", longOption3,
	    @"shortopt4", shortOption4Str,
	    @"longopt4", longOption4,
	    @"shortopt5", shortOption5Str,
	    @"longopt5", longOption5)];
	[OFApplication terminateWithStatus: 1];
}

static void
writingNotSupported(OFString *type)
{
	[of_stderr writeLine: OF_LOCALIZED(
	    @"writing_not_supported",
	    @"Writing archives of type %[type] is not (yet) supported!",
	    @"type", type)];
	[OFApplication terminateWithStatus: 1];
}

@implementation OFZIP
- (void)applicationDidFinishLaunching
{
	OFString *outputDir = nil, *type = nil;
	const of_options_parser_option_t options[] = {
		{ 'a', @"append", 0, NULL, NULL },
		{ 'c', @"create", 0, NULL, NULL },
		{ 'C', @"directory", 1, NULL, &outputDir },
		{ 'f', @"force", 0, NULL, NULL },
		{ 'h', @"help", 0, NULL, NULL },
		{ 'l', @"list", 0, NULL, NULL },
		{ 'n', @"no-clobber", 0, NULL, NULL },
		{ 'p', @"print", 0, NULL, NULL },
		{ 'q', @"quiet", 0, NULL, NULL },
187
188
189
190
191
192
193
194
195



196

197
198
199
200






201
202
203
204
205
206
207
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







-
-
+
+
+

+

-
-
-
+
+
+
+
+
+







		case 'q':
			if (_outputLevel > 0)
				mutuallyExclusiveError(
				    'q', @"quiet", 'v', @"verbose");

			_outputLevel--;
			break;
		case 'l':
		case 'x':
		case 'a':
		case 'c':
		case 'l':
		case 'p':
		case 'x':
			if (mode != '\0')
				mutuallyExclusiveError3(
				    'l', @"list", 'x', @"extract",
				    'p', @"print");
				mutuallyExclusiveError5(
				    'a', @"append",
				    'c', @"create",
				    'l', @"list",
				    'p', @"print",
				    'x', @"extract");

			mode = option;
			break;
		case 'h':
			help(of_stdout, true, 0);
			break;
		case '=':
233
234
235
236
237
238
239
240


241
242
243
244
245
246
247
265
266
267
268
269
270
271

272
273
274
275
276
277
278
279
280







-
+
+








			[OFApplication terminateWithStatus: 1];
		}
	}

	remainingArguments = [optionsParser remainingArguments];
	archive = [self openArchiveWithPath: [remainingArguments firstObject]
				       type: type];
				       type: type
				       mode: mode];

	if (outputDir != nil)
		[[OFFileManager defaultManager]
		    changeCurrentDirectoryPath: outputDir];

	switch (mode) {
	case 'l':
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
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







+

+








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



-
+







	}

	[OFApplication terminateWithStatus: _exitStatus];
}

- (id <Archive>)openArchiveWithPath: (OFString *)path
			       type: (OFString *)type
			       mode: (char)mode
{
	OFString *modeString, *fileModeString;
	OFFile *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 = fileModeString = @"w";
		break;
	case 'l':
	case 'p':
	case 'x':
		modeString = fileModeString = @"r";
		break;
	default:
		@throw [OFInvalidArgumentException exception];
	}

	@try {
		file = [OFFile fileWithPath: path
				       mode: @"r"];
				       mode: fileModeString];
	} @catch (OFOpenItemFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror([e errNo])
			     encoding: [OFLocalization encoding]];
		[of_stderr writeString: @"\r"];
		[of_stderr writeLine: OF_LOCALIZED(
		    @"failed_to_open_file",
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
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







-
+
+

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







+
+
+
+
+
+







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

	@try {
		if ([type isEqual: @"gz"])
			archive = [GZIPArchive archiveWithStream: file];
			archive = [GZIPArchive archiveWithStream: file
							    mode: modeString];
		else if ([type isEqual: @"tar"])
			archive = [TarArchive archiveWithStream: file];
		else if ([type isEqual: @"tgz"])
			archive = [TarArchive archiveWithStream:
			archive = [TarArchive archiveWithStream: file
							   mode: modeString];
		else if ([type isEqual: @"tgz"]) {
			OFStream *GZIPStream = [OFGZIPStream
			    streamWithStream: file
					mode: modeString];
			archive = [TarArchive archiveWithStream: GZIPStream
			    [OFGZIPStream streamWithStream: file
						      mode: @"r"]];
		else if ([type isEqual: @"zip"])
			archive = [ZIPArchive archiveWithStream: file];
							   mode: modeString];
		} else if ([type isEqual: @"zip"])
			archive = [ZIPArchive archiveWithStream: file
							   mode: modeString];
		else {
			[of_stderr writeLine: OF_LOCALIZED(
			    @"unknown_archive_type",
			    @"Unknown archive type: %[type]",
			    @"type", type)];
			[OFApplication terminateWithStatus: 1];
		}
	} @catch (OFNotImplementedException *e) {
		if ((mode == 'a' || mode == 'c') &&
		    sel_isEqual([e selector], @selector(initWithStream:mode:)))
			writingNotSupported(type);

		@throw e;
	} @catch (OFReadFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror([e errNo])
			     encoding: [OFLocalization encoding]];
		[of_stderr writeLine: OF_LOCALIZED(@"failed_to_read_file",
		    @"Failed to read file %[file]: %[error]",
		    @"file", path,
381
382
383
384
385
386
387




388
389
390
391
392
393
394
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461







+
+
+
+







	} @catch (OFInvalidFormatException *e) {
		[of_stderr writeLine: OF_LOCALIZED(
		    @"file_is_not_a_valid_archive",
		    @"File %[file] is not a valid archive!",
		    @"file", path)];
		[OFApplication terminateWithStatus: 1];
	}

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

	return archive;
}

- (bool)shouldExtractFile: (OFString *)fileName
	      outFileName: (OFString *)outFileName
{