Differences From Artifact [2b969b9f4d]:
- File
src/OFFileManager.m
— part of check-in
[4f36894ce7]
at
2017-06-05 17:36:28
on branch trunk
— Clean up exceptions a little
This removes several initializers that omitted the errNo. Removing those
forces to think about whether there is a meaningful errNo to set instead
of just omitting it. (user: js, size: 25351) [annotate] [blame] [check-ins using]
To Artifact [36a1dbfadd]:
- File src/OFFileManager.m — part of check-in [289f2b1272] at 2017-06-05 21:57:38 on branch trunk — OFFileManager: Don't use stat() on MorphOS (user: js, size: 25941) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | #import "OFUnlockFailedException.h" #ifdef OF_WINDOWS # include <windows.h> # include <direct.h> # include <ntdef.h> #endif #if defined(OF_WINDOWS) typedef struct __stat64 of_stat_t; #elif defined(OF_HAVE_OFF64_T) typedef struct stat64 of_stat_t; #else typedef struct stat of_stat_t; #endif static OFFileManager *defaultManager; | > > > > > > > > > > > > > | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > < < | < < | < < < < | 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | #import "OFUnlockFailedException.h" #ifdef OF_WINDOWS # include <windows.h> # include <direct.h> # include <ntdef.h> #endif #ifdef OF_MORPHOS # define BOOL EXEC_BOOL # include <proto/dos.h> # include <proto/locale.h> # undef BOOL #endif #if defined(OF_WINDOWS) typedef struct __stat64 of_stat_t; #elif defined(OF_MORPHOS) typedef struct { of_offset_t st_size; mode_t st_mode; of_time_interval_t st_atime, st_mtime, st_ctime; } of_stat_t; #elif defined(OF_HAVE_OFF64_T) typedef struct stat64 of_stat_t; #else typedef struct stat of_stat_t; #endif static OFFileManager *defaultManager; #if defined(OF_HAVE_CHOWN) && defined(OF_HAVE_THREADS) && !defined(OF_MORPHOS) static OFMutex *passwdMutex; #endif #if !defined(HAVE_READDIR_R) && defined(OF_HAVE_THREADS) && !defined(OF_WINDOWS) static OFMutex *readdirMutex; #endif #ifdef OF_WINDOWS static WINAPI BOOLEAN (*func_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD); #endif static int of_stat(OFString *path, of_stat_t *buffer) { #if defined(OF_WINDOWS) return _wstat64([path UTF16String], buffer); #elif defined(OF_MORPHOS) BPTR lock; struct FileInfoBlock fib; of_time_interval_t timeInterval; struct Locale *locale; if ((lock = Lock([path cStringWithEncoding: [OFLocalization encoding]], SHARED_LOCK)) == 0) { switch (IoErr()) { case ERROR_OBJECT_IN_USE: case ERROR_DISK_NOT_VALIDATED: errno = EBUSY; break; case ERROR_OBJECT_NOT_FOUND: errno = ENOENT; break; default: errno = 0; break; } return -1; } if (!Examine64(lock, &fib, TAG_DONE)) { UnLock(lock); errno = 0; return -1; } UnLock(lock); buffer->st_size = fib.fib_Size64; buffer->st_mode = (fib.fib_DirEntryType > 0 ? S_IFDIR : S_IFREG); timeInterval = 252460800; /* 1978-01-01 */ locale = OpenLocale(NULL); /* * FIXME: This does not take DST into account. But unfortunately, there * is no way to figure out if DST was in effect when the file was * modified. */ timeInterval += locale->loc_GMTOffset * 60.0; CloseLocale(locale); timeInterval += fib.fib_Date.ds_Days * 86400.0; timeInterval += fib.fib_Date.ds_Minute * 60.0; timeInterval += fib.fib_Date.ds_Tick / (of_time_interval_t)TICKS_PER_SECOND; buffer->st_atime = buffer->st_mtime = buffer->st_ctime = timeInterval; return 0; #elif defined(OF_HAVE_OFF64_T) return stat64([path cStringWithEncoding: [OFLocalization encoding]], buffer); #else return stat([path cStringWithEncoding: [OFLocalization encoding]], buffer); #endif } static int of_lstat(OFString *path, of_stat_t *buffer) { #if defined(HAVE_LSTAT) && !defined(OF_WINDOWS) && !defined(OF_MORPHOS) # ifdef OF_HAVE_OFF64_T return lstat64([path cStringWithEncoding: [OFLocalization encoding]], buffer); # else return lstat([path cStringWithEncoding: [OFLocalization encoding]], buffer); # endif #else return of_stat(path, buffer); #endif } @implementation OFFileManager + (void)initialize { #ifdef OF_WINDOWS |
︙ | ︙ | |||
198 199 200 201 202 203 204 | if (path == nil) @throw [OFInvalidArgumentException exception]; if (of_stat(path, &s) == -1) return false; | | < < < | < | < | | < > | < | < < < < < > | 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | if (path == nil) @throw [OFInvalidArgumentException exception]; if (of_stat(path, &s) == -1) return false; return S_ISREG(s.st_mode); } - (bool)directoryExistsAtPath: (OFString *)path { of_stat_t s; if (path == nil) @throw [OFInvalidArgumentException exception]; if (of_stat(path, &s) == -1) return false; return S_ISDIR(s.st_mode); } #ifdef OF_FILE_MANAGER_SUPPORTS_SYMLINKS - (bool)symbolicLinkExistsAtPath: (OFString *)path { # ifndef OF_WINDOWS of_stat_t s; if (path == nil) @throw [OFInvalidArgumentException exception]; if (of_lstat(path, &s) == -1) return false; return S_ISLNK(s.st_mode); # else WIN32_FIND_DATAW data; if (!FindFirstFileW([path UTF16String], &data)) return false; if ((data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) && data.dwReserved0 == IO_REPARSE_TAG_SYMLINK) return true; return false; # endif } #endif - (void)createDirectoryAtPath: (OFString *)path { if (path == nil) @throw [OFInvalidArgumentException exception]; |
︙ | ︙ | |||
499 500 501 502 503 504 505 | @throw [OFStatItemFailedException exceptionWithPath: path errNo: errno]; /* FIXME: We could be more precise on some OSes */ return [OFDate dateWithTimeIntervalSince1970: s.st_ctime]; } | | | 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 | @throw [OFStatItemFailedException exceptionWithPath: path errNo: errno]; /* FIXME: We could be more precise on some OSes */ return [OFDate dateWithTimeIntervalSince1970: s.st_ctime]; } #ifdef OF_FILE_MANAGER_SUPPORTS_PERMISSIONS - (uint16_t)permissionsOfItemAtPath: (OFString *)path { of_stat_t s; if (path == nil) @throw [OFInvalidArgumentException exception]; |
︙ | ︙ | |||
535 536 537 538 539 540 541 | @throw [OFChangePermissionsFailedException exceptionWithPath: path permissions: permissions errNo: errno]; } #endif | | | 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 | @throw [OFChangePermissionsFailedException exceptionWithPath: path permissions: permissions errNo: errno]; } #endif #ifdef OF_FILE_MANAGER_SUPPORTS_OWNER - (void)getOwner: (OFString **)owner group: (OFString **)group ofItemAtPath: (OFString *)path { of_stat_t s; if (path == nil) |
︙ | ︙ | |||
572 573 574 575 576 577 578 | *group = [OFString stringWithCString: group_->gr_name encoding: encoding]; } # ifdef OF_HAVE_THREADS } @finally { [passwdMutex unlock]; } | | | 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | *group = [OFString stringWithCString: group_->gr_name encoding: encoding]; } # ifdef OF_HAVE_THREADS } @finally { [passwdMutex unlock]; } # endif } - (void)changeOwnerOfItemAtPath: (OFString *)path owner: (OFString *)owner group: (OFString *)group { uid_t uid = -1; |
︙ | ︙ | |||
661 662 663 664 665 666 667 | errNo: errno]; if (S_ISDIR(s.st_mode)) { OFArray *contents; @try { [self createDirectoryAtPath: destination]; | | | 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 | errNo: errno]; if (S_ISDIR(s.st_mode)) { OFArray *contents; @try { [self createDirectoryAtPath: destination]; #ifdef OF_FILE_MANAGER_SUPPORTS_PERMISSIONS [self changePermissionsOfItemAtPath: destination permissions: s.st_mode]; #endif contents = [self contentsOfDirectoryAtPath: source]; } @catch (id e) { /* |
︙ | ︙ | |||
722 723 724 725 726 727 728 | length = [sourceFile readIntoBuffer: buffer length: pageSize]; [destinationFile writeBuffer: buffer length: length]; } | | | 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 | length = [sourceFile readIntoBuffer: buffer length: pageSize]; [destinationFile writeBuffer: buffer length: length]; } #ifdef OF_FILE_MANAGER_SUPPORTS_PERMISSIONS [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 |
︙ | ︙ | |||
745 746 747 748 749 750 751 | @throw e; } @finally { [sourceFile close]; [destinationFile close]; free(buffer); } | | | 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 | @throw e; } @finally { [sourceFile close]; [destinationFile close]; free(buffer); } #ifdef OF_FILE_MANAGER_SUPPORTS_SYMLINKS } else if (S_ISLNK(s.st_mode)) { @try { source = [self destinationOfSymbolicLinkAtPath: source]; [self createSymbolicLinkAtPath: destination withDestinationPath: source]; } @catch (id e) { |
︙ | ︙ | |||
782 783 784 785 786 787 788 | } - (void)moveItemAtPath: (OFString *)source toPath: (OFString *)destination { void *pool; of_stat_t s; | < < < | | 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 | } - (void)moveItemAtPath: (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) @throw [OFCopyItemFailedException exceptionWithSourcePath: source destinationPath: destination errNo: EEXIST]; #ifndef OF_WINDOWS of_string_encoding_t encoding = [OFLocalization encoding]; if (rename([source cStringWithEncoding: encoding], [destination cStringWithEncoding: encoding]) != 0) { #else if (_wrename([source UTF16String], [destination UTF16String]) != 0) { #endif if (errno != EXDEV) |
︙ | ︙ | |||
904 905 906 907 908 909 910 | exceptionWithPath: path errNo: errno]; } objc_autoreleasePoolPop(pool); } | < < < < < < | < < < < < < < < < < < < < < < < > > > > > > > > > > > > | < > > | | < < < < < < < < < < < < < > < < > > > > < < < < | < < < < < | 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 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 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 | exceptionWithPath: path errNo: errno]; } objc_autoreleasePoolPop(pool); } #ifdef OF_FILE_MANAGER_SUPPORTS_LINKS - (void)linkItemAtPath: (OFString *)source toPath: (OFString *)destination { void *pool; if (source == nil || destination == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); # ifndef OF_WINDOWS of_string_encoding_t encoding = [OFLocalization encoding]; if (link([source cStringWithEncoding: encoding], [destination cStringWithEncoding: encoding]) != 0) @throw [OFLinkFailedException exceptionWithSourcePath: source destinationPath: destination errNo: errno]; # else if (!CreateHardLinkW([destination UTF16String], [source UTF16String], NULL)) @throw [OFLinkFailedException exceptionWithSourcePath: source destinationPath: destination errNo: 0]; # endif objc_autoreleasePoolPop(pool); } #endif #ifdef OF_FILE_MANAGER_SUPPORTS_SYMLINKS - (void)createSymbolicLinkAtPath: (OFString *)destination withDestinationPath: (OFString *)source { void *pool; if (source == nil || destination == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); # ifndef OF_WINDOWS of_string_encoding_t encoding = [OFLocalization encoding]; if (symlink([source cStringWithEncoding: encoding], [destination cStringWithEncoding: encoding]) != 0) @throw [OFCreateSymbolicLinkFailedException exceptionWithSourcePath: source destinationPath: destination errNo: errno]; # else if (func_CreateSymbolicLinkW == NULL) @throw [OFNotImplementedException exceptionWithSelector: _cmd object: self]; if (!func_CreateSymbolicLinkW([destination UTF16String], [source UTF16String], 0)) @throw [OFCreateSymbolicLinkFailedException exceptionWithSourcePath: source destinationPath: destination errNo: 0]; # endif objc_autoreleasePoolPop(pool); } - (OFString *)destinationOfSymbolicLinkAtPath: (OFString *)path { if (path == nil) @throw [OFInvalidArgumentException exception]; # ifndef OF_WINDOWS char destination[PATH_MAX]; ssize_t length; of_string_encoding_t encoding; encoding = [OFLocalization encoding]; length = readlink([path cStringWithEncoding: encoding], destination, PATH_MAX); if (length < 0) @throw [OFStatItemFailedException exceptionWithPath: path errNo: errno]; return [OFString stringWithCString: destination encoding: encoding length: length]; # else HANDLE handle; /* Check if we're on a version that actually supports symlinks. */ if (func_CreateSymbolicLinkW == NULL) @throw [OFNotImplementedException exceptionWithSelector: _cmd object: self]; if ((handle = CreateFileW([path UTF16String], 0, (FILE_SHARE_READ | FILE_SHARE_WRITE), NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, NULL)) == INVALID_HANDLE_VALUE) @throw [OFStatItemFailedException exceptionWithPath: path errNo: 0]; @try { |
︙ | ︙ | |||
1057 1058 1059 1060 1061 1062 1063 | errNo: 0]; if (buffer.data.ReparseTag != IO_REPARSE_TAG_SYMLINK) @throw [OFStatItemFailedException exceptionWithPath: path errNo: 0]; | | | > | 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 | errNo: 0]; if (buffer.data.ReparseTag != IO_REPARSE_TAG_SYMLINK) @throw [OFStatItemFailedException exceptionWithPath: path errNo: 0]; # define slrb buffer.data.SymbolicLinkReparseBuffer tmp = slrb.PathBuffer + (slrb.SubstituteNameOffset / sizeof(wchar_t)); return [OFString stringWithUTF16String: tmp length: slrb.SubstituteNameLength / sizeof(wchar_t)]; # undef slrb } @finally { CloseHandle(handle); } # endif } #endif @end |