ObjFW  Diff

Differences From Artifact [70af7286c2]:

To Artifact [c9acb9d620]:


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







+




















-
-
-
+
+
+

-
-
-
+
+
+
+










-
+
+








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








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








#import "OFApplication.h"
#import "OFArray.h"
#import "OFFile.h"
#import "OFFileManager.h"
#import "OFOptionsParser.h"
#import "OFStdIOStream.h"
#import "OFLocalization.h"

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

#import "OFCreateDirectoryFailedException.h"
#import "OFInvalidFormatException.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 writeFormat:
	    @"Usage: %@ -[fhlnpqtvx] archive.zip [file1 file2 ...]\n",
	    [OFApplication programName]];
	[stream writeLine: OF_LOCALIZED(@"usage",
	    @"Usage: %[prog] -[fhlnpqtvx] archive.zip [file1 file2 ...]",
	    @"prog", [OFApplication programName])];

	if (full)
		[stream writeString:
		    @"\nOptions:\n"
	if (full) {
		[stream writeString: @"\n"];
		[stream writeLine: OF_LOCALIZED(@"full_usage",
		    @"Options:\n"
		    @"    -f  --force      Force / overwrite files\n"
		    @"    -h  --help       Show this help\n"
		    @"    -l  --list       List all files in the archive\n"
		    @"    -n  --no-clober  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, tar, tgz, zip)\n"
		    @"    -v  --verbose    Verbose output for file list\n"
		    @"    -x  --extract    Extract files\n"];
		    @"    -x  --extract    Extract files")];
	}

	[OFApplication terminateWithStatus: status];
}

static void
mutuallyExclusiveError(of_unichar_t shortOption1, OFString *longOption1,
    of_unichar_t shortOption2, OFString *longOption2)
{
	OFString *shortOption1Str = [OFString stringWithFormat: @"%C",
								shortOption1];
	OFString *shortOption2Str = [OFString stringWithFormat: @"%C",
								shortOption2];

	[of_stderr writeFormat:
	    @"Error: -%C / --%@ and -%C / --%@ are mutually exclusive!\n",
	    shortOption1, longOption1, shortOption2, longOption2];
	[of_stderr writeLine: OF_LOCALIZED(@"2_options_mutually_exclusive",
	    @"Error: -%[shortopt1] / --%[longopt1] and "
	    @"-%[shortopt2] / --%[longopt2] "
	    @"are mutually exclusive!",
	    @"shortopt1", shortOption1Str,
	    @"longopt1", longOption1,
	    @"shortopt2", shortOption2Str,
	    @"longopt2", longOption2)];
	[OFApplication terminateWithStatus: 1];
}

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

	[of_stderr writeFormat:
	    @"Error: -%C / --%@, -%C / --%@ and -%C / --%@ are mutually "
	    @"exclusive!\n", shortOption1, longOption1, shortOption2,
	    longOption2, shortOption3, longOption3];
	[of_stderr writeLine: OF_LOCALIZED(@"3_options_mutually_exclusive",
	    @"Error: -%[shortopt1] / --%[longopt1], "
	    @"-%[shortopt2] / --%[longopt2] and -%[shortopt3] / --%[longopt3] "
	    @"are mutually exclusive!",
	    @"shortopt1", shortOption1Str,
	    @"longopt1", longOption1,
	    @"shortopt2", shortOption2Str,
	    @"longopt2", longOption2,
	    @"shortopt3", shortOption3Str,
	    @"longopt3", longOption3)];
	[OFApplication terminateWithStatus: 1];
}

