Skip to content

FileManager: repair homeDirectoryForCurrentUser on Windows #4695

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
Jan 24, 2023

Conversation

compnerd
Copy link
Member

The homeDirectory(forUser:) is currently not functioning properly for users other than the current user. Switch to directly using the CoreFoundation API which simplifies the logic and makes the functionality work on Windows. This uses the forced unwrap for the return value, which was previously the case and resulted in a trap on Windows.

@compnerd
Copy link
Member Author

CC: @parkera

@compnerd
Copy link
Member Author

@swift-ci please test

Copy link
Contributor

@etcwilde etcwilde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to understand more about what is failing in homeDirectory(forUser:). It looks like it's going through the same steps as your fix here, with the exception of checking that the username isn't an empty string, and using a guard on the user directory URL (any safety gained by that is immediately lost by the force-unwrap at the upper layer, so I don't consider that to be all that different). Is NSUserName() not working right for the current user?

https://github.com/apple/swift-corelibs-foundation/blob/ff83ef2d90e48d9bc3465b13d524ab40cd5f2356/Sources/Foundation/FileManager.swift#L1150-L1154

@compnerd
Copy link
Member Author

It isn't homeDirectory(forUser:) that is failing, it is that NetUserGetInfo will not provide a home directory for the user.

https://github.com/apple/swift-corelibs-foundation/blob/main/CoreFoundation/Base.subproj/CFPlatform.c#L594

By passing in the explicit nil for the username, we directly invoke CFCopyHomeDirectoryURL which will give you the home directory for the current user.

@etcwilde
Copy link
Contributor

We should probably fix CFCopyHomeDirectoryURLForUser so that it can properly handle the current username. homeDirectory(forUser:) and homeDirectoryForCurrentUser should return the same thing when the function is passed the current username (assuming the current user isn't a system account, in which case I expect the computed property to explode and the function to return nil).

For performance/inlining/fast-pathing, I'm fine with the direct nil-passing in the computed property and a special-case for userName == GetCurrentUser() for the function.

@compnerd compnerd force-pushed the my-home-is-not-your-home branch from 0b95e12 to f644506 Compare January 23, 2023 17:52
@compnerd
Copy link
Member Author

@swift-ci please test

`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.
@compnerd compnerd force-pushed the my-home-is-not-your-home branch from f644506 to 0d37921 Compare January 24, 2023 00:33
@compnerd
Copy link
Member Author

@swift-ci please test

Copy link
Contributor

@etcwilde etcwilde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll accept it. It doesn't cause joy, but at least I can read it.

@compnerd compnerd merged commit 9d8da69 into swiftlang:main Jan 24, 2023
@compnerd compnerd deleted the my-home-is-not-your-home branch January 24, 2023 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants