Skip to content

Commit

Permalink
filter: use filepath.Match to maintain consistency with other pattern
Browse files Browse the repository at this point in the history
matching in podman

Following commit ensures that we maintain consistency with how pattern
matching is being carried out everywhere else in podman.

Switch from `regexp` to `filepath.Match`

For example https://github.com/containers/common/blob/main/libimage/filters.go#L162

[NO NEW TESTS NEEDED]

Signed-off-by: Aditya Rajan <[email protected]>
  • Loading branch information
flouthoc committed Nov 19, 2021
1 parent 671e5ee commit 6011149
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions pkg/util/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -95,24 +95,13 @@ func PrepareFilters(r *http.Request) (*map[string][]string, error) {
return &filterMap, nil
}

func wildCardToRegexp(pattern string) string {
var result strings.Builder
for i, literal := range strings.Split(pattern, "*") {
// Replace * with .*
if i > 0 {
result.WriteString(".*")
}
// Quote any regular expression meta characters in the
// literal text.
result.WriteString(regexp.QuoteMeta(literal))
}
return result.String()
}

func matchPattern(pattern string, value string) bool {
if strings.Contains(pattern, "*") {
result, _ := regexp.MatchString(wildCardToRegexp(pattern), value)
return result
filter := fmt.Sprintf("*%s*", pattern)
filter = strings.ReplaceAll(filter, string(filepath.Separator), "|")
newName := strings.ReplaceAll(value, string(filepath.Separator), "|")
match, _ := filepath.Match(filter, newName)
return match
}
return false
}
Expand Down

0 comments on commit 6011149

Please sign in to comment.