Skip to content

Commit 5f3d680

Browse files
authored
fix(sysfs): trim newline while parsing sysfs files (#666)
sysfs files like dm/uuid etc are sometimes suffixed by a newline character which need to be trimmed off for correct parsing, else it will result in incorrect device type being detected Signed-off-by: Akhil Mohan <akhil.mohan@datacore.com>
1 parent 7094461 commit 5f3d680

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

changelogs/unreleased/666-akhilerm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix sysfs parsing by trimming newline suffix when reading from sysfs files

pkg/sysfs/syspath.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ package sysfs
1818

1919
import (
2020
"fmt"
21-
"github.com/openebs/node-disk-manager/blockdevice"
2221
"io/ioutil"
2322
"os"
2423
"path/filepath"
2524
"strings"
25+
26+
"github.com/openebs/node-disk-manager/blockdevice"
2627
)
2728

2829
const (

pkg/sysfs/syspath_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,20 @@ func TestSysFsDeviceGetDeviceType(t *testing.T) {
742742
want: "raid0",
743743
wantErr: false,
744744
},
745+
"device is a dm device with empty uuid": {
746+
sysfsDevice: &Device{
747+
deviceName: "dm-16",
748+
path: "/dev/dm-16",
749+
sysPath: filepath.Join(tmpDir,
750+
"sys/devices/virtual/block/dm-16") + "/",
751+
},
752+
devType: blockdevice.BlockDeviceTypeDisk,
753+
subDirectoryName: "dm",
754+
subFileName: "uuid",
755+
subFileContent: "\n",
756+
want: blockdevice.BlockDeviceTypeDMDevice,
757+
wantErr: false,
758+
},
745759
}
746760
for name, tt := range tests {
747761
t.Run(name, func(t *testing.T) {

pkg/sysfs/util.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func readSysFSFileAsInt64(sysFilePath string) (int64, error) {
3030
if err != nil {
3131
return 0, err
3232
}
33+
// Remove tailing newline (usual in sysfs) before parsing
3334
return strconv.ParseInt(strings.TrimSuffix(string(b), "\n"), 10, 64)
3435
}
3536

@@ -38,7 +39,8 @@ func readSysFSFileAsString(sysFilePath string) (string, error) {
3839
if err != nil {
3940
return "", err
4041
}
41-
return string(b), nil
42+
// Remove tailing newline (usual in sysfs)
43+
return strings.TrimSuffix(string(b), "\n"), nil
4244
}
4345

4446
// addDevPrefix adds the /dev prefix to all the device names

pkg/sysfs/util_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ limitations under the License.
1717
package sysfs
1818

1919
import (
20-
"github.com/stretchr/testify/assert"
2120
"os"
2221
"testing"
22+
23+
"github.com/stretchr/testify/assert"
2324
)
2425

2526
func TestAddDevPrefix(t *testing.T) {
@@ -55,6 +56,13 @@ func TestReadSysFSFileAsString(t *testing.T) {
5556
want: "LVM-OSlVs5gIXuqSKVPukc2aGPh0AeJw31TJqYIRuRHoodYg9Jwkmyvvk0QNYK4YulHt",
5657
wantErr: false,
5758
},
59+
"valid sysfs path with tailing new line": {
60+
path: "/tmp/dm-0/dm/",
61+
fileName: "uuid",
62+
fileContent: "LVM-OSlVs5gIXuqSKVPukc2aGPh0AeJw31TJqYIRuRHoodYg9Jwkmyvvk0QNYK4YulHt\n",
63+
want: "LVM-OSlVs5gIXuqSKVPukc2aGPh0AeJw31TJqYIRuRHoodYg9Jwkmyvvk0QNYK4YulHt",
64+
wantErr: false,
65+
},
5866
}
5967
for name, tt := range tests {
6068
t.Run(name, func(t *testing.T) {

0 commit comments

Comments
 (0)