@implementation OFZIP
- (void)applicationDidFinishLaunching
{
	OFString *type = nil;
105
106
107
108
109
110
111


112
113
114
115
116
117
118
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146







+
+







		{ '\0', nil, 0, NULL, NULL }
	};
	OFOptionsParser *optionsParser =
	    [OFOptionsParser parserWithOptions: options];
	of_unichar_t option, mode = '\0';
	OFArray OF_GENERIC(OFString*) *remainingArguments, *files;
	id <Archive> archive;

	[OFLocalization addLanguageDirectory: @LANGUAGE_DIR];

	while ((option = [optionsParser nextOption]) != '\0') {
		switch (option) {
		case 'f':
			if (_overwrite < 0)
				mutuallyExclusiveError(
				    'f', @"force", 'n', @"no-clobber");
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
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







-
-
-
-
+
+
+
+
+





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








			mode = option;
			break;
		case 'h':
			help(of_stdout, true, 0);
			break;
		case '=':
			[of_stderr writeFormat: @"%@: Option --%@ takes no "
						@"argument!\n",
						[OFApplication programName],
						[optionsParser lastLongOption]];
			[of_stderr writeLine: OF_LOCALIZED(
			    @"option_takes_no_argument",
			    @"%[prog]: Option --%[opt] takes no argument",
			    @"prog", [OFApplication programName],
			    @"opt", [optionsParser lastLongOption])];

			[OFApplication terminateWithStatus: 1];
			break;
		case '?':
			if ([optionsParser lastLongOption] != nil)
				[of_stderr writeFormat:
				    @"%@: Unknown option: --%@\n",
				    [OFApplication programName],
				    [optionsParser lastLongOption]];
			else
				[of_stderr writeFormat:
				    @"%@: Unknown option: -%C\n",
				    [OFApplication programName],
				    [optionsParser lastOption]];
				[of_stderr writeLine: OF_LOCALIZED(
				    @"unknown_long_option",
				    @"%[prog]: Unknown option: --%[opt]",
				    @"prog", [OFApplication programName],
				    @"opt", [optionsParser lastLongOption])];
			else {
				OFString *optStr = [OFString
				    stringWithFormat: @"%c",
				    [optionsParser lastOption]];
				[of_stderr writeLine: OF_LOCALIZED(
				    @"unknown_option",
				    @"%[prog]: Unknown option: -%[opt]",
				    @"prog", [OFApplication programName],
				    @"opt", optStr)];
			}

			[OFApplication terminateWithStatus: 1];
		}
	}

	remainingArguments = [optionsParser remainingArguments];
	archive = [self openArchiveWithPath: [remainingArguments firstObject]
194
195
196
197
198
199
200



201
202
203






204
205



206
207
208






209
210
211
212
213
214
215
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







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


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








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

		@try {
			[archive extractFiles: files];
		} @catch (OFCreateDirectoryFailedException *e) {
			OFString *error = [OFString
			    stringWithCString: strerror([e errNo])
				     encoding: [OFLocalization encoding]];
			[of_stderr writeFormat:
			    @"\rFailed to create directory %@: %s\n",
			    [e path], strerror([e errNo])];
			[of_stderr writeString: @"\r"];
			[of_stderr writeLine: OF_LOCALIZED(
			    @"failed_to_create_directory",
			    @"Failed to create directory %[dir]: %[error]",
			    @"dir", [e path],
			    @"error", error)];
			_exitStatus = 1;
		} @catch (OFOpenItemFailedException *e) {
			OFString *error = [OFString
			    stringWithCString: strerror([e errNo])
				     encoding: [OFLocalization encoding]];
			[of_stderr writeFormat:
			    @"\rFailed to open file %@: %s\n",
			    [e path], strerror([e errNo])];
			[of_stderr writeString: @"\r"];
			[of_stderr writeLine: OF_LOCALIZED(
			    @"failed_to_open_file",
			    @"Failed to open file %[file]: %[error]",
			    @"file", [e path],
			    @"error", error)];
			_exitStatus = 1;
		}

		break;
	case 'p':
		if ([remainingArguments count] < 1)
			help(of_stderr, false, 1);
239
240
241
242
243
244
245



246
247






248
249
250
251
252
253
254
286
287
288
289
290
291
292
293
294
295


296
297
298
299
300
301
302
303
304
305
306
307
308







+
+
+
-
-
+
+
+
+
+
+







	if (path == nil)
		return nil;

	@try {
		file = [OFFile fileWithPath: path
				       mode: @"rb"];
	} @catch (OFOpenItemFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror([e errNo])
			     encoding: [OFLocalization encoding]];
		[of_stderr writeFormat: @"Failed to open file %@: %s\n",
					[e path], strerror([e errNo])];
		[of_stderr writeString: @"\r"];
		[of_stderr writeLine: OF_LOCALIZED(
		    @"failed_to_open_file",
		    @"Failed to open file %[file]: %[error]",
		    @"file", [e path],
		    @"error", error)];
		[OFApplication terminateWithStatus: 1];
	}

	if (type == nil || [type isEqual: @"auto"]) {
		/* This one has to be first for obvious reasons */
		if ([path hasSuffix: @".tar.gz"] || [path hasSuffix: @".tgz"] ||
		    [path hasSuffix: @".TAR.GZ"] || [path hasSuffix: @".TGZ"])
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
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







+
+
-
-
+
+



+
+
+
+
-
-
+
+
+


+
+
+
+
-
-
+
+
+


+
+
-
-
+
+















-

-
-
+
+
+
+
+




+
+
+
+
-
+




+
+
-
-
-
-
+
+
+
+










+
-
+
+




+
-
+
+















+
+
+
+
+
-
-
+
+
+







+
+
+
+
+
-
-
+
+
+






			archive = [TarArchive archiveWithStream: file];
		else if ([type isEqual: @"tgz"])
			archive = [TarArchive archiveWithStream:
			    [OFGZIPStream streamWithStream: file]];
		else if ([type isEqual: @"zip"])
			archive = [ZIPArchive archiveWithStream: file];
		else {
			[of_stderr writeLine: OF_LOCALIZED(
			    @"unknown_archive_type",
			[of_stderr writeFormat: @"Unknown archive type: %@!\n",
						type];
			    @"Unknown archive type: %[type]",
			    @"type", type)];
			[OFApplication terminateWithStatus: 1];
		}
	} @catch (OFReadFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror([e errNo])
			     encoding: [OFLocalization encoding]];
		[of_stderr writeLine: OF_LOCALIZED(@"failed_to_read_file",
		[of_stderr writeFormat: @"Failed to read file %@: %s\n",
					path, strerror([e errNo])];
		    @"Failed to read file %[file]: %[error]",
		    @"file", path,
		    @"error", error)];
		[OFApplication terminateWithStatus: 1];
	} @catch (OFSeekFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror([e errNo])
			     encoding: [OFLocalization encoding]];
		[of_stderr writeLine: OF_LOCALIZED(@"failed_to_seek_in_file",
		[of_stderr writeFormat: @"Failed to seek in file %@: %s\n",
					path, strerror([e errNo])];
		    @"Failed to seek in file %[file]: %[error]",
		    @"file", path,
		    @"error", error)];
		[OFApplication terminateWithStatus: 1];
	} @catch (OFInvalidFormatException *e) {
		[of_stderr writeLine: OF_LOCALIZED(
		    @"file_is_not_a_valid_archive",
		[of_stderr writeFormat: @"File %@ is not a valid archive!\n",
					path];
		    @"File %[file] is not a valid archive!",
		    @"file", path)];
		[OFApplication terminateWithStatus: 1];
	}

	return archive;
}

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

	if (_overwrite == 1 ||
	    ![[OFFileManager defaultManager] fileExistsAtPath: outFileName])
		return true;


	if (_overwrite == -1) {
		if (_outputLevel >= 0)
			[of_stdout writeLine: @" skipped"];
		if (_outputLevel >= 0) {
			[of_stdout writeString: @" "];
			[of_stdout writeLine:
			    OF_LOCALIZED(@"file_skipped", @"skipped")];
		}
		return false;
	}

	do {
		[of_stderr writeString: @"\r"];
		[of_stderr writeString: OF_LOCALIZED(@"ask_overwrite",
		    @"Overwrite %[file]? [ynAN?]",
		    @"file", fileName)];
		[of_stderr writeFormat: @"\rOverwrite %@? [ynAN?] ", fileName];
		[of_stderr writeString: @" "];

		line = [of_stdin readLine];

		if ([line isEqual: @"?"])
			[of_stderr writeLine: OF_LOCALIZED(
			    @"ask_overwrite_help",
			[of_stderr writeString: @" y: yes\n"
						@" n: no\n"
						@" A: always\n"
						@" N: never\n"];
			    @" y: yes\n"
			    @" n: no\n"
			    @" A: always\n"
			    @" N: never")];
	} while (![line isEqual: @"y"] && ![line isEqual: @"n"] &&
	    ![line isEqual: @"N"] && ![line isEqual: @"A"]);

	if ([line isEqual: @"A"])
		_overwrite = 1;
	else if ([line isEqual: @"N"])
		_overwrite = -1;

	if ([line isEqual: @"n"] || [line isEqual: @"N"]) {
		if (_outputLevel >= 0)
			[of_stdout writeLine: OF_LOCALIZED(@"skipping_file",
			[of_stdout writeFormat: @"Skipping %@...\n", fileName];
			    @"Skipping %[file]...",
			    @"file", fileName)];
			return false;
	}

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

	return true;
}

