Skip to content

Commit c5c8f97

Browse files
committed
fix(fs): Expand SystemRoot in file paths
1 parent 161de80 commit c5c8f97

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

pkg/fs/dev.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package fs
2323

2424
import (
2525
"github.com/rabbitstack/fibratus/pkg/sys"
26+
"os"
2627
"strings"
2728
)
2829

@@ -36,14 +37,16 @@ type DevMapper interface {
3637
}
3738

3839
type mapper struct {
39-
cache map[string]string
40+
cache map[string]string
41+
sysroot string
4042
}
4143

4244
// NewDevMapper creates a new instance of the DOS device replacer.
4345
func NewDevMapper() DevMapper {
4446
m := &mapper{
4547
cache: make(map[string]string),
4648
}
49+
4750
// loop through logical drives and query the DOS device name
4851
for _, drive := range sys.GetLogicalDrives() {
4952
device, err := sys.QueryDosDevice(drive)
@@ -52,30 +55,49 @@ func NewDevMapper() DevMapper {
5255
}
5356
m.cache[device] = drive
5457
}
58+
59+
// resolve the SystemRoot environment variable
60+
m.sysroot = os.Getenv("SystemRoot")
61+
if m.sysroot == "" {
62+
m.sysroot = os.Getenv("SYSTEMROOT")
63+
}
64+
5565
return m
5666
}
5767

5868
func (m *mapper) Convert(filename string) string {
5969
if filename == "" || len(filename) < deviceOffset {
6070
return filename
6171
}
62-
i := strings.Index(filename[deviceOffset:], "\\")
63-
if i < 0 {
72+
73+
// find the backslash index
74+
n := strings.Index(filename[deviceOffset:], "\\")
75+
if n < 0 {
6476
if f, ok := m.cache[filename]; ok {
6577
return f
6678
}
6779
return filename
6880
}
69-
dev := filename[:i+deviceOffset]
70-
// convert Windows Sandbox path to native path
71-
if dev == vmsmbDevice {
72-
n := strings.Index(filename, "os")
73-
if n > 0 {
74-
return "C:" + filename[n+2:]
75-
}
76-
}
81+
82+
dev := filename[:n+deviceOffset]
7783
if drive, ok := m.cache[dev]; ok {
84+
// the mapping for the DOS device exists
7885
return strings.Replace(filename, dev, drive, 1)
7986
}
87+
88+
switch {
89+
case dev == vmsmbDevice:
90+
// convert Windows Sandbox path to native path
91+
if n := strings.Index(filename, "os"); n > 0 {
92+
return "C:" + filename[n+2:]
93+
}
94+
case strings.HasPrefix(filename, "\\SystemRoot"):
95+
// normalize paths starting with SystemRoot
96+
return strings.Replace(filename, "\\SystemRoot", m.sysroot, 1)
97+
case strings.HasPrefix(filename, "\\SYSTEMROOT"):
98+
// normalize paths starting with SYSTEMROOT
99+
return strings.Replace(filename, "\\SYSTEMROOT", m.sysroot, 1)
100+
}
101+
80102
return filename
81103
}

pkg/fs/dev_test.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,40 @@ var drives = []string{
5757
"Z"}
5858

5959
func TestConvertDosDevice(t *testing.T) {
60-
mapper := NewDevMapper()
60+
m := NewDevMapper()
6161
files := make([]string, 0, len(drives))
62+
6263
for _, drive := range drives {
6364
files = append(files, fmt.Sprintf("%s:\\Windows\\system32\\kernel32.dll", drive))
6465
}
66+
6567
var filename string
6668
for i := 0; i < len(drives); i++ {
67-
filename = mapper.Convert(fmt.Sprintf("\\Device\\HarddiskVolume%d\\Windows\\system32\\kernel32.dll", i))
69+
filename = m.Convert(fmt.Sprintf("\\Device\\HarddiskVolume%d\\Windows\\system32\\kernel32.dll", i))
6870
if !strings.HasPrefix(filename, "\\Device") {
6971
break
7072
}
7173
}
7274
assert.Contains(t, files, filename)
73-
}
7475

75-
func TestConvertVmsmbDevice(t *testing.T) {
76-
mapper := NewDevMapper()
77-
path := "\\Device\\vmsmb\\VSMB-{dcc079ae-60ba-4d07-847c-3493609c0870}\\os\\Windows\\System32\\ntdll.dll"
78-
assert.Equal(t, "C:\\Windows\\System32\\ntdll.dll", mapper.Convert(path))
76+
m.(*mapper).cache["\\Device\\HarddiskVolume1"] = "C:"
77+
m.(*mapper).sysroot = "C:\\Windows"
78+
79+
var tests = []struct {
80+
inputFilename string
81+
expectedFilename string
82+
}{
83+
{"\\Device\\HarddiskVolume1\\Windows\\system32\\kernel32.dll", "C:\\Windows\\system32\\kernel32.dll"},
84+
{"\\Device\\HarddiskVolume5\\Windows\\system32\\kernel32.dll", "\\Device\\HarddiskVolume5\\Windows\\system32\\kernel32.dll"},
85+
{"\\Device\\vmsmb\\VSMB-{dcc079ae-60ba-4d07-847c-3493609c0870}\\os\\Windows\\System32\\ntdll.dll", "C:\\Windows\\System32\\ntdll.dll"},
86+
{"\\SystemRoot\\system32\\drivers\\wd\\WdNisDrv.sys", "C:\\Windows\\system32\\drivers\\wd\\WdNisDrv.sys"},
87+
{"\\SYSTEMROOT\\system32\\drivers\\wd\\WdNisDrv.sys", "C:\\Windows\\system32\\drivers\\wd\\WdNisDrv.sys"},
88+
{"\\Device\\Mup", "\\Device\\Mup"},
89+
}
90+
91+
for _, tt := range tests {
92+
t.Run(tt.inputFilename, func(t *testing.T) {
93+
assert.Equal(t, tt.expectedFilename, m.Convert(tt.inputFilename))
94+
})
95+
}
7996
}

0 commit comments

Comments
 (0)