eth, node: use APPDATA env to support cygwin/msys correctly (#17786)

This commit is contained in:
Daniel Liu 2024-11-08 17:24:31 +08:00
parent fb2f822abe
commit 3ed9ce95c1
2 changed files with 42 additions and 6 deletions

View file

@ -86,8 +86,15 @@ func init() {
home = user.HomeDir home = user.HomeDir
} }
} }
if runtime.GOOS == "windows" { if runtime.GOOS == "darwin" {
Defaults.Ethash.DatasetDir = filepath.Join(home, "AppData", "Ethash") 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 { } else {
Defaults.Ethash.DatasetDir = filepath.Join(home, ".ethash") Defaults.Ethash.DatasetDir = filepath.Join(home, ".ethash")
} }

View file

@ -56,11 +56,20 @@ func DefaultDataDir() string {
// Try to place the data folder in the user's home dir // Try to place the data folder in the user's home dir
home := homeDir() home := homeDir()
if home != "" { if home != "" {
if runtime.GOOS == "darwin" { switch runtime.GOOS {
case "darwin":
return filepath.Join(home, "Library", "XDCchain") return filepath.Join(home, "Library", "XDCchain")
} else if runtime.GOOS == "windows" { case "windows":
return filepath.Join(home, "AppData", "Roaming", "XDCchain") // We used to put everything in %HOME%\AppData\Roaming, but this caused
} else { // 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") return filepath.Join(home, ".XDC")
} }
} }
@ -68,6 +77,26 @@ func DefaultDataDir() string {
return "" 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 { func homeDir() string {
if home := os.Getenv("HOME"); home != "" { if home := os.Getenv("HOME"); home != "" {
return home return home