Skip to content

Commit 4bfad26

Browse files
committed
common: use Readdirnames to check if dir is empty
1 parent 33dbd64 commit 4bfad26

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

common/path.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,14 @@ func AbsolutePath(datadir string, filename string) string {
3737
}
3838
return filepath.Join(datadir, filename)
3939
}
40+
41+
// IsNonEmptyDir checks if a directory exists and is non-empty.
42+
func IsNonEmptyDir(dir string) bool {
43+
f, err := os.Open(dir)
44+
if err != nil {
45+
return false
46+
}
47+
defer f.Close()
48+
names, _ := f.Readdirnames(1)
49+
return len(names) > 0
50+
}

node/defaults.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"path/filepath"
2323
"runtime"
2424

25+
"github.com/ethereum/go-ethereum/common"
2526
"github.com/ethereum/go-ethereum/p2p"
2627
"github.com/ethereum/go-ethereum/p2p/nat"
2728
"github.com/ethereum/go-ethereum/rpc"
@@ -90,7 +91,7 @@ func DefaultDataDir() string {
9091
// is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%.
9192
fallback := filepath.Join(home, "AppData", "Roaming", "Ethereum")
9293
appdata := windowsAppData()
93-
if appdata == "" || isNonEmptyDir(fallback) {
94+
if appdata == "" || common.IsNonEmptyDir(fallback) {
9495
return fallback
9596
}
9697
return filepath.Join(appdata, "Ethereum")
@@ -113,16 +114,6 @@ func windowsAppData() string {
113114
return v
114115
}
115116

116-
func isNonEmptyDir(dir string) bool {
117-
f, err := os.Open(dir)
118-
if err != nil {
119-
return false
120-
}
121-
names, _ := f.Readdir(1)
122-
f.Close()
123-
return len(names) > 0
124-
}
125-
126117
func homeDir() string {
127118
if home := os.Getenv("HOME"); home != "" {
128119
return home

0 commit comments

Comments
 (0)