ObjFW  Diff

Differences From Artifact [9e887a707d]:

To Artifact [f9e4a0a447]:

  • File src/OFFile.m — part of check-in [440e95fd4a] at 2012-09-12 17:27:53 on branch trunk — Split -[OFStream fileDescriptor].

    It is now -[fileDescriptorForReading] and -[fileDescriptorForWriting].
    The split was necessary as some stream types (e.g. OFProcess) don't have
    a single file descriptor, but two. This allows to use those stream types
    with OFStreamObserver as well. (user: js, size: 18903) [annotate] [blame] [check-ins using]


529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
					      length: length];
		}

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

			if (fstat(sourceFile->fileDescriptor, &s) == 0)
				fchmod(destinationFile->fileDescriptor,
				    s.st_mode);
		}
#else
		(void)override;
#endif
	} @finally {
		[sourceFile close];
		[destinationFile close];







|
|
<







529
530
531
532
533
534
535
536
537

538
539
540
541
542
543
544
					      length: length];
		}

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

			if (fstat(sourceFile->fd, &s) == 0)
				fchmod(destinationFile->fd, s.st_mode);

		}
#else
		(void)override;
#endif
	} @finally {
		[sourceFile close];
		[destinationFile close];
657
658
659
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
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

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

		if ((fileDescriptor = open([path cStringWithEncoding:
		    OF_STRING_ENCODING_NATIVE], flags, DEFAULT_MODE)) == -1)
			@throw [OFOpenFileFailedException
			    exceptionWithClass: [self class]
					  path: path
					  mode: mode];

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

	return self;
}

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

	fileDescriptor = fileDescriptor_;

	return self;
}

- (BOOL)_isAtEndOfStream
{
	if (fileDescriptor == -1)
		return YES;

	return atEndOfStream;
}

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

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

	if (ret == 0)
		atEndOfStream = YES;

	return ret;
}

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

- (void)_seekToOffset: (off_t)offset
{
	if (lseek(fileDescriptor, offset, SEEK_SET) == -1)
		@throw [OFSeekFailedException exceptionWithClass: [self class]
							  stream: self
							  offset: offset
							  whence: SEEK_SET];
}

- (off_t)_seekForwardWithOffset: (off_t)offset
{
	off_t ret;

	if ((ret = lseek(fileDescriptor, offset, SEEK_CUR)) == -1)
		@throw [OFSeekFailedException exceptionWithClass: [self class]
							  stream: self
							  offset: offset
							  whence: SEEK_CUR];

	return ret;
}

- (off_t)_seekToOffsetRelativeToEnd: (off_t)offset
{
	off_t ret;

	if ((ret = lseek(fileDescriptor, offset, SEEK_END)) == -1)
		@throw [OFSeekFailedException exceptionWithClass: [self class]
							  stream: self
							  offset: offset
							  whence: SEEK_END];

	return ret;
}

- (int)fileDescriptor
{
	return fileDescriptor;





}

- (void)close
{
	if (fileDescriptor != -1)
		close(fileDescriptor);

	fileDescriptor = -1;
}

- (void)dealloc
{
	if (closable && fileDescriptor != -1)
		close(fileDescriptor);

	[super dealloc];
}
@end

@implementation OFFileSingleton
+ (void)load







|















|



|






|










|
<













<
|







|










|












|








|

|
>
>
>
>
>




|
|

|




|
|







656
657
658
659
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
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

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

		if ((fd = open([path cStringWithEncoding:
		    OF_STRING_ENCODING_NATIVE], flags, DEFAULT_MODE)) == -1)
			@throw [OFOpenFileFailedException
			    exceptionWithClass: [self class]
					  path: path
					  mode: mode];

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

	return self;
}

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

	fd = fileDescriptor;

	return self;
}

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

	return atEndOfStream;
}

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

	if (fd == -1 || atEndOfStream || (ret = read(fd, buffer, length)) < 0)

		@throw [OFReadFailedException exceptionWithClass: [self class]
							  stream: self
						 requestedLength: length];

	if (ret == 0)
		atEndOfStream = YES;

	return ret;
}

- (void)_writeBuffer: (const void*)buffer
	      length: (size_t)length
{

	if (fd == -1 || atEndOfStream || write(fd, buffer, length) < length)
		@throw [OFWriteFailedException exceptionWithClass: [self class]
							   stream: self
						  requestedLength: length];
}

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

- (off_t)_seekForwardWithOffset: (off_t)offset
{
	off_t ret;

	if ((ret = lseek(fd, offset, SEEK_CUR)) == -1)
		@throw [OFSeekFailedException exceptionWithClass: [self class]
							  stream: self
							  offset: offset
							  whence: SEEK_CUR];

	return ret;
}

- (off_t)_seekToOffsetRelativeToEnd: (off_t)offset
{
	off_t ret;

	if ((ret = lseek(fd, offset, SEEK_END)) == -1)
		@throw [OFSeekFailedException exceptionWithClass: [self class]
							  stream: self
							  offset: offset
							  whence: SEEK_END];

	return ret;
}

- (int)fileDescriptorForReading
{
	return fd;
}

- (int)fileDescriptorForWriting
{
	return fd;
}

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

	fd = -1;
}

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

	[super dealloc];
}
@end

@implementation OFFileSingleton
+ (void)load