Skip to content

Commit 0d37921

Browse files
committed
FileManager: repair homeDirectoryForCurrentUser on Windows
`NetUserGetInfo` will not provide the home directory for the specified user most of the time, even if the user is the currently logged in user. Prefer to use the `CFCopyHomeDirectoryURLForUser` path which will provide the home directory for the current user. Fast-path that into the computed property and add a special case for `homeDirectory(forUser:)` to accommodate this behavioural difference.
1 parent ff83ef2 commit 0d37921

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

Sources/Foundation/FileManager.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ open class FileManager : NSObject {
11401140
}
11411141

11421142
open var homeDirectoryForCurrentUser: URL {
1143-
return homeDirectory(forUser: NSUserName())!
1143+
CFCopyHomeDirectoryURLForUser(nil)!.takeRetainedValue()._swiftObject
11441144
}
11451145

11461146
open var temporaryDirectory: URL {
@@ -1149,8 +1149,13 @@ open class FileManager : NSObject {
11491149

11501150
open func homeDirectory(forUser userName: String) -> URL? {
11511151
guard !userName.isEmpty else { return nil }
1152-
guard let url = CFCopyHomeDirectoryURLForUser(userName._cfObject) else { return nil }
1153-
return url.takeRetainedValue()._swiftObject
1152+
// Prefer to take the `CFCopyHomeDirectoryURLForUser` path for the
1153+
// current user.
1154+
return CFCopyHomeDirectoryURLForUser(userName == NSUserName()
1155+
? nil
1156+
: userName._cfObject)?
1157+
.takeRetainedValue()
1158+
._swiftObject
11541159
}
11551160
}
11561161

0 commit comments

Comments
 (0)