blob: ed91af717e909abf68def4d3a3c5d4097f20b42e [file] [log] [blame]
Colin Crossc45c3b52019-03-26 15:50:03 -07001// Copyright 2019 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18 "errors"
19 "path/filepath"
20 "strings"
21)
22
23// Match returns true if name matches pattern using the same rules as filepath.Match, but supporting
24// recursive globs (**).
25func Match(pattern, name string) (bool, error) {
26 if filepath.Base(pattern) == "**" {
27 return false, errors.New("pattern has '**' as last path element")
28 }
29
30 patternDir := pattern[len(pattern)-1] == '/'
31 nameDir := name[len(name)-1] == '/'
32
33 if patternDir != nameDir {
34 return false, nil
35 }
36
37 if nameDir {
38 name = name[:len(name)-1]
39 pattern = pattern[:len(pattern)-1]
40 }
41
42 for {
43 var patternFile, nameFile string
44 pattern, patternFile = filepath.Dir(pattern), filepath.Base(pattern)
45
46 if patternFile == "**" {
47 if strings.Contains(pattern, "**") {
48 return false, errors.New("pattern contains multiple '**'")
49 }
50 // Test if the any prefix of name matches the part of the pattern before **
51 for {
52 if name == "." || name == "/" {
53 return name == pattern, nil
54 }
55 if match, err := filepath.Match(pattern, name); err != nil {
56 return false, err
57 } else if match {
58 return true, nil
59 }
60 name = filepath.Dir(name)
61 }
62 } else if strings.Contains(patternFile, "**") {
63 return false, errors.New("pattern contains other characters between '**' and path separator")
64 }
65
66 name, nameFile = filepath.Dir(name), filepath.Base(name)
67
68 if nameFile == "." && patternFile == "." {
69 return true, nil
70 } else if nameFile == "/" && patternFile == "/" {
71 return true, nil
72 } else if nameFile == "." || patternFile == "." || nameFile == "/" || patternFile == "/" {
73 return false, nil
74 }
75
76 match, err := filepath.Match(patternFile, nameFile)
77 if err != nil || !match {
78 return match, err
79 }
80 }
81}