Skip to content

Commit d504ec7

Browse files
committed
[android] fix the android build
Platform.swift regressed after 71eefee
1 parent f077992 commit d504ec7

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

Sources/FoundationEssentials/Platform.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ fileprivate let _pageSize: Int = {
2929
// WebAssembly defines a fixed page size
3030
fileprivate let _pageSize: Int = 65_536
3131
#elseif canImport(Android)
32-
import Bionic
33-
import unistd
32+
import Android
3433
fileprivate let _pageSize: Int = Int(getpagesize())
3534
#elseif canImport(Glibc)
3635
import Glibc
@@ -142,7 +141,7 @@ extension Platform {
142141
typealias Operation<Input, Output> = (Input, UnsafeMutablePointer<Output>, UnsafeMutablePointer<CChar>, Int, UnsafeMutablePointer<UnsafeMutablePointer<Output>?>) -> Int32
143142
#endif
144143

145-
private static func withUserGroupBuffer<Input, Output, R>(_ input: Input, _ output: Output, sizeProperty: Int32, operation: Operation<Input, Output>, block: (Output) throws -> R) rethrows -> R? {
144+
private static func withUserGroupBuffer<Input, Output, R>(_ input: Input, _ output: Output, sizeProperty: Int32, operation: Operation<Input, Output>, block: (Output) throws -> R?) rethrows -> R? {
146145
var bufferLen = sysconf(sizeProperty)
147146
if bufferLen == -1 {
148147
bufferLen = 4096 // Generous default size estimate
@@ -172,31 +171,46 @@ extension Platform {
172171

173172
static func name(forUID uid: uid_t) -> String? {
174173
withUserGroupBuffer(uid, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwuid_r) {
175-
String(cString: $0.pw_name)
174+
// Android's pw_name `char *`` is nullable, so always coerce to a nullable pointer
175+
// in order to be compatible with Android.
176+
let pw_name: UnsafeMutablePointer<CChar>? = $0.pw_name
177+
return pw_name.flatMap { String(cString: $0) }
176178
}
177179
}
178180

179181
static func fullName(forUID uid: uid_t) -> String? {
180182
withUserGroupBuffer(uid, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwuid_r) {
181-
String(cString: $0.pw_gecos)
183+
// Android's pw_gecos `char *`` is nullable, so always coerce to a nullable pointer
184+
// in order to be compatible with Android.
185+
let pw_gecos: UnsafeMutablePointer<CChar>? = $0.pw_gecos
186+
return pw_gecos.flatMap { String(cString: $0) }
182187
}
183188
}
184189

185190
static func name(forGID gid: gid_t) -> String? {
186191
withUserGroupBuffer(gid, group(), sizeProperty: Int32(_SC_GETGR_R_SIZE_MAX), operation: getgrgid_r) {
187-
String(cString: $0.gr_name)
192+
// Android's gr_name `char *`` is nullable, so always coerce to a nullable pointer
193+
// in order to be compatible with Android.
194+
let gr_name: UnsafeMutablePointer<CChar>? = $0.gr_name
195+
return gr_name.flatMap { String(cString: $0) }
188196
}
189197
}
190198

191199
static func homeDirectory(forUserName userName: String) -> String? {
192200
withUserGroupBuffer(userName, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwnam_r) {
193-
String(cString: $0.pw_dir)
201+
// Android's pw_dir `char *`` is nullable, so always coerce to a nullable pointer
202+
// in order to be compatible with Android.
203+
let pw_dir: UnsafeMutablePointer<CChar>? = $0.pw_dir
204+
return pw_dir.flatMap { String(cString: $0) }
194205
}
195206
}
196207

197208
static func homeDirectory(forUID uid: uid_t) -> String? {
198209
withUserGroupBuffer(uid, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwuid_r) {
199-
String(cString: $0.pw_dir)
210+
// Android's pw_dir `char *`` is nullable, so always coerce to a nullable pointer
211+
// in order to be compatible with Android.
212+
let pw_dir: UnsafeMutablePointer<CChar>? = $0.pw_dir
213+
return pw_dir.flatMap { String(cString: $0) }
200214
}
201215
}
202216
}

0 commit comments

Comments
 (0)