Merge pull request #1861 from shirou/feat/fix_paritions_opts_split

[disk][linux]: fix parsing mount option when use 1/mounts on Partition
fix/aix_host_comma
shirou 4 weeks ago committed by GitHub
commit afdd6f1f91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -285,18 +285,31 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
return nil, err
}
ret := make([]PartitionStat, 0, len(lines))
var ret []PartitionStat
if useMounts { // use mounts file
ret = parseFieldsOnMounts(lines, all, fs)
return ret, nil
}
// use mountinfo
ret, err = parseFieldsOnMountinfo(ctx, lines, all, fs, filename)
if err != nil {
return nil, fmt.Errorf("error parsing mountinfo file %s: %w", filename, err)
}
return ret, nil
}
func parseFieldsOnMounts(lines []string, all bool, fs []string) []PartitionStat {
ret := make([]PartitionStat, 0, len(lines))
for _, line := range lines {
var d PartitionStat
if useMounts {
fields := strings.Fields(line)
d = PartitionStat{
d := PartitionStat{
Device: fields[0],
Mountpoint: unescapeFstab(fields[1]),
Fstype: fields[2],
Opts: strings.Fields(fields[3]),
Opts: strings.Split(fields[3], ","),
}
if !all {
@ -304,7 +317,16 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
continue
}
}
} else {
ret = append(ret, d)
}
return ret
}
func parseFieldsOnMountinfo(ctx context.Context, lines []string, all bool, fs []string, filename string) ([]PartitionStat, error) {
ret := make([]PartitionStat, 0, len(lines))
for _, line := range lines {
// a line of 1/mountinfo has the following structure:
// 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
// (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
@ -328,7 +350,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
fstype := fields[0]
device := fields[1]
d = PartitionStat{
d := PartitionStat{
Device: device,
Mountpoint: unescapeFstab(mountPoint),
Fstype: fstype,
@ -356,10 +378,8 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
d.Device = strings.Replace(d.Device, "root", filepath.Base(devpath), 1)
}
}
}
ret = append(ret, d)
}
return ret, nil
}

@ -0,0 +1,83 @@
// SPDX-License-Identifier: BSD-3-Clause
//go:build linux
package disk
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_parseFieldsOnMountinfo(t *testing.T) {
fs := []string{"sysfs", "tmpfs"}
lines := []string{
"111 80 0:22 / /sys rw,nosuid,nodev,noexec,noatime shared:15 - sysfs sysfs rw",
"114 80 0:61 / /run rw,nosuid,nodev shared:18 - tmpfs none rw,mode=755",
}
cases := map[string]struct {
all bool
expect []PartitionStat
}{
"all": {
all: true,
expect: []PartitionStat{
{Device: "sysfs", Mountpoint: "/sys", Fstype: "sysfs", Opts: []string{"rw", "nosuid", "nodev", "noexec", "noatime"}},
{Device: "none", Mountpoint: "/run", Fstype: "tmpfs", Opts: []string{"rw", "nosuid", "nodev"}},
},
},
"not all": {
all: false,
expect: []PartitionStat{
{Device: "sysfs", Mountpoint: "/sys", Fstype: "sysfs", Opts: []string{"rw", "nosuid", "nodev", "noexec", "noatime"}},
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
actual, err := parseFieldsOnMountinfo(context.Background(), lines, c.all, fs, "")
require.NoError(t, err)
assert.Equal(t, c.expect, actual)
})
}
}
func Test_parseFieldsOnMounts(t *testing.T) {
fs := []string{"sysfs", "tmpfs"}
lines := []string{
"sysfs /sys sysfs rw,nosuid,nodev,noexec,noatime 0 0",
"none /run tmpfs rw,nosuid,nodev,mode=755 0 0",
}
cases := map[string]struct {
all bool
expect []PartitionStat
}{
"all": {
all: true,
expect: []PartitionStat{
{Device: "sysfs", Mountpoint: "/sys", Fstype: "sysfs", Opts: []string{"rw", "nosuid", "nodev", "noexec", "noatime"}},
{Device: "none", Mountpoint: "/run", Fstype: "tmpfs", Opts: []string{"rw", "nosuid", "nodev", "mode=755"}},
},
},
"not all": {
all: false,
expect: []PartitionStat{
{Device: "sysfs", Mountpoint: "/sys", Fstype: "sysfs", Opts: []string{"rw", "nosuid", "nodev", "noexec", "noatime"}},
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
actual := parseFieldsOnMounts(lines, c.all, fs)
assert.Equal(t, c.expect, actual)
})
}
}
Loading…
Cancel
Save