Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions common/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ func AbsolutePath(datadir string, filename string) string {
}
return filepath.Join(datadir, filename)
}

// IsNonEmptyDir checks if a directory exists and is non-empty.
func IsNonEmptyDir(dir string) bool {
f, err := os.Open(dir)
if err != nil {
return false
}
defer f.Close()
names, _ := f.Readdirnames(1)
return len(names) > 0
}
2 changes: 1 addition & 1 deletion core/rawdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func resolveChainFreezerDir(ancient string) string {
// - chain freezer exists in legacy location (root ancient folder)
freezer := filepath.Join(ancient, ChainFreezerName)
if !common.FileExist(freezer) {
if !common.FileExist(ancient) {
if !common.FileExist(ancient) || !common.IsNonEmptyDir(ancient) {
// The entire ancient store is not initialized, still use the sub
// folder for initialization.
} else {
Expand Down
13 changes: 2 additions & 11 deletions node/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"runtime"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -90,7 +91,7 @@ func DefaultDataDir() string {
// is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%.
fallback := filepath.Join(home, "AppData", "Roaming", "Ethereum")
appdata := windowsAppData()
if appdata == "" || isNonEmptyDir(fallback) {
if appdata == "" || common.IsNonEmptyDir(fallback) {
return fallback
}
return filepath.Join(appdata, "Ethereum")
Expand All @@ -113,16 +114,6 @@ func windowsAppData() string {
return v
}

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