fork/gdu

git clone https://git.julia.blue/fork/gdu

Daniel Milderefactor: correct ordering5312bf1

subvolume
1.3 KiB56 linesraw
1package device
2
3import "strings"
4
5// Device struct
6type Device struct {
7	Name       string
8	MountPoint string
9	Fstype     string
10	Size       int64
11	Free       int64
12}
13
14// GetUsage returns used size of device
15func (d Device) GetUsage() int64 {
16	return d.Size - d.Free
17}
18
19// DevicesInfoGetter is type for GetDevicesInfo function
20type DevicesInfoGetter interface {
21	GetMounts() (Devices, error)
22	GetDevicesInfo() (Devices, error)
23}
24
25// Devices if slice of Device items
26type Devices []*Device
27
28// ByUsedSize sorts devices by used size
29type ByUsedSize Devices
30
31func (f ByUsedSize) Len() int      { return len(f) }
32func (f ByUsedSize) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
33func (f ByUsedSize) Less(i, j int) bool {
34	return f[i].GetUsage() < f[j].GetUsage()
35}
36
37// ByName sorts devices by device name
38type ByName Devices
39
40func (f ByName) Len() int      { return len(f) }
41func (f ByName) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
42func (f ByName) Less(i, j int) bool {
43	return f[i].Name < f[j].Name
44}
45
46// GetNestedMountpointsPaths returns paths of nested mount points
47func GetNestedMountpointsPaths(path string, mounts Devices) []string {
48	paths := make([]string, 0, len(mounts))
49
50	for _, mount := range mounts {
51		if strings.HasPrefix(mount.MountPoint, path) && mount.MountPoint != path {
52			paths = append(paths, mount.MountPoint)
53		}
54	}
55	return paths
56}