From fe2caadf7abfddf534f4c999cef7aa3d255b451c Mon Sep 17 00:00:00 2001 From: Lorenzo Fontana Date: Tue, 26 Apr 2022 01:24:18 +0200 Subject: [PATCH] docs: examples in readme Signed-off-by: Lorenzo Fontana --- README.md | 27 +++++++++++++++++++++++++++ go.sum | 0 mountinfo.go | 6 +++--- mountinfo_test.go | 17 ++++++++++++++++- 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 go.sum diff --git a/README.md b/README.md index 55e4884..554fa91 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,30 @@ The purpose of this library is to read the `mountinfo` file as a whole or as single lines in order to get information about the mount points of a specific process as described in `man 5 proc`. + +## API + +- `ParseMountInfo(buffer io.Reader) ([]Mountinfo, error)`: ParseMountInfo parses the mountinfo content from an io.Reader, e.g a file +- `ParseMountInfoString(tx string) *Mountinfo`: ParseMountInfoString transforms a mountinfo string in a struct of type Mountinfo +- `GetMountInfo(mountinfoPath string) ([]Mountinfo, error)`: GetMountInfo reads the mountinfo file and returns a slice of structs of type Mountinfo + +## Examples + +```go +package main + +import ( + "fmt" + "log" + + "github.com/fntlnz/mountinfo" +) + +func main() { + minfo, err := mountinfo.GetMountInfo("/proc/self/mountinfo") + if err != nil { + log.Fatal("error getting mountinfo: %v", err) + } + fmt.Printf("Mountpoint: %s", minfo[0].MountPoint) +} +``` diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/mountinfo.go b/mountinfo.go index 18dfcdf..1ae1f66 100644 --- a/mountinfo.go +++ b/mountinfo.go @@ -28,9 +28,9 @@ func getMountPart(pieces []string, index int) string { return "" } -// GetMountInfo opens a mountinfo file, returns -func GetMountInfo(fd string) ([]Mountinfo, error) { - file, err := os.Open(fd) +// GetMountInfo opens a mountinfo file, returns a slice of Mountinfo structs +func GetMountInfo(mountinfoPath string) ([]Mountinfo, error) { + file, err := os.Open(mountinfoPath) if err != nil { return nil, err } diff --git a/mountinfo_test.go b/mountinfo_test.go index f3bcd86..bcd9508 100644 --- a/mountinfo_test.go +++ b/mountinfo_test.go @@ -76,6 +76,21 @@ func ParseMountDataset() []ParseMountData { SuperOptions: "rw,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd", }, }, + { + rawline: "824 723 0:52 / /var/lib/containers/storage/overlay/62ef728cd5abf2cf711bed7912828d1283ca2b5cb2200e65941d86e22cd6c205/merged rw,nodev,relatime - overlay overlay rw,context=\"system_u:object_r:container_file_t:s0:c120,c334\",lowerdir=/var/lib/containers/storage/overlay/l/NZWCWDRTRJEXYQAOIXQLIOWOOJ:/var/lib/containers/storage/overlay/l/EG2LSO3NQSV6ZKSAG2SF7BUO4L:/var/lib/containers/storage/overlay/l/EMD4ZHWPE6MJ4KZBHPTFDVJV7B:/var/lib/containers/storage/overlay/l/MYIZJQPW4OD3J4TEJRXTMV5NVJ:/var/lib/containers/storage/overlay/l/6MMB3RGZNRYW6YZQI3FUUN2GTM:/var/lib/containers/storage/overlay/l/O5Z3Y63L3AVQLPB7WND73PQG6W:/var/lib/containers/storage/overlay/l/UACPLH6JBVALN7TSY7AYPRQT4F:/var/lib/containers/storage/overlay/l/CT6LZD32636MU36BUPS4KQ7GUK:/var/lib/containers/storage/overlay/l/S7ZQGTZDQOU5AH2E3R74EPBKW7:/var/lib/containers/storage/overlay/l/66SOAQGJFJDCHQ6G3WVQENMBEP:/var/lib/containers/storage/overlay/l/RQQO2I2OI47TML4V3DPSMMQ5KQ:/var/lib/containers/storage/overlay/l/BH2MWLJXSNYW4NRNQZHVJVF454:/var/lib/containers/storage/overlay/l/L7WEQU6QPK2GQJZUFPBHVEOBFM,upperdir=/var/lib/containers/storage/overlay/62ef728cd5abf2cf711bed7912828d1283ca2b5cb2200e65941d86e22cd6c205/diff,workdir=/var/lib/containers/storage/overlay/62ef728cd5abf2cf711bed7912828d1283ca2b5cb2200e65941d86e22cd6c205/work,metacopy=on", + expectedset: Mountinfo{ + MountID: "824", + ParentID: "723", + MajorMinor: "0:52", + Root: "/", + MountPoint: "/var/lib/containers/storage/overlay/62ef728cd5abf2cf711bed7912828d1283ca2b5cb2200e65941d86e22cd6c205/merged", + MountOptions: "rw,nodev,relatime", + OptionalFields: "", + FilesystemType: "overlay", + MountSource: "overlay", + SuperOptions: "rw,context=\"system_u:object_r:container_file_t:s0:c120,c334\",lowerdir=/var/lib/containers/storage/overlay/l/NZWCWDRTRJEXYQAOIXQLIOWOOJ:/var/lib/containers/storage/overlay/l/EG2LSO3NQSV6ZKSAG2SF7BUO4L:/var/lib/containers/storage/overlay/l/EMD4ZHWPE6MJ4KZBHPTFDVJV7B:/var/lib/containers/storage/overlay/l/MYIZJQPW4OD3J4TEJRXTMV5NVJ:/var/lib/containers/storage/overlay/l/6MMB3RGZNRYW6YZQI3FUUN2GTM:/var/lib/containers/storage/overlay/l/O5Z3Y63L3AVQLPB7WND73PQG6W:/var/lib/containers/storage/overlay/l/UACPLH6JBVALN7TSY7AYPRQT4F:/var/lib/containers/storage/overlay/l/CT6LZD32636MU36BUPS4KQ7GUK:/var/lib/containers/storage/overlay/l/S7ZQGTZDQOU5AH2E3R74EPBKW7:/var/lib/containers/storage/overlay/l/66SOAQGJFJDCHQ6G3WVQENMBEP:/var/lib/containers/storage/overlay/l/RQQO2I2OI47TML4V3DPSMMQ5KQ:/var/lib/containers/storage/overlay/l/BH2MWLJXSNYW4NRNQZHVJVF454:/var/lib/containers/storage/overlay/l/L7WEQU6QPK2GQJZUFPBHVEOBFM,upperdir=/var/lib/containers/storage/overlay/62ef728cd5abf2cf711bed7912828d1283ca2b5cb2200e65941d86e22cd6c205/diff,workdir=/var/lib/containers/storage/overlay/62ef728cd5abf2cf711bed7912828d1283ca2b5cb2200e65941d86e22cd6c205/work,metacopy=on", + }, + }, } } @@ -84,7 +99,7 @@ func TestParseMountString(t *testing.T) { info := ParseMountInfoString(e.rawline) if reflect.DeepEqual(e.expectedset, *info) == false { - t.Error("expected set is different than the resulting set") + t.Errorf("Expected %v got %v", e.expectedset, *info) } } }