From 6474851c5ca3e001e15bc341aa2db8cd19343c87 Mon Sep 17 00:00:00 2001 From: Ahmet Ozturk Date: Fri, 17 Jul 2026 15:21:59 +0200 Subject: [PATCH 1/2] check_files: remove all directories when a pattern is used previously the files would be selectively added matching the pattern with their filenames, and directories which did not contain any of those files were removed from entryList now remove all directories completely when pattern is used --- pkg/snclient/check_files.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/snclient/check_files.go b/pkg/snclient/check_files.go index 1e354b11..2b576a49 100644 --- a/pkg/snclient/check_files.go +++ b/pkg/snclient/check_files.go @@ -168,7 +168,9 @@ func (l *CheckFiles) Check(_ context.Context, _ *Agent, check *CheckData, _ []Ar // Cleanup the listData if a filter is used if l.pattern != "*" { - l.removeDirectoriesWithoutFilesUnder(check) + // l.removeDirectoriesWithoutFilesUnder(check) + + l.removeDirectories(check) } if l.calculateSubdirectorySizes { @@ -558,6 +560,18 @@ func checkSlowFileOperations(check *CheckData, entry map[string]string, path str return nil } +func (l *CheckFiles) removeDirectories(check *CheckData) { + newListData := make([]map[string]string, 0) + + for _, data := range check.listData { + if data["type"] != "dir" { + newListData = append(newListData, data) + } + } + + check.listData = newListData +} + // The WalkDir normally adds every directory and files under the search path. // If a pattern is specified, this prevents files that dont match the pattern to be skipped. // This can lead to some directories being in the listData, while not having any matched files under them. From 6e97b01f0586b8e3895ef131d566f69905150189 Mon Sep 17 00:00:00 2001 From: Ahmet Oeztuerk Date: Fri, 17 Jul 2026 16:08:28 +0200 Subject: [PATCH 2/2] check_files: make patterns apply to the directory names previously, pattersns only applied to file names. afterwards, directories that did not contain any matching files were removed now, the patterns apply directly to the directories as well. no futher processing for directories are done --- pkg/snclient/check_files.go | 28 ++++++++++++++++------------ pkg/snclient/check_files_test.go | 18 ++++++++++++++---- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/pkg/snclient/check_files.go b/pkg/snclient/check_files.go index 2b576a49..8136fa12 100644 --- a/pkg/snclient/check_files.go +++ b/pkg/snclient/check_files.go @@ -166,12 +166,7 @@ func (l *CheckFiles) Check(_ context.Context, _ *Agent, check *CheckData, _ []Ar } } - // Cleanup the listData if a filter is used - if l.pattern != "*" { - // l.removeDirectoriesWithoutFilesUnder(check) - - l.removeDirectories(check) - } + l.removeDirectoriesThatDontMatchPattern(check) if l.calculateSubdirectorySizes { l.addSubdirectorySizes(check) @@ -560,22 +555,31 @@ func checkSlowFileOperations(check *CheckData, entry map[string]string, path str return nil } -func (l *CheckFiles) removeDirectories(check *CheckData) { - newListData := make([]map[string]string, 0) +func (l *CheckFiles) removeDirectoriesThatDontMatchPattern(check *CheckData) { + if l.pattern == "*" { + return + } + + newListdata := make([]map[string]string, 0) for _, data := range check.listData { - if data["type"] != "dir" { - newListData = append(newListData, data) + if data["type"] == "dir" { + if match, _ := filepath.Match(l.pattern, data["filename"]); !match { + continue + } } + newListdata = append(newListdata, data) } - check.listData = newListData + check.listData = newListdata } // The WalkDir normally adds every directory and files under the search path. -// If a pattern is specified, this prevents files that dont match the pattern to be skipped. +// If a pattern/filter is specified, this prevents files that dont match the pattern/filter to be skipped. // This can lead to some directories being in the listData, while not having any matched files under them. // This function cleans those directories up. +// +//nolint:unused // this function was called if pattern was specified, in previous versions. still keeping it for possible future use with filters. func (l *CheckFiles) removeDirectoriesWithoutFilesUnder(check *CheckData) { fileFilepaths := make([]string, 0) diff --git a/pkg/snclient/check_files_test.go b/pkg/snclient/check_files_test.go index 223c5fed..2bc5939d 100644 --- a/pkg/snclient/check_files_test.go +++ b/pkg/snclient/check_files_test.go @@ -493,7 +493,7 @@ func TestCheckFilesSizePerfdata(t *testing.T) { // The second pass should remove the "a" folder where files with "a" extension is found res = snc.RunCheck("check_files", []string{"path=" + geneartionDirectory, "pattern=*.b", "crit='size > 100MiB'"}) outputString = string(res.BuildPluginOutput()) - assert.Containsf(t, outputString, "OK - All 6 files are ok", "output matches") + assert.Containsf(t, outputString, "OK - All 5 files are ok", "output matches") res = snc.RunCheck("check_files", []string{"path=" + geneartionDirectory, "pattern=*.b", "crit='size > 100MiB'", "filter=' type == file'"}) outputString = string(res.BuildPluginOutput()) assert.Containsf(t, outputString, "OK - All 5 files are ok", "output matches") @@ -517,9 +517,19 @@ func TestCheckFilesSizePerfdata(t *testing.T) { // When using a pattern and calculate subdirectory sizes is enabled, it will add the subdirectory sizes as metrics res = snc.RunCheck("check_files", []string{"path=" + geneartionDirectory, "pattern=*_3.*", "crit='size > 0Mib'", "calculate-subdirectory-sizes=true"}) outputString = string(res.BuildPluginOutput()) - assert.Containsf(t, outputString, "CRITICAL - 6/6 files (3.50 MiB) critical", "output matches") - assert.Containsf(t, outputString, "'a size'=1048576B", "should calculate the size of the subfolder a") - assert.Containsf(t, outputString, "'b size'=1048576B", "should calculate the size of the subfolder b") + assert.Containsf(t, outputString, "CRITICAL - 4/4 files (3.50 MiB) critical", "output matches") + assert.NotContainsf(t, outputString, "'a size'=1048576B", "should calculate the size of the subfolder a, as it does not match pattern") + assert.NotContainsf(t, outputString, "'b size'=1048576B", "should calculate the size of the subfolder b, as it does not match pattern") + + // only matches the directory "a" itself + res = snc.RunCheck("check_files", []string{"path=" + geneartionDirectory, "pattern=a"}) + outputString = string(res.BuildPluginOutput()) + assert.Containsf(t, outputString, "OK - All 1 files are ok: (0 B)", "output matches") + + // matches the four files inside directory "a": file_1024kb_1.a , file_1024kb_2.a , file_1024kb_3.a , file_1024kb_4.a + res = snc.RunCheck("check_files", []string{"path=" + geneartionDirectory, "pattern=*.a"}) + outputString = string(res.BuildPluginOutput()) + assert.Containsf(t, outputString, "OK - All 4 files are ok: (4.00 MiB)", "output matches") StopTestAgent(t, snc) }