| 1 | package mountinfo |
| 2 | |
| 3 | import "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. |
| 15 | type FilterFunc func(*Info) (skip, stop bool) |
| 16 | |
| 17 | // PrefixFilter discards all entries whose mount points do not start with, or |
| 18 | // are equal to the path specified in prefix. The prefix path must be absolute, |
| 19 | // have all symlinks resolved, and cleaned (i.e. no extra slashes or dots). |
| 20 | // |
| 21 | // PrefixFilter treats prefix as a path, not a partial prefix, which means that |
| 22 | // given "/foo", "/foo/bar" and "/foobar" entries, PrefixFilter("/foo") returns |
| 23 | // "/foo" and "/foo/bar", and discards "/foobar". |
| 24 | func PrefixFilter(prefix string) FilterFunc { |
| 25 | return func(m *Info) (bool, bool) { |
| 26 | skip := !strings.HasPrefix(m.Mountpoint+"/", prefix+"/") |
| 27 | return skip, false |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // SingleEntryFilter looks for a specific entry. |
| 32 | func SingleEntryFilter(mp string) FilterFunc { |
| 33 | return func(m *Info) (bool, bool) { |
| 34 | if m.Mountpoint == mp { |
| 35 | return false, true // don't skip, stop now |
| 36 | } |
| 37 | return true, false // skip, keep going |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // ParentsFilter returns all entries whose mount points |
| 42 | // can be parents of a path specified, discarding others. |
| 43 | // |
| 44 | // For example, given /var/lib/docker/something, entries |
| 45 | // like /var/lib/docker, /var and / are returned. |
| 46 | func ParentsFilter(path string) FilterFunc { |
| 47 | return func(m *Info) (bool, bool) { |
| 48 | skip := !strings.HasPrefix(path, m.Mountpoint) |
| 49 | return skip, false |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // FSTypeFilter returns all entries that match provided fstype(s). |
| 54 | func FSTypeFilter(fstype ...string) FilterFunc { |
| 55 | return func(m *Info) (bool, bool) { |
| 56 | for _, t := range fstype { |
| 57 | if m.FSType == t { |
| 58 | return false, false // don't skip, keep going |
| 59 | } |
| 60 | } |
| 61 | return true, false // skip, keep going |
| 62 | } |
| 63 | } |