Index: src/OFFileURLHandler.m ================================================================== --- src/OFFileURLHandler.m +++ src/OFFileURLHandler.m @@ -137,74 +137,60 @@ { return (double)((int64_t)filetime->dwHighDateTime << 32 | filetime->dwLowDateTime) / 10000000.0 - 11644473600.0; } -static void -setErrno(void) +static int +retrieveError(void) { switch (GetLastError()) { case ERROR_FILE_NOT_FOUND: case ERROR_PATH_NOT_FOUND: case ERROR_NO_MORE_FILES: - errno = ENOENT; - return; + return ENOENT; case ERROR_ACCESS_DENIED: - errno = EACCES; - return; + return EACCES; case ERROR_DIRECTORY: - errno = ENOTDIR; - return; + return ENOTDIR; case ERROR_NOT_READY: - errno = EBUSY; - return; + return EBUSY; + default: + return EIO; } - - errno = 0; } #endif #ifdef OF_AMIGAOS -static void -setErrno(void) +static int +retrieveError(void) { switch (IoErr()) { case ERROR_DELETE_PROTECTED: case ERROR_READ_PROTECTED: case ERROR_WRITE_PROTECTED: - errno = EACCES; - break; + return EACCES; case ERROR_DISK_NOT_VALIDATED: case ERROR_OBJECT_IN_USE: - errno = EBUSY; - break; + return EBUSY; case ERROR_OBJECT_EXISTS: - errno = EEXIST; - break; + return EEXIST; case ERROR_DIR_NOT_FOUND: case ERROR_NO_MORE_ENTRIES: case ERROR_OBJECT_NOT_FOUND: - errno = ENOENT; - break; + return ENOENT; case ERROR_NO_FREE_STORE: - errno = ENOMEM; - break; + return ENOMEM; case ERROR_DISK_FULL: - errno = ENOSPC; - break; + return ENOSPC; case ERROR_DIRECTORY_NOT_EMPTY: - errno = ENOTEMPTY; - break; + return ENOTEMPTY; case ERROR_DISK_WRITE_PROTECTED: - errno = EROFS; - break; + return EROFS; case ERROR_RENAME_ACROSS_DEVICES: - errno = EXDEV; - break; + return EXDEV; default: - errno = 0; - break; + return EIO; } } #endif static int @@ -220,14 +206,12 @@ else success = GetFileAttributesExA( [path cStringWithEncoding: [OFLocale encoding]], GetFileExInfoStandard, &data); - if (!success) { - setErrno(); - return -1; - } + if (!success) + return retrieveError(); buffer->st_size = (uint64_t)data.nFileSizeHigh << 32 | data.nFileSizeLow; if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) @@ -239,22 +223,18 @@ */ WIN32_FIND_DATAW findData; HANDLE findHandle; if ((findHandle = FindFirstFileW(path.UTF16String, - &findData)) == INVALID_HANDLE_VALUE) { - setErrno(); - return -1; - } + &findData)) == INVALID_HANDLE_VALUE) + return retrieveError(); @try { if (!(findData.dwFileAttributes & - FILE_ATTRIBUTE_REPARSE_POINT)) { + FILE_ATTRIBUTE_REPARSE_POINT)) /* Race? Indicate to try again. */ - errno = EAGAIN; - return -1; - } + return EAGAIN; buffer->st_mode = (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK ? S_IFLNK : S_IFREG); } @finally { @@ -283,24 +263,21 @@ of_time_interval_t timeInterval; struct Locale *locale; struct DateStamp *date; if ((lock = Lock([path cStringWithEncoding: [OFLocale encoding]], - SHARED_LOCK)) == 0) { - setErrno(); - return -1; - } + SHARED_LOCK)) == 0) + return retrieveError(); # ifdef OF_AMIGAOS4 if ((ed = ExamineObjectTags(EX_FileLockInput, lock, TAG_END)) == NULL) { # else if (!Examine(lock, &fib)) { # endif + int error = retrieveError(); UnLock(lock); - - errno = 0; - return -1; + return error(); } UnLock(lock); # ifdef OF_AMIGAOS4 @@ -337,26 +314,38 @@ FreeDosObject(DOS_EXAMINEDATA, ed); # endif return 0; #elif defined(HAVE_STAT64) - return stat64([path cStringWithEncoding: [OFLocale encoding]], buffer); + if (stat64([path cStringWithEncoding: [OFLocale encoding]], + buffer) != 0) + return errno; + + return 0; #else - return stat([path cStringWithEncoding: [OFLocale encoding]], buffer); + if (stat([path cStringWithEncoding: [OFLocale encoding]], buffer) != 0) + return errno; + + return 0; #endif } static int of_lstat(OFString *path, of_stat_t *buffer) { #if defined(HAVE_LSTAT) && !defined(OF_WINDOWS) && !defined(OF_AMIGAOS) && \ !defined(OF_NINTENDO_3DS) && !defined(OF_WII) # ifdef HAVE_LSTAT64 - return lstat64([path cStringWithEncoding: [OFLocale encoding]], buffer); + if (lstat64([path cStringWithEncoding: [OFLocale encoding]], + buffer) != 0) + return errno; # else - return lstat([path cStringWithEncoding: [OFLocale encoding]], buffer); + if (lstat([path cStringWithEncoding: [OFLocale encoding]], buffer) != 0) + return errno; # endif + + return 0; #else return of_stat(path, buffer); #endif } @@ -497,11 +486,11 @@ 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 [OFRetrieveItemAttributesFailedException exceptionWithURL: URL - errNo: 0]; + errNo: retrieveError()]; @try { union { char bytes[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; REPARSE_DATA_BUFFER data; @@ -513,16 +502,16 @@ if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, buffer.bytes, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &size, NULL)) @throw [OFRetrieveItemAttributesFailedException exceptionWithURL: URL - errNo: 0]; + errNo: retrieveError()]; if (buffer.data.ReparseTag != IO_REPARSE_TAG_SYMLINK) @throw [OFRetrieveItemAttributesFailedException exceptionWithURL: URL - errNo: 0]; + errNo: retrieveError()]; # define slrb buffer.data.SymbolicLinkReparseBuffer tmp = slrb.PathBuffer + (slrb.SubstituteNameOffset / sizeof(wchar_t)); @@ -586,11 +575,11 @@ + (bool)of_directoryExistsAtPath: (OFString *)path { of_stat_t s; - if (of_stat(path, &s) == -1) + if (of_stat(path, &s) != 0) return false; return S_ISDIR(s.st_mode); } @@ -610,10 +599,11 @@ - (of_file_attributes_t)attributesOfItemAtURL: (OFURL *)URL { of_mutable_file_attributes_t ret = [OFMutableDictionary dictionary]; void *pool = objc_autoreleasePoolPush(); OFString *path; + int error; of_stat_t s; if (URL == nil) @throw [OFInvalidArgumentException exception]; @@ -620,14 +610,14 @@ if (![[URL scheme] isEqual: _scheme]) @throw [OFInvalidArgumentException exception]; path = URL.fileSystemRepresentation; - if (of_lstat(path, &s) == -1) + if ((error = of_lstat(path, &s)) != 0) @throw [OFRetrieveItemAttributesFailedException exceptionWithURL: URL - errNo: errno]; + errNo: error]; if (s.st_size < 0) @throw [OFOutOfRangeException exception]; [ret setObject: [NSNumber numberWithUnsignedLongLong: s.st_size] @@ -728,23 +718,20 @@ date.ds_Minute = ((LONG)modificationTime % 86400) / 60; date.ds_Tick = fmod(modificationTime, 60) * TICKS_PER_SECOND; # ifdef OF_AMIGAOS4 if (!SetDate([path cStringWithEncoding: [OFLocale encoding]], - &date) != 0) { + &date) != 0) # else if (!SetFileDate([path cStringWithEncoding: [OFLocale encoding]], - &date) != 0) { + &date) != 0) # endif - setErrno(); - @throw [OFSetItemAttributesFailedException exceptionWithURL: URL attributes: attributes failedAttribute: attributeKey - errNo: errno]; - } + errNo: retrieveError()]; #else of_time_interval_t lastAccessTime = lastAccessDate.timeIntervalSince1970; of_time_interval_t modificationTime = modificationDate.timeIntervalSince1970; @@ -933,11 +920,11 @@ @throw [OFInvalidArgumentException exception]; if (![URL.scheme isEqual: _scheme]) @throw [OFInvalidArgumentException exception]; - if (of_stat(URL.fileSystemRepresentation, &s) == -1) { + if (of_stat(URL.fileSystemRepresentation, &s) != 0) { objc_autoreleasePoolPop(pool); return false; } ret = S_ISREG(s.st_mode); @@ -957,11 +944,11 @@ @throw [OFInvalidArgumentException exception]; if (![URL.scheme isEqual: _scheme]) @throw [OFInvalidArgumentException exception]; - if (of_stat(URL.fileSystemRepresentation, &s) == -1) { + if (of_stat(URL.fileSystemRepresentation, &s) != 0) { objc_autoreleasePoolPop(pool); return false; } ret = S_ISDIR(s.st_mode); @@ -999,17 +986,14 @@ errNo: errno]; #elif defined(OF_AMIGAOS) BPTR lock; if ((lock = CreateDir( - [path cStringWithEncoding: [OFLocale encoding]])) == 0) { - setErrno(); - + [path cStringWithEncoding: [OFLocale encoding]])) == 0) @throw [OFCreateDirectoryFailedException exceptionWithURL: URL - errNo: errno]; - } + errNo: retrieveError()]; UnLock(lock); #else if (mkdir([path cStringWithEncoding: [OFLocale encoding]], 0777) != 0) @throw [OFCreateDirectoryFailedException @@ -1041,21 +1025,15 @@ if ([OFSystemInfo isWindowsNT]) { WIN32_FIND_DATAW fd; if ((handle = FindFirstFileW(path.UTF16String, - &fd)) == INVALID_HANDLE_VALUE) { - int errNo = 0; - - if (GetLastError() == ERROR_FILE_NOT_FOUND) - errNo = ENOENT; - + &fd)) == INVALID_HANDLE_VALUE) @throw [OFOpenItemFailedException exceptionWithURL: URL mode: nil - errNo: errNo]; - } + errNo: retrieveError()]; @try { do { OFString *file; @@ -1074,31 +1052,25 @@ if (GetLastError() != ERROR_NO_MORE_FILES) @throw [OFReadFailedException exceptionWithObject: self requestedLength: 0 - errNo: EIO]; + errNo: retrieveError()]; } @finally { FindClose(handle); } } else { of_string_encoding_t encoding = [OFLocale encoding]; WIN32_FIND_DATA fd; if ((handle = FindFirstFileA( [path cStringWithEncoding: encoding], &fd)) == - INVALID_HANDLE_VALUE) { - int errNo = 0; - - if (GetLastError() == ERROR_FILE_NOT_FOUND) - errNo = ENOENT; - + INVALID_HANDLE_VALUE) @throw [OFOpenItemFailedException exceptionWithURL: URL mode: nil - errNo: errNo]; - } + errNo: retrieveError()]; @try { do { OFString *file; @@ -1118,27 +1090,25 @@ if (GetLastError() != ERROR_NO_MORE_FILES) @throw [OFReadFailedException exceptionWithObject: self requestedLength: 0 - errNo: EIO]; + errNo: retrieveError()]; } @finally { FindClose(handle); } } #elif defined(OF_AMIGAOS) of_string_encoding_t encoding = [OFLocale encoding]; BPTR lock; if ((lock = Lock([path cStringWithEncoding: encoding], - SHARED_LOCK)) == 0) { - setErrno(); - - @throw [OFOpenItemFailedException exceptionWithURL: URL - mode: nil - errNo: errno]; - } + SHARED_LOCK)) == 0) + @throw [OFOpenItemFailedException + exceptionWithURL: URL + mode: nil + errNo: retrieveError()]; @try { # ifdef OF_AMIGAOS4 struct ExamineData *ed; APTR context; @@ -1147,11 +1117,11 @@ EX_DoCurrentDir, TRUE, EX_DataFields, EXF_NAME, TAG_END)) == NULL) @throw [OFOpenItemFailedException exceptionWithURL: URL mode: nil - errNo: 0]; + errNo: retrieveError()]; @try { while ((ed = ExamineDir(context)) != NULL) { OFString *file = [[OFString alloc] initWithCString: ed->Name @@ -1171,11 +1141,11 @@ if (!Examine(lock, &fib)) @throw [OFOpenItemFailedException exceptionWithURL: URL mode: nil - errNo: 0]; + errNo: retrieveError()]; while (ExNext(lock, &fib)) { OFString *file = [[OFString alloc] initWithCString: fib.fib_FileName encoding: encoding]; @@ -1190,11 +1160,11 @@ if (IoErr() != ERROR_NO_MORE_ENTRIES) @throw [OFReadFailedException exceptionWithObject: self requestedLength: 0 - errNo: EIO]; + errNo: retrieveError()]; } @finally { UnLock(lock); } #else of_string_encoding_t encoding = [OFLocale encoding]; @@ -1273,10 +1243,11 @@ - (void)removeItemAtURL: (OFURL *)URL { void *pool = objc_autoreleasePoolPush(); OFString *path; + int error; of_stat_t s; if (URL == nil) @throw [OFInvalidArgumentException exception]; @@ -1283,13 +1254,13 @@ if (![URL.scheme isEqual: _scheme]) @throw [OFInvalidArgumentException exception]; path = URL.fileSystemRepresentation; - if (of_lstat(path, &s) != 0) + if ((error = of_lstat(path, &s)) != 0) @throw [OFRemoveItemFailedException exceptionWithURL: URL - errNo: errno]; + errNo: error]; if (S_ISDIR(s.st_mode)) { OFArray *contents; @try { @@ -1351,16 +1322,14 @@ errNo: errno]; #endif } #ifdef OF_AMIGAOS - if (!DeleteFile([path cStringWithEncoding: [OFLocale encoding]])) { - setErrno(); - - @throw [OFRemoveItemFailedException exceptionWithURL: URL - errNo: errno]; - } + if (!DeleteFile([path cStringWithEncoding: [OFLocale encoding]])) + @throw [OFRemoveItemFailedException + exceptionWithURL: URL + errNo: retrieveError()]; #endif objc_autoreleasePoolPop(pool); } @@ -1398,11 +1367,11 @@ if (!func_CreateHardLinkW(destinationPath.UTF16String, sourcePath.UTF16String, NULL)) @throw [OFLinkFailedException exceptionWithSourceURL: source destinationURL: destination - errNo: 0]; + errNo: retrieveError()]; # endif objc_autoreleasePoolPop(pool); } #endif @@ -1438,11 +1407,11 @@ if (!func_CreateSymbolicLinkW(path.UTF16String, target.UTF16String, 0)) @throw [OFCreateSymbolicLinkFailedException exceptionWithURL: URL target: target - errNo: 0]; + errNo: retrieveError()]; # endif objc_autoreleasePoolPop(pool); } #endif @@ -1468,18 +1437,15 @@ of_string_encoding_t encoding = [OFLocale encoding]; if (!Rename([source.fileSystemRepresentation cStringWithEncoding: encoding], [destination.fileSystemRepresentation - cStringWithEncoding: encoding])) { - setErrno(); - + cStringWithEncoding: encoding])) @throw [OFMoveItemFailedException exceptionWithSourceURL: source destinationURL: destination - errNo: errno]; - } + errNo: retrieveError()]; #else int status; # ifdef OF_WINDOWS if ([OFSystemInfo isWindowsNT])