github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/kvstore/utils.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package kvstore
     4  
     5  import (
     6  	"errors"
     7  	"os"
     8  	"regexp"
     9  	"strings"
    10  )
    11  
    12  var nameRegexp = regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`)
    13  
    14  // IsValidName check if store name or database name or table name is valid
    15  func IsValidName(name string) bool {
    16  	return nameRegexp.MatchString(name)
    17  }
    18  
    19  // RemoveStore remove store directory
    20  func RemoveStore(path string) error {
    21  	var errNotStoreDirectory = errors.New("not store directory")
    22  
    23  	if strings.HasSuffix(path, StoreSuffix) {
    24  		_, err := os.Stat(path)
    25  		if err != nil {
    26  			if os.IsNotExist(err) {
    27  				return nil
    28  			}
    29  			return err
    30  		}
    31  
    32  		// validate store
    33  		{
    34  			_, err = os.Stat(path + "/CURRENT")
    35  			if err != nil {
    36  				return errNotStoreDirectory
    37  			}
    38  		}
    39  		{
    40  			_, err = os.Stat(path + "/LOCK")
    41  			if err != nil {
    42  				return errNotStoreDirectory
    43  			}
    44  		}
    45  
    46  		return os.RemoveAll(path)
    47  	}
    48  
    49  	return errNotStoreDirectory
    50  }