ObjFW  Diff

Differences From Artifact [864c3ff8dd]:

To Artifact [43dd30c121]:

  • File src/OFFile.m — part of check-in [62e2de30b9] at 2015-02-16 08:39:17 on branch trunk — Explicitly pass errno to exceptions

    The old behaviour where the exception would access errno directly on
    creation of the exception was very fragile. The two main problems with
    it were that sometimes it would pick up an errno even though none had
    been set and in other cases that when the exception was created errno
    had already been overridden.

    This also greatly increases errno handling on Win32, especially in
    conjunction with sockets. It can still be improved further, though. (user: js, size: 26006) [annotate] [blame] [check-ins using]


19
20
21
22
23
24
25


26
27
28
29
30
31
32
33
34
35
36
37
19
20
21
22
23
24
25
26
27
28
29
30
31

32
33
34
35
36
37
38







+
+




-







#include "config.h"

/* Work around a bug with Clang + glibc */
#ifdef __clang__
# define _HAVE_STRING_ARCH_strcmp
#endif

#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

/* Work around __block being used by glibc */
#ifdef __GLIBC__
# undef __USE_XOPEN
#endif

#include <unistd.h>
324
325
326
327
328
329
330
331


332
333
334
335
336
337
338
325
326
327
328
329
330
331

332
333
334
335
336
337
338
339
340







-
+
+







#ifndef _WIN32
	if (mkdir([path cStringWithEncoding: [OFSystemInfo native8BitEncoding]],
	    DIR_MODE) != 0)
#else
	if (_wmkdir([path UTF16String]) != 0)
#endif
		@throw [OFCreateDirectoryFailedException
		    exceptionWithPath: path];
		    exceptionWithPath: path
				errNo: errno];
}

+ (void)createDirectoryAtPath: (OFString*)path
		createParents: (bool)createParents
{
	void *pool;
	OFArray *pathComponents;
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
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







-
+
+



















-
-
+
+
-
-



+
+
+
+
+
+
+
+
+
+
+







#ifndef _WIN32
	DIR *dir;

	encoding = [OFSystemInfo native8BitEncoding];

	if ((dir = opendir([path cStringWithEncoding: encoding])) == NULL)
		@throw [OFOpenFileFailedException exceptionWithPath: path
							       mode: @"r"];
							       mode: @"r"
							      errNo: errno];

# if !defined(HAVE_READDIR_R) && defined(OF_HAVE_THREADS)
	if (!of_mutex_lock(&mutex))
		@throw [OFLockFailedException exception];
# endif

	@try {
		for (;;) {
			struct dirent *dirent;
# ifdef HAVE_READDIR_R
			struct dirent buffer;
# endif
			void *pool;
			OFString *file;

# ifdef HAVE_READDIR_R
			if (readdir_r(dir, &buffer, &dirent) != 0)
				@throw [OFReadFailedException
				    exceptionWithObject: self
					requestedLength: 0];
# else
					requestedLength: 0
						  errNo: errno];
			dirent = readdir(dir);
# endif

			if (dirent == NULL)
				break;
# else
			if ((dirent = readdir(dir)) == NULL) {
				if (errno == 0)
					break;
				else
					@throw [OFReadFailedException
					    exceptionWithObject: self
						requestedLength: 0
							  errNo: errno];
			}
# endif

			if (strcmp(dirent->d_name, ".") == 0 ||
			    strcmp(dirent->d_name, "..") == 0)
				continue;

			pool = objc_autoreleasePoolPush();

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







-
+
+
+
+
+
+

-
+
+
+















+
+
+
+







	void *pool = objc_autoreleasePoolPush();
	HANDLE handle;
	WIN32_FIND_DATAW fd;

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

	if ((handle = FindFirstFileW([path UTF16String],
	    &fd)) == INVALID_HANDLE_VALUE)
	    &fd)) == INVALID_HANDLE_VALUE) {
		int errNo = 0;

		if (GetLastError() == ERROR_FILE_NOT_FOUND)
			errNo = ENOENT;

		@throw [OFOpenFileFailedException exceptionWithPath: path
							       mode: @"r"];
							       mode: @"r"
							      errNo: errNo];
	}

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

			if (!wcscmp(fd.cFileName, L".") ||
			    !wcscmp(fd.cFileName, L".."))
				continue;

			file = [OFString stringWithUTF16String: fd.cFileName];
			[files addObject: file];

			objc_autoreleasePoolPop(pool2);
		} while (FindNextFileW(handle, &fd));

		if (GetLastError() != ERROR_NO_MORE_FILES)
			@throw [OFReadFailedException exceptionWithObject: self
							  requestedLength: 0];
	} @finally {
		FindClose(handle);
	}

	objc_autoreleasePoolPop(pool);
