mount: add mount.Lookup for ease of implementing snapshotters
`func Lookup(dir string) (Info, error)` returns the mount info that corresponds to the dir Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
37
mount/lookup_unix.go
Normal file
37
mount/lookup_unix.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// +build !windows
|
||||
|
||||
package mount
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Lookup returns the mount info corresponds to the path.
|
||||
func Lookup(dir string) (Info, error) {
|
||||
var dirStat syscall.Stat_t
|
||||
if err := syscall.Stat(dir, &dirStat); err != nil {
|
||||
return Info{}, errors.Wrapf(err, "failed to access %q", dir)
|
||||
}
|
||||
|
||||
mounts, err := Self()
|
||||
if err != nil {
|
||||
return Info{}, err
|
||||
}
|
||||
for _, m := range mounts {
|
||||
// Note that m.{Major, Minor} are generally unreliable for our purpose here
|
||||
// https://www.spinics.net/lists/linux-btrfs/msg58908.html
|
||||
var st syscall.Stat_t
|
||||
if err := syscall.Stat(m.Mountpoint, &st); err != nil {
|
||||
// may fail; ignore err
|
||||
continue
|
||||
}
|
||||
if st.Dev == dirStat.Dev {
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
return Info{}, fmt.Errorf("failed to find the mount info for %q", dir)
|
||||
}
|
Reference in New Issue
Block a user