Support detecting mountpoints correctly (#7288)

Currently windows support was relying on Symlink as
a way to detect a drive, this doesn't work in latest
Windows 2016, fix this to use a proper mechanism by
using win32 APIs.

Additionally also add support for detecting bind mounts
on Linux.
This commit is contained in:
Harshavardhana
2019-02-26 18:04:53 -08:00
committed by kannappanr
parent 5085bef397
commit 5e69a107d8
25 changed files with 6925 additions and 32 deletions

View File

@@ -19,7 +19,7 @@
package mountinfo
import (
"os"
"golang.org/x/sys/windows"
)
// CheckCrossDevice - check if any input path has multiple sub-mounts.
@@ -28,32 +28,21 @@ func CheckCrossDevice(paths []string) error {
return nil
}
// fileExists checks if specified file exists.
func fileExists(filename string) (bool, error) {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}
// IsLikelyMountPoint determines if a directory is a mountpoint.
func IsLikelyMountPoint(file string) bool {
stat, err := os.Lstat(file)
if err != nil {
func IsLikelyMountPoint(path string) bool {
wpath, _ := windows.UTF16PtrFromString(path)
wvolume := make([]uint16, len(path)+1)
if err := windows.GetVolumePathName(wpath, &wvolume[0], uint32(len(wvolume))); err != nil {
return false
}
// If current file is a symlink, then it is a mountpoint.
if stat.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(file)
if err != nil {
return false
}
exists, _ := fileExists(target)
return exists
switch windows.GetDriveType(&wvolume[0]) {
case windows.DRIVE_FIXED, windows.DRIVE_REMOVABLE, windows.DRIVE_REMOTE, windows.DRIVE_RAMDISK:
// Recognize "fixed", "removable", "remote" and "ramdisk" drives as proper drives
// which can be treated as an actual mount-point, rest can be ignored.
// https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getdrivetypew
return true
}
return false
}