@@ -25,6 +25,8 @@ import (
2525 "os"
2626 "strconv"
2727 "strings"
28+
29+ "github.com/pkg/errors"
2830)
2931
3032// Self retrieves a list of mounts for the current running process.
@@ -41,13 +43,15 @@ func Self() ([]Info, error) {
4143func parseInfoFile (r io.Reader ) ([]Info , error ) {
4244 s := bufio .NewScanner (r )
4345 out := []Info {}
44-
46+ var err error
4547 for s .Scan () {
46- if err : = s .Err (); err != nil {
48+ if err = s .Err (); err != nil {
4749 return nil , err
4850 }
4951
5052 /*
53+ See http://man7.org/linux/man-pages/man5/proc.5.html
54+
5155 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
5256 (1)(2)(3) (4) (5) (6) (7) (8) (9) (10) (11)
5357 (1) mount ID: unique identifier of the mount (may be reused after umount)
@@ -68,21 +72,27 @@ func parseInfoFile(r io.Reader) ([]Info, error) {
6872 numFields := len (fields )
6973 if numFields < 10 {
7074 // should be at least 10 fields
71- return nil , fmt .Errorf ("parsing '%s' failed: not enough fields (%d)" , text , numFields )
75+ return nil , errors .Errorf ("parsing '%s' failed: not enough fields (%d)" , text , numFields )
7276 }
7377 p := Info {}
7478 // ignore any numbers parsing errors, as there should not be any
7579 p .ID , _ = strconv .Atoi (fields [0 ])
7680 p .Parent , _ = strconv .Atoi (fields [1 ])
7781 mm := strings .Split (fields [2 ], ":" )
7882 if len (mm ) != 2 {
79- return nil , fmt .Errorf ("parsing '%s' failed: unexpected minor:major pair %s" , text , mm )
83+ return nil , errors .Errorf ("parsing '%s' failed: unexpected minor:major pair %s" , text , mm )
8084 }
8185 p .Major , _ = strconv .Atoi (mm [0 ])
8286 p .Minor , _ = strconv .Atoi (mm [1 ])
8387
84- p .Root = fields [3 ]
85- p .Mountpoint = fields [4 ]
88+ p .Root , err = strconv .Unquote (`"` + fields [3 ] + `"` )
89+ if err != nil {
90+ return nil , errors .Wrapf (err , "parsing '%s' failed: unable to unquote root field" , fields [3 ])
91+ }
92+ p .Mountpoint , err = strconv .Unquote (`"` + fields [4 ] + `"` )
93+ if err != nil {
94+ return nil , errors .Wrapf (err , "parsing '%s' failed: unable to unquote mount point field" , fields [4 ])
95+ }
8696 p .Options = fields [5 ]
8797
8898 // one or more optional fields, when a separator (-)
@@ -101,11 +111,11 @@ func parseInfoFile(r io.Reader) ([]Info, error) {
101111 }
102112 }
103113 if i == numFields {
104- return nil , fmt .Errorf ("parsing '%s' failed: missing separator ('-')" , text )
114+ return nil , errors .Errorf ("parsing '%s' failed: missing separator ('-')" , text )
105115 }
106116 // There should be 3 fields after the separator...
107117 if i + 4 > numFields {
108- return nil , fmt .Errorf ("parsing '%s' failed: not enough fields after a separator" , text )
118+ return nil , errors .Errorf ("parsing '%s' failed: not enough fields after a separator" , text )
109119 }
110120 // ... but in Linux <= 3.9 mounting a cifs with spaces in a share name
111121 // (like "//serv/My Documents") _may_ end up having a space in the last field
0 commit comments