Skip to content

Commit

Permalink
🐛 fix find files for tar
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev committed Dec 12, 2024
1 parent c17e048 commit 556c9e7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
20 changes: 19 additions & 1 deletion 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,14 +195,17 @@ 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]
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

0 comments on commit 556c9e7

Please sign in to comment.