529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
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->fileDescriptor, &s) == 0)
fchmod(destinationFile->fileDescriptor,
if (fstat(sourceFile->fd, &s) == 0)
fchmod(destinationFile->fd, s.st_mode);
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
|
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 ((fileDescriptor = open([path cStringWithEncoding:
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_
- initWithFileDescriptor: (int)fileDescriptor
{
self = [super init];
fileDescriptor = fileDescriptor_;
fd = fileDescriptor;
return self;
}
- (BOOL)_isAtEndOfStream
{
if (fileDescriptor == -1)
if (fd == -1)
return YES;
return atEndOfStream;
}
- (size_t)_readIntoBuffer: (void*)buffer
length: (size_t)length
{
ssize_t ret;
if (fileDescriptor == -1 || atEndOfStream ||
if (fd == -1 || atEndOfStream || (ret = read(fd, buffer, length)) < 0)
(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)
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(fileDescriptor, offset, SEEK_SET) == -1)
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(fileDescriptor, offset, SEEK_CUR)) == -1)
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(fileDescriptor, offset, SEEK_END)) == -1)
if ((ret = lseek(fd, offset, SEEK_END)) == -1)
@throw [OFSeekFailedException exceptionWithClass: [self class]
stream: self
offset: offset
whence: SEEK_END];
return ret;
}
- (int)fileDescriptor
- (int)fileDescriptorForReading
{
return fileDescriptor;
return fd;
}
- (int)fileDescriptorForWriting
{
return fd;
}
- (void)close
{
if (fileDescriptor != -1)
close(fileDescriptor);
if (fd != -1)
close(fd);
fileDescriptor = -1;
fd = -1;
}
- (void)dealloc
{
if (closable && fileDescriptor != -1)
close(fileDescriptor);
if (closable && fd != -1)
close(fd);
[super dealloc];
}
@end
@implementation OFFileSingleton
+ (void)load
|