2014-11-16 21:13:20 +01:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 21:43:32 +02:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-09-04 22:29:53 +02:00
|
|
|
|
|
|
|
package ignore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2014-10-12 23:35:15 +02:00
|
|
|
"sync"
|
2014-09-04 22:29:53 +02:00
|
|
|
|
2014-09-22 21:42:11 +02:00
|
|
|
"github.com/syncthing/syncthing/internal/fnmatch"
|
2014-09-04 22:29:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Pattern struct {
|
|
|
|
match *regexp.Regexp
|
|
|
|
include bool
|
|
|
|
}
|
|
|
|
|
2014-10-12 23:35:15 +02:00
|
|
|
type Matcher struct {
|
|
|
|
patterns []Pattern
|
2014-12-02 23:13:03 +01:00
|
|
|
matches *cache
|
|
|
|
mut sync.Mutex
|
2014-10-12 23:35:15 +02:00
|
|
|
}
|
2014-09-04 22:29:53 +02:00
|
|
|
|
2014-10-12 23:35:15 +02:00
|
|
|
func Load(file string, cache bool) (*Matcher, error) {
|
2014-09-04 22:29:53 +02:00
|
|
|
seen := make(map[string]bool)
|
2014-10-12 23:35:15 +02:00
|
|
|
matcher, err := loadIgnoreFile(file, seen)
|
|
|
|
if !cache || err != nil {
|
|
|
|
return matcher, err
|
|
|
|
}
|
|
|
|
|
2014-12-02 23:13:03 +01:00
|
|
|
cacheMut.Lock()
|
|
|
|
defer cacheMut.Unlock()
|
|
|
|
|
2014-10-12 23:35:15 +02:00
|
|
|
// Get the current cache object for the given file
|
|
|
|
cached, ok := caches[file]
|
|
|
|
if !ok || !patternsEqual(cached.patterns, matcher.patterns) {
|
|
|
|
// Nothing in cache or a cache mismatch, create a new cache which will
|
|
|
|
// store matches for the given set of patterns.
|
|
|
|
// Initialize oldMatches to indicate that we are interested in
|
|
|
|
// caching.
|
2014-12-02 23:13:03 +01:00
|
|
|
cached = newCache(matcher.patterns)
|
|
|
|
matcher.matches = cached
|
|
|
|
caches[file] = cached
|
2014-10-12 23:35:15 +02:00
|
|
|
return matcher, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Patterns haven't changed, so we can reuse the old matches, create a new
|
|
|
|
// matches map and update the pointer. (This prevents matches map from
|
|
|
|
// growing indefinately, as we only cache whatever we've matched in the last
|
|
|
|
// iteration, rather than through runtime history)
|
2014-12-02 23:13:03 +01:00
|
|
|
matcher.matches = cached
|
2014-10-12 23:35:15 +02:00
|
|
|
return matcher, nil
|
2014-09-04 22:29:53 +02:00
|
|
|
}
|
|
|
|
|
2014-10-12 23:35:15 +02:00
|
|
|
func Parse(r io.Reader, file string) (*Matcher, error) {
|
2014-09-04 22:29:53 +02:00
|
|
|
seen := map[string]bool{
|
|
|
|
file: true,
|
|
|
|
}
|
|
|
|
return parseIgnoreFile(r, file, seen)
|
|
|
|
}
|
|
|
|
|
2014-10-12 23:35:15 +02:00
|
|
|
func (m *Matcher) Match(file string) (result bool) {
|
|
|
|
if len(m.patterns) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-12-02 23:13:03 +01:00
|
|
|
if m.matches != nil {
|
|
|
|
// Check the cache for a known result.
|
|
|
|
res, ok := m.matches.get(file)
|
2014-10-12 23:35:15 +02:00
|
|
|
if ok {
|
2014-12-02 23:13:03 +01:00
|
|
|
return res
|
2014-10-12 23:35:15 +02:00
|
|
|
}
|
2014-12-02 23:13:03 +01:00
|
|
|
|
|
|
|
// Update the cache with the result at return time
|
|
|
|
defer func() {
|
|
|
|
m.matches.set(file, result)
|
|
|
|
}()
|
2014-10-12 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2014-12-02 23:13:03 +01:00
|
|
|
// Check all the patterns for a match.
|
2014-10-12 23:35:15 +02:00
|
|
|
for _, pattern := range m.patterns {
|
2014-09-04 22:29:53 +02:00
|
|
|
if pattern.match.MatchString(file) {
|
|
|
|
return pattern.include
|
|
|
|
}
|
|
|
|
}
|
2014-12-02 23:13:03 +01:00
|
|
|
|
|
|
|
// Default to false.
|
2014-09-04 22:29:53 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-11-08 22:12:18 +01:00
|
|
|
// Patterns return a list of the loaded regexp patterns, as strings
|
|
|
|
func (m *Matcher) Patterns() []string {
|
|
|
|
patterns := make([]string, len(m.patterns))
|
|
|
|
for i, pat := range m.patterns {
|
|
|
|
if pat.include {
|
|
|
|
patterns[i] = pat.match.String()
|
|
|
|
} else {
|
|
|
|
patterns[i] = "(?exclude)" + pat.match.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return patterns
|
|
|
|
}
|
|
|
|
|
2014-10-12 23:35:15 +02:00
|
|
|
func loadIgnoreFile(file string, seen map[string]bool) (*Matcher, error) {
|
2014-09-04 22:29:53 +02:00
|
|
|
if seen[file] {
|
2014-10-14 22:30:00 +02:00
|
|
|
return nil, fmt.Errorf("Multiple include of ignore file %q", file)
|
2014-09-04 22:29:53 +02:00
|
|
|
}
|
|
|
|
seen[file] = true
|
|
|
|
|
|
|
|
fd, err := os.Open(file)
|
|
|
|
if err != nil {
|
2014-10-14 22:30:00 +02:00
|
|
|
return nil, err
|
2014-09-04 22:29:53 +02:00
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
return parseIgnoreFile(fd, file, seen)
|
|
|
|
}
|
|
|
|
|
2014-10-12 23:35:15 +02:00
|
|
|
func parseIgnoreFile(fd io.Reader, currentFile string, seen map[string]bool) (*Matcher, error) {
|
2014-10-14 22:30:00 +02:00
|
|
|
var exps Matcher
|
2014-09-04 22:29:53 +02:00
|
|
|
|
|
|
|
addPattern := func(line string) error {
|
|
|
|
include := true
|
|
|
|
if strings.HasPrefix(line, "!") {
|
|
|
|
line = line[1:]
|
|
|
|
include = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "/") {
|
|
|
|
// Pattern is rooted in the current dir only
|
|
|
|
exp, err := fnmatch.Convert(line[1:], fnmatch.FNM_PATHNAME)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
|
|
|
}
|
2014-10-12 23:35:15 +02:00
|
|
|
exps.patterns = append(exps.patterns, Pattern{exp, include})
|
2014-09-04 22:29:53 +02:00
|
|
|
} else if strings.HasPrefix(line, "**/") {
|
|
|
|
// Add the pattern as is, and without **/ so it matches in current dir
|
|
|
|
exp, err := fnmatch.Convert(line, fnmatch.FNM_PATHNAME)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
|
|
|
}
|
2014-10-12 23:35:15 +02:00
|
|
|
exps.patterns = append(exps.patterns, Pattern{exp, include})
|
2014-09-04 22:29:53 +02:00
|
|
|
|
|
|
|
exp, err = fnmatch.Convert(line[3:], fnmatch.FNM_PATHNAME)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
|
|
|
}
|
2014-10-12 23:35:15 +02:00
|
|
|
exps.patterns = append(exps.patterns, Pattern{exp, include})
|
2014-09-04 22:29:53 +02:00
|
|
|
} else if strings.HasPrefix(line, "#include ") {
|
|
|
|
includeFile := filepath.Join(filepath.Dir(currentFile), line[len("#include "):])
|
|
|
|
includes, err := loadIgnoreFile(includeFile, seen)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2014-10-12 23:35:15 +02:00
|
|
|
exps.patterns = append(exps.patterns, includes.patterns...)
|
2014-09-04 22:29:53 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Path name or pattern, add it so it matches files both in
|
|
|
|
// current directory and subdirs.
|
|
|
|
exp, err := fnmatch.Convert(line, fnmatch.FNM_PATHNAME)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
|
|
|
}
|
2014-10-12 23:35:15 +02:00
|
|
|
exps.patterns = append(exps.patterns, Pattern{exp, include})
|
2014-09-04 22:29:53 +02:00
|
|
|
|
|
|
|
exp, err = fnmatch.Convert("**/"+line, fnmatch.FNM_PATHNAME)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
|
|
|
}
|
2014-10-12 23:35:15 +02:00
|
|
|
exps.patterns = append(exps.patterns, Pattern{exp, include})
|
2014-09-04 22:29:53 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(fd)
|
|
|
|
var err error
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
switch {
|
|
|
|
case line == "":
|
|
|
|
continue
|
2014-09-16 23:22:21 +02:00
|
|
|
case strings.HasPrefix(line, "//"):
|
|
|
|
continue
|
2014-09-04 22:29:53 +02:00
|
|
|
case strings.HasPrefix(line, "#"):
|
|
|
|
err = addPattern(line)
|
|
|
|
case strings.HasSuffix(line, "/**"):
|
|
|
|
err = addPattern(line)
|
|
|
|
case strings.HasSuffix(line, "/"):
|
|
|
|
err = addPattern(line)
|
|
|
|
if err == nil {
|
|
|
|
err = addPattern(line + "**")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = addPattern(line)
|
|
|
|
if err == nil {
|
|
|
|
err = addPattern(line + "/**")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-10-14 22:30:00 +02:00
|
|
|
return nil, err
|
2014-09-04 22:29:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-12 23:35:15 +02:00
|
|
|
return &exps, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func patternsEqual(a, b []Pattern) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range a {
|
|
|
|
if a[i].include != b[i].include || a[i].match.String() != b[i].match.String() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2014-09-04 22:29:53 +02:00
|
|
|
}
|