Skip to content

Commit

Permalink
eth, node: use APPDATA env to support cygwin/msys correctly (ethereum…
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Nov 8, 2024
1 parent 6695a08 commit ac20866
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
11 changes: 9 additions & 2 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
37 changes: 33 additions & 4 deletions node/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,47 @@ 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")
}
}
// As we cannot guess a stable location, return empty and handle later
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
Expand Down

0 comments on commit ac20866

Please sign in to comment.