-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontains.go
69 lines (66 loc) · 1.57 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
63
64
65
66
67
68
69
package vfs
import (
"errors"
"io/fs"
"os"
"path/filepath"
"syscall"
)
// A Stater implements Stat. It is assumed that the fs.FileInfos returned by
// Stat are compatible with os.SameFile.
type Stater interface {
Stat(name string) (fs.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(fileSystem Stater, p, prefix string) (bool, error) {
prefixFI, err := fileSystem.Stat(prefix)
if err != nil {
return false, err
}
for {
fi, err := fileSystem.Stat(p)
switch {
case err == nil:
if os.SameFile(fi, prefixFI) {
return true, nil
}
goto TryParent
case errors.Is(err, fs.ErrNotExist):
goto TryParent
case errors.Is(err, fs.ErrPermission):
goto TryParent
default:
// Remove any fs.PathError or os.SyscallError wrapping, if present.
Unwrap:
for {
var pathError *fs.PathError
var syscallError *os.SyscallError
switch {
case errors.As(err, &pathError):
err = pathError.Err
case errors.As(err, &syscallError):
err = syscallError.Err
default:
break Unwrap
}
}
// Ignore some syscall.Errnos.
var syscallErrno syscall.Errno
if errors.As(err, &syscallErrno) {
if _, ignore := ignoreErrnoInContains[syscallErrno]; 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
}
}