ObjFW  Diff

Differences From Artifact [408df46115]:

To Artifact [4de267fd9e]:


172
173
174
175
176
177
178
179
180
181
182
183
184






185
186

187
188
189
190
191
192
193
172
173
174
175
176
177
178






179
180
181
182
183
184
185

186
187
188
189
190
191
192
193







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

-
+







	of_unichar_t option, mode = '\0';
	of_string_encoding_t encoding = OF_STRING_ENCODING_AUTODETECT;
	OFArray OF_GENERIC(OFString *) *remainingArguments, *files;
	id <Archive> archive;

#ifdef OF_HAVE_SANDBOX
	OFSandbox *sandbox = [OFSandbox sandbox];
	[sandbox setAllowsStdIO: true];
	[sandbox setAllowsReadingFiles: true];
	[sandbox setAllowsWritingFiles: true];
	[sandbox setAllowsCreatingFiles: true];
	[sandbox setAllowsChangingFileAttributes: true];
	[sandbox setAllowsUserDatabaseReading: true];
	sandbox.allowsStdIO = true;
	sandbox.allowsReadingFiles = true;
	sandbox.allowsWritingFiles = true;
	sandbox.allowsCreatingFiles = true;
	sandbox.allowsChangingFileAttributes = true;
	sandbox.allowsUserDatabaseReading = true;
	/* Dropped after parsing options */
	[sandbox setAllowsUnveil: true];
	sandbox.allowsUnveil = true;

	[OFApplication activateSandbox: sandbox];
#endif

#ifndef OF_AMIGAOS
	[OFLocale addLanguageDirectory: @LANGUAGE_DIR];
#else
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
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







-
+




-
+





-
+



-
+











-
+




-
+



-
+







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

			[OFApplication terminateWithStatus: 1];
			break;
		case ':':
			if ([optionsParser lastLongOption] != nil)
			if (optionsParser.lastLongOption != nil)
				[of_stderr writeLine: OF_LOCALIZED(
				    @"long_option_requires_argument",
				    @"%[prog]: Option --%[opt] requires an "
				    @"argument",
				    @"prog", [OFApplication programName],
				    @"opt", [optionsParser lastLongOption])];
				    @"opt", optionsParser.lastLongOption)];
			else {
				OFString *optStr = [OFString
				    stringWithFormat: @"%C",
				    [optionsParser lastOption]];
				    optionsParser.lastOption];
				[of_stderr writeLine: OF_LOCALIZED(
				    @"option_requires_argument",
				    @"%[prog]: Option -%[opt] requires an "
				    @"argument",
				    @"prog", [OFApplication programName],
				    @"opt", optStr)];
			}

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

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







-
+




-
+



-
+


-
+






-
+




-
+







-
+



-
+

-
+




-
+







-
+



-
+

-
+




-
+


-
+







-
+



-
+


-
+


-
+











-
+




-
+












-
+





-
+




-
+





-
+







		    @"%[prog]: Invalid encoding: %[encoding]",
		    @"prog", [OFApplication programName],
		    @"encoding", encodingString)];

		[OFApplication terminateWithStatus: 1];
	}

	remainingArguments = [optionsParser remainingArguments];
	remainingArguments = optionsParser.remainingArguments;

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

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

