Browse code

Merge pull request #8937 from vbatts/vbatts-mount_optional_fields

pkg/mount: include optional field

Michael Crosby authored on 2014/11/18 11:25:00
Showing 3 changed files
... ...
@@ -1,7 +1,7 @@
1 1
 package mount
2 2
 
3 3
 type MountInfo struct {
4
-	Id, Parent, Major, Minor int
5
-	Root, Mountpoint, Opts   string
6
-	Fstype, Source, VfsOpts  string
4
+	Id, Parent, Major, Minor         int
5
+	Root, Mountpoint, Opts, Optional string
6
+	Fstype, Source, VfsOpts          string
7 7
 }
... ...
@@ -25,7 +25,7 @@ const (
25 25
 	   (9) filesystem type:  name of filesystem of the form "type[.subtype]"
26 26
 	   (10) mount source:  filesystem specific information or "none"
27 27
 	   (11) super options:  per super block options*/
28
-	mountinfoFormat = "%d %d %d:%d %s %s %s "
28
+	mountinfoFormat = "%d %d %d:%d %s %s %s %s"
29 29
 )
30 30
 
31 31
 // Parse /proc/self/mountinfo because comparing Dev and ino does not work from bind mounts
... ...
@@ -51,13 +51,14 @@ func parseInfoFile(r io.Reader) ([]*MountInfo, error) {
51 51
 		}
52 52
 
53 53
 		var (
54
-			p    = &MountInfo{}
55
-			text = s.Text()
54
+			p              = &MountInfo{}
55
+			text           = s.Text()
56
+			optionalFields string
56 57
 		)
57 58
 
58 59
 		if _, err := fmt.Sscanf(text, mountinfoFormat,
59 60
 			&p.Id, &p.Parent, &p.Major, &p.Minor,
60
-			&p.Root, &p.Mountpoint, &p.Opts); err != nil {
61
+			&p.Root, &p.Mountpoint, &p.Opts, &optionalFields); err != nil {
61 62
 			return nil, fmt.Errorf("Scanning '%s' failed: %s", text, err)
62 63
 		}
63 64
 		// Safe as mountinfo encodes mountpoints with spaces as \040.
... ...
@@ -67,6 +68,10 @@ func parseInfoFile(r io.Reader) ([]*MountInfo, error) {
67 67
 			return nil, fmt.Errorf("Error found less than 3 fields post '-' in %q", text)
68 68
 		}
69 69
 
70
+		if optionalFields != "-" {
71
+			p.Optional = optionalFields
72
+		}
73
+
70 74
 		p.Fstype = postSeparatorFields[0]
71 75
 		p.Source = postSeparatorFields[1]
72 76
 		p.VfsOpts = strings.Join(postSeparatorFields[2:], " ")
... ...
@@ -446,3 +446,32 @@ func TestParseGentooMountinfo(t *testing.T) {
446 446
 		t.Fatal(err)
447 447
 	}
448 448
 }
449
+
450
+func TestParseFedoraMountinfoFields(t *testing.T) {
451
+	r := bytes.NewBuffer([]byte(fedoraMountinfo))
452
+	infos, err := parseInfoFile(r)
453
+	if err != nil {
454
+		t.Fatal(err)
455
+	}
456
+	expectedLength := 58
457
+	if len(infos) != expectedLength {
458
+		t.Fatalf("Expected %d entries, got %d", expectedLength, len(infos))
459
+	}
460
+	mi := MountInfo{
461
+		Id:         15,
462
+		Parent:     35,
463
+		Major:      0,
464
+		Minor:      3,
465
+		Root:       "/",
466
+		Mountpoint: "/proc",
467
+		Opts:       "rw,nosuid,nodev,noexec,relatime",
468
+		Optional:   "shared:5",
469
+		Fstype:     "proc",
470
+		Source:     "proc",
471
+		VfsOpts:    "rw",
472
+	}
473
+
474
+	if *infos[0] != mi {
475
+		t.Fatalf("expected %#v, got %#v", mi, infos[0])
476
+	}
477
+}