Rename IsMountPoint to IsLikelyNotMountPoint
IsLikelyNotMountPoint determines if a directory is not a mountpoint. It is fast but not necessarily ALWAYS correct. If the path is in fact a bind mount from one part of a mount to another it will not be detected. mkdir /tmp/a /tmp/b; mount --bin /tmp/a /tmp/b; IsLikelyNotMountPoint("/tmp/b") will return true. When in fact /tmp/b is a mount point. So this patch renames the function and switches it from a positive to a negative (I could think of a good positive name). This should make future users of this function aware that it isn't quite perfect, but probably good enough.
This commit is contained in:
@@ -62,11 +62,11 @@ func (f *FakeMounter) List() ([]MountPoint, error) {
|
||||
return f.MountPoints, nil
|
||||
}
|
||||
|
||||
func (f *FakeMounter) IsMountPoint(file string) (bool, error) {
|
||||
func (f *FakeMounter) IsLikelyNotMountPoint(file string) (bool, error) {
|
||||
for _, mp := range f.MountPoints {
|
||||
if mp.Path == file {
|
||||
return true, nil
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
return true, nil
|
||||
}
|
||||
|
@@ -30,8 +30,8 @@ type Interface interface {
|
||||
// it could change between chunked reads). This is guaranteed to be
|
||||
// consistent.
|
||||
List() ([]MountPoint, error)
|
||||
// IsMountPoint determines if a directory is a mountpoint.
|
||||
IsMountPoint(file string) (bool, error)
|
||||
// IsLikelyNotMountPoint determines if a directory is a mountpoint.
|
||||
IsLikelyNotMountPoint(file string) (bool, error)
|
||||
}
|
||||
|
||||
// This represents a single line in /proc/mounts or /etc/fstab.
|
||||
|
@@ -141,20 +141,27 @@ func (*Mounter) List() ([]MountPoint, error) {
|
||||
return listProcMounts(procMountsPath)
|
||||
}
|
||||
|
||||
// IsMountPoint determines if a directory is a mountpoint, by comparing the device for the
|
||||
// directory with the device for it's parent. If they are the same, it's not a mountpoint,
|
||||
// if they're different, it is.
|
||||
func (mounter *Mounter) IsMountPoint(file string) (bool, error) {
|
||||
// IsLikelyNotMountPoint determines if a directory is not a mountpoint.
|
||||
// It is fast but not necessarily ALWAYS correct. If the path is in fact
|
||||
// a bind mount from one part of a mount to another it will not be detected.
|
||||
// mkdir /tmp/a /tmp/b; mount --bin /tmp/a /tmp/b; IsLikelyNotMountPoint("/tmp/b")
|
||||
// will return true. When in fact /tmp/b is a mount point. If this situation
|
||||
// if of interest to you, don't use this function...
|
||||
func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) {
|
||||
stat, err := os.Stat(file)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return true, err
|
||||
}
|
||||
rootStat, err := os.Lstat(file + "/..")
|
||||
if err != nil {
|
||||
return false, err
|
||||
return true, err
|
||||
}
|
||||
// If the directory has the same device as parent, then it's not a mountpoint.
|
||||
return stat.Sys().(*syscall.Stat_t).Dev != rootStat.Sys().(*syscall.Stat_t).Dev, nil
|
||||
// If the directory has a different device as parent, then it is a mountpoint.
|
||||
if stat.Sys().(*syscall.Stat_t).Dev != rootStat.Sys().(*syscall.Stat_t).Dev {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func listProcMounts(mountFilePath string) ([]MountPoint, error) {
|
||||
|
@@ -32,6 +32,6 @@ func (mounter *Mounter) List() ([]MountPoint, error) {
|
||||
return []MountPoint{}, nil
|
||||
}
|
||||
|
||||
func (mounter *Mounter) IsMountPoint(file string) (bool, error) {
|
||||
return false, nil
|
||||
func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
@@ -162,12 +162,12 @@ func (*NsenterMounter) List() ([]MountPoint, error) {
|
||||
return listProcMounts(hostProcMountsPath)
|
||||
}
|
||||
|
||||
// IsMountPoint determines whether a path is a mountpoint by calling findmnt
|
||||
// IsLikelyNotMountPoint determines whether a path is a mountpoint by calling findmnt
|
||||
// in the host's root mount namespace.
|
||||
func (n *NsenterMounter) IsMountPoint(file string) (bool, error) {
|
||||
func (n *NsenterMounter) IsLikelyNotMountPoint(file string) (bool, error) {
|
||||
file, err := filepath.Abs(file)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return true, err
|
||||
}
|
||||
|
||||
args := []string{"--mount=/rootfs/proc/1/ns/mnt", "--", n.absHostPath("findmnt"), "-o", "target", "--noheadings", "--target", file}
|
||||
@@ -177,16 +177,16 @@ func (n *NsenterMounter) IsMountPoint(file string) (bool, error) {
|
||||
out, err := exec.Command(nsenterPath, args...).CombinedOutput()
|
||||
if err != nil {
|
||||
// If findmnt didn't run, just claim it's not a mount point.
|
||||
return false, nil
|
||||
return true, nil
|
||||
}
|
||||
strOut := strings.TrimSuffix(string(out), "\n")
|
||||
|
||||
glog.V(5).Infof("IsMountPoint findmnt output: %v", strOut)
|
||||
glog.V(5).Infof("IsLikelyNotMountPoint findmnt output: %v", strOut)
|
||||
if strOut == file {
|
||||
return true, nil
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (n *NsenterMounter) absHostPath(command string) string {
|
||||
|
@@ -38,6 +38,6 @@ func (*NsenterMounter) List() ([]MountPoint, error) {
|
||||
return []MountPoint{}, nil
|
||||
}
|
||||
|
||||
func (*NsenterMounter) IsMountPoint(file string) (bool, error) {
|
||||
return false, nil
|
||||
func (*NsenterMounter) IsLikelyNotMountPoint(file string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user