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
|
// Package fstab parses and serializes linux filesystem mounts information
package fstab
import (
"bufio"
"fmt"
"io"
"os"
)
type Mounts []*Mount
// String serializes a list of mounts to the fstab format
func (mounts Mounts) String() (output string) {
for i, mount := range mounts {
if i > 0 {
output += "\n"
}
output += mount.String()
}
return
}
// PaddedString serializes a list of mounts to the fstab format with padding.
func (mounts Mounts) PaddedString(paddings ...int) (output string) {
for i, mount := range mounts {
if i > 0 {
output += "\n"
}
output += mount.PaddedString(paddings...)
}
return
}
// ParseSystem parses your system fstab ("/etc/fstab")
func ParseSystem() (mounts Mounts, err error) {
return ParseFile("/etc/fstab")
}
// ParseProc parses procfs information
func ParseProc() (mounts Mounts, err error) {
return ParseFile("/proc/mounts")
}
// ParseFile parses the given file
func ParseFile(filename string) (mounts Mounts, err error) {
file, err := os.Open(filename)
if nil != err {
return nil, err
} else {
defer file.Close()
return Parse(file)
}
}
func Parse(source io.Reader) (mounts Mounts, err error) {
mounts = make([]*Mount, 0, 10)
scanner := bufio.NewScanner(source)
lineNo := 0
for scanner.Scan() {
lineNo++
mount, err := ParseLine(scanner.Text())
if nil != err {
return nil, fmt.Errorf("Syntax error at line %d: %s", lineNo, err)
}
if nil != mount {
mounts = append(mounts, mount)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return mounts, nil
}
|