#endif

486
487
488
489
490
491
492
493


494
495
496
497
498
499
500
501
502
503

504
505
506


507
508
509
510
511
512
513
514
515
516
517
518

519
520
521


522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542


543
544
545
546
547
548
549
509
510
511
512
513
514
515

516
517
518
519
520
521
522
523
524
525
526

527
528
529

530
531
532
533
534
535
536
537
538
539
540
541
542

543
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







-
+
+









-
+


-
+
+











-
+


-
+
+




















-
+
+







#ifndef _WIN32
	if (chdir([path cStringWithEncoding:
	    [OFSystemInfo native8BitEncoding]]) != 0)
#else
	if (_wchdir([path UTF16String]) != 0)
#endif
		@throw [OFChangeCurrentDirectoryPathFailedException
		    exceptionWithPath: path];
		    exceptionWithPath: path
				errNo: errno];
}

+ (of_offset_t)sizeOfFileAtPath: (OFString*)path
{
	of_stat_t s;

	if (path == nil)
		@throw [OFInvalidArgumentException exception];

	if (of_stat(path, &s) == -1)
	if (of_stat(path, &s) != 0)
		/* FIXME: Maybe use another exception? */
		@throw [OFOpenFileFailedException exceptionWithPath: path
							       mode: @"r"];
							       mode: @"r"
							      errNo: errno];

	return s.st_size;
}

+ (OFDate*)modificationDateOfFileAtPath: (OFString*)path
{
	of_stat_t s;

	if (path == nil)
		@throw [OFInvalidArgumentException exception];

	if (of_stat(path, &s) == -1)
	if (of_stat(path, &s) != 0)
		/* FIXME: Maybe use another exception? */
		@throw [OFOpenFileFailedException exceptionWithPath: path
							       mode: @"r"];
							       mode: @"r"
							      errNo: errno];

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

#ifdef OF_HAVE_CHMOD
+ (void)changePermissionsOfItemAtPath: (OFString*)path
			  permissions: (mode_t)permissions
{
	if (path == nil)
		@throw [OFInvalidArgumentException exception];

# ifndef _WIN32
	if (chmod([path cStringWithEncoding: [OFSystemInfo native8BitEncoding]],
	    permissions) != 0)
# else
	if (_wchmod([path UTF16String], permissions) != 0)
# endif
		@throw [OFChangePermissionsFailedException
		    exceptionWithPath: path
			  permissions: permissions];
			  permissions: permissions
				errNo: errno];
}
#endif

#ifdef OF_HAVE_CHOWN
+ (void)changeOwnerOfItemAtPath: (OFString*)path
			  owner: (OFString*)owner
			  group: (OFString*)group
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
648
649
650
651
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
648

649

650
651
652
653
654

655
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







-
+
+












-
+
+













-
+
+














-
+
-


-
+
-
+




-
+
+















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







			struct passwd *passwd;

			if ((passwd = getpwnam([owner
			    cStringWithEncoding: encoding])) == NULL)
				@throw [OFChangeOwnerFailedException
				    exceptionWithPath: path
						owner: owner
						group: group];
						group: group
						errNo: errno];

			uid = passwd->pw_uid;
		}

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

			if ((group_ = getgrnam([group
			    cStringWithEncoding: encoding])) == NULL)
				@throw [OFChangeOwnerFailedException
				    exceptionWithPath: path
						owner: owner
						group: group];
						group: group
						errNo: errno];

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

	if (chown([path cStringWithEncoding: encoding], uid, gid) != 0)
		@throw [OFChangeOwnerFailedException exceptionWithPath: path
								 owner: owner
								 group: group];
								 group: group
								 errNo: errno];
}
#endif

