Skip to content

Commit 66ede86

Browse files
compnerdhyp
authored andcommitted
Foundation: repair the build for Android API level 28+
The newer level introduces APIs with additional nullability attribution. This causes issues as the attribution sometimes collides with expectations. Unwrap more cases to appease the nullability attributes. One problematic area of this change is the handling for `Process.run()`. The posix spawn APIs are described as: ``` typedef struct __posix_spawnattr* posix_spawnattr_t; int posix_spawnattr_init(posix_spawnattr_t _Nonnull * _Nonnull __attr); ``` As the inner `_Nonnull` appertains to `posix_spawnattr_t`, it expects a non-null pointer to a non-null pointer. However, as `struct __posix_spawnattr` is opaque, we cannot allocate space for it and thus must be acceptable to take a pointer to null to permit the allocation.
1 parent 77af9c5 commit 66ede86

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

Sources/Foundation/FileHandle.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ open class FileHandle : NSObject {
324324
let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0)
325325
// Swift does not currently expose MAP_FAILURE
326326
if data != UnsafeMutableRawPointer(bitPattern: -1) {
327-
return NSData.NSDataReadResult(bytes: data!, length: mapSize) { buffer, length in
327+
return NSData.NSDataReadResult(bytes: data, length: mapSize) { buffer, length in
328328
munmap(buffer, length)
329329
}
330330
}

Sources/Foundation/FileManager+POSIX.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -803,29 +803,31 @@ extension FileManager {
803803
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
804804
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
805805
ps.advanced(by: 1).initialize(to: nil)
806-
let stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
806+
let stream = ps.withMemoryRebound(to: UnsafeMutablePointer<CChar>.self, capacity: 2) {
807+
fts_open($0, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
808+
}
807809
ps.deinitialize(count: 2)
808810
ps.deallocate()
809811

810-
if stream != nil {
812+
if let stream {
811813
defer {
812814
fts_close(stream)
813815
}
814816

815-
while let current = fts_read(stream)?.pointee {
816-
let itemPath = string(withFileSystemRepresentation: current.fts_path, length: Int(current.fts_pathlen))
817+
while let current = fts_read(stream)?.pointee, let fts_path = current.fts_path {
818+
let itemPath = string(withFileSystemRepresentation: fts_path, length: Int(current.fts_pathlen))
817819
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
818820
continue
819821
}
820822

821823
do {
822824
switch Int32(current.fts_info) {
823825
case FTS_DEFAULT, FTS_F, FTS_NSOK, FTS_SL, FTS_SLNONE:
824-
if unlink(current.fts_path) == -1 {
826+
if unlink(fts_path) == -1 {
825827
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
826828
}
827829
case FTS_DP:
828-
if rmdir(current.fts_path) == -1 {
830+
if rmdir(fts_path) == -1 {
829831
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
830832
}
831833
case FTS_DNR, FTS_ERR, FTS_NS:
@@ -1171,7 +1173,9 @@ extension FileManager {
11711173
defer { ps.deallocate() }
11721174
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
11731175
ps.advanced(by: 1).initialize(to: nil)
1174-
return fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
1176+
return ps.withMemoryRebound(to: UnsafeMutablePointer<CChar>.self, capacity: 2) {
1177+
fts_open($0, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
1178+
}
11751179
}
11761180
if _stream == nil {
11771181
throw _NSErrorWithErrno(errno, reading: true, url: url)
@@ -1218,13 +1222,13 @@ extension FileManager {
12181222

12191223
_current = fts_read(stream)
12201224
while let current = _current {
1221-
let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path, length: Int(current.pointee.fts_pathlen))
1225+
let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path!, length: Int(current.pointee.fts_pathlen))
12221226

12231227
switch Int32(current.pointee.fts_info) {
12241228
case FTS_D:
12251229
let (showFile, skipDescendants) = match(filename: filename, to: _options, isDir: true)
12261230
if skipDescendants {
1227-
fts_set(_stream, _current, FTS_SKIP)
1231+
fts_set(stream, _current!, FTS_SKIP)
12281232
}
12291233
if showFile {
12301234
return URL(fileURLWithPath: filename, isDirectory: true)
@@ -1398,7 +1402,7 @@ extension FileManager {
13981402
let finalErrno = originalItemURL.withUnsafeFileSystemRepresentation { (originalFS) -> Int32? in
13991403
return newItemURL.withUnsafeFileSystemRepresentation { (newItemFS) -> Int32? in
14001404
// This is an atomic operation in many OSes, but is not guaranteed to be atomic by the standard.
1401-
if rename(newItemFS, originalFS) == 0 {
1405+
if rename(newItemFS!, originalFS!) == 0 {
14021406
return nil
14031407
} else {
14041408
return errno

Sources/Foundation/FileManager.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,13 +579,13 @@ open class FileManager : NSObject {
579579
#elseif os(WASI)
580580
let type = FileAttributeType(statMode: mode_t(s.st_mode))
581581
#else
582-
if let pwd = getpwuid(s.st_uid), pwd.pointee.pw_name != nil {
583-
let name = String(cString: pwd.pointee.pw_name)
582+
if let pwd = getpwuid(s.st_uid), let pw_name = pwd.pointee.pw_name {
583+
let name = String(cString: pw_name)
584584
result[.ownerAccountName] = name
585585
}
586586

587-
if let grd = getgrgid(s.st_gid), grd.pointee.gr_name != nil {
588-
let name = String(cString: grd.pointee.gr_name)
587+
if let grd = getgrgid(s.st_gid), let gr_name = grd.pointee.gr_name {
588+
let name = String(cString: gr_name)
589589
result[.groupOwnerAccountName] = name
590590
}
591591

Sources/Foundation/Host.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import WinSDK
2424
}
2525

2626
// getnameinfo uses size_t for its 4th and 6th arguments.
27-
private func getnameinfo(_ addr: UnsafePointer<sockaddr>?, _ addrlen: socklen_t, _ host: UnsafeMutablePointer<Int8>?, _ hostlen: socklen_t, _ serv: UnsafeMutablePointer<Int8>?, _ servlen: socklen_t, _ flags: Int32) -> Int32 {
27+
private func getnameinfo(_ addr: UnsafePointer<sockaddr>, _ addrlen: socklen_t, _ host: UnsafeMutablePointer<Int8>?, _ hostlen: socklen_t, _ serv: UnsafeMutablePointer<Int8>?, _ servlen: socklen_t, _ flags: Int32) -> Int32 {
2828
return Glibc.getnameinfo(addr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
2929
}
3030

Sources/Foundation/Process.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,13 @@ open class Process: NSObject {
928928
var spawnAttrs: posix_spawnattr_t? = nil
929929
#else
930930
var spawnAttrs: posix_spawnattr_t = posix_spawnattr_t()
931+
#endif
932+
#if os(Android)
933+
guard var spawnAttrs else {
934+
throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno), userInfo: [
935+
NSURLErrorKey:self.executableURL!
936+
])
937+
}
931938
#endif
932939
try _throwIfPosixError(posix_spawnattr_init(&spawnAttrs))
933940
try _throwIfPosixError(posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_SETPGROUP)))

0 commit comments

Comments
 (0)