Skip to content

Commit f7f6a46

Browse files
hackmodfjl
authored andcommitted
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 bnb-chain#2239 Fixes bnb-chain#2237 Fixes #16437
1 parent d225624 commit f7f6a46

3 files changed

Lines changed: 48 additions & 7 deletions

File tree

‎cmd/clef/main.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,12 @@ func DefaultConfigDir() string {
533533
if runtime.GOOS == "darwin" {
534534
return filepath.Join(home, "Library", "Signer")
535535
} else if runtime.GOOS == "windows" {
536-
return filepath.Join(home, "AppData", "Roaming", "Signer")
536+
appdata := os.Getenv("APPDATA")
537+
if appdata != "" {
538+
return filepath.Join(appdata, "Signer")
539+
} else {
540+
return filepath.Join(home, "AppData", "Roaming", "Signer")
541+
}
537542
} else {
538543
return filepath.Join(home, ".clef")
539544
}

‎eth/config.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,15 @@ func init() {
6868
home = user.HomeDir
6969
}
7070
}
71-
if runtime.GOOS == "windows" {
72-
DefaultConfig.Ethash.DatasetDir = filepath.Join(home, "AppData", "Ethash")
71+
if runtime.GOOS == "darwin" {
72+
DefaultConfig.Ethash.DatasetDir = filepath.Join(home, "Library", "Ethash")
73+
} else if runtime.GOOS == "windows" {
74+
localappdata := os.Getenv("LOCALAPPDATA")
75+
if localappdata != "" {
76+
DefaultConfig.Ethash.DatasetDir = filepath.Join(localappdata, "Ethash")
77+
} else {
78+
DefaultConfig.Ethash.DatasetDir = filepath.Join(home, "AppData", "Local", "Ethash")
79+
}
7380
} else {
7481
DefaultConfig.Ethash.DatasetDir = filepath.Join(home, ".ethash")
7582
}

‎node/defaults.go‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,47 @@ func DefaultDataDir() string {
5858
// Try to place the data folder in the user's home dir
5959
home := homeDir()
6060
if home != "" {
61-
if runtime.GOOS == "darwin" {
61+
switch runtime.GOOS {
62+
case "darwin":
6263
return filepath.Join(home, "Library", "Ethereum")
63-
} else if runtime.GOOS == "windows" {
64-
return filepath.Join(home, "AppData", "Roaming", "Ethereum")
65-
} else {
64+
case "windows":
65+
// We used to put everything in %HOME%\AppData\Roaming, but this caused
66+
// problems with non-typical setups. If this fallback location exists and
67+
// is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%.
68+
fallback := filepath.Join(home, "AppData", "Roaming", "Ethereum")
69+
appdata := windowsAppData()
70+
if appdata == "" || isNonEmptyDir(fallback) {
71+
return fallback
72+
}
73+
return filepath.Join(appdata, "Ethereum")
74+
default:
6675
return filepath.Join(home, ".ethereum")
6776
}
6877
}
6978
// As we cannot guess a stable location, return empty and handle later
7079
return ""
7180
}
7281

82+
func windowsAppData() string {
83+
if v := os.Getenv("LOCALAPPDATA"); v != "" {
84+
return v // Vista+
85+
}
86+
if v := os.Getenv("APPDATA"); v != "" {
87+
return filepath.Join(v, "Local")
88+
}
89+
return ""
90+
}
91+
92+
func isNonEmptyDir(dir string) bool {
93+
f, err := os.Open(dir)
94+
if err != nil {
95+
return false
96+
}
97+
names, _ := f.Readdir(1)
98+
f.Close()
99+
return len(names) > 0
100+
}
101+
73102
func homeDir() string {
74103
if home := os.Getenv("HOME"); home != "" {
75104
return home

0 commit comments

Comments
 (0)