+ (void)copyItemAtPath: (OFString*)source
		toPath: (OFString*)destination
{
	void *pool;
	of_stat_t s;

	if (source == nil || destination == nil)
		@throw [OFInvalidArgumentException exception];

	pool = objc_autoreleasePoolPush();

	if (of_lstat(destination, &s) == 0) {
	if (of_lstat(destination, &s) == 0)
		errno = EEXIST;
		@throw [OFCopyItemFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination];
			    destinationPath: destination
	}
				      errNo: EEXIST];

	if (of_lstat(source, &s) != 0)
		@throw [OFCopyItemFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination];
			    destinationPath: destination
				      errNo: errno];

	if (S_ISDIR(s.st_mode)) {
		OFArray *contents;
		OFEnumerator *enumerator;
		OFString *item;

		@try {
			[OFFile createDirectoryAtPath: destination];
#ifdef OF_HAVE_CHMOD
			[OFFile changePermissionsOfItemAtPath: destination
						  permissions: s.st_mode];
#endif

			contents = [OFFile contentsOfDirectoryAtPath: source];
		} @catch (id e) {
			/*
			 * Only convert exceptions to OFCopyItemFailedException
			 * that have an errNo property. This covers all I/O
			 * related exceptions from the operations used to copy
			 * an item, all others should be left as is.
			 */
			if ([e respondsToSelector: @selector(errNo)])
			@throw [OFCopyItemFailedException
			    exceptionWithSourcePath: source
				    destinationPath: destination];
				@throw [OFCopyItemFailedException
				    exceptionWithSourcePath: source
					    destinationPath: destination
						      errNo: [e errNo]];

			@throw e;
		}

		enumerator = [contents objectEnumerator];
		while ((item = [enumerator nextObject]) != nil) {
			void *pool2 = objc_autoreleasePoolPush();
			OFString *sourcePath, *destinationPath;

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


790
791
792
793
794
795
796
797
798








799
800





801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820


821
822
823
824
825
826
827
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
790
791
792
793
794
795
796

797

798
799

800

801
802
803
804
805
806
807
808
809
810
811
812
813

814
815
816
817
818
819
820
821
822
823
824

825
826
827
828
829
830
831
832
833

834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852

853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871


872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895

896
897
898
899
900
901
902
903
904







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














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





-
+
+


















-
+
-


-
+
-
+












-
+
+






+


-
+
+







-
+
+

















-
+
+









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



















-
+
+







			}

#ifdef OF_HAVE_CHMOD
			[self changePermissionsOfItemAtPath: destination
						permissions: s.st_mode];
#endif
		} @catch (id e) {
			/*
			 * Only convert exceptions to OFCopyItemFailedException
			 * that have an errNo property. This covers all I/O
			 * related exceptions from the operations used to copy
			 * an item, all others should be left as is.
			 */
			if ([e respondsToSelector: @selector(errNo)])
			@throw [OFCopyItemFailedException
			    exceptionWithSourcePath: source
				    destinationPath: destination];
				@throw [OFCopyItemFailedException
				    exceptionWithSourcePath: source
					    destinationPath: destination
						      errNo: [e errNo]];

			@throw e;
		} @finally {
			[sourceFile close];
			[destinationFile close];
			free(buffer);
		}
#ifdef OF_HAVE_SYMLINK
	} else if (S_ISLNK(s.st_mode)) {
		@try {
			source = [OFFile
			    destinationOfSymbolicLinkAtPath: source];

			[OFFile createSymbolicLinkAtPath: destination
				     withDestinationPath: source];
		} @catch (id e) {
			/*
			 * Only convert exceptions to OFCopyItemFailedException
			 * that have an errNo property. This covers all I/O
			 * related exceptions from the operations used to copy
			 * an item, all others should be left as is.
			 */
			if ([e respondsToSelector: @selector(errNo)])
			@throw [OFCopyItemFailedException
			    exceptionWithSourcePath: source
				    destinationPath: destination];
				@throw [OFCopyItemFailedException
				    exceptionWithSourcePath: source
					    destinationPath: destination
						      errNo: [e errNo]];

			@throw e;
		}
#endif
	} else
		@throw [OFCopyItemFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination];
			    destinationPath: destination
				      errNo: ENOTSUP];

	objc_autoreleasePoolPop(pool);
}

+ (void)moveItemAtPath: (OFString*)source
		toPath: (OFString*)destination
{
	void *pool;
	of_stat_t s;
#ifndef _WIN32
	of_string_encoding_t encoding;
#endif

	if (source == nil || destination == nil)
		@throw [OFInvalidArgumentException exception];

	pool = objc_autoreleasePoolPush();

	if (of_lstat(destination, &s) == 0) {
	if (of_lstat(destination, &s) == 0)
		errno = EEXIST;
		@throw [OFCopyItemFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination];
			    destinationPath: destination
	}
				      errNo: EEXIST];

