refact IsLikelyNotMountPoint

Signed-off-by: j4ckstraw <j4ckstraw@foxmail.com>
This commit is contained in:
j4ckstraw 2024-01-08 09:46:37 +08:00
parent c3775de747
commit d5664276bf

View File

@ -404,7 +404,7 @@ func statx(file string) (unix.Statx_t, error) {
return stat, nil
}
func (mounter *Mounter) isLikelyNotMountPoint(file string) (bool, error) {
func (mounter *Mounter) isLikelyNotMountPointStat(file string) (bool, error) {
stat, err := os.Stat(file)
if err != nil {
return true, err
@ -421,27 +421,11 @@ func (mounter *Mounter) isLikelyNotMountPoint(file string) (bool, error) {
return true, nil
}
// 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.
// It also can not distinguish between mountpoints and symbolic links.
// mkdir /tmp/a /tmp/b; mount --bind /tmp/a /tmp/b; IsLikelyNotMountPoint("/tmp/b")
// will return true. When in fact /tmp/b is a mount point. If this situation
// is of interest to you, don't use this function...
func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) {
func (mounter *Mounter) isLikelyNotMountPointStatx(file string) (bool, error) {
var stat, rootStat unix.Statx_t
var err error
if stat, err = statx(file); err != nil {
if errors.Is(err, errStatxNotSupport) {
// not support statx, go slow path
// mnt, mntErr := mounter.IsMountPoint(file)
// return !mnt, mntErr
// fall back to isLikelyNotMountPoint
return mounter.isLikelyNotMountPoint(file)
}
return true, err
}
@ -465,6 +449,23 @@ func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) {
return (stat.Dev_major == rootStat.Dev_major && stat.Dev_minor == rootStat.Dev_minor), nil
}
// 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.
// It also can not distinguish between mountpoints and symbolic links.
// mkdir /tmp/a /tmp/b; mount --bind /tmp/a /tmp/b; IsLikelyNotMountPoint("/tmp/b")
// will return true. When in fact /tmp/b is a mount point. If this situation
// is of interest to you, don't use this function...
func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) {
notMountPoint, err := mounter.isLikelyNotMountPointStatx(file)
if errors.Is(err, errStatxNotSupport) {
// fall back to isLikelyNotMountPointStat
return mounter.isLikelyNotMountPointStat(file)
}
return notMountPoint, err
}
// CanSafelySkipMountPointCheck relies on the detected behavior of umount when given a target that is not a mount point.
func (mounter *Mounter) CanSafelySkipMountPointCheck() bool {
return mounter.withSafeNotMountedBehavior