Skip to content

Commit

Permalink
fix: mc find --metadata work without prefix (#4926)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiuker authored May 9, 2024
1 parent a96123b commit 0d8c4f8
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion cmd/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit 0d8c4f8

Please sign in to comment.