1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
package system
import (
"context"
"fmt"
"strings"
"time"
"github.com/goss-org/goss/util"
"github.com/moby/sys/mountinfo"
"github.com/samber/lo"
)
type Mount interface {
MountPoint() string
Exists() (bool, error)
Opts() ([]string, error)
VfsOpts() ([]string, error)
Source() (string, error)
Filesystem() (string, error)
Usage() (int, error)
}
type DefMount struct {
mountPoint string
loaded bool
exists bool
mountInfo *mountinfo.Info
usage int
Timeout int
err error
}
func NewDefMount(_ context.Context, mountPoint string, system *System, config util.Config) Mount {
return &DefMount{
mountPoint: mountPoint,
Timeout: config.TimeOutMilliSeconds(),
}
}
func (m *DefMount) setup() error {
if m.loaded {
return m.err
}
m.loaded = true
mountInfo, err := getMount(m.mountPoint, m.Timeout)
if err != nil {
m.exists = false
m.err = err
return m.err
}
m.mountInfo = mountInfo
m.exists = true
usage, err := getUsage(m.mountPoint)
if err != nil {
m.err = err
return m.err
}
m.usage = usage
return nil
}
func (m *DefMount) ID() string {
return m.mountPoint
}
func (m *DefMount) MountPoint() string {
return m.mountPoint
}
func (m *DefMount) Exists() (bool, error) {
if err := m.setup(); err != nil {
return false, err
}
return m.exists, nil
}
func (m *DefMount) Opts() ([]string, error) {
if err := m.setup(); err != nil {
return nil, err
}
allOpts := splitMountInfo(m.mountInfo.Options)
return lo.Uniq(allOpts), nil
}
func (m *DefMount) VfsOpts() ([]string, error) {
if err := m.setup(); err != nil {
return nil, err
}
opts := splitMountInfo(m.mountInfo.VFSOptions)
return opts, nil
}
func (m *DefMount) Source() (string, error) {
if err := m.setup(); err != nil {
return "", err
}
return m.mountInfo.Source, nil
}
func (m *DefMount) Filesystem() (string, error) {
if err := m.setup(); err != nil {
return "", err
}
return m.mountInfo.FSType, nil
}
func (m *DefMount) Usage() (int, error) {
if err := m.setup(); err != nil {
return -1, err
}
return m.usage, nil
}
func getMount(mountpoint string, timeout int) (*mountinfo.Info, error) {
c1 := make(chan *mountinfo.Info, 1)
e1 := make(chan error, 1)
timeoutD := time.Duration(timeout) * time.Millisecond
go func() {
entries, err := mountinfo.GetMounts(mountinfo.SingleEntryFilter(mountpoint))
if err != nil {
e1 <- err
return
}
if len(entries) == 0 {
e1 <- fmt.Errorf("Mountpoint not found")
return
}
c1 <- entries[0]
}()
select {
case result := <-c1:
return result, nil
case err := <-e1:
return nil, err
case <-time.After(timeoutD):
return nil, fmt.Errorf("getMount operation timed out after %s milliseconds", timeoutD)
}
}
func splitMountInfo(s string) []string {
quoted := false
return strings.FieldsFunc(s, func(r rune) bool {
if r == '"' {
quoted = !quoted
}
return !quoted && r == ','
})
}
|