diffs and relevant changes: docker/docker:c2cc352355...af34b94a78- replace pkg/symlink with github.com/moby/sys/symlink moby/sys:6154f11e68...1bc8673b57changes: - mount, mountinfo: Add support for OpenBSD in addition to FreeBSD - mount, mountinfo: Exclude macOS (darwin) - mount.RecursiveUnmount(): minor improvements - mount.RecursiveUnmount: add a fast path - mount: bump mountinfo to v0.3.1 - mount: Some refactor and improved GoDoc about Windows support - mount: use MNT_* flags from golang.org/x/sys/unix on freebsd (reduces use of cgo) - mountinfo.Mounted: add fast path for Linux using openat2 - mountinfo.Mounted: optimize by adding fast paths - mountinfo: correctness improvements - mountinfo: deprecate PidMountInfo - mountinfo: fix not showing package doc, typos - mountinfo: fix path unescaping - mountinfo: make GetMountinfoFromReader Linux-specific - mountinfo: rename FstypeFilter -> FSTypeFilter, fix FilterFunc docs - mountinfo: use idiomatic naming for fields - Migrate github.com/docker/docker/pkg/symlink Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
// +build linux freebsd,cgo openbsd,cgo
|
|
|
|
package mountinfo
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func mountedByStat(path string) (bool, error) {
|
|
var st unix.Stat_t
|
|
|
|
if err := unix.Lstat(path, &st); err != nil {
|
|
if err == unix.ENOENT {
|
|
// Treat ENOENT as "not mounted".
|
|
return false, nil
|
|
}
|
|
return false, &os.PathError{Op: "stat", Path: path, Err: err}
|
|
}
|
|
dev := st.Dev
|
|
parent := filepath.Dir(path)
|
|
if err := unix.Lstat(parent, &st); err != nil {
|
|
return false, &os.PathError{Op: "stat", Path: parent, Err: err}
|
|
}
|
|
if dev != st.Dev {
|
|
// Device differs from that of parent,
|
|
// so definitely a mount point.
|
|
return true, nil
|
|
}
|
|
// NB: this does not detect bind mounts on Linux.
|
|
return false, nil
|
|
}
|
|
|
|
func normalizePath(path string) (realPath string, err error) {
|
|
if realPath, err = filepath.Abs(path); err != nil {
|
|
return "", fmt.Errorf("unable to get absolute path for %q: %w", path, err)
|
|
}
|
|
if realPath, err = filepath.EvalSymlinks(realPath); err != nil {
|
|
return "", fmt.Errorf("failed to canonicalise path for %q: %w", path, err)
|
|
}
|
|
if _, err := os.Stat(realPath); err != nil {
|
|
return "", fmt.Errorf("failed to stat target of %q: %w", path, err)
|
|
}
|
|
return realPath, nil
|
|
}
|
|
|
|
func mountedByMountinfo(path string) (bool, error) {
|
|
path, err := normalizePath(path)
|
|
if err != nil {
|
|
if errors.Is(err, unix.ENOENT) {
|
|
// treat ENOENT as "not mounted"
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|
|
entries, err := GetMounts(SingleEntryFilter(path))
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return len(entries) > 0, nil
|
|
}
|