-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileutil.go
73 lines (62 loc) · 1.34 KB
/
fileutil.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
64
65
66
67
68
69
70
71
72
73
package main
import (
"errors"
"io/fs"
"log"
"os"
"path/filepath"
)
func fwrite(f *os.File, line string) {
_, err := f.Write([]byte(line))
if err != nil {
log.Fatal(err)
}
}
func fwriteln(f *os.File, line string) {
_, err := f.Write([]byte(line+"\n"))
if err != nil {
log.Fatal(err)
}
}
func getDirEntries(directory string) ([]fs.DirEntry, error) {
files, err := os.ReadDir(directory)
if err != nil {
return files, err
}
return files, nil
}
func resolveSymLink(path string) (string, error) {
stat, err := os.Stat(path)
if err != nil {
return "", errors.New("Error getting stats for "+path)
}
if stat.Mode() & os.ModeSymlink != 0 {
target, err := os.Readlink(path)
if err != nil {
return "", errors.New("Error resolving "+path)
}
return resolveSymLink(target)
}
return path, nil
}
func isSymDir(dir string, f fs.DirEntry) bool {
if f.Type() & os.ModeSymlink != 0 {
resolvedPath, err := os.Readlink(filepath.Join(dir, f.Name()))
//log.Printf("%s resolvedPath: %s", f.Name, resolvedPath)
//resolvedPath, err := filepath.EvalSymlinks(filepath.Join(dir, f.Name()))
if err != nil {
log.Print(err)
return false
}
info, err := os.Stat(resolvedPath)
if err != nil {
// commonly errors here when symlink is broken
log.Print(err)
return false
}
if info.IsDir() {
return true
}
}
return false
}