Skip to content

Commit ebb00b8

Browse files
authored
Use fallback home directory on Windows (#854)
* Use %SystemDrive\Users\Public as fallback home directory on Windows * Update to ALLUSERSPROFILE * Fix test failure * Fix iOS test failure
1 parent c15a5e1 commit ebb00b8

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

Sources/FoundationEssentials/String/String+Path.swift

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -369,33 +369,27 @@ extension String {
369369
#if !NO_FILESYSTEM
370370
internal static func homeDirectoryPath(forUser user: String? = nil) -> String {
371371
#if os(Windows)
372-
func GetUserProfile() -> String? {
373-
return "USERPROFILE".withCString(encodedAs: UTF16.self) { pwszVariable in
374-
let dwLength: DWORD = GetEnvironmentVariableW(pwszVariable, nil, 0)
375-
// Ensure that `USERPROFILE` is defined.
376-
if dwLength == 0 { return nil }
377-
return withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(dwLength)) {
378-
guard GetEnvironmentVariableW(pwszVariable, $0.baseAddress, dwLength) == dwLength - 1 else {
379-
return nil
380-
}
381-
return String(decodingCString: $0.baseAddress!, as: UTF16.self)
372+
if let user {
373+
func fallbackUserDirectory() -> String {
374+
guard let fallback = ProcessInfo.processInfo.environment["ALLUSERSPROFILE"] else {
375+
fatalError("Unable to find home directory for user \(user) and ALLUSERSPROFILE environment variable is not set")
382376
}
377+
378+
return fallback
383379
}
384-
}
385-
386-
if let user {
380+
387381
return user.withCString(encodedAs: UTF16.self) { pwszUserName in
388382
var cbSID: DWORD = 0
389383
var cchReferencedDomainName: DWORD = 0
390384
var eUse: SID_NAME_USE = SidTypeUnknown
391385
guard LookupAccountNameW(nil, pwszUserName, nil, &cbSID, nil, &cchReferencedDomainName, &eUse) else {
392-
fatalError("unable to lookup SID for user \(user)")
386+
return fallbackUserDirectory()
393387
}
394388

395389
return withUnsafeTemporaryAllocation(of: CChar.self, capacity: Int(cbSID)) { pSID in
396390
return withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(cchReferencedDomainName)) { pwszReferencedDomainName in
397391
guard LookupAccountNameW(nil, pwszUserName, pSID.baseAddress, &cbSID, pwszReferencedDomainName.baseAddress, &cchReferencedDomainName, &eUse) else {
398-
fatalError("unable to lookup SID for user \(user)")
392+
return fallbackUserDirectory()
399393
}
400394

401395
var pwszSID: LPWSTR? = nil
@@ -425,7 +419,7 @@ extension String {
425419

426420
var hToken: HANDLE? = nil
427421
guard OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken) else {
428-
guard let UserProfile = GetUserProfile() else {
422+
guard let UserProfile = ProcessInfo.processInfo.environment["UserProfile"] else {
429423
fatalError("unable to evaluate `%UserProfile%`")
430424
}
431425
return UserProfile

Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,4 +897,19 @@ final class FileManagerTests : XCTestCase {
897897
}
898898
}
899899
}
900+
901+
func testHomeDirectoryForNonExistantUser() throws {
902+
#if canImport(Darwin) && !os(macOS)
903+
throw XCTSkip("This test is not applicable on this platform")
904+
#else
905+
#if os(Windows)
906+
let fallbackPath = URL(filePath: try XCTUnwrap(ProcessInfo.processInfo.environment["ALLUSERSPROFILE"]), directoryHint: .isDirectory)
907+
#else
908+
let fallbackPath = URL(filePath: "/var/empty", directoryHint: .isDirectory)
909+
#endif
910+
911+
XCTAssertEqual(FileManager.default.homeDirectory(forUser: ""), fallbackPath)
912+
XCTAssertEqual(FileManager.default.homeDirectory(forUser: UUID().uuidString), fallbackPath)
913+
#endif
914+
}
900915
}

0 commit comments

Comments
 (0)