-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
62 lines (51 loc) · 1.31 KB
/
fs.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
package utils
import (
"fmt"
"io/fs"
"io/ioutil"
"os"
"path"
"path/filepath"
)
func WalkFS(filePath string, fn func(string, string, fs.FileInfo) error) error {
var walk func(string, string, fs.FileInfo) error
walk = func(virtualPath, filePath string, info fs.FileInfo) error {
if (info.Mode() & os.ModeSymlink) != 0 {
targetPath, err := filepath.EvalSymlinks(filePath)
if err != nil {
return fmt.Errorf("cannot resolve symlink %q: %w",
filePath, err)
}
virtualPath = filePath
filePath = targetPath
info, err = os.Stat(filePath)
if err != nil {
return fmt.Errorf("cannot stat %q: %w", filePath, err)
}
}
if info.IsDir() {
filenames, err := ioutil.ReadDir(filePath)
if err != nil {
return fmt.Errorf("cannot list directory %q: %w",
filePath, err)
}
for _, childInfo := range filenames {
childName := childInfo.Name()
childVirtualPath := path.Join(virtualPath, childName)
childFilePath := path.Join(filePath, childName)
err := walk(childVirtualPath, childFilePath, childInfo)
if err != nil {
return err
}
}
return nil
} else {
return fn(virtualPath, filePath, info)
}
}
info, err := os.Stat(filePath)
if err != nil {
return fmt.Errorf("cannot stat %q: %w", filePath, err)
}
return walk(filePath, filePath, info)
}