diff options
author | HackyMiner <hackyminer@gmail.com> | 2019-02-19 19:15:15 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2019-02-19 19:15:15 +0800 |
commit | f7f6a46029c23d2c1b5a5293d470becbdb4e3366 (patch) | |
tree | bfe3e6fa048e8bdc0d16357156909e079fc24413 /node/defaults.go | |
parent | d2256244c4f6dbb7312fa280b5523c544d8c10af (diff) | |
download | go-tangerine-f7f6a46029c23d2c1b5a5293d470becbdb4e3366.tar go-tangerine-f7f6a46029c23d2c1b5a5293d470becbdb4e3366.tar.gz go-tangerine-f7f6a46029c23d2c1b5a5293d470becbdb4e3366.tar.bz2 go-tangerine-f7f6a46029c23d2c1b5a5293d470becbdb4e3366.tar.lz go-tangerine-f7f6a46029c23d2c1b5a5293d470becbdb4e3366.tar.xz go-tangerine-f7f6a46029c23d2c1b5a5293d470becbdb4e3366.tar.zst go-tangerine-f7f6a46029c23d2c1b5a5293d470becbdb4e3366.zip |
eth, node: use APPDATA env to support cygwin/msys correctly (#17786)
This changes default location of the data directory to use the LOCALAPPDATA
environment variable, resolving issues with remote home directories an improving
compatibility with Cygwin.
Fixes #2239
Fixes #2237
Fixes #16437
Diffstat (limited to 'node/defaults.go')
-rw-r--r-- | node/defaults.go | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/node/defaults.go b/node/defaults.go index cea4997cb..73b262429 100644 --- a/node/defaults.go +++ b/node/defaults.go @@ -58,11 +58,20 @@ func DefaultDataDir() string { // Try to place the data folder in the user's home dir home := homeDir() if home != "" { - if runtime.GOOS == "darwin" { + switch runtime.GOOS { + case "darwin": return filepath.Join(home, "Library", "Ethereum") - } else if runtime.GOOS == "windows" { - return filepath.Join(home, "AppData", "Roaming", "Ethereum") - } else { + case "windows": + // We used to put everything in %HOME%\AppData\Roaming, but this caused + // problems with non-typical setups. If this fallback location exists and + // is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%. + fallback := filepath.Join(home, "AppData", "Roaming", "Ethereum") + appdata := windowsAppData() + if appdata == "" || isNonEmptyDir(fallback) { + return fallback + } + return filepath.Join(appdata, "Ethereum") + default: return filepath.Join(home, ".ethereum") } } @@ -70,6 +79,26 @@ func DefaultDataDir() string { return "" } +func windowsAppData() string { + if v := os.Getenv("LOCALAPPDATA"); v != "" { + return v // Vista+ + } + if v := os.Getenv("APPDATA"); v != "" { + return filepath.Join(v, "Local") + } + return "" +} + +func isNonEmptyDir(dir string) bool { + f, err := os.Open(dir) + if err != nil { + return false + } + names, _ := f.Readdir(1) + f.Close() + return len(names) > 0 +} + func homeDir() string { if home := os.Getenv("HOME"); home != "" { return home |