@@ -20,34 +20,30 @@ package mount
2020
2121import (
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.
3029func 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}
0 commit comments