88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
|
|
package vfs
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"os"
|
||
|
|
)
|
||
|
|
|
||
|
|
// SameFile reports whether pa on a and pb on b are the same file, following
|
||
|
|
// symlinks. local+local compares device and inode (os.SameFile), which also
|
||
|
|
// catches hard links and bind mounts; other FSes on the same Volume compare
|
||
|
|
// RealPath; different volumes are never the same. A missing path is not an
|
||
|
|
// error: the answer is false.
|
||
|
|
func SameFile(ctx context.Context, a FS, pa string, b FS, pb string) (bool, error) {
|
||
|
|
if a.Volume() != b.Volume() {
|
||
|
|
return false, nil
|
||
|
|
}
|
||
|
|
if a.Kind() == KindLocal && b.Kind() == KindLocal {
|
||
|
|
fa, err := os.Stat(pa)
|
||
|
|
if err != nil {
|
||
|
|
return false, notExistIsFalse(err)
|
||
|
|
}
|
||
|
|
fb, err := os.Stat(pb)
|
||
|
|
if err != nil {
|
||
|
|
return false, notExistIsFalse(err)
|
||
|
|
}
|
||
|
|
return os.SameFile(fa, fb), nil
|
||
|
|
}
|
||
|
|
ra, err := a.RealPath(ctx, pa)
|
||
|
|
if err != nil {
|
||
|
|
return false, notExistIsFalse(err)
|
||
|
|
}
|
||
|
|
rb, err := b.RealPath(ctx, pb)
|
||
|
|
if err != nil {
|
||
|
|
return false, notExistIsFalse(err)
|
||
|
|
}
|
||
|
|
return a.Paths().Clean(ra) == b.Paths().Clean(rb), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func notExistIsFalse(err error) error {
|
||
|
|
if errors.Is(err, ErrNotExist) {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
// Within reports whether child is parent or lies inside it after resolving
|
||
|
|
// symlinks on both (e.g. "copy a directory into its own subdirectory through
|
||
|
|
// an alias"). child need not exist yet: its deepest existing ancestor is
|
||
|
|
// resolved and the rest appended. parent must exist.
|
||
|
|
func Within(ctx context.Context, fsys FS, child, parent string) (bool, error) {
|
||
|
|
rp, err := fsys.RealPath(ctx, parent)
|
||
|
|
if err != nil {
|
||
|
|
return false, err
|
||
|
|
}
|
||
|
|
rc, err := realPathLenient(ctx, fsys, child)
|
||
|
|
if err != nil {
|
||
|
|
return false, err
|
||
|
|
}
|
||
|
|
return IsWithin(fsys.Paths(), rc, rp), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// realPathLenient resolves p, or — if p does not exist — its deepest existing
|
||
|
|
// ancestor joined with the missing tail.
|
||
|
|
func realPathLenient(ctx context.Context, fsys FS, p string) (string, error) {
|
||
|
|
ops := fsys.Paths()
|
||
|
|
p = ops.Clean(p)
|
||
|
|
var tail []string
|
||
|
|
for {
|
||
|
|
r, err := fsys.RealPath(ctx, p)
|
||
|
|
if err == nil {
|
||
|
|
for i := len(tail) - 1; i >= 0; i-- {
|
||
|
|
r = ops.Join(r, tail[i])
|
||
|
|
}
|
||
|
|
return r, nil
|
||
|
|
}
|
||
|
|
if !errors.Is(err, ErrNotExist) {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
parent := ops.Dir(p)
|
||
|
|
if parent == p {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
tail = append(tail, ops.Base(p))
|
||
|
|
p = parent
|
||
|
|
}
|
||
|
|
}
|