node: check for previous default datadir

This commit is contained in:
Felix Lange 2019-02-11 12:30:24 +01:00
parent e7bb3468d3
commit 3ef8310edb

View file

@ -56,16 +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", "Ethereum") return filepath.Join(home, "Library", "Ethereum")
} else if runtime.GOOS == "windows" { case "windows":
appdata := os.Getenv("APPDATA") // We used to put everything in %HOME%\AppData\Roaming, but this caused
if appdata != "" { // problems with non-typical setups. If this fallback location exists and
return filepath.Join(appdata, "Ethereum") // is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%.
} else { fallback := filepath.Join(home, "AppData", "Roaming", "Ethereum")
return filepath.Join(home, "AppData", "Roaming", "Ethereum") appdata := windowsAppData()
if appdata == "" || isNonEmptyDir(fallback) {
return fallback
} }
} else { return filepath.Join(appdata, "Ethereum")
default:
return filepath.Join(home, ".ethereum") return filepath.Join(home, ".ethereum")
} }
} }
@ -73,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