Skip to content

Commit b3c8ce0

Browse files
committed
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 dbca8c7 commit b3c8ce0

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

Sources/Foundation/FileHandle.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ open class FileHandle : NSObject {
310310
let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0)
311311
// Swift does not currently expose MAP_FAILURE
312312
if data != UnsafeMutableRawPointer(bitPattern: -1) {
313-
return NSData.NSDataReadResult(bytes: data!, length: mapSize) { buffer, length in
313+
return NSData.NSDataReadResult(bytes: data, length: mapSize) { buffer, length in
314314
munmap(buffer, length)
315315
}
316316
}

Sources/Foundation/FileManager+POSIX.swift

+14-10
Original file line numberDiff line numberDiff line change
@@ -744,29 +744,31 @@ extension FileManager {
744744
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
745745
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
746746
ps.advanced(by: 1).initialize(to: nil)
747-
let stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
747+
let stream = ps.withMemoryRebound(to: UnsafeMutablePointer<CChar>.self, capacity: 2) {
748+
fts_open($0, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
749+
}
748750
ps.deinitialize(count: 2)
749751
ps.deallocate()
750752

751-
if stream != nil {
753+
if let stream {
752754
defer {
753755
fts_close(stream)
754756
}
755757

756-
while let current = fts_read(stream)?.pointee {
757-
let itemPath = string(withFileSystemRepresentation: current.fts_path, length: Int(current.fts_pathlen))
758+
while let current = fts_read(stream)?.pointee, let fts_path = current.fts_path {
759+
let itemPath = string(withFileSystemRepresentation: fts_path, length: Int(current.fts_pathlen))
758760
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
759761
continue
760762
}
761763

762764
do {
763765
switch Int32(current.fts_info) {
764766
case FTS_DEFAULT, FTS_F, FTS_NSOK, FTS_SL, FTS_SLNONE:
765-
if unlink(current.fts_path) == -1 {
767+
if unlink(fts_path) == -1 {
766768
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
767769
}
768770
case FTS_DP:
769-
if rmdir(current.fts_path) == -1 {
771+
if rmdir(fts_path) == -1 {
770772
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
771773
}
772774
case FTS_DNR, FTS_ERR, FTS_NS:
@@ -1089,7 +1091,9 @@ extension FileManager {
10891091
defer { ps.deallocate() }
10901092
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
10911093
ps.advanced(by: 1).initialize(to: nil)
1092-
return fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
1094+
return ps.withMemoryRebound(to: UnsafeMutablePointer<CChar>.self, capacity: 2) {
1095+
fts_open($0, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
1096+
}
10931097
}
10941098
if _stream == nil {
10951099
throw _NSErrorWithErrno(errno, reading: true, url: url)
@@ -1136,13 +1140,13 @@ extension FileManager {
11361140

11371141
_current = fts_read(stream)
11381142
while let current = _current {
1139-
let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path, length: Int(current.pointee.fts_pathlen))
1143+
let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path!, length: Int(current.pointee.fts_pathlen))
11401144

11411145
switch Int32(current.pointee.fts_info) {
11421146
case FTS_D:
11431147
let (showFile, skipDescendants) = match(filename: filename, to: _options, isDir: true)
11441148
if skipDescendants {
1145-
fts_set(_stream, _current, FTS_SKIP)
1149+
fts_set(stream, _current!, FTS_SKIP)
11461150
}
11471151
if showFile {
11481152
return URL(fileURLWithPath: filename, isDirectory: true)
@@ -1315,7 +1319,7 @@ extension FileManager {
13151319
let finalErrno = originalItemURL.withUnsafeFileSystemRepresentation { (originalFS) -> Int32? in
13161320
return newItemURL.withUnsafeFileSystemRepresentation { (newItemFS) -> Int32? in
13171321
// This is an atomic operation in many OSes, but is not guaranteed to be atomic by the standard.
1318-
if rename(newItemFS, originalFS) == 0 {
1322+
if rename(newItemFS!, originalFS!) == 0 {
13191323
return nil
13201324
} else {
13211325
return errno

Sources/Foundation/FileManager.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,13 @@ open class FileManager : NSObject {
568568
let attributes = try windowsFileAttributes(atPath: path)
569569
let type = FileAttributeType(attributes: attributes, atPath: path)
570570
#else
571-
if let pwd = getpwuid(s.st_uid), pwd.pointee.pw_name != nil {
572-
let name = String(cString: pwd.pointee.pw_name)
571+
if let pwd = getpwuid(s.st_uid), let pw_name = pwd.pointee.pw_name {
572+
let name = String(cString: pw_name)
573573
result[.ownerAccountName] = name
574574
}
575575

576-
if let grd = getgrgid(s.st_gid), grd.pointee.gr_name != nil {
577-
let name = String(cString: grd.pointee.gr_name)
576+
if let grd = getgrgid(s.st_gid), let gr_name = grd.pointee.gr_name {
577+
let name = String(cString: gr_name)
578578
result[.groupOwnerAccountName] = name
579579
}
580580

Sources/Foundation/Host.swift

+1-1
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

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

0 commit comments

Comments
 (0)