1package mountinfo
2
3import "strings"
4
5// FilterFunc is a type defining a callback function for GetMount(),
6// used to filter out mountinfo entries we're not interested in,
7// and/or stop further processing if we found what we wanted.
8//
9// It takes a pointer to the Info struct (fully populated with all available
10// fields on the GOOS platform), and returns two booleans:
11//
12// skip: true if the entry should be skipped;
13//
14// stop: true if parsing should be stopped after the entry.
15type FilterFunc func(*Info) (skip, stop bool)
16
17// PrefixFilter discards all entries whose mount points
18// do not start with a specific prefix.
19func PrefixFilter(prefix string) FilterFunc {
20	return func(m *Info) (bool, bool) {
21		skip := !strings.HasPrefix(m.Mountpoint, prefix)
22		return skip, false
23	}
24}
25
26// SingleEntryFilter looks for a specific entry.
27func SingleEntryFilter(mp string) FilterFunc {
28	return func(m *Info) (bool, bool) {
29		if m.Mountpoint == mp {
30			return false, true // don't skip, stop now
31		}
32		return true, false // skip, keep going
33	}
34}
35
36// ParentsFilter returns all entries whose mount points
37// can be parents of a path specified, discarding others.
38//
39// For example, given /var/lib/docker/something, entries
40// like /var/lib/docker, /var and / are returned.
41func ParentsFilter(path string) FilterFunc {
42	return func(m *Info) (bool, bool) {
43		skip := !strings.HasPrefix(path, m.Mountpoint)
44		return skip, false
45	}
46}
47
48// FSTypeFilter returns all entries that match provided fstype(s).
49func FSTypeFilter(fstype ...string) FilterFunc {
50	return func(m *Info) (bool, bool) {
51		for _, t := range fstype {
52			if m.FSType == t {
53				return false, false // don't skip, keep going
54			}
55		}
56		return true, false // skip, keep going
57	}
58}
59