From 4cafcc96f6aa635e1bdbe05a4c9b858b752f08cf Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Wed, 20 Mar 2024 17:27:38 +0000 Subject: [PATCH] Changes after comments from Saleem. Use idiomatic Swift notation, not `Optional`. Prefer Win32 APIs to UCRT APIs. rdar://125087707 --- Sources/System/FilePath/FilePathTemp.swift | 5 ++--- Sources/System/Internals/Mocking.swift | 4 ++-- .../System/Internals/WindowsSyscallAdapters.swift | 15 +++++++++++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Sources/System/FilePath/FilePathTemp.swift b/Sources/System/FilePath/FilePathTemp.swift index 0028671..437d233 100644 --- a/Sources/System/FilePath/FilePathTemp.swift +++ b/Sources/System/FilePath/FilePathTemp.swift @@ -29,8 +29,7 @@ internal func getTemporaryDirectory() throws -> FilePath { capacity: Int(MAX_PATH) + 1) { buffer in - let count = GetTempPath2W(DWORD(buffer.count), buffer.baseAddress) - if count == 0 { + guard GetTempPath2W(DWORD(buffer.count), buffer.baseAddress) != 0 else { throw Errno(windowsError: GetLastError()) } @@ -116,7 +115,7 @@ internal func getTemporaryDirectory() throws -> FilePath { while true { let path: FilePath? = withUnsafeTemporaryAllocation( of: CInterop.PlatformChar.self, - capacity: 1024) { buffer in + capacity: capacity) { buffer in let len = system_confstr(SYSTEM_CS_DARWIN_USER_TEMP_DIR, buffer.baseAddress!, buffer.count) diff --git a/Sources/System/Internals/Mocking.swift b/Sources/System/Internals/Mocking.swift index 08c0f71..2945651 100644 --- a/Sources/System/Internals/Mocking.swift +++ b/Sources/System/Internals/Mocking.swift @@ -110,7 +110,7 @@ private var contextualMockingEnabled: Bool { extension MockingDriver { internal static var enabled: Bool { mockingEnabled } - internal static var forceWindowsPaths: Optional { + internal static var forceWindowsPaths: Bool? { currentMockingDriver?.forceWindowsSyntaxForPaths } } @@ -128,7 +128,7 @@ internal var mockingEnabled: Bool { } @inline(__always) -internal var forceWindowsPaths: Optional { +internal var forceWindowsPaths: Bool? { #if !ENABLE_MOCKING return false #else diff --git a/Sources/System/Internals/WindowsSyscallAdapters.swift b/Sources/System/Internals/WindowsSyscallAdapters.swift index bc83782..6d5c755 100644 --- a/Sources/System/Internals/WindowsSyscallAdapters.swift +++ b/Sources/System/Internals/WindowsSyscallAdapters.swift @@ -160,14 +160,25 @@ internal func mkdir( _ path: UnsafePointer, _ mode: CInterop.Mode ) -> CInt { - return _wmkdir(path) + // TODO: Read/write permissions (these need mapping to a SECURITY_DESCRIPTOR). + if !CreateDirectoryW(path, nil) { + ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + return -1 + } + + return 0; } @inline(__always) internal func rmdir( _ path: UnsafePointer ) -> CInt { - return _wrmdir(path) + if !RemoveDirectoryW(path) { + ucrt._set_errno(mapWindowsErrorToErrno(GetLastError())) + return -1 + } + + return 0; } @usableFromInline