Skip to content

[android] fix the android build #999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions Sources/FoundationEssentials/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ fileprivate let _pageSize: Int = {
// WebAssembly defines a fixed page size
fileprivate let _pageSize: Int = 65_536
#elseif canImport(Android)
import Bionic
import unistd
import Android
fileprivate let _pageSize: Int = Int(getpagesize())
#elseif canImport(Glibc)
import Glibc
Expand Down Expand Up @@ -142,7 +141,7 @@ extension Platform {
typealias Operation<Input, Output> = (Input, UnsafeMutablePointer<Output>, UnsafeMutablePointer<CChar>, Int, UnsafeMutablePointer<UnsafeMutablePointer<Output>?>) -> Int32
#endif

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

static func name(forUID uid: uid_t) -> String? {
withUserGroupBuffer(uid, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwuid_r) {
String(cString: $0.pw_name)
// Android's pw_name `char *`` is nullable when it should be non-null.
// FIXME: avoid the coerce cast workaround once https://github.com/android/ndk/issues/2098 is fixed.
let pw_name: UnsafeMutablePointer<CChar>? = $0.pw_name
return pw_name.flatMap { String(cString: $0) }
}
}

static func fullName(forUID uid: uid_t) -> String? {
withUserGroupBuffer(uid, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwuid_r) {
String(cString: $0.pw_gecos)
#if os(Android) && _pointerBitWidth(_32)
// pw_gecos isn't available on 32-bit Android.
let pw_gecos: UnsafeMutablePointer<CChar>? = nil
#else
// Android's pw_gecos `char *`` is nullable, so always coerce to a nullable pointer
// in order to be compatible with Android.
let pw_gecos: UnsafeMutablePointer<CChar>? = $0.pw_gecos
#endif
return pw_gecos.flatMap { String(cString: $0) }
}
}

static func name(forGID gid: gid_t) -> String? {
withUserGroupBuffer(gid, group(), sizeProperty: Int32(_SC_GETGR_R_SIZE_MAX), operation: getgrgid_r) {
String(cString: $0.gr_name)
// Android's gr_name `char *`` is nullable when it should be non-null.
// FIXME: avoid the coerce cast workaround once https://github.com/android/ndk/issues/2098 is fixed.
let gr_name: UnsafeMutablePointer<CChar>? = $0.gr_name
return gr_name.flatMap { String(cString: $0) }
}
}

static func homeDirectory(forUserName userName: String) -> String? {
withUserGroupBuffer(userName, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwnam_r) {
String(cString: $0.pw_dir)
// Android's pw_dir `char *`` is nullable when it should be non-null.
// FIXME: avoid the coerce cast workaround once https://github.com/android/ndk/issues/2098 is fixed.
let pw_dir: UnsafeMutablePointer<CChar>? = $0.pw_dir
return pw_dir.flatMap { String(cString: $0) }
}
}

static func homeDirectory(forUID uid: uid_t) -> String? {
withUserGroupBuffer(uid, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwuid_r) {
String(cString: $0.pw_dir)
// Android's pw_dir `char *`` is nullable when it should be non-null.
// FIXME: avoid the coerce cast workaround once https://github.com/android/ndk/issues/2098 is fixed.
let pw_dir: UnsafeMutablePointer<CChar>? = $0.pw_dir
return pw_dir.flatMap { String(cString: $0) }
}
}
}
Expand Down