Skip to content

Commit e62d03b

Browse files
authored
Merge pull request #4578 from thaJeztah/use_moby_sys
mount: replace mountinfo handling with moby/sys/mountinfo
2 parents 9db6aa6 + 2374178 commit e62d03b

23 files changed

Lines changed: 854 additions & 968 deletions

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ require (
3737
github.com/hashicorp/go-multierror v1.0.0
3838
github.com/imdario/mergo v0.3.10
3939
github.com/klauspost/compress v1.11.3
40+
github.com/moby/sys/mountinfo v0.4.0
4041
github.com/moby/sys/symlink v0.1.0
4142
github.com/opencontainers/go-digest v1.0.0
4243
github.com/opencontainers/image-spec v1.0.1

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible/go
345345
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
346346
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
347347
github.com/moby/sys/mountinfo v0.1.3/go.mod h1:w2t2Avltqx8vE7gX5l+QiBKxODu2TX0+Syr3h52Tw4o=
348+
github.com/moby/sys/mountinfo v0.4.0 h1:1KInV3Huv18akCu58V7lzNlt+jFmqlu1EaErnEHE/VM=
349+
github.com/moby/sys/mountinfo v0.4.0/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A=
348350
github.com/moby/sys/symlink v0.1.0 h1:MTFZ74KtNI6qQQpuBxU+uKCim4WtOMokr03hCfJcazE=
349351
github.com/moby/sys/symlink v0.1.0/go.mod h1:GGDODQmbFOjFsXvfLVn3+ZRxkch54RkSiGqsZeMYowQ=
350352
github.com/moby/term v0.0.0-20200312100748-672ec06f55cd/go.mod h1:DdlQx2hp0Ss5/fLikoLlEeIYiATotOjgB//nb973jeo=
@@ -611,6 +613,7 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
611613
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
612614
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
613615
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
616+
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
614617
golang.org/x/sys v0.0.0-20200916030750-2334cc1a136f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
615618
golang.org/x/sys v0.0.0-20200922070232-aee5d888a860/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
616619
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3 h1:kzM6+9dur93BcC2kVlYl34cHU+TYZLanmpSJHVMmL64=

mount/lookup_unix.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,30 @@ package mount
2020

2121
import (
2222
"path/filepath"
23-
"sort"
24-
"strings"
2523

24+
"github.com/moby/sys/mountinfo"
2625
"github.com/pkg/errors"
2726
)
2827

2928
// Lookup returns the mount info corresponds to the path.
3029
func Lookup(dir string) (Info, error) {
3130
dir = filepath.Clean(dir)
3231

33-
mounts, err := Self()
32+
m, err := mountinfo.GetMounts(mountinfo.ParentsFilter(dir))
3433
if err != nil {
35-
return Info{}, err
34+
return Info{}, errors.Wrapf(err, "failed to find the mount info for %q", dir)
35+
}
36+
if len(m) == 0 {
37+
return Info{}, errors.Errorf("failed to find the mount info for %q", dir)
3638
}
3739

38-
// Sort descending order by Info.Mountpoint
39-
sort.SliceStable(mounts, func(i, j int) bool {
40-
return mounts[j].Mountpoint < mounts[i].Mountpoint
41-
})
42-
for _, m := range mounts {
43-
// Note that m.{Major, Minor} are generally unreliable for our purpose here
44-
// https://www.spinics.net/lists/linux-btrfs/msg58908.html
45-
// Note that device number is not checked here, because for overlayfs files
46-
// may have different device number with the mountpoint.
47-
if strings.HasPrefix(dir, m.Mountpoint) {
48-
return m, nil
40+
// find the longest matching mount point
41+
var idx, maxlen int
42+
for i := range m {
43+
if len(m[i].Mountpoint) > maxlen {
44+
maxlen = len(m[i].Mountpoint)
45+
idx = i
4946
}
5047
}
51-
52-
return Info{}, errors.Errorf("failed to find the mount info for %q", dir)
48+
return *m[idx], nil
5349
}

mount/mountinfo.go

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,8 @@
1616

1717
package mount
1818

19+
import "github.com/moby/sys/mountinfo"
20+
1921
// Info reveals information about a particular mounted filesystem. This
2022
// struct is populated from the content in the /proc/<pid>/mountinfo file.
21-
type Info struct {
22-
// ID is a unique identifier of the mount (may be reused after umount).
23-
ID int
24-
25-
// Parent indicates the ID of the mount parent (or of self for the top of the
26-
// mount tree).
27-
Parent int
28-
29-
// Major indicates one half of the device ID which identifies the device class.
30-
Major int
31-
32-
// Minor indicates one half of the device ID which identifies a specific
33-
// instance of device.
34-
Minor int
35-
36-
// Root of the mount within the filesystem.
37-
Root string
38-
39-
// Mountpoint indicates the mount point relative to the process's root.
40-
Mountpoint string
41-
42-
// Options represents mount-specific options.
43-
Options string
44-
45-
// Optional represents optional fields.
46-
Optional string
47-
48-
// FSType indicates the type of filesystem, such as EXT3.
49-
FSType string
50-
51-
// Source indicates filesystem specific information or "none".
52-
Source string
53-
54-
// VFSOptions represents per super block options.
55-
VFSOptions string
56-
}
23+
type Info = mountinfo.Info

mount/mountinfo_bsd.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

mount/mountinfo_linux.go

Lines changed: 0 additions & 145 deletions
This file was deleted.

0 commit comments

Comments
 (0)