From 4bfad2651dee9d79f3eacb70c1f256554440b63c Mon Sep 17 00:00:00 2001 From: jsvisa Date: Mon, 27 Oct 2025 23:44:15 +0800 Subject: [PATCH 1/2] common: use Readdirnames to check if dir is empty --- common/path.go | 11 +++++++++++ node/defaults.go | 13 ++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/common/path.go b/common/path.go index 49c6a5efc2a4..19f24d426aa0 100644 --- a/common/path.go +++ b/common/path.go @@ -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 +} diff --git a/node/defaults.go b/node/defaults.go index 307d9e186a25..6c643e2b540e 100644 --- a/node/defaults.go +++ b/node/defaults.go @@ -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" @@ -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") @@ -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 From 44d3ae1e2d46271dbaa13faaf67a788e4466502f Mon Sep 17 00:00:00 2001 From: jsvisa Date: Mon, 27 Oct 2025 23:50:05 +0800 Subject: [PATCH 2/2] core: check ancient empty --- core/rawdb/database.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/rawdb/database.go b/core/rawdb/database.go index 724c90ead6a8..29483baa5f7b 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -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 {