github.com/ethersphere/bee/v2@v2.2.0/pkg/util/ioutil/ioutil.go (about)

     1  // Copyright 2022 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ioutil
     6  
     7  import (
     8  	"errors"
     9  	"os"
    10  	"path/filepath"
    11  )
    12  
    13  // DB folders paths from bee datadir
    14  const (
    15  	DataPathLocalstore = "localstore"
    16  	DataPathKademlia   = "kademlia-metrics"
    17  )
    18  
    19  // The WriterFunc type is an adapter to allow the use of
    20  // ordinary functions as io.Writer Write method. If f is
    21  // a function with the appropriate signature, WriterFunc(f)
    22  // is an io.Writer that calls f.
    23  type WriterFunc func([]byte) (int, error)
    24  
    25  // WriterFunc calls f(p).
    26  func (f WriterFunc) Write(p []byte) (n int, err error) {
    27  	return f(p)
    28  }
    29  
    30  // RemoveContent removes all files in path. Copied function from cmd/db.go
    31  func RemoveContent(path string) error {
    32  	dir, err := os.Open(path)
    33  	if errors.Is(err, os.ErrNotExist) {
    34  		return nil
    35  	}
    36  	if err != nil {
    37  		return err
    38  	}
    39  	defer dir.Close()
    40  
    41  	subpaths, err := dir.Readdirnames(0)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	for _, sub := range subpaths {
    47  		err = os.RemoveAll(filepath.Join(path, sub))
    48  		if err != nil {
    49  			return err
    50  		}
    51  	}
    52  	return nil
    53  }