Skip to content

Commit e157130

Browse files
j4ckstrawk8s-publishing-bot
authored andcommitted
refact IsLikelyNotMountPoint
Signed-off-by: j4ckstraw <[email protected]> Kubernetes-commit: d5664276bf5baf7ebf997609051dad240d09e1f5
1 parent ab4d08a commit e157130

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

mount_linux.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ func statx(file string) (unix.Statx_t, error) {
404404
return stat, nil
405405
}
406406

407-
func (mounter *Mounter) isLikelyNotMountPoint(file string) (bool, error) {
407+
func (mounter *Mounter) isLikelyNotMountPointStat(file string) (bool, error) {
408408
stat, err := os.Stat(file)
409409
if err != nil {
410410
return true, err
@@ -421,27 +421,11 @@ func (mounter *Mounter) isLikelyNotMountPoint(file string) (bool, error) {
421421
return true, nil
422422
}
423423

424-
// IsLikelyNotMountPoint determines if a directory is not a mountpoint.
425-
// It is fast but not necessarily ALWAYS correct. If the path is in fact
426-
// a bind mount from one part of a mount to another it will not be detected.
427-
// It also can not distinguish between mountpoints and symbolic links.
428-
// mkdir /tmp/a /tmp/b; mount --bind /tmp/a /tmp/b; IsLikelyNotMountPoint("/tmp/b")
429-
// will return true. When in fact /tmp/b is a mount point. If this situation
430-
// is of interest to you, don't use this function...
431-
func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) {
424+
func (mounter *Mounter) isLikelyNotMountPointStatx(file string) (bool, error) {
432425
var stat, rootStat unix.Statx_t
433426
var err error
434427

435428
if stat, err = statx(file); err != nil {
436-
if errors.Is(err, errStatxNotSupport) {
437-
// not support statx, go slow path
438-
// mnt, mntErr := mounter.IsMountPoint(file)
439-
// return !mnt, mntErr
440-
441-
// fall back to isLikelyNotMountPoint
442-
return mounter.isLikelyNotMountPoint(file)
443-
}
444-
445429
return true, err
446430
}
447431

@@ -465,6 +449,23 @@ func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) {
465449
return (stat.Dev_major == rootStat.Dev_major && stat.Dev_minor == rootStat.Dev_minor), nil
466450
}
467451

452+
// IsLikelyNotMountPoint determines if a directory is not a mountpoint.
453+
// It is fast but not necessarily ALWAYS correct. If the path is in fact
454+
// a bind mount from one part of a mount to another it will not be detected.
455+
// It also can not distinguish between mountpoints and symbolic links.
456+
// mkdir /tmp/a /tmp/b; mount --bind /tmp/a /tmp/b; IsLikelyNotMountPoint("/tmp/b")
457+
// will return true. When in fact /tmp/b is a mount point. If this situation
458+
// is of interest to you, don't use this function...
459+
func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) {
460+
notMountPoint, err := mounter.isLikelyNotMountPointStatx(file)
461+
if errors.Is(err, errStatxNotSupport) {
462+
// fall back to isLikelyNotMountPointStat
463+
return mounter.isLikelyNotMountPointStat(file)
464+
}
465+
466+
return notMountPoint, err
467+
}
468+
468469
// CanSafelySkipMountPointCheck relies on the detected behavior of umount when given a target that is not a mount point.
469470
func (mounter *Mounter) CanSafelySkipMountPointCheck() bool {
470471
return mounter.withSafeNotMountedBehavior

0 commit comments

Comments
 (0)