Debian Codegraph @

mountinfo/mounted_linux.go

raw symbols SCIP
1 package mountinfo
2
3 import (
4 "os"
5 "path/filepath"
6
7 "golang.org/x/sys/unix"
8 )
9
10 // MountedFast is a method of detecting a mount point without reading
11 // mountinfo from procfs. A caller can only trust the result if no error
12 // and sure == true are returned. Otherwise, other methods (e.g. parsing
13 // /proc/mounts) have to be used. If unsure, use Mounted instead (which
14 // uses MountedFast, but falls back to parsing mountinfo if needed).
15 //
16 // If a non-existent path is specified, an appropriate error is returned.
17 // In case the caller is not interested in this particular error, it should
18 // be handled separately using e.g. errors.Is(err, fs.ErrNotExist).
19 //
20 // This function is only available on Linux. When available (since kernel
21 // v5.6), openat2(2) syscall is used to reliably detect all mounts. Otherwise,
22 // the implementation falls back to using stat(2), which can reliably detect
23 // normal (but not bind) mounts.
24 func MountedFast(path string) (mounted, sure bool, err error) {
25 // Root is always mounted.
26 if path == string(os.PathSeparator) {
27 return true, true, nil
28 }
29
30 path, err = normalizePath(path)
31 if err != nil {
32 return false, false, err
33 }
34 mounted, sure, err = mountedFast(path)
35 return
36 }
37
38 // mountedByOpenat2 is a method of detecting a mount that works for all kinds
39 // of mounts (incl. bind mounts), but requires a recent (v5.6+) linux kernel.
40 func mountedByOpenat2(path string) (bool, error) {
41 dir, last := filepath.Split(path)
42
43 dirfd, err := unix.Openat2(unix.AT_FDCWD, dir, &unix.OpenHow{
44 Flags: unix.O_PATH | unix.O_CLOEXEC,
45 })
46 if err != nil {
47 return false, &os.PathError{Op: "openat2", Path: dir, Err: err}
48 }
49 fd, err := unix.Openat2(dirfd, last, &unix.OpenHow{
50 Flags: unix.O_PATH | unix.O_CLOEXEC | unix.O_NOFOLLOW,
51 Resolve: unix.RESOLVE_NO_XDEV,
52 })
53 _ = unix.Close(dirfd)
54 switch err {
55 case nil: // definitely not a mount
56 _ = unix.Close(fd)
57 return false, nil
58 case unix.EXDEV: // definitely a mount
59 return true, nil
60 }
61 // not sure
62 return false, &os.PathError{Op: "openat2", Path: path, Err: err}
63 }
64
65 // mountedFast is similar to MountedFast, except it expects a normalized path.
66 func mountedFast(path string) (mounted, sure bool, err error) {
67 // Root is always mounted.
68 if path == string(os.PathSeparator) {
69 return true, true, nil
70 }
71
72 // Try a fast path, using openat2() with RESOLVE_NO_XDEV.
73 mounted, err = mountedByOpenat2(path)
74 if err == nil {
75 return mounted, true, nil
76 }
77
78 // Another fast path: compare st.st_dev fields.
79 mounted, err = mountedByStat(path)
80 // This does not work for bind mounts, so false negative
81 // is possible, therefore only trust if return is true.
82 if mounted && err == nil {
83 return true, true, nil
84 }
85
86 return
87 }
88
89 func mounted(path string) (bool, error) {
90 path, err := normalizePath(path)
91 if err != nil {
92 return false, err
93 }
94 mounted, sure, err := mountedFast(path)
95 if sure && err == nil {
96 return mounted, nil
97 }
98
99 // Fallback to parsing mountinfo.
100 return mountedByMountinfo(path)
101 }