-
Notifications
You must be signed in to change notification settings - Fork 1
/
remove.go
63 lines (51 loc) · 1.33 KB
/
remove.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package wrfs
import (
"path"
)
// RemoveFS is a file system that supports the Remove function.
type RemoveFS interface {
FS
// Remove removes the named file or (empty) directory.
Remove(name string) error
}
// Remove removes the named file or (empty) directory.
func Remove(fsys FS, name string) error {
if fsys, ok := fsys.(RemoveFS); ok {
return fsys.Remove(name)
}
return &PathError{Op: "remove", Path: name, Err: ErrUnsupported}
}
// RemoveAllFS is a file system that supports the RemoveAll function.
type RemoveAllFS interface {
FS
// RemoveAll removes path and any children it contains.
RemoveAll(path string) error
}
// RemoveAll removes path and any children it contains.
func RemoveAll(fsys FS, removePath string) error {
if fsys, ok := fsys.(RemoveAllFS); ok {
return fsys.RemoveAll(removePath)
}
fi, err := Stat(fsys, removePath)
if err != nil {
return err
}
// Check if we are removing a file or a directory.
if !fi.IsDir() {
return Remove(fsys, removePath)
}
files, err := ReadDir(fsys, removePath)
if err != nil {
return err
}
for _, fi := range files {
if fi.IsDir() {
if err = RemoveAll(fsys, path.Join(removePath, fi.Name())); err != nil {
return err
}
} else if err = Remove(fsys, path.Join(removePath, fi.Name())); err != nil {
return err
}
}
return Remove(fsys, removePath)
}