Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 0 additions & 75 deletions utils/filesystem.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
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
Expand Down Expand Up @@ -39,71 +32,3 @@ func GetDiskUsageStats(path string) (uint64, uint64, error) {

return dirSize, inodeCount, err
}

// GetDeviceUUIDFromPath accepts a path, and will find the device
// corresponding to the path and return the UUID of that device
func GetDeviceUUIDFromPath(devicePath string) (string, error) {
const dir = "/dev/disk/by-uuid"

if _, err := os.Stat(dir); os.IsNotExist(err) {
return "", nil
}

files, err := ioutil.ReadDir(dir)
if err != nil {
return "", err
}

for _, file := range files {
path := filepath.Join(dir, file.Name())
target, err := os.Readlink(path)
if err != nil {
continue
}
device, err := filepath.Abs(filepath.Join(dir, target))
if err != nil {
return "", fmt.Errorf("failed to resolve the absolute path of %q", filepath.Join(dir, target))
}
if strings.Compare(device, devicePath) == 0 {
return file.Name(), nil
}
}

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