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
…#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 ethereum#2239
Fixes ethereum#2237
Fixes ethereum#16437
  • Loading branch information
hackmod committed Apr 14, 2019
1 parent b6c12eb commit ef8c08a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
7 changes: 6 additions & 1 deletion cmd/clef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,12 @@ func DefaultConfigDir() string {
if runtime.GOOS == "darwin" {
return filepath.Join(home, "Library", "Signer")
} else if runtime.GOOS == "windows" {
return filepath.Join(home, "AppData", "Roaming", "Signer")
appdata := os.Getenv("APPDATA")
if appdata != "" {
return filepath.Join(appdata, "Signer")
} else {
return filepath.Join(home, "AppData", "Roaming", "Signer")
}
} else {
return filepath.Join(home, ".clef")
}
Expand Down
11 changes: 9 additions & 2 deletions eth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@ func init() {
home = user.HomeDir
}
}
if runtime.GOOS == "windows" {
DefaultConfig.Ethash.DatasetDir = filepath.Join(home, "AppData", "Ethash")
if runtime.GOOS == "darwin" {
DefaultConfig.Ethash.DatasetDir = filepath.Join(home, "Library", "Ethash")
} else if runtime.GOOS == "windows" {
localappdata := os.Getenv("LOCALAPPDATA")
if localappdata != "" {
DefaultConfig.Ethash.DatasetDir = filepath.Join(localappdata, "Ethash")
} else {
DefaultConfig.Ethash.DatasetDir = filepath.Join(home, "AppData", "Local", "Ethash")
}
} else {
DefaultConfig.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", "Ethersocial")
} else if runtime.GOOS == "windows" {
return filepath.Join(home, "AppData", "Roaming", "Ethersocial")
} 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", "Ethersocial")
appdata := windowsAppData()
if appdata == "" || isNonEmptyDir(fallback) {
return fallback
}
return filepath.Join(appdata, "Ethersocial")
default:
return filepath.Join(home, ".ethersocial")
}
}
// 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 ef8c08a

Please sign in to comment.