forked from twpayne/go-vfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contains.go
62 lines (59 loc) · 1.4 KB
/
contains.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 vfs
import (
"os"
"path/filepath"
"syscall"
)
// A Stater implements Stat. It is assumed that the os.FileInfos returned by
// Stat are compatible with os.SameFile.
type Stater interface {
Stat(string) (os.FileInfo, error)
}
// Contains returns true if p is reachable by traversing through prefix. prefix
// must exist, but p may not. It is an expensive but accurate alternative to the
// deprecated filepath.HasPrefix.
func Contains(fs Stater, p, prefix string) (bool, error) {
prefixFI, err := fs.Stat(prefix)
if err != nil {
return false, err
}
for {
fi, err := fs.Stat(p)
switch {
case err == nil:
if os.SameFile(fi, prefixFI) {
return true, nil
}
goto TryParent
case os.IsNotExist(err):
goto TryParent
case os.IsPermission(err):
goto TryParent
default:
// Remove any os.PathError or os.SyscallError wrapping, if present.
for {
if pathError, ok := err.(*os.PathError); ok {
err = pathError.Err
} else if syscallError, ok := err.(*os.SyscallError); ok {
err = syscallError.Err
} else {
break
}
}
// Ignore some syscall.Errnos.
if errno, ok := err.(syscall.Errno); ok {
if _, ignore := ignoreErrnoInContains[errno]; ignore {
goto TryParent
}
}
return false, err
}
TryParent:
parentDir := filepath.Dir(p)
if parentDir == p {
// Return when we stop making progress.
return false, nil
}
p = parentDir
}
}