From feb211e1fae3a1e92977ced7e39217c6b4480fb8 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Fri, 8 Nov 2024 14:47:37 +0800 Subject: [PATCH] eth, node: use APPDATA env to support cygwin/msys correctly (#17786) --- eth/ethconfig/config.go | 11 +++++++++-- node/defaults.go | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index 512e573876201..642b59c4a0c0e 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -86,8 +86,15 @@ func init() { home = user.HomeDir } } - if runtime.GOOS == "windows" { - Defaults.Ethash.DatasetDir = filepath.Join(home, "AppData", "Ethash") + if runtime.GOOS == "darwin" { + Defaults.Ethash.DatasetDir = filepath.Join(home, "Library", "Ethash") + } else if runtime.GOOS == "windows" { + localappdata := os.Getenv("LOCALAPPDATA") + if localappdata != "" { + Defaults.Ethash.DatasetDir = filepath.Join(localappdata, "Ethash") + } else { + Defaults.Ethash.DatasetDir = filepath.Join(home, "AppData", "Local", "Ethash") + } } else { Defaults.Ethash.DatasetDir = filepath.Join(home, ".ethash") } diff --git a/node/defaults.go b/node/defaults.go index 3f36a50d8f726..f43fbeec98bd3 100644 --- a/node/defaults.go +++ b/node/defaults.go @@ -56,11 +56,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", "XDCchain") - } else if runtime.GOOS == "windows" { - return filepath.Join(home, "AppData", "Roaming", "XDCchain") - } 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", "XDCchain") + appdata := windowsAppData() + if appdata == "" || isNonEmptyDir(fallback) { + return fallback + } + return filepath.Join(appdata, "XDCchain") + default: return filepath.Join(home, ".XDC") } } @@ -68,6 +77,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