2022-08-29 21:22:02 +00:00
|
|
|
package blefs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (blefs *FS) RemoveAll(path string) error {
|
|
|
|
if path == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.Clean(path) == "/" {
|
|
|
|
return ErrNoRemoveRoot
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := blefs.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
return blefs.removeAllChildren(path)
|
|
|
|
} else {
|
|
|
|
err = blefs.Remove(path)
|
2022-10-17 19:19:23 +00:00
|
|
|
|
|
|
|
var code int8
|
|
|
|
if err, ok := err.(FSError); ok {
|
|
|
|
code = err.Code
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && code != -2 {
|
2022-08-29 21:22:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blefs *FS) removeAllChildren(path string) error {
|
|
|
|
list, err := blefs.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range list {
|
2022-10-16 19:39:11 +00:00
|
|
|
name := entry.Name()
|
|
|
|
|
|
|
|
if name == "." || name == ".." {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
entryPath := filepath.Join(path, name)
|
|
|
|
|
2022-08-29 21:22:02 +00:00
|
|
|
if entry.IsDir() {
|
2022-10-16 19:39:11 +00:00
|
|
|
err = blefs.removeAllChildren(entryPath)
|
2022-08-29 21:22:02 +00:00
|
|
|
} else {
|
2022-10-16 19:39:11 +00:00
|
|
|
err = blefs.Remove(entryPath)
|
2022-08-29 21:22:02 +00:00
|
|
|
}
|
2022-10-17 19:19:23 +00:00
|
|
|
|
|
|
|
var code int8
|
|
|
|
if err, ok := err.(FSError); ok {
|
|
|
|
code = err.Code
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && code != -2 {
|
2022-08-29 21:22:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blefs *FS) MkdirAll(path string) error {
|
|
|
|
if path == "" || path == "/" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
splitPath := strings.Split(path, "/")
|
|
|
|
for i := 1; i < len(splitPath); i++ {
|
2022-10-16 19:39:11 +00:00
|
|
|
curPath := strings.Join(splitPath[0:i+1], "/")
|
2022-08-29 21:22:02 +00:00
|
|
|
|
|
|
|
err := blefs.Mkdir(curPath)
|
2022-10-17 19:19:23 +00:00
|
|
|
|
|
|
|
var code int8
|
|
|
|
if err, ok := err.(FSError); ok {
|
|
|
|
code = err.Code
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && code != -17 {
|
2022-08-29 21:22:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|