From 0d8c4f8aa87164aaf2b9b80ff25d2d36b5b83c57 Mon Sep 17 00:00:00 2001 From: jiuker <2818723467@qq.com> Date: Thu, 9 May 2024 22:13:21 +0800 Subject: [PATCH] fix: mc find --metadata work without prefix (#4926) --- cmd/find.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/cmd/find.go b/cmd/find.go index fc5dddda..431c3f1f 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 +}