- (ssize_t)copyBlockFromStream: (OFStream*)input
		      toStream: (OFStream*)output
		      fileName: (OFString*)fileName
{
	char buffer[BUFFER_SIZE];
	size_t length;

	@try {
		length = [input readIntoBuffer: buffer
					length: BUFFER_SIZE];
	} @catch (OFReadFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror([e errNo])
			     encoding: [OFLocalization encoding]];
		[of_stdout writeString: @"\r"];
		[of_stderr writeLine: OF_LOCALIZED(@"failed_to_read_file",
		[of_stderr writeFormat: @"\rFailed to read file %@: %s\n",
					fileName, strerror([e errNo])];
		    @"Failed to read file %[file]: %[error]",
		    @"file", fileName,
		    @"error", error)];
		return -1;
	}

	@try {
		[output writeBuffer: buffer
			     length: length];
	} @catch (OFWriteFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror([e errNo])
			     encoding: [OFLocalization encoding]];
		[of_stdout writeString: @"\r"];
		[of_stderr writeLine: OF_LOCALIZED(@"failed_to_write_file",
		[of_stderr writeFormat: @"\rFailed to write file %@: %s\n",
					fileName, strerror([e errNo])];
		    @"Failed to write file %[file]: %[error]",
		    @"file", fileName,
		    @"error", error)];
		return -1;
	}

	return length;
}
@end