@@ -6,7 +6,6 @@ package resources
66
77import (
88 "errors"
9- "fmt"
109 "regexp"
1110 "strconv"
1211 "strings"
@@ -15,17 +14,9 @@ import (
1514 "go.mondoo.com/cnquery/v12/llx"
1615 "go.mondoo.com/cnquery/v12/providers-sdk/v1/plugin"
1716 "go.mondoo.com/cnquery/v12/providers/os/connection/shared"
17+ "go.mondoo.com/cnquery/v12/providers/os/resources/filesfind"
1818)
1919
20- var findTypes = map [string ]string {
21- "file" : "f" ,
22- "directory" : "d" ,
23- "character" : "c" ,
24- "block" : "b" ,
25- "socket" : "s" ,
26- "link" : "l" ,
27- }
28-
2920func initFilesFind (runtime * plugin.Runtime , args map [string ]* llx.RawData ) (map [string ]* llx.RawData , plugin.Resource , error ) {
3021 if args ["permissions" ] == nil {
3122 args ["permissions" ] = llx .IntData (int64 (0o777 ))
@@ -34,10 +25,6 @@ func initFilesFind(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[s
3425 return args , nil , nil
3526}
3627
37- func octal2string (o int64 ) string {
38- return fmt .Sprintf ("%o" , o )
39- }
40-
4128func (l * mqlFilesFind ) id () (string , error ) {
4229 var id strings.Builder
4330 id .WriteString (l .From .Data )
@@ -62,7 +49,7 @@ func (l *mqlFilesFind) id() (string, error) {
6249 }
6350
6451 if l .Permissions .Data != 0o777 {
65- id .WriteString (" permissions=" + octal2string (l .Permissions .Data ))
52+ id .WriteString (" permissions=" + filesfind . Octal2string (l .Permissions .Data ))
6653 }
6754
6855 return id .String (), nil
@@ -84,6 +71,9 @@ func (l *mqlFilesFind) list() ([]any, error) {
8471 }
8572 } else if conn .Capabilities ().Has (shared .Capability_RunCommand ) && pf .IsFamily ("unix" ) {
8673 foundFiles , err = l .unixFilesFindCmd ()
74+ if err != nil {
75+ return nil , err
76+ }
8777 } else {
8878 return nil , errors .New ("find is not supported for your platform" )
8979 }
@@ -142,45 +132,14 @@ func (l *mqlFilesFind) fsFilesFind(conn shared.Connection) ([]string, error) {
142132}
143133
144134func (l * mqlFilesFind ) unixFilesFindCmd () ([]string , error ) {
145- var call strings.Builder
146- call .WriteString ("find -L " )
147- call .WriteString (strconv .Quote (l .From .Data ))
148-
149- if ! l .Xdev .Data {
150- call .WriteString (" -xdev" )
151- }
152-
153- if l .Type .Data != "" {
154- t , ok := findTypes [l .Type .Data ]
155- if ok {
156- call .WriteString (" -type " + t )
157- }
158- }
159-
160- if l .Regex .Data != "" {
161- // TODO: we need to escape regex here
162- call .WriteString (" -regex '" )
163- call .WriteString (l .Regex .Data )
164- call .WriteString ("'" )
165- }
166-
167- if l .Permissions .Data != 0o777 {
168- call .WriteString (" -perm -" )
169- call .WriteString (octal2string (l .Permissions .Data ))
170- }
171-
172- if l .Name .Data != "" {
173- call .WriteString (" -name " )
174- call .WriteString (l .Name .Data )
175- }
176-
135+ var depth * int64
177136 if l .Depth .IsSet () {
178- call .WriteString (" -maxdepth " )
179- call .WriteString (octal2string (l .Depth .Data ))
137+ depth = & l .Depth .Data
180138 }
181139
140+ callCmd := filesfind .BuildFilesFindCmd (l .From .Data , l .Xdev .Data , l .Type .Data , l .Regex .Data , l .Permissions .Data , l .Name .Data , depth )
182141 rawCmd , err := CreateResource (l .MqlRuntime , "command" , map [string ]* llx.RawData {
183- "command" : llx .StringData (call . String () ),
142+ "command" : llx .StringData (callCmd ),
184143 })
185144 if err != nil {
186145 return nil , err
0 commit comments