Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/snclient/check_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,18 @@ func (l *CheckFiles) normalizePath(path string) string {
// Special handling for Windows drive letters,
// Files directly under the drive do not have separators, e.g: C:pagefile.sys
// This confuses the depth calculation
if runtime.GOOS == "windows" {
// Convert forward slashes to backward slashes
fromSlash := filepath.FromSlash(path)
path = fromSlash
}

// Remove groups of seperators next to each other , and replace them with a single separator
sep := string(os.PathSeparator)
for strings.Contains(path, sep+sep) {
path = strings.ReplaceAll(path, sep+sep, sep)
}

if runtime.GOOS == "windows" {
// C: -> C:\
if len(path) == 2 && unicode.IsUpper(rune(path[0])) && path[1] == ':' {
Expand Down
28 changes: 28 additions & 0 deletions pkg/snclient/check_files_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,31 @@ func TestCheckDriveLetterPaths(t *testing.T) {

StopTestAgent(t, snc)
}

func TestCheckPathSpecifications(t *testing.T) {
snc := StartTestAgent(t, "")
var res *CheckResult

// =============== C:\Windows\Fonts\arial.ttf
// Intended separator is backward slashes like this
res = snc.RunCheck("check_files", []string{"path=C:\\Windows\\fonts", "max-depth=1", "filter= type == 'file' and name == 'arial.ttf' "})
assert.Containsf(t, string(res.BuildPluginOutput()), "OK - All 1 files are ok", "output matches")

// Forward slashes should be converted to backward slashes
res = snc.RunCheck("check_files", []string{"path=C:/Windows/fonts", "max-depth=1", "filter= type == 'file' and name == 'arial.ttf' "})
assert.Containsf(t, string(res.BuildPluginOutput()), "OK - All 1 files are ok", "output matches")

// Multiple backward slashes that do not actually go into subfolders and add depth should be ignored
res = snc.RunCheck("check_files", []string{"path=C:\\\\\\Windows\\\\fonts", "max-depth=1", "filter= type == 'file' and name == 'arial.ttf' "})
assert.Containsf(t, string(res.BuildPluginOutput()), "OK - All 1 files are ok", "output matches")

// Multiple backward slashes that do not actually go into subfolders and add depth should be ignored
res = snc.RunCheck("check_files", []string{"path=C:\\\\Windows\\\\fonts", "max-depth=1", "filter= type == 'file' and name == 'arial.ttf' "})
assert.Containsf(t, string(res.BuildPluginOutput()), "OK - All 1 files are ok", "output matches")

// Multiple forward slashes that do not actually go into subfolders and add depth should be ignored
res = snc.RunCheck("check_files", []string{"path=C://Windows////fonts", "max-depth=1", "filter= type == 'file' and name == 'arial.ttf' "})
assert.Containsf(t, string(res.BuildPluginOutput()), "OK - All 1 files are ok", "output matches")

StopTestAgent(t, snc)
}
Loading