mirror of
https://github.com/apple/swift-nio
synced 2026-11-30 05:28:44 +00:00
Add homeDirectory accessor to FileSystem (#3471)
Add homeDirectory accessor to FileSystem ### Motivation: Addresses #3381 by adding a `homeDirectory` property to FileSystem, equivalent to `FileManager.default.homeDirectoryForCurrentUser`. ### Modifications: - Added `homeDirectory` property to `FileSystemProtocol` and implemented in `FileSystem` - Implementation checks `HOME` environment variable first, falls back to `USERPROFILE` on Windows, or uses `getpwuid_r(3)` on POSIX systems - Added system call wrappers (`system_getuid`, `libc_getpwuid_r`) with proper platform guards - Added `FileSystemError.getpwuid_r()` error helper ### Result: Users can now access the home directory via `FileSystem.homeDirectory`, returning a `FilePath` asynchronously. Follows the same pattern as `currentWorkingDirectory` and `temporaryDirectory`. Co-authored-by: Cory Benfield <lukasa@apple.com>
This commit is contained in:
1 parent
4a9a971110
commit
608e511d7a
11 files changed
+267
No files matched your search
@@ -651,6 +651,37 @@ public struct FileSystem: Sendable, FileSystemProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the path of the current user's home directory.
|
||||
///
|
||||
/// #### Implementation details
|
||||
///
|
||||
/// This function first checks the `HOME` environment variable (and `USERPROFILE` on Windows).
|
||||
/// If not set, on Darwin/Linux/Android it uses `getpwuid_r(3)` to query the password database.
|
||||
///
|
||||
/// Note: `getpwuid_r` can potentially block on I/O (e.g., when using NIS or LDAP),
|
||||
/// which is why this property is async when falling back to the password database.
|
||||
///
|
||||
/// - Returns: The path to the current user's home directory.
|
||||
public var homeDirectory: NIOFilePath {
|
||||
get async throws {
|
||||
if let path = Libc.homeDirectoryFromEnvironment() {
|
||||
return NIOFilePath(path)
|
||||
}
|
||||
|
||||
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||
return try await self.threadPool.runIfActive {
|
||||
NIOFilePath(
|
||||
try Libc.homeDirectoryFromPasswd().mapError { errno in
|
||||
FileSystemError.getpwuid_r(errno: errno, location: .here())
|
||||
}.get()
|
||||
)
|
||||
}
|
||||
#else
|
||||
throw FileSystemError.getpwuid_r(errno: .noSuchFileOrDirectory, location: .here())
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a path to a temporary directory.
|
||||
///
|
||||
/// #### Implementation details
|
||||
|
||||
@@ -985,6 +985,17 @@ extension FileSystemError {
|
||||
)
|
||||
}
|
||||
|
||||
@_spi(Testing)
|
||||
public static func getpwuid_r(errno: Errno, location: SourceLocation) -> Self {
|
||||
FileSystemError(
|
||||
code: .unavailable,
|
||||
message: "Can't get home directory for current user.",
|
||||
systemCall: "getpwuid_r",
|
||||
errno: errno,
|
||||
location: location
|
||||
)
|
||||
}
|
||||
|
||||
@_spi(Testing)
|
||||
public static func fcopyfile(
|
||||
errno: Errno,
|
||||
|
||||
@@ -108,6 +108,9 @@ public protocol FileSystemProtocol: Sendable {
|
||||
/// Returns the current working directory.
|
||||
var currentWorkingDirectory: NIOFilePath { get async throws }
|
||||
|
||||
/// Returns the current user's home directory.
|
||||
var homeDirectory: NIOFilePath { get async throws }
|
||||
|
||||
/// Returns the path of the temporary directory.
|
||||
var temporaryDirectory: NIOFilePath { get async throws }
|
||||
|
||||
|
||||
@@ -278,6 +278,13 @@ public enum Syscall: Sendable {
|
||||
system_futimens(fd.rawValue, times)
|
||||
}
|
||||
}
|
||||
|
||||
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||
@_spi(Testing)
|
||||
public static func getuid() -> uid_t {
|
||||
system_getuid()
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@_spi(Testing)
|
||||
@@ -437,4 +444,47 @@ public enum Libc: Sendable {
|
||||
libc_fts_close(pointer)
|
||||
}
|
||||
}
|
||||
|
||||
@_spi(Testing)
|
||||
public static func homeDirectoryFromEnvironment() -> FilePath? {
|
||||
if let home = getenv("HOME"), home.pointee != 0 {
|
||||
return FilePath(String(cString: home))
|
||||
}
|
||||
#if os(Windows)
|
||||
if let profile = getenv("USERPROFILE"), profile.pointee != 0 {
|
||||
return FilePath(String(cString: profile))
|
||||
}
|
||||
#endif
|
||||
return nil
|
||||
}
|
||||
|
||||
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||
@_spi(Testing)
|
||||
public static func homeDirectoryFromPasswd() -> Result<FilePath, Errno> {
|
||||
let uid = Syscall.getuid()
|
||||
var pwd = passwd()
|
||||
var result: UnsafeMutablePointer<passwd>? = nil
|
||||
|
||||
return withUnsafeTemporaryAllocation(of: CChar.self, capacity: 1024) { buffer in
|
||||
let callResult = nothingOrErrno(retryOnInterrupt: true) {
|
||||
libc_getpwuid_r(
|
||||
uid,
|
||||
&pwd,
|
||||
buffer.baseAddress!,
|
||||
buffer.count,
|
||||
&result
|
||||
)
|
||||
}
|
||||
switch callResult {
|
||||
case .success:
|
||||
guard result != nil, let directoryPointer = pwd.pw_dir else {
|
||||
return .failure(.noSuchFileOrDirectory)
|
||||
}
|
||||
return .success(FilePath(String(cString: directoryPointer)))
|
||||
case .failure(let errno):
|
||||
return .failure(errno)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -479,3 +479,21 @@ internal func libc_fts_close(
|
||||
) -> CInt {
|
||||
fts_close(fts)
|
||||
}
|
||||
|
||||
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Android)
|
||||
/// getuid(2): Get user identification
|
||||
internal func system_getuid() -> uid_t {
|
||||
getuid()
|
||||
}
|
||||
|
||||
/// getpwuid_r(3): Get password file entry
|
||||
internal func libc_getpwuid_r(
|
||||
_ uid: uid_t,
|
||||
_ pwd: UnsafeMutablePointer<passwd>,
|
||||
_ buffer: UnsafeMutablePointer<CChar>,
|
||||
_ bufferSize: Int,
|
||||
_ result: UnsafeMutablePointer<UnsafeMutablePointer<passwd>?>
|
||||
) -> CInt {
|
||||
getpwuid_r(uid, pwd, buffer, bufferSize, result)
|
||||
}
|
||||
#endif
|
||||
@@ -651,6 +651,35 @@ public struct FileSystem: Sendable, FileSystemProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the path of the current user's home directory.
|
||||
///
|
||||
/// #### Implementation details
|
||||
///
|
||||
/// This function first checks the `HOME` environment variable (and `USERPROFILE` on Windows).
|
||||
/// If not set, on Darwin/Linux/Android it uses `getpwuid_r(3)` to query the password database.
|
||||
///
|
||||
/// Note: `getpwuid_r` can potentially block on I/O (e.g., when using NIS or LDAP),
|
||||
/// which is why this property is async when falling back to the password database.
|
||||
///
|
||||
/// - Returns: The path to the current user's home directory.
|
||||
public var homeDirectory: FilePath {
|
||||
get async throws {
|
||||
if let path = Libc.homeDirectoryFromEnvironment() {
|
||||
return path
|
||||
}
|
||||
|
||||
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Android)
|
||||
return try await self.threadPool.runIfActive {
|
||||
try Libc.homeDirectoryFromPasswd().mapError { errno in
|
||||
FileSystemError.getpwuid_r(errno: errno, location: .here())
|
||||
}.get()
|
||||
}
|
||||
#else
|
||||
throw FileSystemError.getpwuid_r(errno: .noSuchFileOrDirectory, location: .here())
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a path to a temporary directory.
|
||||
///
|
||||
/// #### Implementation details
|
||||
|
||||
@@ -985,6 +985,17 @@ extension FileSystemError {
|
||||
)
|
||||
}
|
||||
|
||||
@_spi(Testing)
|
||||
public static func getpwuid_r(errno: Errno, location: SourceLocation) -> Self {
|
||||
FileSystemError(
|
||||
code: .unavailable,
|
||||
message: "Can't get home directory for current user.",
|
||||
systemCall: "getpwuid_r",
|
||||
errno: errno,
|
||||
location: location
|
||||
)
|
||||
}
|
||||
|
||||
@_spi(Testing)
|
||||
public static func fcopyfile(
|
||||
errno: Errno,
|
||||
|
||||
@@ -108,6 +108,9 @@ public protocol FileSystemProtocol: Sendable {
|
||||
/// Returns the current working directory.
|
||||
var currentWorkingDirectory: FilePath { get async throws }
|
||||
|
||||
/// Returns the current user's home directory.
|
||||
var homeDirectory: FilePath { get async throws }
|
||||
|
||||
/// Returns the path of the temporary directory.
|
||||
var temporaryDirectory: FilePath { get async throws }
|
||||
|
||||
|
||||
@@ -278,6 +278,13 @@ public enum Syscall: Sendable {
|
||||
system_futimens(fd.rawValue, times)
|
||||
}
|
||||
}
|
||||
|
||||
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||
@_spi(Testing)
|
||||
public static func getuid() -> uid_t {
|
||||
system_getuid()
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@_spi(Testing)
|
||||
@@ -437,4 +444,47 @@ public enum Libc: Sendable {
|
||||
libc_fts_close(pointer)
|
||||
}
|
||||
}
|
||||
|
||||
@_spi(Testing)
|
||||
public static func homeDirectoryFromEnvironment() -> FilePath? {
|
||||
if let home = getenv("HOME"), home.pointee != 0 {
|
||||
return FilePath(String(cString: home))
|
||||
}
|
||||
#if os(Windows)
|
||||
if let profile = getenv("USERPROFILE"), profile.pointee != 0 {
|
||||
return FilePath(String(cString: profile))
|
||||
}
|
||||
#endif
|
||||
return nil
|
||||
}
|
||||
|
||||
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||
@_spi(Testing)
|
||||
public static func homeDirectoryFromPasswd() -> Result<FilePath, Errno> {
|
||||
let uid = Syscall.getuid()
|
||||
var pwd = passwd()
|
||||
var result: UnsafeMutablePointer<passwd>? = nil
|
||||
|
||||
return withUnsafeTemporaryAllocation(of: CChar.self, capacity: 1024) { buffer in
|
||||
let callResult = nothingOrErrno(retryOnInterrupt: true) {
|
||||
libc_getpwuid_r(
|
||||
uid,
|
||||
&pwd,
|
||||
buffer.baseAddress!,
|
||||
buffer.count,
|
||||
&result
|
||||
)
|
||||
}
|
||||
switch callResult {
|
||||
case .success:
|
||||
guard result != nil, let directoryPointer = pwd.pw_dir else {
|
||||
return .failure(.noSuchFileOrDirectory)
|
||||
}
|
||||
return .success(FilePath(String(cString: directoryPointer)))
|
||||
case .failure(let errno):
|
||||
return .failure(errno)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -479,3 +479,21 @@ internal func libc_fts_close(
|
||||
) -> CInt {
|
||||
fts_close(fts)
|
||||
}
|
||||
|
||||
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Android)
|
||||
/// getuid(2): Get user identification
|
||||
internal func system_getuid() -> uid_t {
|
||||
getuid()
|
||||
}
|
||||
|
||||
/// getpwuid_r(3): Get password file entry
|
||||
internal func libc_getpwuid_r(
|
||||
_ uid: uid_t,
|
||||
_ pwd: UnsafeMutablePointer<passwd>,
|
||||
_ buffer: UnsafeMutablePointer<CChar>,
|
||||
_ bufferSize: Int,
|
||||
_ result: UnsafeMutablePointer<UnsafeMutablePointer<passwd>?>
|
||||
) -> CInt {
|
||||
getpwuid_r(uid, pwd, buffer, bufferSize, result)
|
||||
}
|
||||
#endif
|
||||
@@ -548,6 +548,49 @@ final class FileSystemTests: XCTestCase {
|
||||
XCTAssert(directory.underlying.isAbsolute)
|
||||
}
|
||||
|
||||
func testHomeDirectory() async throws {
|
||||
let directory = try await self.fs.homeDirectory
|
||||
XCTAssert(!directory.underlying.isEmpty)
|
||||
XCTAssert(directory.underlying.isAbsolute)
|
||||
let info = try await self.fs.info(forFileAt: directory, infoAboutSymbolicLink: false)
|
||||
XCTAssertEqual(info?.type, .directory)
|
||||
}
|
||||
|
||||
func testHomeDirectoryFromEnvironment() async throws {
|
||||
// Should return a value when HOME is set (which it typically is)
|
||||
if let path = Libc.homeDirectoryFromEnvironment() {
|
||||
XCTAssert(!path.isEmpty)
|
||||
XCTAssert(path.isAbsolute)
|
||||
|
||||
// Verify it matches the high-level API
|
||||
let fsHome = try await self.fs.homeDirectory
|
||||
XCTAssertEqual(path, fsHome.underlying)
|
||||
} else {
|
||||
// If it returns nil, then HOME check should fail
|
||||
let home = getenv("HOME")
|
||||
XCTAssertTrue(home == nil || home!.pointee == 0, "Expected HOME to be unset or empty")
|
||||
|
||||
#if os(Windows)
|
||||
let profile = getenv("USERPROFILE")
|
||||
XCTAssertTrue(profile == nil || profile!.pointee == 0, "Expected USERPROFILE to be unset or empty")
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Android)
|
||||
func testHomeDirectoryFromPasswd() {
|
||||
// Should always succeed on Unix-like systems for the current user
|
||||
let result = Libc.homeDirectoryFromPasswd()
|
||||
switch result {
|
||||
case .success(let path):
|
||||
XCTAssert(!path.isEmpty)
|
||||
XCTAssert(path.isAbsolute)
|
||||
case .failure(let errno):
|
||||
XCTFail("Expected success, got error: \(errno)")
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
func testInfo() async throws {
|
||||
let info = try await self.fs.info(forFileAt: .testDataReadme, infoAboutSymbolicLink: false)
|
||||
XCTAssertEqual(info?.type, .regular)
|
||||
|
||||
Reference in new issue
Block a user