@@ -23,6 +23,7 @@ package fs
2323
2424import (
2525 "github.com/rabbitstack/fibratus/pkg/sys"
26+ "os"
2627 "strings"
2728)
2829
@@ -36,14 +37,16 @@ type DevMapper interface {
3637}
3738
3839type 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.
4345func 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
5868func (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}
0 commit comments