fork/gdu
git clone https://git.julia.blue/fork/gdu
ede21d2
ignore
1//go:build netbsd || openbsd 2 3package device 4 5import ( 6"bufio" 7"bytes" 8"errors" 9"io" 10"os/exec" 11"regexp" 12"strings" 13) 14 15// BSDDevicesInfoGetter returns info for Darwin devices 16type BSDDevicesInfoGetter struct { 17MountCmd string 18} 19 20// Getter is current instance of DevicesInfoGetter 21var Getter DevicesInfoGetter = BSDDevicesInfoGetter {MountCmd :"/sbin/mount" } 22 23// GetMounts returns all mounted filesystems from output of /sbin/mount 24func (t BSDDevicesInfoGetter )GetMounts () (devices Devices ,err error ) { 25out ,err := exec .Command (t .MountCmd ).Output () 26if err != nil { 27return nil ,err 28 } 29 30rdr := bytes .NewReader (out ) 31 32return readMountOutput (rdr ) 33} 34 35// GetDevicesInfo returns result of GetMounts with usage info about mounted devices (by calling Statfs syscall) 36func (t BSDDevicesInfoGetter )GetDevicesInfo () (devices Devices ,err error ) { 37mounts ,err := t .GetMounts () 38if err != nil { 39return nil ,err 40 } 41 42return processMounts (mounts ,false ) 43} 44 45func readMountOutput (rdr io.Reader ) (mounts Devices ,err error ) { 46scanner := bufio .NewScanner (rdr ) 47for scanner .Scan () { 48line := scanner .Text () 49 50re := regexp .MustCompile ("^(.*) on (/.*) type (.*) \\(([^)]+)\\)$" ) 51parts := re .FindAllStringSubmatch (line ,- 1 ) 52 53if len (parts )< 1 { 54return nil ,errors .New ("Cannot parse mount output" ) 55 } 56 57fstype := strings .TrimSpace (strings .Split (parts [0 ][3 ],"," )[0 ]) 58 59device := & Device { 60Name :parts [0 ][1 ], 61MountPoint :parts [0 ][2 ], 62Fstype :fstype , 63 } 64mounts = append (mounts ,device ) 65 } 66 67if err := scanner .Err ();err != nil { 68return nil ,err 69 } 70 71return mounts ,nil 72}