utils: platform independent calls

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>

utils: isolate unix only functions

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2018-02-28 10:38:43 -05:00
parent e937df1a07
commit f387b33f84
3 changed files with 74 additions and 40 deletions

View file

@ -2,14 +2,10 @@ package utils
import ( import (
"fmt" "fmt"
"golang.org/x/sys/unix"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"syscall"
"github.com/docker/docker/pkg/mount"
) )
// GetDiskUsageStats accepts a path to a directory or file // 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) 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")
}

59
utils/filesystem_unix.go Normal file
View file

@ -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")
}

View file

@ -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)
}