ObjFW  Diff

Differences From Artifact [c7437b1fa1]:

To Artifact [d114dfb10e]:

  • File src/OFFile.m — part of check-in [3d16a30f41] at 2013-06-22 12:12:36 on branch trunk — Rework exceptions.

    This mostly removes the argument for the class in which the exception
    occurred. As backtraces were recently added for all platforms, the
    passed class does not give any extra information on where the exception
    occurred anymore.

    This also removes a few other arguments which were not too helpful. In
    the past, the idea was to pass as many arguments as possible so that it
    is easier to find the origin of the exception. However, as backtraces
    are a much better way to find the origin, those are not useful anymore
    and just make the exception more cumbersome to use. The rule is now to
    only pass arguments that might help in recovering from the exception or
    provide information that is otherwise not easily accessible. (user: js, size: 16168) [annotate] [blame] [check-ins using]


156
157
158
159
160
161
162
163
164


165
166
167
168
169
170
171
156
157
158
159
160
161
162


163
164
165
166
167
168
169
170
171







-
-
+
+







		    exceptionWithClass: self];
#endif
}

+ (instancetype)fileWithPath: (OFString*)path
			mode: (OFString*)mode
{
	return [[[self alloc] initWithPath: path
				      mode: mode] autorelease];
	return [[(OFFile*)[self alloc] initWithPath: path
					       mode: mode] autorelease];
}

+ (instancetype)fileWithFileDescriptor: (int)filedescriptor
{
	return [[[self alloc]
	    initWithFileDescriptor: filedescriptor] autorelease];
}
240
241
242
243
244
245
246
247

248
249
250
251
252
253
254
255
240
241
242
243
244
245
246

247

248
249
250
251
252
253
254







-
+
-







#ifndef _WIN32
	if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    DIR_MODE))
#else
	if (_wmkdir([path UTF16String]))
#endif
		@throw [OFCreateDirectoryFailedException
		    exceptionWithClass: self
		    exceptionWithPath: path];
				  path: path];
}

