Skip to content

Commit cd2a69e

Browse files
ImSingeerobbat2
authored andcommitted
optimize raid type check
Signed-off-by: Singee <[email protected]>
1 parent 8a07435 commit cd2a69e

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

mdstat.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,14 @@ func parseMDStat(mdStatData []byte) ([]MDStat, error) {
9999
mdName := deviceFields[0] // mdx
100100
state := deviceFields[2] // active or inactive
101101

102-
mdType := "unknown"
103-
if len(deviceFields) > 3 && isRaidType(deviceFields[3]) {
104-
mdType = deviceFields[3] // raid1, raid5, etc.
105-
} else if len(deviceFields) > 4 && isRaidType(deviceFields[4]) {
106-
mdType = deviceFields[4]
102+
mdType := "unknown" // raid1, raid5, etc.
103+
if len(deviceFields) > 3 { // mdType may be in the 3rd or 4th field
104+
if isRaidType(deviceFields[3]) {
105+
mdType = deviceFields[3]
106+
} else if len(deviceFields) > 4 && isRaidType(deviceFields[4]) {
107+
// if the 3rd field is (...), the 4th field is the mdType
108+
mdType = deviceFields[4]
109+
}
107110
}
108111

109112
if len(lines) <= i+3 {
@@ -179,8 +182,11 @@ func parseMDStat(mdStatData []byte) ([]MDStat, error) {
179182
return mdStats, nil
180183
}
181184

185+
// check if a string's format is like the mdType
186+
// Rule 1: mdType should not be like (...)
187+
// Rule 2: mdType should not be like sda[0]
182188
func isRaidType(mdType string) bool {
183-
return strings.HasPrefix(mdType, "raid") || mdType == "linear"
189+
return !strings.ContainsAny(mdType, "([")
184190
}
185191

186192
func evalStatusLine(deviceLine, statusLine string) (active, total, down, size int64, err error) {

0 commit comments

Comments
 (0)