@@ -29,6 +29,7 @@ import (
29
29
"path/filepath"
30
30
"strconv"
31
31
"strings"
32
+ "syscall"
32
33
"time"
33
34
34
35
"github.com/moby/sys/mountinfo"
@@ -403,21 +404,42 @@ func statx(file string) (unix.Statx_t, error) {
403
404
return stat , nil
404
405
}
405
406
407
+ func (mounter * Mounter ) isLikelyNotMountPoint (file string ) (bool , error ) {
408
+ stat , err := os .Stat (file )
409
+ if err != nil {
410
+ return true , err
411
+ }
412
+ rootStat , err := os .Stat (filepath .Dir (strings .TrimSuffix (file , "/" )))
413
+ if err != nil {
414
+ return true , err
415
+ }
416
+ // If the directory has a different device as parent, then it is a mountpoint.
417
+ if stat .Sys ().(* syscall.Stat_t ).Dev != rootStat .Sys ().(* syscall.Stat_t ).Dev {
418
+ return false , nil
419
+ }
420
+
421
+ return true , nil
422
+ }
423
+
406
424
// IsLikelyNotMountPoint determines if a directory is not a mountpoint.
407
- // If fast check failed, fall back to slow path . If the path is in fact
408
- // a bind mount from one part of a mount to another it will be detected.
409
- // It also can distinguish between mountpoints and symbolic links.
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.
410
428
// mkdir /tmp/a /tmp/b; mount --bind /tmp/a /tmp/b; IsLikelyNotMountPoint("/tmp/b")
411
- // will return false. When in fact /tmp/b is a mount point.
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...
412
431
func (mounter * Mounter ) IsLikelyNotMountPoint (file string ) (bool , error ) {
413
432
var stat , rootStat unix.Statx_t
414
433
var err error
415
434
416
435
if stat , err = statx (file ); err != nil {
417
436
if errors .Is (err , errStatxNotSupport ) {
418
437
// not support statx, go slow path
419
- mnt , mntErr := mounter .IsMountPoint (file )
420
- return ! mnt , mntErr
438
+ // mnt, mntErr := mounter.IsMountPoint(file)
439
+ // return !mnt, mntErr
440
+
441
+ // fall back to isLikelyNotMountPoint
442
+ return mounter .isLikelyNotMountPoint (file )
421
443
}
422
444
423
445
return false , err
0 commit comments