Skip to content

Foundation: update for BOOL bridging #2177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions Foundation/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ open class FileHandle : NSObject {
}

var fiFileInfo: BY_HANDLE_FILE_INFORMATION = BY_HANDLE_FILE_INFORMATION()
if GetFileInformationByHandle(_handle, &fiFileInfo) == FALSE {
if !GetFileInformationByHandle(_handle, &fiFileInfo) {
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
}

Expand All @@ -230,10 +230,10 @@ open class FileHandle : NSObject {
MapViewOfFile(hMapping, DWORD(FILE_MAP_READ), 0, 0, szMapSize)

return NSData.NSDataReadResult(bytes: pData, length: Int(szMapSize)) { buffer, length in
if UnmapViewOfFile(buffer) == FALSE {
if !UnmapViewOfFile(buffer) {
fatalError("UnmapViewOfFile failed")
}
if CloseHandle(hMapping) == FALSE {
if !CloseHandle(hMapping) {
fatalError("CloseHandle failed")
}
}
Expand All @@ -255,7 +255,7 @@ open class FileHandle : NSObject {
}

var BytesRead: DWORD = 0
if ReadFile(_handle, buffer.advanced(by: total), BytesToRead, &BytesRead, nil) == FALSE {
if !ReadFile(_handle, buffer.advanced(by: total), BytesToRead, &BytesRead, nil) {
free(buffer)
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
}
Expand Down Expand Up @@ -349,7 +349,7 @@ open class FileHandle : NSObject {
#if os(Windows)
var BytesRead: DWORD = 0
let BytesToRead: DWORD = DWORD(length)
if ReadFile(_handle, buffer, BytesToRead, &BytesRead, nil) == FALSE {
if !ReadFile(_handle, buffer, BytesToRead, &BytesRead, nil) {
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
}
return Int(BytesRead)
Expand All @@ -367,7 +367,7 @@ open class FileHandle : NSObject {
var bytesRemaining = length
while bytesRemaining > 0 {
var bytesWritten: DWORD = 0
if WriteFile(handle, buf.advanced(by: length - bytesRemaining), DWORD(bytesRemaining), &bytesWritten, nil) == FALSE {
if !WriteFile(handle, buf.advanced(by: length - bytesRemaining), DWORD(bytesRemaining), &bytesWritten, nil) {
throw _NSErrorWithErrno(Int32(GetLastError()), reading: false, path: nil)
}
if bytesWritten == 0 {
Expand Down Expand Up @@ -399,10 +399,7 @@ open class FileHandle : NSObject {
public init(fileDescriptor fd: Int32, closeOnDealloc closeopt: Bool) {
if (closeopt) {
var handle: HANDLE?
if DuplicateHandle(GetCurrentProcess(),
HANDLE(bitPattern: _get_osfhandle(fd))!,
GetCurrentProcess(), &handle,
0, FALSE, DWORD(DUPLICATE_SAME_ACCESS)) == FALSE {
if !DuplicateHandle(GetCurrentProcess(), HANDLE(bitPattern: _get_osfhandle(fd))!, GetCurrentProcess(), &handle, 0, false, DWORD(DUPLICATE_SAME_ACCESS)) {
fatalError("DuplicateHandle() failed: \(GetLastError())")
}
_close(fd)
Expand Down Expand Up @@ -512,7 +509,7 @@ open class FileHandle : NSObject {

#if os(Windows)
var liPointer: LARGE_INTEGER = LARGE_INTEGER(QuadPart: 0)
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: 0), &liPointer, DWORD(FILE_CURRENT)) != FALSE else {
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: 0), &liPointer, DWORD(FILE_CURRENT)) else {
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
}
return UInt64(liPointer.QuadPart)
Expand All @@ -532,7 +529,7 @@ open class FileHandle : NSObject {

#if os(Windows)
var liPointer: LARGE_INTEGER = LARGE_INTEGER(QuadPart: 0)
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: 0), &liPointer, DWORD(FILE_END)) != FALSE else {
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: 0), &liPointer, DWORD(FILE_END)) else {
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
}
return UInt64(liPointer.QuadPart)
Expand All @@ -550,7 +547,7 @@ open class FileHandle : NSObject {
guard _isPlatformHandleValid else { throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadUnknown.rawValue) }

#if os(Windows)
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)), nil, DWORD(FILE_BEGIN)) != FALSE else {
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)), nil, DWORD(FILE_BEGIN)) else {
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
}
#else
Expand All @@ -565,11 +562,10 @@ open class FileHandle : NSObject {
guard _isPlatformHandleValid else { throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue) }

#if os(Windows)
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)),
nil, DWORD(FILE_BEGIN)) != FALSE else {
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)), nil, DWORD(FILE_BEGIN)) else {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
}
guard SetEndOfFile(_handle) != FALSE else {
guard SetEndOfFile(_handle) else {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
}
#else
Expand All @@ -583,7 +579,7 @@ open class FileHandle : NSObject {
guard self != FileHandle._nulldeviceFileHandle else { return }

#if os(Windows)
guard FlushFileBuffers(_handle) != FALSE else {
guard FlushFileBuffers(_handle) else {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
}
#else
Expand Down Expand Up @@ -626,7 +622,7 @@ open class FileHandle : NSObject {
privateAsyncVariablesLock.unlock()

#if os(Windows)
guard CloseHandle(_handle) != FALSE else {
guard CloseHandle(_handle) else {
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
}
_handle = INVALID_HANDLE_VALUE
Expand Down Expand Up @@ -971,7 +967,7 @@ open class Pipe: NSObject {
#if os(Windows)
var hReadPipe: HANDLE?
var hWritePipe: HANDLE?
if CreatePipe(&hReadPipe, &hWritePipe, nil, 0) == FALSE {
if !CreatePipe(&hReadPipe, &hWritePipe, nil, 0) {
fatalError("CreatePipe failed")
}
self.fileHandleForReading = FileHandle(handle: hReadPipe!,
Expand Down
46 changes: 22 additions & 24 deletions Foundation/FileManager+Win32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extension FileManager {
var wszPathNames: UnsafeMutableBufferPointer<WCHAR> = UnsafeMutableBufferPointer<WCHAR>.allocate(capacity: Int(dwCChReturnLength + 1))
defer { wszPathNames.deallocate() }

if GetVolumePathNamesForVolumeNameW(wszVolumeName.baseAddress, wszPathNames.baseAddress, DWORD(wszPathNames.count), &dwCChReturnLength) == FALSE {
if !GetVolumePathNamesForVolumeNameW(wszVolumeName.baseAddress, wszPathNames.baseAddress, DWORD(wszPathNames.count), &dwCChReturnLength) {
// TODO(compnerd) handle error
continue
}
Expand All @@ -55,7 +55,7 @@ extension FileManager {
urls.append(URL(fileURLWithPath: path, isDirectory: true))
pPath += DWORD(path.length + 1)
} while pPath < dwCChReturnLength
} while FindNextVolumeW(hVolumes, wszVolumeName.baseAddress, DWORD(wszVolumeName.count)) != FALSE
} while FindNextVolumeW(hVolumes, wszVolumeName.baseAddress, DWORD(wszVolumeName.count))
return urls
}
internal func _urls(for directory: SearchPathDirectory, in domainMask: SearchPathDomainMask) -> [URL] {
Expand Down Expand Up @@ -174,13 +174,13 @@ extension FileManager {
var saAttributes: SECURITY_ATTRIBUTES =
SECURITY_ATTRIBUTES(nLength: DWORD(MemoryLayout<SECURITY_ATTRIBUTES>.size),
lpSecurityDescriptor: nil,
bInheritHandle: FALSE)
bInheritHandle: false)
let psaAttributes: UnsafeMutablePointer<SECURITY_ATTRIBUTES> =
UnsafeMutablePointer<SECURITY_ATTRIBUTES>(&saAttributes)


try path.withCString(encodedAs: UTF16.self) {
if CreateDirectoryW($0, psaAttributes) == FALSE {
if !CreateDirectoryW($0, psaAttributes) {
// FIXME(compnerd) pass along path
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
}
Expand Down Expand Up @@ -208,7 +208,7 @@ extension FileManager {
}

try closure(path, Int32(ffd.dwFileAttributes))
} while FindNextFileW(hDirectory, &ffd) != FALSE
} while FindNextFileW(hDirectory, &ffd)
}
}

Expand All @@ -229,7 +229,7 @@ extension FileManager {
internal func windowsFileAttributes(atPath path: String) throws -> WIN32_FILE_ATTRIBUTE_DATA {
var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = WIN32_FILE_ATTRIBUTE_DATA()
return try path.withCString(encodedAs: UTF16.self) {
if GetFileAttributesExW($0, GetFileExInfoStandard, &faAttributes) == FALSE {
if !GetFileAttributesExW($0, GetFileExInfoStandard, &faAttributes) {
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
}
return faAttributes
Expand All @@ -244,14 +244,14 @@ extension FileManager {
let szVolumePath: UnsafeMutableBufferPointer<WCHAR> = UnsafeMutableBufferPointer<WCHAR>.allocate(capacity: Int(dwLength + 1))
defer { szVolumePath.deallocate() }

guard GetVolumePathNameW($0, szVolumePath.baseAddress, dwLength) != FALSE else {
guard GetVolumePathNameW($0, szVolumePath.baseAddress, dwLength) else {
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
}

var liTotal: ULARGE_INTEGER = ULARGE_INTEGER()
var liFree: ULARGE_INTEGER = ULARGE_INTEGER()

guard GetDiskFreeSpaceExW(szVolumePath.baseAddress, nil, &liTotal, &liFree) != FALSE else {
guard GetDiskFreeSpaceExW(szVolumePath.baseAddress, nil, &liTotal, &liFree) else {
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
}

Expand All @@ -271,7 +271,7 @@ extension FileManager {

try path.withCString(encodedAs: UTF16.self) { name in
try destPath.withCString(encodedAs: UTF16.self) { dest in
guard CreateSymbolicLinkW(name, dest, dwFlags) != FALSE else {
guard CreateSymbolicLinkW(name, dest, dwFlags) != 0 else {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
}
}
Expand Down Expand Up @@ -303,7 +303,7 @@ extension FileManager {
internal func _copyRegularFile(atPath srcPath: String, toPath dstPath: String, variant: String = "Copy") throws {
try srcPath.withCString(encodedAs: UTF16.self) { src in
try dstPath.withCString(encodedAs: UTF16.self) { dst in
if CopyFileW(src, dst, FALSE) == FALSE {
if !CopyFileW(src, dst, false) {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
}
}
Expand Down Expand Up @@ -365,7 +365,7 @@ extension FileManager {

try srcPath.withCString(encodedAs: UTF16.self) { src in
try dstPath.withCString(encodedAs: UTF16.self) { dst in
if MoveFileExW(src, dst, DWORD(MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) == FALSE {
if !MoveFileExW(src, dst, DWORD(MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
}
}
Expand All @@ -383,7 +383,7 @@ extension FileManager {
case .typeRegular:
try srcPath.withCString(encodedAs: UTF16.self) { src in
try dstPath.withCString(encodedAs: UTF16.self) { dst in
if CreateHardLinkW(src, dst, nil) == FALSE {
if !CreateHardLinkW(src, dst, nil) {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
}
}
Expand All @@ -408,7 +408,7 @@ extension FileManager {
}
let faAttributes = try! windowsFileAttributes(atPath: path)
if faAttributes.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY) == 0 {
if path.withCString(encodedAs: UTF16.self, DeleteFileW) == 0 {
if !path.withCString(encodedAs: UTF16.self, DeleteFileW) {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
}
return
Expand All @@ -421,7 +421,7 @@ extension FileManager {
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
continue
}
guard path.withCString(encodedAs: UTF16.self, RemoveDirectoryW) == 0 else {
guard !path.withCString(encodedAs: UTF16.self, RemoveDirectoryW) else {
continue
}
guard GetLastError() == ERROR_DIR_NOT_EMPTY else {
Expand Down Expand Up @@ -451,11 +451,11 @@ extension FileManager {
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
continue
}
if itemPath.withCString(encodedAs: UTF16.self, DeleteFileW) == 0 {
if !itemPath.withCString(encodedAs: UTF16.self, DeleteFileW) {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
}
}
} while(FindNextFileW(h, &ffd) != 0)
} while FindNextFileW(h, &ffd)
} catch {
if !shouldProceedAfterError(error, removingItemAtPath: itemPath, isURL: isURL) {
throw error
Expand All @@ -475,7 +475,7 @@ extension FileManager {

@discardableResult
internal func _changeCurrentDirectoryPath(_ path: String) -> Bool {
return path.withCString(encodedAs: UTF16.self) { SetCurrentDirectoryW($0) != FALSE }
return path.withCString(encodedAs: UTF16.self) { SetCurrentDirectoryW($0) }
}

internal func _fileExists(atPath path: String, isDirectory: UnsafeMutablePointer<ObjCBool>?) -> Bool {
Expand Down Expand Up @@ -597,7 +597,7 @@ extension FileManager {
internal func _appendSymlinkDestination(_ dest: String, toPath: String) -> String {
var isAbsolutePath: Bool = false
dest.withCString(encodedAs: UTF16.self) {
isAbsolutePath = PathIsRelativeW($0) == FALSE
isAbsolutePath = !PathIsRelativeW($0)
}

if isAbsolutePath {
Expand Down Expand Up @@ -645,7 +645,7 @@ extension FileManager {
|| (ffd.dwFileAttributes & DWORD(FILE_ATTRIBUTE_HIDDEN) == 0)) {
files.append(URL(fileURLWithPath: joinPath(prefix: dirFSR, suffix: file)))
}
} while(FindNextFileW(h, &ffd) != 0)
} while FindNextFileW(h, &ffd)
return files
}
while let url = _stack.popLast() {
Expand Down Expand Up @@ -699,18 +699,16 @@ extension FileManager.NSPathDirectoryEnumerator {
(fsr.flatMap { String(utf8String: $0) })?.withCString(encodedAs: UTF16.self) { f($0) }
}
}
let result = withURLCString(url: baseURL) { pszFrom -> BOOL? in
guard withURLCString(url: baseURL, { pszFrom -> Bool? in
withURLCString(url: url) { pszTo in
let fromAttrs = GetFileAttributesW(pszFrom)
let toAttrs = GetFileAttributesW(pszTo)
guard fromAttrs != INVALID_FILE_ATTRIBUTES, toAttrs != INVALID_FILE_ATTRIBUTES else {
return FALSE
return false
}
return PathRelativePathToW(relativePath.baseAddress, pszFrom, fromAttrs, pszTo, toAttrs)
}
}

guard result == TRUE, let (path, _) = String.decodeCString(relativePath.baseAddress, as: UTF16.self) else {
}) == true, let (path, _) = String.decodeCString(relativePath.baseAddress, as: UTF16.self) else {
return nil
}
_currentItemPath = path
Expand Down
5 changes: 1 addition & 4 deletions Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -997,10 +997,7 @@ public struct FileAttributeType : RawRepresentable, Equatable, Hashable {
}
defer { CloseHandle(fileHandle) }
var tagInfo = FILE_ATTRIBUTE_TAG_INFO()
guard 0 != GetFileInformationByHandleEx(fileHandle,
FileAttributeTagInfo,
&tagInfo,
DWORD(MemoryLayout<FILE_ATTRIBUTE_TAG_INFO>.size)) else {
guard GetFileInformationByHandleEx(fileHandle, FileAttributeTagInfo, &tagInfo, DWORD(MemoryLayout<FILE_ATTRIBUTE_TAG_INFO>.size)) else {
self = .typeUnknown
return
}
Expand Down
2 changes: 1 addition & 1 deletion Foundation/Host.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ open class Host: NSObject {
return "localhost"
}
defer { hostname.deallocate() }
guard GetComputerNameExA(ComputerNameDnsHostname, hostname, &dwLength) != FALSE else {
guard GetComputerNameExA(ComputerNameDnsHostname, hostname, &dwLength) else {
return "localhost"
}
return String(cString: hostname)
Expand Down
15 changes: 7 additions & 8 deletions Foundation/NSLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ open class NSLock: NSObject, NSLocking {

open func `try`() -> Bool {
#if os(Windows)
return TryAcquireSRWLockExclusive(mutex) != FALSE
return TryAcquireSRWLockExclusive(mutex) != 0
#else
return pthread_mutex_trylock(mutex) == 0
#endif
}

open func lock(before limit: Date) -> Bool {
#if os(Windows)
if TryAcquireSRWLockExclusive(mutex) != FALSE {
if TryAcquireSRWLockExclusive(mutex) != 0 {
return true
}
#else
Expand Down Expand Up @@ -287,15 +287,15 @@ open class NSRecursiveLock: NSObject, NSLocking {

open func `try`() -> Bool {
#if os(Windows)
return TryEnterCriticalSection(mutex) != FALSE
return TryEnterCriticalSection(mutex)
#else
return pthread_mutex_trylock(mutex) == 0
#endif
}

open func lock(before limit: Date) -> Bool {
#if os(Windows)
if TryEnterCriticalSection(mutex) != FALSE {
if TryEnterCriticalSection(mutex) {
return true
}
#else
Expand Down Expand Up @@ -370,8 +370,7 @@ open class NSCondition: NSObject, NSLocking {

open func wait(until limit: Date) -> Bool {
#if os(Windows)
return SleepConditionVariableSRW(cond, mutex, timeoutFrom(date: limit),
0) != FALSE
return SleepConditionVariableSRW(cond, mutex, timeoutFrom(date: limit), 0)
#else
guard var timeout = timeSpecFrom(date: limit) else {
return false
Expand Down Expand Up @@ -450,7 +449,7 @@ private func timedLock(mutex: _MutexPointer, endTime: Date,
SleepConditionVariableSRW(timeoutCond, timeoutMutex,
timeoutFrom(date: endTime), 0)
ReleaseSRWLockExclusive(timeoutMutex)
if TryAcquireSRWLockExclusive(mutex) != FALSE {
if TryAcquireSRWLockExclusive(mutex) != 0 {
return true
}
} while timeoutFrom(date: endTime) != 0
Expand All @@ -465,7 +464,7 @@ private func timedLock(mutex: _RecursiveMutexPointer, endTime: Date,
SleepConditionVariableSRW(timeoutCond, timeoutMutex,
timeoutFrom(date: endTime), 0)
ReleaseSRWLockExclusive(timeoutMutex)
if TryEnterCriticalSection(mutex) != FALSE {
if TryEnterCriticalSection(mutex) {
return true
}
} while timeoutFrom(date: endTime) != 0
Expand Down
Loading