From f387b33f841347f95538727c2493acfd9735ceaf Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Wed, 28 Feb 2018 10:38:43 -0500 Subject: [PATCH] utils: platform independent calls Signed-off-by: Vincent Batts utils: isolate unix only functions Signed-off-by: Vincent Batts --- utils/filesystem.go | 40 ------------------------- utils/filesystem_unix.go | 59 +++++++++++++++++++++++++++++++++++++ utils/filesystem_windows.go | 15 ++++++++++ 3 files changed, 74 insertions(+), 40 deletions(-) create mode 100644 utils/filesystem_unix.go create mode 100644 utils/filesystem_windows.go diff --git a/utils/filesystem.go b/utils/filesystem.go index 392ef4ec..d990e8f8 100644 --- a/utils/filesystem.go +++ b/utils/filesystem.go @@ -2,14 +2,10 @@ package utils import ( "fmt" - "golang.org/x/sys/unix" "io/ioutil" "os" "path/filepath" "strings" - "syscall" - - "github.com/docker/docker/pkg/mount" ) // GetDiskUsageStats accepts a path to a directory or file @@ -71,39 +67,3 @@ func GetDeviceUUIDFromPath(devicePath string) (string, error) { return "", fmt.Errorf("device path %s not found", devicePath) } - -// GetStattFromPath is a helper function that returns the Stat_t -// object for a given path -func GetStattFromPath(path string) (syscall.Stat_t, error) { - statInfo := syscall.Stat_t{} - err := syscall.Lstat(path, &statInfo) - if err != nil { - return statInfo, err - } - return statInfo, nil -} - -// GetDeviceNameFromPath iterates through the mounts and matches -// the one that the provided path is on -func GetDeviceNameFromPath(path string) (string, error) { - statInfo, err := GetStattFromPath(path) - if err != nil { - return "", err - } - - mounts, err := mount.GetMounts() - if err != nil { - return "", err - } - - queryMajor := int(unix.Major(uint64(statInfo.Dev))) - queryMinor := int(unix.Minor(uint64(statInfo.Dev))) - - for _, mount := range mounts { - if mount.Minor == queryMinor && mount.Major == queryMajor { - return mount.Source, nil - } - } - - return "", fmt.Errorf("no match found") -} diff --git a/utils/filesystem_unix.go b/utils/filesystem_unix.go new file mode 100644 index 00000000..a91c5a94 --- /dev/null +++ b/utils/filesystem_unix.go @@ -0,0 +1,59 @@ +// +build linux darwin netbsd + +package utils + +import ( + "fmt" + "syscall" + + "github.com/docker/docker/pkg/mount" + "golang.org/x/sys/unix" +) + +func Major(dev uint64) uint32 { + return unix.Major(dev) +} + +func Minor(dev uint64) uint32 { + return unix.Minor(dev) +} + +func Mkdev(major, minor uint32) uint64 { + return unix.Mkdev(major, minor) +} + +// GetStattFromPath is a helper function that returns the Stat_t +// object for a given path +func GetStattFromPath(path string) (syscall.Stat_t, error) { + statInfo := syscall.Stat_t{} + err := syscall.Lstat(path, &statInfo) + if err != nil { + return statInfo, err + } + return statInfo, nil +} + +// GetDeviceNameFromPath iterates through the mounts and matches +// the one that the provided path is on +func GetDeviceNameFromPath(path string) (string, error) { + statInfo, err := GetStattFromPath(path) + if err != nil { + return "", err + } + + mounts, err := mount.GetMounts() + if err != nil { + return "", err + } + + queryMajor := int(Major(uint64(statInfo.Dev))) + queryMinor := int(Minor(uint64(statInfo.Dev))) + + for _, mount := range mounts { + if mount.Minor == queryMinor && mount.Major == queryMajor { + return mount.Source, nil + } + } + + return "", fmt.Errorf("no match found") +} diff --git a/utils/filesystem_windows.go b/utils/filesystem_windows.go new file mode 100644 index 00000000..f2f72f4f --- /dev/null +++ b/utils/filesystem_windows.go @@ -0,0 +1,15 @@ +// +build windows + +package utils + +func Major(device uint64) uint64 { + return (device >> 8) & 0xfff +} + +func Minor(device uint64) uint64 { + return (device & 0xff) | ((device >> 12) & 0xfff00) +} + +func Mkdev(major, minor uint32) uint64 { + return uint64((major << 24) | minor) +}