#ifdef OF_HAVE_SANDBOX
		[sandbox unveilPath: [remainingArguments firstObject]
		[sandbox unveilPath: remainingArguments.firstObject
			permissions: (mode == 'a' ? @"rwc" : @"wc")];

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

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

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

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

#ifdef OF_HAVE_SANDBOX
		[sandbox unveilPath: [remainingArguments firstObject]
		[sandbox unveilPath: remainingArguments.firstObject
			permissions: @"r"];
		[sandbox setAllowsUnveil: false];
		sandbox.allowsUnveil = false;
		[OFApplication activateSandbox: sandbox];
#endif

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

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

#ifdef OF_HAVE_SANDBOX
		[sandbox unveilPath: [remainingArguments firstObject]
		[sandbox unveilPath: remainingArguments.firstObject
			permissions: @"r"];
		[sandbox setAllowsUnveil: false];
		sandbox.allowsUnveil = false;
		[OFApplication activateSandbox: sandbox];
#endif

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

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

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

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

#ifdef OF_HAVE_SANDBOX
		[sandbox unveilPath: [remainingArguments firstObject]
		[sandbox unveilPath: remainingArguments.firstObject
			permissions: @"r"];

		if ([files count] > 0)
		if (files.count > 0)
			for (OFString *path in files)
				[sandbox unveilPath: path
					permissions: @"wc"];
		else {
			OFString *path = (outputDir != nil
			    ? outputDir : OF_PATH_CURRENT_DIRECTORY);
			/* We need 'r' to change the directory to it. */
			[sandbox unveilPath: path
				permissions: @"rwc"];
		}

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

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

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

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

		break;
	default:
		help(of_stderr, true, 1);
486
487
488
489
490
491
492
493

494
495
496
497
498
499

500
501
502
503
504
505
506
486
487
488
489
490
491
492

493
494
495
496
497
498

499
500
501
502
503
504
505
506







-
+





-
+







	}

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







-
+
-








-
+








-
+







			[of_stderr writeLine: OF_LOCALIZED(
			    @"unknown_archive_type",
			    @"Unknown archive type: %[type]",
			    @"type", type)];
			goto error;
		}
	} @catch (OFNotImplementedException *e) {
		if ((mode == 'a' || mode == 'c') &&
		if ((mode == 'a' || mode == 'c') && sel_isEqual(e.selector,
		    sel_isEqual([e selector],
		    @selector(initWithStream:mode:))) {
			writingNotSupported(type);
			goto error;
		}

		@throw e;
	} @catch (OFReadFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror([e errNo])
		    stringWithCString: strerror(e.errNo)
			     encoding: [OFLocale encoding]];
		[of_stderr writeLine: OF_LOCALIZED(@"failed_to_read_file",
		    @"Failed to read file %[file]: %[error]",
		    @"file", path,
		    @"error", error)];
		goto error;
	} @catch (OFSeekFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror([e errNo])
		    stringWithCString: strerror(e.errNo)
			     encoding: [OFLocale encoding]];
		[of_stderr writeLine: OF_LOCALIZED(@"failed_to_seek_in_file",
		    @"Failed to seek in file %[file]: %[error]",
		    @"file", path,
		    @"error", error)];
		goto error;
	} @catch (OFInvalidFormatException *e) {
664
665
666
667
668
669
670
671

672
673
674
675
676
677
678
679
680
681
682
683
684
685
686

687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703

704
705
706
707
708
709
710
711
712
713
714
715
716

717
718
719
720
721
722
723
724
725
726
727

728
729

730
731

732
733
734
735
736
737
738
739
740
741
742
743
744
663
664
665
666
667
668
669

670
671
672
673
674
675
676
677
678
679
680
681
682
683
684

685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701

702
703
704
705
706
707
708
709
710
711
712
713
714

715
716
717
718
719
720
721
722
723
724
725

726
727

728
729

730
731
732
733
734
735
736
737
738
739
740
741
742
743







-
+














-
+
















-
+












-
+










-
+

-
+

-
+













	size_t length;

	@try {
		length = [input readIntoBuffer: buffer
					length: BUFFER_SIZE];
	} @catch (OFReadFailedException *e) {
		OFString *error = [OFString
		    stringWithCString: strerror([e errNo])
		    stringWithCString: strerror(e.errNo)
			     encoding: [OFLocale encoding]];
		[of_stdout writeString: @"\r"];
		[of_stderr writeLine: OF_LOCALIZED(@"failed_to_read_file",
		    @"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])
		    stringWithCString: strerror(e.errNo)
			     encoding: [OFLocale encoding]];
		[of_stdout writeString: @"\r"];
		[of_stderr writeLine: OF_LOCALIZED(@"failed_to_write_file",
		    @"Failed to write file %[file]: %[error]",
		    @"file", fileName,
		    @"error", error)];
		return -1;
	}

	return length;
}

- (OFString *)safeLocalPathForPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();

	path = [path stringByStandardizingPath];
	path = path.stringByStandardizingPath;

#if defined(OF_WINDOWS) || defined(OF_MSDOS)
	if ([path containsString: @":"] || [path hasPrefix: @"\\"]) {
#elif defined(OF_AMIGAOS)
	if ([path containsString: @":"] || [path hasPrefix: @"/"]) {
#else
	if ([path hasPrefix: @"/"]) {
#endif
		objc_autoreleasePoolPop(pool);
		return nil;
	}

	if ([path length] == 0) {
	if (path.length == 0) {
		objc_autoreleasePoolPop(pool);
		return nil;
	}

	/*
	 * After -[stringByStandardizingPath], everything representing parent
	 * directory should be at the beginning, so in theory checking the
	 * first component should be enough. But it does not hurt being
	 * paranoid and checking all components, just in case.
	 */
	for (OFString *component in [path pathComponents]) {
	for (OFString *component in path.pathComponents) {
#ifdef OF_AMIGAOS
		if ([component length] == 0 || [component isEqual: @"/"]) {
		if (component.length == 0 || [component isEqual: @"/"]) {
#else
		if ([component length] == 0 || [component isEqual: @".."]) {
		if (component.length == 0 || [component isEqual: @".."]) {
#endif
			objc_autoreleasePoolPop(pool);
			return nil;
		}
	}

	[path retain];

	objc_autoreleasePoolPop(pool);

	return [path autorelease];
}
@end