diff --git a/cmd/find.go b/cmd/find.go index fc5dddda48..431c3f1fce 100644 --- a/cmd/find.go +++ b/cmd/find.go @@ -21,6 +21,7 @@ import ( "bytes" "context" "fmt" + "net/http" "os" "os/exec" "path/filepath" @@ -426,7 +427,7 @@ func matchFind(ctx *findContext, fileContent contentMessage) (match bool) { match = int64(ctx.smallerSize) > fileContent.Size } if match && len(ctx.matchMeta) > 0 { - match = matchRegexMaps(ctx.matchMeta, fileContent.Metadata) + match = matchMetadataRegexMaps(ctx.matchMeta, fileContent.Metadata) } if match && len(ctx.matchTags) > 0 { match = matchRegexMaps(ctx.matchTags, fileContent.Tags) @@ -512,3 +513,25 @@ func matchRegexMaps(m map[string]*regexp.Regexp, v map[string]string) bool { } return true } + +// matchMetadataRegexMaps will check if all regexes in 'm' match values in 'v' with the same key. +// If a regex is nil, it must either not exist in v or have a 0 length value. +func matchMetadataRegexMaps(m map[string]*regexp.Regexp, v map[string]string) bool { + for k, reg := range m { + if reg == nil { + if v[k] != "" { + return false + } + // Does not exist or empty, that is fine. + continue + } + val, ok := v[k] + if !ok { + val, ok = v[http.CanonicalHeaderKey(fmt.Sprintf("X-Amz-Meta-%s", k))] + } + if !ok || !reg.MatchString(val) { + return false + } + } + return true +}