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:
parent
e937df1a07
commit
f387b33f84
3 changed files with 74 additions and 40 deletions
|
@ -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")
|
||||
}
|
||||
|
|
59
utils/filesystem_unix.go
Normal file
59
utils/filesystem_unix.go
Normal 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")
|
||||
}
|
15
utils/filesystem_windows.go
Normal file
15
utils/filesystem_windows.go
Normal 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)
|
||||
}
|
Loading…
Reference in a new issue