summaryrefslogtreecommitdiffstats
path: root/hugofs/glob/filename_filter.go
blob: c4b582bd5c5a916bf1dd1785a6d1dbcde9f3dd3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2021 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package glob

import (
	"path"
	"path/filepath"
	"strings"

	"github.com/gobwas/glob"
)

type FilenameFilter struct {
	shouldInclude func(filename string) bool
	inclusions    []glob.Glob
	dirInclusions []glob.Glob
	exclusions    []glob.Glob
	isWindows     bool
}

func normalizeFilenameGlobPattern(s string) string {
	// Use Unix separators even on Windows.
	s = filepath.ToSlash(s)
	if !strings.HasPrefix(s, "/") {
		s = "/" + s
	}
	return s
}

// NewFilenameFilter creates a new Glob where the Match method will
// return true if the file should be included.
// Note that the inclusions will be checked first.
func NewFilenameFilter(inclusions, exclusions []string) (*FilenameFilter, error) {
	if inclusions == nil && exclusions == nil {
		return nil, nil
	}
	filter := &FilenameFilter{isWindows: isWindows}

	for _, include := range inclusions {
		include = normalizeFilenameGlobPattern(include)
		g, err := filenamesGlobCache.GetGlob(include)
		if err != nil {
			return nil, err
		}
		filter.inclusions = append(filter.inclusions, g)

		// For mounts that do directory walking (e.g. content) we
		// must make sure that all directories up to this inclusion also
		// gets included.
		dir := path.Dir(include)
		parts := strings.Split(dir, "/")
		for i, _ := range parts {
			pattern := "/" + filepath.Join(parts[:i+1]...)
			g, err := filenamesGlobCache.GetGlob(pattern)
			if err != nil {
				return nil, err
			}
			filter.dirInclusions = append(filter.dirInclusions, g)
		}
	}

	for _, exclude := range exclusions {
		exclude = normalizeFilenameGlobPattern(exclude)
		g, err := filenamesGlobCache.GetGlob(exclude)
		if err != nil {
			return nil, err
		}
		filter.exclusions = append(filter.exclusions, g)
	}

	return filter, nil
}

// MustNewFilenameFilter invokes NewFilenameFilter and panics on error.
func MustNewFilenameFilter(inclusions, exclusions []string) *FilenameFilter {
	filter, err := NewFilenameFilter(inclusions, exclusions)
	if err != nil {
		panic(err)
	}
	return filter
}

// NewFilenameFilterForInclusionFunc create a new filter using the provided inclusion func.
func NewFilenameFilterForInclusionFunc(shouldInclude func(filename string) bool) *FilenameFilter {
	return &FilenameFilter{shouldInclude: shouldInclude, isWindows: isWindows}
}

// Match returns whether filename should be included.
func (f *FilenameFilter) Match(filename string, isDir bool) bool {
	if f == nil {
		return true
	}
	return f.doMatch(filename, isDir)
	/*if f.shouldInclude == nil {
		fmt.Printf("Match: %q (%t) => %t\n", filename, isDir, isMatch)
	}
	return isMatch*/
}

func (f *FilenameFilter) doMatch(filename string, isDir bool) bool {
	if f == nil {
		return true
	}

	if !strings.HasPrefix(filename, filepathSeparator) {
		filename = filepathSeparator + filename
	}

	if f.shouldInclude != nil {
		if f.shouldInclude(filename) {
			return true
		}
		if f.isWindows {
			// The Glob matchers below handles this by themselves,
			// for the shouldInclude we need to take some extra steps
			// to make this robust.
			winFilename := filepath.FromSlash(filename)
			if filename != winFilename {
				if f.shouldInclude(winFilename) {
					return true
				}
			}
		}

	}

	for _, inclusion := range f.inclusions {
		if inclusion.Match(filename) {
			return true
		}
	}

	if isDir && f.inclusions != nil {
		for _, inclusion := range f.dirInclusions {
			if inclusion.Match(filename) {
				return true
			}
		}
	}

	for _, exclusion := range f.exclusions {
		if exclusion.Match(filename) {
			return false
		}
	}

	return f.inclusions == nil && f.shouldInclude == nil
}