remove only etl-tmp content, but not dir itself #4816

This commit is contained in:
Alex Sharov 2022-07-25 11:31:57 +07:00 committed by GitHub
parent b231856c1c
commit 9e371fef5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
}