Skip to content
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

Properly Expand Appdata Directory #1508

Closed

Conversation

ukane-philemon
Copy link
Contributor

This diff properly expands the ~ to user home directory, provides support for windows %VARIABLE% style when expanding appdata dir and expands dcr rpcwallet cert path. Closes #436

@chappjc chappjc requested a review from JoeGruffins March 6, 2022 14:07
Copy link
Member

@JoeGruffins JoeGruffins left a comment

Choose a reason for hiding this comment

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

I apologize, #436 specifically was closed by #1475, I had forgotten it existed. This pr still has merit for the expanding the cert file and Windows users. Will test out on my windows machine, after some setup...

client/cmd/dexc/config.go Show resolved Hide resolved
dex/path.go Outdated Show resolved Hide resolved
Copy link
Member

@JoeGruffins JoeGruffins left a comment

Choose a reason for hiding this comment

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

Have confirmed on Windows that at least %% is expanded to $HOME/AppData/Local. Using Windows 10 (I think) and powershell. I'm not sure how windows users use this but I assume %PLACE% makes shortcuts to various places. Changes look good to me except for removing the clean for the config file location, unless there is a reason I do not see?

client/cmd/dexc/config.go Outdated Show resolved Hide resolved
Comment on lines -31 to 49
// Expand initial ~ to the current user's home directory, or ~otheruser
// to otheruser's home directory. On Windows, both forward and backward
// slashes can be used.
path = path[1:]

var pathSeparators string
if runtime.GOOS == "windows" {
pathSeparators = string(os.PathSeparator) + "/"
} else {
pathSeparators = string(os.PathSeparator)
}

userName := ""
if i := strings.IndexAny(path, pathSeparators); i != -1 {
userName = path[:i]
path = path[i:]
}

homeDir := ""
var u *user.User
var err error
if userName == "" {
u, err = user.Current()
} else {
u, err = user.Lookup(userName)
}
if err == nil {
homeDir = u.HomeDir
}
// Fallback to CWD if user lookup fails or user has no home directory.
if homeDir == "" {
homeDir = "."
dirName, err := os.UserHomeDir()
if err != nil {
// Fallback to CWD if retrieving user home directory fails.
dirName = "."
}
Copy link
Member

Choose a reason for hiding this comment

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

This does look like a better option than parsing the file name looking for a user name.

I guess we lose the ability to expand to ~otheruser? I can't imagine when that would be used though. Actually seems like something we should not encourage, so I like this change.

dex/path.go Show resolved Hide resolved
Copy link
Member

@chappjc chappjc left a comment

Choose a reason for hiding this comment

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

Not a strong use case for this change, and there are issues with the impl. nACK, but my mind can be changed.

client/asset/dcr/rpcwallet.go Show resolved Hide resolved
client/cmd/dexc/config.go Show resolved Hide resolved
client/cmd/dexc/config.go Show resolved Hide resolved
dirName := ""

// This supports Windows cmd.exe-style %VARIABLE%.
if strings.HasPrefix(path, "%") {
Copy link
Member

Choose a reason for hiding this comment

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

Only if os is windows.

Also, why prefix? What if user is doing something like C:\dexdata\%USERNAME%?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was intended to expand %variable%\path
%USERNAME% will eventually expand to a full path C:\users\username. Currently, C:\dexdata\%USERNAME% will be handled by os.ExpandEnv. Will provide support for such cases.

// This supports Windows cmd.exe-style %VARIABLE%.
if strings.HasPrefix(path, "%") {
// Split path into %VARIABLE% and path
pathArray := strings.SplitAfterN(path, "/", 2)
Copy link
Member

@chappjc chappjc Mar 15, 2022

Choose a reason for hiding this comment

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

What about "\" as a path separator?
The filepath package has methods and consts for this purpose.

dex/path.go Show resolved Hide resolved
@buck54321
Copy link
Member

Hard to justify this change without any previous complaints or requests for it. Little changes like this can end up breaking stuff.

@ukane-philemon ukane-philemon marked this pull request as draft March 29, 2022 12:14
@ukane-philemon
Copy link
Contributor Author

Considering the helpful comments from @chappjc, @buck54321, @JoeGruffins, I would mark this PR closed pending request to support expanding windows %VARIBLE% in paths.

#1525 is open to fixing expanding dcrwallet rpc cert path.

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.

dexc: AppData parameter does not expand tilde.
4 participants