pax_global_header00006660000000000000000000000064141605664360014525gustar00rootroot0000000000000052 comment=01647e5beb71b3c9adc9d4fa1a9274cd495ac0b7 mtab-1.0.0/000077500000000000000000000000001416056643600124465ustar00rootroot00000000000000mtab-1.0.0/LICENSE.txt000066400000000000000000000020611416056643600142700ustar00rootroot00000000000000MIT License Copyright (c) 2014 Artyom Pervukhin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. mtab-1.0.0/go.mod000066400000000000000000000000471416056643600135550ustar00rootroot00000000000000module github.com/artyom/mtab go 1.16 mtab-1.0.0/mounts.testfile000066400000000000000000000002321416056643600155310ustar00rootroot00000000000000rootfs / rootfs rw 0 0 sysfs /sys sysfs rw,nosuid,nodev,noexec 0 0 proc /proc proc rw,nosuid,nodev,noexec 0 0 /dev/sdb /path\040with\040spaces xfs rw 0 0 mtab-1.0.0/mtab.go000066400000000000000000000027611416056643600137260ustar00rootroot00000000000000// Package mtab parses /proc/self/mounts entries on a Linux system. package mtab // import "github.com/artyom/mtab" import ( "fmt" "io" "os" "strings" ) // Entry corresponds to mntent struct. See getmntent(3) manpage for further // details. type Entry struct { Fsname string // name of mounted file system Dir string // file system path prefix Type string // mount type Opts string // mount options Freq int // dump frequency in days Passno int // pass number on parallel fsck } const mtabFmt = `%s %s %s %s %d %d` var escaped = strings.NewReplacer( `\040`, " ", `\011`, "\t", `\012`, "\n", `\134`, `\`, ) // unescapeFields unescapes characters on string fields. See getmntent(3) // manpage for details. func unescapeFields(m *Entry) { for _, f := range [...]*string{&m.Fsname, &m.Dir, &m.Type, &m.Opts} { *f = escaped.Replace(*f) } } // Entries reads mtab entries from a given file. Usually you should use // `/etc/fstab` or `/proc/self/mounts` as a file name. func Entries(fname string) ([]Entry, error) { f, err := os.Open(fname) if err != nil { return nil, err } defer f.Close() var out []Entry READLOOP: for { e := Entry{} n, err := fmt.Fscanf(f, mtabFmt, &e.Fsname, &e.Dir, &e.Type, &e.Opts, &e.Freq, &e.Passno) switch err { case io.EOF: break READLOOP case nil: default: return out, err } if n != 6 { return out, fmt.Errorf("wrong line format (invalid number of fields)") } unescapeFields(&e) out = append(out, e) } return out, nil } mtab-1.0.0/mtab_test.go000066400000000000000000000012571416056643600147640ustar00rootroot00000000000000package mtab import ( "reflect" "testing" ) func TestEntries(t *testing.T) { entries, err := Entries("mounts.testfile") if err != nil { t.Fatal(err) } if !reflect.DeepEqual(entries, reference) { t.Log("entries read:") for _, e := range entries { t.Logf("%#v", e) } t.Log("entries expected:") for _, e := range reference { t.Logf("%#v", e) } t.Fatal("read entries do not match reference") } } var reference = []Entry{ Entry{"rootfs", "/", "rootfs", "rw", 0, 0}, Entry{"sysfs", "/sys", "sysfs", "rw,nosuid,nodev,noexec", 0, 0}, Entry{"proc", "/proc", "proc", "rw,nosuid,nodev,noexec", 0, 0}, Entry{"/dev/sdb", "/path with spaces", "xfs", "rw", 0, 0}, }