Skip to content

Commit

Permalink
Add GetUdevData
Browse files Browse the repository at this point in the history
  • Loading branch information
blp1526 committed Jan 28, 2019
1 parent bcf5952 commit 11d5250
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
21 changes: 21 additions & 0 deletions _samples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"

"github.com/blp1526/go-udevinfo/blk"
)

func main() {
mountpoint := "/"
fmt.Printf("## mountpoint\n%s\n\n", mountpoint)

devPath, _ := blk.GetDevPath("/")
fmt.Printf("## devPath\n%s\n\n", devPath)

majorMinor, _ := blk.GetMajorMinor(devPath)
fmt.Printf("## majorMinor\n%s\n\n", majorMinor)

udevData, _ := blk.GetUdevData(majorMinor)
fmt.Printf("## udevData\n%s\n", udevData)
}
21 changes: 15 additions & 6 deletions blk/blk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ import (

// Errors
var (
// ErrNotFound ...
ErrNotFound = errors.New("Not Found")
)

// Paths
var (
// MtabPath ...
MtabPath = filepath.Join("/", "etc", "mtab")
// SysBlockPath ...
MtabPath = filepath.Join("/", "etc", "mtab")
SysBlockPath = filepath.Join("/", "sys", "block")
UdevDataPath = filepath.Join("/", "run", "udev", "data")
)

func mtab() (string, error) {
Expand All @@ -30,8 +28,8 @@ func mtab() (string, error) {
return strings.TrimSpace(string(b)), nil
}

// GetPath ...
func GetPath(mountpoint string) (string, error) {
// GetDevPath ...
func GetDevPath(mountpoint string) (string, error) {
mtab, err := mtab()
if err != nil {
return "", err
Expand Down Expand Up @@ -82,5 +80,16 @@ func GetMajorMinor(devPath string) (string, error) {
return "", err
}

majorMinor := "b" + strings.TrimSpace(string(b))
return majorMinor, nil
}

// GetUdevData ...
func GetUdevData(majorMinor string) (string, error) {
b, err := ioutil.ReadFile(filepath.Join(UdevDataPath, majorMinor))
if err != nil {
return "", err
}

return strings.TrimSpace(string(b)), nil
}

0 comments on commit 11d5250

Please sign in to comment.