Skip to content

Implementing NSFileManager.homeDirectoryForCurrentUser and homeDirect… #860

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
Feb 7, 2017

Conversation

saiHemak
Copy link
Contributor

@saiHemak saiHemak commented Feb 2, 2017

…ory(forUser userName: )

@@ -712,9 +712,15 @@ extension FileManager {
}

extension FileManager {
open var homeDirectoryForCurrentUser: URL { NSUnimplemented() }
open var homeDirectoryForCurrentUser: URL {
return homeDirectory(forUser: CFCopyUserName().takeRetainedValue()._swiftObject)!
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, is a force unwrap the right answer here if we can't find a home directory? Maybe a fallback instead?

Choose a reason for hiding this comment

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

I wonder if the var declaration could be changed to open var homeDirectoryForCurrentUser: URL?. It would make sense for it to be nil if it couldn't be obtained by homeDirectory(forUser:)

Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at CF, it seems the only way for this to totally fail is if even $HOME is unset. Perhaps that's an environment error and asserting is the right answer in that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@parkera When you say asserting is the right answer do you mean we abort the application like this

        let currentUser = CFCopyUserName().takeRetainedValue()._swiftObject
        guard !currentUser.isEmpty  else { fatalError ("$HOME not set") }
        return homeDirectory(forUser: currentUser)!  

I agree with @OscarSwanros . I cannot return nil as the declaration says that I cannot return nil.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm saying it's ok as written.

@parkera
Copy link
Contributor

parkera commented Feb 2, 2017

@swift-ci please test

@parkera parkera merged commit 6afa19a into swiftlang:master Feb 7, 2017
@saiHemak saiHemak deleted the nsfilemanager branch February 8, 2017 08:37
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.

3 participants