From 9e371fef5ce22c8be559c630cc3c415e89848db1 Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Mon, 25 Jul 2022 11:31:57 +0700 Subject: [PATCH] remove only etl-tmp content, but not dir itself #4816 --- eth/backend.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/eth/backend.go b/eth/backend.go index 89cd93328..ef9657036 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -149,7 +149,7 @@ func New(stack *node.Node, config *ethconfig.Config, logger log.Logger) (*Ethere } tmpdir := stack.Config().Dirs.Tmp - if err := os.RemoveAll(tmpdir); err != nil { // clean it on startup + if err := RemoveContents(tmpdir); err != nil { // clean it on startup return nil, fmt.Errorf("clean tmp dir: %s, %w", tmpdir, err) } @@ -912,3 +912,23 @@ func (s *Ethereum) SentryCtx() context.Context { func (s *Ethereum) SentryControlServer() *sentry.MultiClient { return s.sentriesClient } + +// RemoveContents is like os.RemoveAll, but preserve dir itself +func RemoveContents(dir string) error { + d, err := os.Open(dir) + if err != nil { + return err + } + defer d.Close() + names, err := d.Readdirnames(-1) + if err != nil { + return err + } + for _, name := range names { + err = os.RemoveAll(filepath.Join(dir, name)) + if err != nil { + return err + } + } + return nil +}