fork/gdu

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

Daniel Mildefix: build on other architecturesede21d2

master
1.6 KiB72 linesraw
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 {
17	MountCmd 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) {
25	out, err := exec.Command(t.MountCmd).Output()
26	if err != nil {
27		return nil, err
28	}
29
30	rdr := bytes.NewReader(out)
31
32	return 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) {
37	mounts, err := t.GetMounts()
38	if err != nil {
39		return nil, err
40	}
41
42	return processMounts(mounts, false)
43}
44
45func readMountOutput(rdr io.Reader) (mounts Devices, err error) {
46	scanner := bufio.NewScanner(rdr)
47	for scanner.Scan() {
48		line := scanner.Text()
49
50		re := regexp.MustCompile("^(.*) on (/.*) type (.*) \\(([^)]+)\\)$")
51		parts := re.FindAllStringSubmatch(line, -1)
52
53		if len(parts) < 1 {
54			return nil, errors.New("Cannot parse mount output")
55		}
56
57		fstype := strings.TrimSpace(strings.Split(parts[0][3], ",")[0])
58
59		device := &Device{
60			Name:       parts[0][1],
61			MountPoint: parts[0][2],
62			Fstype:     fstype,
63		}
64		mounts = append(mounts, device)
65	}
66
67	if err := scanner.Err(); err != nil {
68		return nil, err
69	}
70
71	return mounts, nil
72}