refact IsLikelyNotMountPoint
Signed-off-by: j4ckstraw <j4ckstraw@foxmail.com>
This commit is contained in:
		@@ -404,7 +404,7 @@ func statx(file string) (unix.Statx_t, error) {
 | 
				
			|||||||
	return stat, nil
 | 
						return stat, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (mounter *Mounter) isLikelyNotMountPoint(file string) (bool, error) {
 | 
					func (mounter *Mounter) isLikelyNotMountPointStat(file string) (bool, error) {
 | 
				
			||||||
	stat, err := os.Stat(file)
 | 
						stat, err := os.Stat(file)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return true, err
 | 
							return true, err
 | 
				
			||||||
@@ -421,27 +421,11 @@ func (mounter *Mounter) isLikelyNotMountPoint(file string) (bool, error) {
 | 
				
			|||||||
	return true, nil
 | 
						return true, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// IsLikelyNotMountPoint determines if a directory is not a mountpoint.
 | 
					func (mounter *Mounter) isLikelyNotMountPointStatx(file string) (bool, error) {
 | 
				
			||||||
// 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) {
 | 
					 | 
				
			||||||
	var stat, rootStat unix.Statx_t
 | 
						var stat, rootStat unix.Statx_t
 | 
				
			||||||
	var err error
 | 
						var err error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if stat, err = statx(file); err != nil {
 | 
						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
 | 
							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
 | 
						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.
 | 
					// CanSafelySkipMountPointCheck relies on the detected behavior of umount when given a target that is not a mount point.
 | 
				
			||||||
func (mounter *Mounter) CanSafelySkipMountPointCheck() bool {
 | 
					func (mounter *Mounter) CanSafelySkipMountPointCheck() bool {
 | 
				
			||||||
	return mounter.withSafeNotMountedBehavior
 | 
						return mounter.withSafeNotMountedBehavior
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user