ObjFW  Diff

Differences From Artifact [a15c73b575]:

To Artifact [7a0b90bbf0]:


547
548
549
550
551
552
553
554
555


556
557
558
559
560
561
562
547
548
549
550
551
552
553


554
555
556
557
558
559
560
561
562







-
-
+
+







					      length: length];
		}

#if !defined(_WIN32) && !defined(_PSP)
		if (!override) {
			struct stat s;

			if (fstat(sourceFile->fd, &s) == 0)
				fchmod(destinationFile->fd, s.st_mode);
			if (fstat(sourceFile->_fd, &s) == 0)
				fchmod(destinationFile->_fd, s.st_mode);
		}
#else
		(void)override;
#endif
	} @finally {
		[sourceFile close];
		[destinationFile close];
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

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
771
772
773


774
775

776
777
778
779
780
781


782
783
784
785
786
787
788
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

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


773
774
775

776
777
778
779
780


781
782
783
784
785
786
787
788
789







-
+


-
+







-
+








-
+



-
+






-
+


-
+







+
-
+





-
+







-
+








-
+








-
+




-
+




-
-
+
+

-
+




-
-
+
+








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

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

		closable = YES;
		_closable = YES;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithFileDescriptor: (int)fileDescriptor
- initWithFileDescriptor: (int)fd
{
	self = [super init];

	fd = fileDescriptor;
	_fd = fd;

	return self;
}

- (BOOL)lowlevelIsAtEndOfStream
{
	if (fd == -1)
	if (_fd == -1)
		return YES;

	return atEndOfStream;
	return _atEndOfStream;
}

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

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

	if (ret == 0)
		atEndOfStream = YES;
		_atEndOfStream = YES;

	return ret;
}

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

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

- (int)fileDescriptorForReading
{
	return fd;
	return _fd;
}

- (int)fileDescriptorForWriting
{
	return fd;
	return _fd;
}

- (void)close
{
	if (fd != -1)
		close(fd);
	if (_fd != -1)
		close(_fd);

	fd = -1;
	_fd = -1;
}

- (void)dealloc
{
	if (closable && fd != -1)
		close(fd);
	if (_closable && _fd != -1)
		close(_fd);

	[super dealloc];
}
@end

@implementation OFFileSingleton
+ (void)load