#ifndef _WIN32
	encoding = [OFSystemInfo native8BitEncoding];

	if (rename([source cStringWithEncoding: encoding],
	    [destination cStringWithEncoding: encoding]) != 0) {
#else
	if (_wrename([source UTF16String], [destination UTF16String]) != 0) {
#endif
		if (errno != EXDEV)
			@throw [OFMoveItemFailedException
			    exceptionWithSourcePath: source
				    destinationPath: destination];
				    destinationPath: destination
					      errNo: errno];

		@try {
			[OFFile copyItemAtPath: source
					toPath: destination];
		} @catch (OFCopyItemFailedException *e) {
			[OFFile removeItemAtPath: destination];

			@throw [OFMoveItemFailedException
			    exceptionWithSourcePath: source
				    destinationPath: destination];
				    destinationPath: destination
					      errNo: [e errNo]];
		}

		@try {
			[OFFile removeItemAtPath: source];
		} @catch (OFRemoveItemFailedException *e) {
			@throw [OFMoveItemFailedException
			    exceptionWithSourcePath: source
				    destinationPath: destination];
				    destinationPath: destination
					      errNo: [e errNo]];
		}
	}

	objc_autoreleasePoolPop(pool);
}

+ (void)removeItemAtPath: (OFString*)path
{
	void *pool;
	of_stat_t s;

	if (path == nil)
		@throw [OFInvalidArgumentException exception];

	pool = objc_autoreleasePoolPush();

	if (of_lstat(path, &s) != 0)
		@throw [OFRemoveItemFailedException exceptionWithPath: path];
		@throw [OFRemoveItemFailedException exceptionWithPath: path
								errNo: errno];

	if (S_ISDIR(s.st_mode)) {
		OFArray *contents;
		OFEnumerator *enumerator;
		OFString *item;

		@try {
			contents = [OFFile contentsOfDirectoryAtPath: path];
		} @catch (id e) {
			/*
			 * Only convert exceptions to
			 * OFRemoveItemFailedException that have an errNo
			 * property. This covers all I/O related exceptions
			 * from the operations used to remove an item, all
			 * others should be left as is.
			 */
			if ([e respondsToSelector: @selector(errNo)])
			@throw [OFRemoveItemFailedException
			    exceptionWithPath: path];
				@throw [OFRemoveItemFailedException
				    exceptionWithPath: path
						errNo: [e errNo]];

			@throw e;
		}

		enumerator = [contents objectEnumerator];
		while ((item = [enumerator nextObject]) != nil) {
			void *pool2 = objc_autoreleasePoolPush();

			[OFFile removeItemAtPath:
			    [path stringByAppendingPathComponent: item]];

			objc_autoreleasePoolPop(pool2);
		}
	}

#ifndef _WIN32
	if (remove([path cStringWithEncoding:
	    [OFSystemInfo native8BitEncoding]]) != 0)
#else
	if (_wremove([path UTF16String]) != 0)
#endif
		@throw [OFRemoveItemFailedException exceptionWithPath: path];
		@throw [OFRemoveItemFailedException exceptionWithPath: path
								errNo: errno];

	objc_autoreleasePoolPop(pool);
}

#ifdef OF_HAVE_LINK
+ (void)linkItemAtPath: (OFString*)source
		toPath: (OFString*)destination
835
836
837
838
839
840
841
842


843
844
845
846
847
848
849
912
913
914
915
916
917
918

919
920
921
922
923
924
925
926
927







-
+
+







	pool = objc_autoreleasePoolPush();
	encoding = [OFSystemInfo native8BitEncoding];

	if (link([source cStringWithEncoding: encoding],
	    [destination cStringWithEncoding: encoding]) != 0)
		@throw [OFLinkFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination];
			    destinationPath: destination
				      errNo: errno];

	objc_autoreleasePoolPop(pool);
}
#endif

#ifdef OF_HAVE_SYMLINK
+ (void)createSymbolicLinkAtPath: (OFString*)destination
858
859
860
861
862
863
864
865


866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885


886
887
888
889
890
891
892
936
937
938
939
940
941
942

943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963

964
965
966
967
968
969
970
971
972







-
+
+



















-
+
+







	pool = objc_autoreleasePoolPush();
	encoding = [OFSystemInfo native8BitEncoding];

	if (symlink([source cStringWithEncoding: encoding],
	    [destination cStringWithEncoding: encoding]) != 0)
		@throw [OFCreateSymbolicLinkFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination];
			    destinationPath: destination
				      errNo: errno];

	objc_autoreleasePoolPop(pool);
}