+ (void)createDirectoryAtPath: (OFString*)path
		createParents: (bool)createParents
{
	void *pool;
	OFArray *pathComponents;
294
295
296
297
298
299
300
301

302
303

304
305
306
307
308
309
310
293
294
295
296
297
298
299

300


301
302
303
304
305
306
307
308







-
+
-
-
+








#ifndef _WIN32
	DIR *dir;
	struct dirent *dirent;

	if ((dir = opendir([path cStringWithEncoding:
	    OF_STRING_ENCODING_NATIVE])) == NULL)
		@throw [OFOpenFileFailedException exceptionWithClass: self
		@throw [OFOpenFileFailedException exceptionWithPath: path
								path: path
								mode: @"r"];
							       mode: @"r"];

	@try {
		while ((dirent = readdir(dir)) != NULL) {
			void *pool = objc_autoreleasePoolPush();
			OFString *file;

			if (!strcmp(dirent->d_name, ".") ||
326
327
328
329
330
331
332
333

334
335

336
337
338
339
340
341
342
324
325
326
327
328
329
330

331


332
333
334
335
336
337
338
339







-
+
-
-
+







	HANDLE handle;
	WIN32_FIND_DATAW fd;

	path = [path stringByAppendingString: @"\\*"];

	if ((handle = FindFirstFileW([path UTF16String],
	    &fd)) == INVALID_HANDLE_VALUE)
		@throw [OFOpenFileFailedException exceptionWithClass: self
		@throw [OFOpenFileFailedException exceptionWithPath: path
								path: path
								mode: @"r"];
							       mode: @"r"];

	@try {
		do {
			void *pool2 = objc_autoreleasePoolPush();
			OFString *file;

			if (!wcscmp(fd.cFileName, L".") ||
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
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







-
+
-












-
+
-
-
+
















-
+
-
-
+

















-
+
-
-
+














-
+
-



-
+









-
+
-
-
-
+
+










-
+
-
-
-
+
+






-
+
-






-
+
-
-
-
+
+







{
#ifndef _WIN32
	if (chdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#else
	if (_wchdir([path UTF16String]))
#endif
		@throw [OFChangeDirectoryFailedException
		    exceptionWithClass: self
		    exceptionWithPath: path];
				  path: path];
}

#ifdef OF_HAVE_CHMOD
+ (void)changeModeOfFileAtPath: (OFString*)path
			  mode: (mode_t)mode
{
# ifndef _WIN32
	if (chmod([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], mode))
# else
	if (_wchmod([path UTF16String], mode))
# endif
		@throw [OFChangeFileModeFailedException
		    exceptionWithClass: self
		    exceptionWithPath: path
				  path: path
				  mode: mode];
				 mode: mode];
}
#endif

+ (off_t)sizeOfFileAtPath: (OFString*)path
{
#ifndef _WIN32
	struct stat s;

	if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)
#else
	struct _stat s;

	if (_wstat([path UTF16String], &s) == -1)
#endif
		/* FIXME: Maybe use another exception? */
		@throw [OFOpenFileFailedException exceptionWithClass: self
		@throw [OFOpenFileFailedException exceptionWithPath: path
								path: path
								mode: @"r"];
							       mode: @"r"];

	return s.st_size;
}

+ (OFDate*)modificationDateOfFileAtPath: (OFString*)path
{
#ifndef _WIN32
	struct stat s;

	if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)
#else
	struct _stat s;

	if (_wstat([path UTF16String], &s) == -1)
#endif
		/* FIXME: Maybe use another exception? */
		@throw [OFOpenFileFailedException exceptionWithClass: self
		@throw [OFOpenFileFailedException exceptionWithPath: path
								path: path
								mode: @"r"];
							       mode: @"r"];

	/* FIXME: We could be more precise on some OSes */
	return [OFDate dateWithTimeIntervalSince1970: s.st_mtime];
}

#ifdef OF_HAVE_CHOWN
+ (void)changeOwnerOfFileAtPath: (OFString*)path
			  owner: (OFString*)owner
			  group: (OFString*)group
{
	uid_t uid = -1;
	gid_t gid = -1;

	if (owner == nil && group == nil)
		@throw [OFInvalidArgumentException exceptionWithClass: self
		@throw [OFInvalidArgumentException exception];
							     selector: _cmd];

# ifdef OF_HAVE_THREADS
	if (!of_mutex_lock(&mutex))
		@throw [OFLockFailedException exceptionWithClass: self];
		@throw [OFLockFailedException exception];

	@try {
# endif
		if (owner != nil) {
			struct passwd *passwd;

			if ((passwd = getpwnam([owner cStringWithEncoding:
			    OF_STRING_ENCODING_NATIVE])) == NULL)
				@throw [OFChangeFileOwnerFailedException
				    exceptionWithClass: self
				    exceptionWithPath: path
						  path: path
						 owner: owner
						 group: group];
						owner: owner
						group: group];

			uid = passwd->pw_uid;
		}

		if (group != nil) {
			struct group *group_;

			if ((group_ = getgrnam([group cStringWithEncoding:
			    OF_STRING_ENCODING_NATIVE])) == NULL)
				@throw [OFChangeFileOwnerFailedException
				    exceptionWithClass: self
				    exceptionWithPath: path
						  path: path
						 owner: owner
						 group: group];
						owner: owner
						group: group];

			gid = group_->gr_gid;
		}
# ifdef OF_HAVE_THREADS
	} @finally {
		if (!of_mutex_unlock(&mutex))
			@throw [OFUnlockFailedException
			@throw [OFUnlockFailedException exception];
			    exceptionWithClass: self];
	}
# endif

	if (chown([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    uid, gid))
		@throw [OFChangeFileOwnerFailedException
		    exceptionWithClass: self
		    exceptionWithPath: path
				  path: path
				 owner: owner
				 group: group];
				owner: owner
				group: group];
}
#endif

+ (void)copyFileAtPath: (OFString*)source
		toPath: (OFString*)destination
{
	void *pool = objc_autoreleasePoolPush();
508
509
510
511
512
513
514
515
516


517
518
519
520
521
522
523
496
497
498
499
500
501
502


503
504
505
506
507
508
509
510
511







-
-
+
+







							nil];
	}

	override = [self fileExistsAtPath: destination];
	pageSize = [OFSystemInfo pageSize];

	if ((buffer = malloc(pageSize)) == NULL)
		@throw [OFOutOfMemoryException exceptionWithClass: self
						    requestedSize: pageSize];
		@throw [OFOutOfMemoryException
		    exceptionWithRequestedSize: pageSize];

	@try {
		sourceFile = [OFFile fileWithPath: source
					     mode: @"rb"];
		destinationFile = [OFFile fileWithPath: destination
						  mode: @"wb"];

564
565
566
567
568
569
570
571

572
573

574
575
576
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
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

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







-
+
-
-
+











-
+
-










-
+
-
















-
-
-
+
+
+




















-
+
-
-
+







#ifndef _WIN32
	if (rename([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#else
	if (_wrename([source UTF16String], [destination UTF16String]))
#endif
		@throw [OFRenameFileFailedException
		    exceptionWithClass: self
		    exceptionWithSourcePath: source
			    sourcePath: source
		       destinationPath: destination];
			    destinationPath: destination];

	objc_autoreleasePoolPop(pool);
}

+ (void)deleteFileAtPath: (OFString*)path
{
#ifndef _WIN32
	if (unlink([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#else
	if (_wunlink([path UTF16String]))
#endif
		@throw [OFDeleteFileFailedException exceptionWithClass: self
		@throw [OFDeleteFileFailedException exceptionWithPath: path];
								  path: path];
}

+ (void)deleteDirectoryAtPath: (OFString*)path
{
#ifndef _WIN32
	if (rmdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#else
	if (_wrmdir([path UTF16String]))
#endif
		@throw [OFDeleteDirectoryFailedException
		    exceptionWithClass: self
		    exceptionWithPath: path];
				  path: path];
}

#ifdef OF_HAVE_LINK
+ (void)linkFileAtPath: (OFString*)source
		toPath: (OFString*)destination
{
	void *pool = objc_autoreleasePoolPush();

	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}

	if (link([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]) != 0)
		@throw [OFLinkFailedException exceptionWithClass: self
						      sourcePath: source
						 destinationPath: destination];
		@throw [OFLinkFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination];

	objc_autoreleasePoolPop(pool);
}
#endif

#ifdef OF_HAVE_SYMLINK
+ (void)symlinkFileAtPath: (OFString*)source
		   toPath: (OFString*)destination
{
	void *pool = objc_autoreleasePoolPush();

	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}

	if (symlink([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]) != 0)
		@throw [OFSymlinkFailedException
		    exceptionWithClass: self
		    exceptionWithSourcePath: source
			    sourcePath: source
		       destinationPath: destination];
			    destinationPath: destination];

	objc_autoreleasePoolPop(pool);
}
#endif

- init
{
660
661
662
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
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







-
+
-
-









-
+
-
-
+







{
	self = [super init];

	@try {
		int flags;

		if ((flags = parse_mode([mode UTF8String])) == -1)
			@throw [OFInvalidArgumentException
			@throw [OFInvalidArgumentException exception];
			    exceptionWithClass: [self class]
				      selector: _cmd];

#ifndef _WIN32
		if ((_fd = open([path cStringWithEncoding:
		    OF_STRING_ENCODING_NATIVE], flags, DEFAULT_MODE)) == -1)
#else
		if ((_fd = _wopen([path UTF16String], flags,
		    DEFAULT_MODE)) == -1)
#endif
			@throw [OFOpenFileFailedException
			    exceptionWithClass: [self class]
			    exceptionWithPath: path
					  path: path
					  mode: mode];
					 mode: mode];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
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
745
746
747
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







-
+
-
-
+











-
+
-
-
+






-
+
-
-
-
+
+







- (size_t)lowlevelReadIntoBuffer: (void*)buffer
			  length: (size_t)length
{
	ssize_t ret;

	if (_fd == -1 || _atEndOfStream ||
	    (ret = read(_fd, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithClass: [self class]
		@throw [OFReadFailedException exceptionWithStream: self
							  stream: self
						 requestedLength: length];
						  requestedLength: length];

	if (ret == 0)
		_atEndOfStream = true;

	return ret;
}

- (void)lowlevelWriteBuffer: (const void*)buffer
		     length: (size_t)length
{
	if (_fd == -1 || _atEndOfStream || write(_fd, buffer, length) < length)
		@throw [OFWriteFailedException exceptionWithClass: [self class]
		@throw [OFWriteFailedException exceptionWithStream: self
							   stream: self
						  requestedLength: length];
						   requestedLength: length];
}

- (void)lowlevelSeekToOffset: (off_t)offset
		      whence: (int)whence
{
	if (lseek(_fd, offset, whence) == -1)
		@throw [OFSeekFailedException exceptionWithClass: [self class]
		@throw [OFSeekFailedException exceptionWithStream: self
							  stream: self
							  offset: offset
							  whence: whence];
							   offset: offset
							   whence: whence];
}

- (int)fileDescriptorForReading
{
	return _fd;
}