From fda2fe23afa19a55472ecc1573989e13fafb8095 Mon Sep 17 00:00:00 2001 From: Finagolfin Date: Sun, 11 Aug 2024 07:49:07 +0530 Subject: [PATCH] Use the new Bionic module from Swift 6 where possible Also, keep force unwrap for non-Android in `FileHandle._readDataOfLength()` and remove wrong `guard` for Android in `Process.run()`. --- Sources/Foundation/FileHandle.swift | 5 +++++ Sources/Foundation/FileManager.swift | 4 ++-- Sources/Foundation/NSLock.swift | 4 ++-- Sources/Foundation/NSURL.swift | 4 ++-- Sources/Foundation/Process.swift | 7 ------- Sources/Foundation/Thread.swift | 4 ++-- Sources/plutil/main.swift | 4 ++-- Sources/xdgTestHelper/main.swift | 4 ++-- Tests/Foundation/FTPServer.swift | 4 ++-- Tests/Foundation/HTTPServer.swift | 4 ++-- Tests/Foundation/TestFileHandle.swift | 4 ++-- Tests/Foundation/TestProcess.swift | 4 ++-- Tests/Foundation/TestSocketPort.swift | 4 ++-- Tests/Foundation/TestURL.swift | 4 ++-- 14 files changed, 29 insertions(+), 31 deletions(-) diff --git a/Sources/Foundation/FileHandle.swift b/Sources/Foundation/FileHandle.swift index 7a985f2ef8..b540e2844c 100644 --- a/Sources/Foundation/FileHandle.swift +++ b/Sources/Foundation/FileHandle.swift @@ -326,7 +326,12 @@ open class FileHandle : NSObject, @unchecked Sendable { if options.contains(.alwaysMapped) { // Filesizes are often 64bit even on 32bit systems let mapSize = min(length, Int(clamping: statbuf.st_size)) + #if canImport(Android) + // Bionic mmap() now returns _Nonnull, so the force unwrap below isn't needed. let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0) + #else + let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0)! + #endif // Swift does not currently expose MAP_FAILURE if data != UnsafeMutableRawPointer(bitPattern: -1) { return NSData.NSDataReadResult(bytes: data, length: mapSize) { buffer, length in diff --git a/Sources/Foundation/FileManager.swift b/Sources/Foundation/FileManager.swift index b3809f72fd..a19464d766 100644 --- a/Sources/Foundation/FileManager.swift +++ b/Sources/Foundation/FileManager.swift @@ -21,8 +21,8 @@ import WinSDK #if os(WASI) import WASILibc -#elseif canImport(Android) -import Android +#elseif canImport(Bionic) +import Bionic #endif #if os(Windows) diff --git a/Sources/Foundation/NSLock.swift b/Sources/Foundation/NSLock.swift index ddb63125d8..9d0fadc910 100644 --- a/Sources/Foundation/NSLock.swift +++ b/Sources/Foundation/NSLock.swift @@ -11,8 +11,8 @@ #if canImport(Glibc) import Glibc -#elseif canImport(Android) -import Android +#elseif canImport(Bionic) +import Bionic #endif #if os(Windows) diff --git a/Sources/Foundation/NSURL.swift b/Sources/Foundation/NSURL.swift index e93b1eb02e..9e6f20d0bd 100644 --- a/Sources/Foundation/NSURL.swift +++ b/Sources/Foundation/NSURL.swift @@ -22,8 +22,8 @@ import Darwin import Glibc #elseif canImport(Musl) import Musl -#elseif canImport(Android) -import Android +#elseif canImport(Bionic) +import Bionic #endif // NOTE: this represents PLATFORM_PATH_STYLE diff --git a/Sources/Foundation/Process.swift b/Sources/Foundation/Process.swift index 9b4e14d950..5e8cfbaa55 100644 --- a/Sources/Foundation/Process.swift +++ b/Sources/Foundation/Process.swift @@ -932,13 +932,6 @@ open class Process: NSObject, @unchecked Sendable { var spawnAttrs: posix_spawnattr_t? = nil #else var spawnAttrs: posix_spawnattr_t = posix_spawnattr_t() -#endif -#if os(Android) - guard var spawnAttrs else { - throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno), userInfo: [ - NSURLErrorKey:self.executableURL! - ]) - } #endif try _throwIfPosixError(posix_spawnattr_init(&spawnAttrs)) try _throwIfPosixError(posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_SETPGROUP))) diff --git a/Sources/Foundation/Thread.swift b/Sources/Foundation/Thread.swift index c08fe68333..0985a4826d 100644 --- a/Sources/Foundation/Thread.swift +++ b/Sources/Foundation/Thread.swift @@ -17,8 +17,8 @@ import WinSDK import Glibc #elseif canImport(Musl) import Musl -#elseif canImport(Android) -import Android +#elseif canImport(Bionic) +import Bionic #endif // WORKAROUND_SR9811 diff --git a/Sources/plutil/main.swift b/Sources/plutil/main.swift index b2b1996a10..29584596d3 100644 --- a/Sources/plutil/main.swift +++ b/Sources/plutil/main.swift @@ -15,9 +15,9 @@ import Glibc #elseif canImport(Musl) import Foundation import Musl -#elseif canImport(Android) +#elseif canImport(Bionic) import Foundation -import Android +import Bionic #elseif canImport(CRT) import Foundation import CRT diff --git a/Sources/xdgTestHelper/main.swift b/Sources/xdgTestHelper/main.swift index 51ca70e51b..f6d2156c44 100644 --- a/Sources/xdgTestHelper/main.swift +++ b/Sources/xdgTestHelper/main.swift @@ -19,8 +19,8 @@ import FoundationNetworking #endif #if os(Windows) import WinSDK -#elseif os(Android) -import Android +#elseif canImport(Bionic) +import Bionic #endif enum HelperCheckStatus : Int32 { diff --git a/Tests/Foundation/FTPServer.swift b/Tests/Foundation/FTPServer.swift index 8328a7ff7e..f6cff1c61a 100644 --- a/Tests/Foundation/FTPServer.swift +++ b/Tests/Foundation/FTPServer.swift @@ -15,8 +15,8 @@ import Dispatch import Glibc #elseif canImport(Darwin) import Darwin -#elseif canImport(Android) - import Android +#elseif canImport(Bionic) + import Bionic #endif final class ServerSemaphore : Sendable { diff --git a/Tests/Foundation/HTTPServer.swift b/Tests/Foundation/HTTPServer.swift index 96d849f2b1..2d8ff83c6c 100644 --- a/Tests/Foundation/HTTPServer.swift +++ b/Tests/Foundation/HTTPServer.swift @@ -21,8 +21,8 @@ import Dispatch import Darwin #elseif canImport(Glibc) import Glibc -#elseif canImport(Android) - import Android +#elseif canImport(Bionic) + import Bionic #endif #if !os(Windows) diff --git a/Tests/Foundation/TestFileHandle.swift b/Tests/Foundation/TestFileHandle.swift index 8650c207e2..5ec83946f2 100644 --- a/Tests/Foundation/TestFileHandle.swift +++ b/Tests/Foundation/TestFileHandle.swift @@ -19,8 +19,8 @@ import Dispatch #if os(Windows) import WinSDK -#elseif canImport(Android) -import Android +#elseif canImport(Bionic) +import Bionic #endif class TestFileHandle : XCTestCase { diff --git a/Tests/Foundation/TestProcess.swift b/Tests/Foundation/TestProcess.swift index 2133879951..5e54116f6f 100644 --- a/Tests/Foundation/TestProcess.swift +++ b/Tests/Foundation/TestProcess.swift @@ -8,8 +8,8 @@ // import Synchronization -#if canImport(Android) -import Android +#if canImport(Bionic) +import Bionic #endif class TestProcess : XCTestCase { diff --git a/Tests/Foundation/TestSocketPort.swift b/Tests/Foundation/TestSocketPort.swift index 0a6ec281a9..7488546b9b 100644 --- a/Tests/Foundation/TestSocketPort.swift +++ b/Tests/Foundation/TestSocketPort.swift @@ -8,8 +8,8 @@ // #if os(Windows) import WinSDK -#elseif canImport(Android) -import Android +#elseif canImport(Bionic) +import Bionic #endif class TestPortDelegateWithBlock: NSObject, PortDelegate { diff --git a/Tests/Foundation/TestURL.swift b/Tests/Foundation/TestURL.swift index e38ab55b54..77ecafabbd 100644 --- a/Tests/Foundation/TestURL.swift +++ b/Tests/Foundation/TestURL.swift @@ -7,8 +7,8 @@ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // -#if canImport(Android) -import Android +#if canImport(Bionic) +import Bionic #endif let kURLTestParsingTestsKey = "ParsingTests"