+ (OFString*)destinationOfSymbolicLinkAtPath: (OFString*)path
{
	char destination[PATH_MAX];
	ssize_t length;
	of_string_encoding_t encoding;

	if (path == nil)
		@throw [OFInvalidArgumentException exception];

	encoding = [OFSystemInfo native8BitEncoding];
	length = readlink([path cStringWithEncoding: encoding],
	    destination, PATH_MAX);

	if (length < 0)
		@throw [OFOpenFileFailedException exceptionWithPath: path
							       mode: @"r"];
							       mode: @"r"
							      errNo: errno];

	return [OFString stringWithCString: destination
				  encoding: encoding
				    length: length];
}
#endif

916
917
918
919
920
921
922
923


924
925
926
927
928
929
930
996
997
998
999
1000
1001
1002

1003
1004
1005
1006
1007
1008
1009
1010
1011







-
+
+







		    native8BitEncoding]], flags, DEFAULT_MODE)) == -1)
#else
		if ((_fd = open([path cStringWithEncoding: [OFSystemInfo
		    native8BitEncoding]], flags, DEFAULT_MODE)) == -1)
#endif
			@throw [OFOpenFileFailedException
			    exceptionWithPath: path
					 mode: mode];
					 mode: mode
					errNo: errno];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
947
948
949
950
951
952
953
954
955
956







957
958


959
960
961
962
963
964

965
966


967
968
969
970
971
972
973
974
975
976
977




978
979

980
981


982
983
984
985
986
987

988
989


990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007


1008
1009
1010
1011
1012
1013
1014
1028
1029
1030
1031
1032
1033
1034



1035
1036
1037
1038
1039
1040
1041
1042

1043
1044
1045
1046
1047
1048


1049
1050

1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068

1069
1070

1071
1072
1073
1074
1075
1076


1077
1078

1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097

1098
1099
1100
1101
1102
1103
1104
1105
1106







-
-
-
+
+
+
+
+
+
+

-
+
+




-
-
+

-
+
+











+
+
+
+

-
+

-
+
+




-
-
+

-
+
+

















-
+
+







}

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

#ifndef _WIN32
	if (_fd == -1 || _atEndOfStream ||
	    (ret = read(_fd, buffer, length)) < 0)

	if (_fd == -1 || _atEndOfStream)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];

#ifndef _WIN32
	if ((ret = read(_fd, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];
						  requestedLength: length
							    errNo: errno];
#else
	if (length > UINT_MAX)
		@throw [OFOutOfRangeException exception];

	if (_fd == -1 || _atEndOfStream ||
	    (ret = read(_fd, buffer, (unsigned int)length)) < 0)
	if ((ret = read(_fd, buffer, (unsigned int)length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];
						  requestedLength: length
							    errNo: errno];
#endif

	if (ret == 0)
		_atEndOfStream = true;

	return ret;
}

- (void)lowlevelWriteBuffer: (const void*)buffer
		     length: (size_t)length
{
	if (_fd == -1 || _atEndOfStream)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];

#ifndef _WIN32
	if (_fd == -1 || _atEndOfStream || write(_fd, buffer, length) < length)
	if (write(_fd, buffer, length) < length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];
						   requestedLength: length
							     errNo: errno];
#else
	if (length > UINT_MAX)
		@throw [OFOutOfRangeException exception];

	if (_fd == -1 || _atEndOfStream ||
	    write(_fd, buffer, (unsigned int)length) < length)
	if (write(_fd, buffer, (unsigned int)length) < length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];
						   requestedLength: length
							     errNo: errno];
#endif
}

- (of_offset_t)lowlevelSeekToOffset: (of_offset_t)offset
			     whence: (int)whence
{
#if defined(_WIN32)
	of_offset_t ret = _lseeki64(_fd, offset, whence);
#elif defined(OF_HAVE_OFF64_T)
	of_offset_t ret = lseek64(_fd, offset, whence);
#else
	of_offset_t ret = lseek(_fd, offset, whence);
#endif

	if (ret == -1)
		@throw [OFSeekFailedException exceptionWithStream: self
							   offset: offset
							   whence: whence];
							   whence: whence
							    errNo: errno];

	_atEndOfStream = false;

	return ret;
}

- (int)fileDescriptorForReading