Skip to content

Commit

Permalink
🐛 fix find files for tar (#4974)
Browse files Browse the repository at this point in the history
* 🐛 fix find files for tar

Signed-off-by: Ivan Milchev <[email protected]>

* fix find by name

Signed-off-by: Ivan Milchev <[email protected]>

---------

Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev authored Dec 12, 2024
1 parent c17e048 commit ccf731d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
4 changes: 2 additions & 2 deletions providers/os/connection/container/image_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ func TestImageConnections(t *testing.T) {
fSearch := fs.(*tar.FS)

if test.testfile == "/etc/alpine-release" {
infos, err := fSearch.Find("/", regexp.MustCompile(`alpine-release`), "file")
infos, err := fSearch.Find("/", regexp.MustCompile(`alpine-release`), "file", nil, nil)
require.NoError(t, err)
assert.Equal(t, 1, len(infos))
} else if test.testfile == "/etc/centos-release" {
infos, err := fSearch.Find("/", regexp.MustCompile(`centos-release`), "file")
infos, err := fSearch.Find("/", regexp.MustCompile(`centos-release`), "file", nil, nil)
require.NoError(t, err)
assert.Equal(t, 6, len(infos))
}
Expand Down
2 changes: 1 addition & 1 deletion providers/os/connection/tar/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func TestTarFileFind(t *testing.T) {

fSearch := fs.(*tar.FS)

infos, err := fSearch.Find("/", regexp.MustCompile(`alpine-release`), "file")
infos, err := fSearch.Find("/", regexp.MustCompile(`alpine-release`), "file", nil, nil)
require.NoError(t, err)

assert.Equal(t, 1, len(infos))
Expand Down
22 changes: 20 additions & 2 deletions providers/os/connection/tar/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ import (

"github.com/rs/zerolog/log"
"github.com/spf13/afero"
"go.mondoo.com/cnquery/v11/providers/os/connection/shared"
"go.mondoo.com/cnquery/v11/providers/os/fsutil"
)

var _ shared.FileSearch = (*FS)(nil)

func NewFs(source string) *FS {
return &FS{
Source: source,
Expand Down Expand Up @@ -192,18 +195,21 @@ func (fs *FS) tar(path string, header *tar.Header) (io.ReadCloser, error) {

// searches for files and returns the file info
// regex can be nil
func (fs *FS) Find(from string, r *regexp.Regexp, typ string) ([]string, error) {
func (fs *FS) Find(from string, r *regexp.Regexp, typ string, perm *uint32, depth *int) ([]string, error) {
list := []string{}
for k := range fs.FileMap {
p := strings.HasPrefix(k, from)
m := true
if r != nil {
m = r.MatchString(k)
}
if !depthMatch(from, k, depth) {
continue
}
log.Trace().Str("path", k).Str("from", from).Str("prefix", from).Bool("prefix", p).Bool("m", m).Msg("check if matches")
if p && m {
entry := fs.FileMap[k]
if (typ == "directory" && entry.Typeflag == tar.TypeDir) || (typ == "file" && entry.Typeflag == tar.TypeReg) {
if (typ == "directory" && entry.Typeflag == tar.TypeDir) || (typ == "file" && entry.Typeflag == tar.TypeReg) || typ == "" {
list = append(list, k)
log.Debug().Msg("matches")
continue
Expand All @@ -212,3 +218,15 @@ func (fs *FS) Find(from string, r *regexp.Regexp, typ string) ([]string, error)
}
return list, nil
}

func depthMatch(from, filepath string, depth *int) bool {
if depth == nil {
return true
}

trimmed := strings.TrimPrefix(filepath, from)
// WalkDir always uses slash for separating, ignoring the OS separator. This is why we need to replace it.
normalized := strings.ReplaceAll(trimmed, string(os.PathSeparator), "/")
fileDepth := strings.Count(normalized, "/")
return fileDepth <= *depth
}
3 changes: 3 additions & 0 deletions providers/os/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
"time"

"github.com/spf13/afero"
"go.mondoo.com/cnquery/v11/providers/os/connection/shared"
)

var _ shared.FileSearch = (*MountedFs)(nil)

var notSupported = errors.New("not supported")

type MountedFs struct {
Expand Down
5 changes: 5 additions & 0 deletions providers/os/resources/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func (l *mqlFilesFind) list() ([]interface{}, error) {
if err != nil {
return nil, err
}
} else if len(l.Name.Data) > 0 {
compiledRegexp, err = regexp.Compile(l.Name.Data)
if err != nil {
return nil, err
}
}

var foundFiles []string
Expand Down

0 comments on commit ccf731d

Please sign in to comment.