Skip to content

Commit

Permalink
docs: examples in readme
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Fontana <fontanalorenz@gmail.com>
  • Loading branch information
fntlnz committed Apr 25, 2022
1 parent f6873ff commit fe2caad
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
```
Empty file added go.sum
Empty file.
6 changes: 3 additions & 3 deletions mountinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 16 additions & 1 deletion mountinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
}
}

Expand All @@ -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)
}
}
}

0 comments on commit fe2caad

Please # to comment.