2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-09-04 13:29:53 -07:00
|
|
|
|
|
|
|
package ignore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2014-12-23 02:05:08 -07:00
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
2014-09-04 13:29:53 -07:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-04-02 12:03:24 -07:00
|
|
|
"runtime"
|
2014-09-04 13:29:53 -07:00
|
|
|
"strings"
|
2014-12-23 02:05:08 -07:00
|
|
|
"time"
|
2014-09-04 13:29:53 -07:00
|
|
|
|
2016-04-02 12:03:24 -07:00
|
|
|
"github.com/gobwas/glob"
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2014-09-04 13:29:53 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Pattern struct {
|
2016-04-02 12:03:24 -07:00
|
|
|
pattern string
|
|
|
|
match glob.Glob
|
|
|
|
include bool
|
|
|
|
foldCase bool
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
|
|
|
|
2014-12-23 02:05:08 -07:00
|
|
|
func (p Pattern) String() string {
|
2016-04-02 12:03:24 -07:00
|
|
|
ret := p.pattern
|
|
|
|
if !p.include {
|
|
|
|
ret = "!" + ret
|
2014-12-23 02:05:08 -07:00
|
|
|
}
|
2016-04-02 12:03:24 -07:00
|
|
|
if p.foldCase {
|
|
|
|
ret = "(?i)" + ret
|
|
|
|
}
|
|
|
|
return ret
|
2014-12-23 02:05:08 -07:00
|
|
|
}
|
|
|
|
|
2014-10-12 14:35:15 -07:00
|
|
|
type Matcher struct {
|
2014-12-23 02:05:08 -07:00
|
|
|
patterns []Pattern
|
|
|
|
withCache bool
|
|
|
|
matches *cache
|
|
|
|
curHash string
|
|
|
|
stop chan struct{}
|
|
|
|
mut sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(withCache bool) *Matcher {
|
|
|
|
m := &Matcher{
|
|
|
|
withCache: withCache,
|
|
|
|
stop: make(chan struct{}),
|
2015-04-22 15:54:31 -07:00
|
|
|
mut: sync.NewMutex(),
|
2014-12-23 02:05:08 -07:00
|
|
|
}
|
|
|
|
if withCache {
|
|
|
|
go m.clean(2 * time.Hour)
|
|
|
|
}
|
|
|
|
return m
|
2014-10-12 14:35:15 -07:00
|
|
|
}
|
2014-09-04 13:29:53 -07:00
|
|
|
|
2014-12-23 02:05:08 -07:00
|
|
|
func (m *Matcher) Load(file string) error {
|
|
|
|
// No locking, Parse() does the locking
|
|
|
|
|
|
|
|
fd, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
// We do a parse with empty patterns to clear out the hash, cache etc.
|
|
|
|
m.Parse(&bytes.Buffer{}, file)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
return m.Parse(fd, file)
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
|
|
|
|
2014-12-23 02:05:08 -07:00
|
|
|
func (m *Matcher) Parse(r io.Reader, file string) error {
|
|
|
|
m.mut.Lock()
|
|
|
|
defer m.mut.Unlock()
|
|
|
|
|
|
|
|
seen := map[string]bool{file: true}
|
|
|
|
patterns, err := parseIgnoreFile(r, file, seen)
|
|
|
|
// Error is saved and returned at the end. We process the patterns
|
|
|
|
// (possibly blank) anyway.
|
|
|
|
|
|
|
|
newHash := hashPatterns(patterns)
|
|
|
|
if newHash == m.curHash {
|
|
|
|
// We've already loaded exactly these patterns.
|
|
|
|
return err
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
2014-12-23 02:05:08 -07:00
|
|
|
|
|
|
|
m.curHash = newHash
|
|
|
|
m.patterns = patterns
|
|
|
|
if m.withCache {
|
|
|
|
m.matches = newCache(patterns)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
|
|
|
|
2014-10-12 14:35:15 -07:00
|
|
|
func (m *Matcher) Match(file string) (result bool) {
|
2015-04-27 12:49:10 -07:00
|
|
|
if m == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-12-23 02:05:08 -07:00
|
|
|
m.mut.Lock()
|
|
|
|
defer m.mut.Unlock()
|
|
|
|
|
2014-10-12 14:35:15 -07:00
|
|
|
if len(m.patterns) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-12-02 15:13:03 -07:00
|
|
|
if m.matches != nil {
|
|
|
|
// Check the cache for a known result.
|
|
|
|
res, ok := m.matches.get(file)
|
2014-10-12 14:35:15 -07:00
|
|
|
if ok {
|
2014-12-02 15:13:03 -07:00
|
|
|
return res
|
2014-10-12 14:35:15 -07:00
|
|
|
}
|
2014-12-02 15:13:03 -07:00
|
|
|
|
|
|
|
// Update the cache with the result at return time
|
|
|
|
defer func() {
|
|
|
|
m.matches.set(file, result)
|
|
|
|
}()
|
2014-10-12 14:35:15 -07:00
|
|
|
}
|
|
|
|
|
2014-12-02 15:13:03 -07:00
|
|
|
// Check all the patterns for a match.
|
2016-04-02 12:03:24 -07:00
|
|
|
file = filepath.ToSlash(file)
|
|
|
|
var lowercaseFile string
|
2014-10-12 14:35:15 -07:00
|
|
|
for _, pattern := range m.patterns {
|
2016-04-02 12:03:24 -07:00
|
|
|
if pattern.foldCase {
|
|
|
|
if lowercaseFile == "" {
|
|
|
|
lowercaseFile = strings.ToLower(file)
|
|
|
|
}
|
|
|
|
if pattern.match.Match(lowercaseFile) {
|
|
|
|
return pattern.include
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if pattern.match.Match(file) {
|
|
|
|
return pattern.include
|
|
|
|
}
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
|
|
|
}
|
2014-12-02 15:13:03 -07:00
|
|
|
|
|
|
|
// Default to false.
|
2014-09-04 13:29:53 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-04-02 12:03:24 -07:00
|
|
|
// Patterns return a list of the loaded patterns, as they've been parsed
|
2014-11-08 14:12:18 -07:00
|
|
|
func (m *Matcher) Patterns() []string {
|
2015-04-27 12:49:10 -07:00
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-23 02:05:08 -07:00
|
|
|
m.mut.Lock()
|
|
|
|
defer m.mut.Unlock()
|
|
|
|
|
2014-11-08 14:12:18 -07:00
|
|
|
patterns := make([]string, len(m.patterns))
|
|
|
|
for i, pat := range m.patterns {
|
2014-12-23 02:05:08 -07:00
|
|
|
patterns[i] = pat.String()
|
2014-11-08 14:12:18 -07:00
|
|
|
}
|
|
|
|
return patterns
|
|
|
|
}
|
|
|
|
|
2014-12-23 02:05:08 -07:00
|
|
|
func (m *Matcher) Hash() string {
|
|
|
|
m.mut.Lock()
|
|
|
|
defer m.mut.Unlock()
|
|
|
|
return m.curHash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Matcher) Stop() {
|
|
|
|
close(m.stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Matcher) clean(d time.Duration) {
|
|
|
|
t := time.NewTimer(d / 2)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.stop:
|
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
m.mut.Lock()
|
|
|
|
if m.matches != nil {
|
|
|
|
m.matches.clean(d)
|
|
|
|
}
|
|
|
|
t.Reset(d / 2)
|
|
|
|
m.mut.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashPatterns(patterns []Pattern) string {
|
|
|
|
h := md5.New()
|
|
|
|
for _, pat := range patterns {
|
|
|
|
h.Write([]byte(pat.String()))
|
|
|
|
h.Write([]byte("\n"))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadIgnoreFile(file string, seen map[string]bool) ([]Pattern, error) {
|
2014-09-04 13:29:53 -07:00
|
|
|
if seen[file] {
|
2014-10-14 13:30:00 -07:00
|
|
|
return nil, fmt.Errorf("Multiple include of ignore file %q", file)
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
|
|
|
seen[file] = true
|
|
|
|
|
|
|
|
fd, err := os.Open(file)
|
|
|
|
if err != nil {
|
2014-10-14 13:30:00 -07:00
|
|
|
return nil, err
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
return parseIgnoreFile(fd, file, seen)
|
|
|
|
}
|
|
|
|
|
2014-12-23 02:05:08 -07:00
|
|
|
func parseIgnoreFile(fd io.Reader, currentFile string, seen map[string]bool) ([]Pattern, error) {
|
|
|
|
var patterns []Pattern
|
2014-09-04 13:29:53 -07:00
|
|
|
|
|
|
|
addPattern := func(line string) error {
|
2016-04-02 12:03:24 -07:00
|
|
|
pattern := Pattern{
|
|
|
|
pattern: line,
|
|
|
|
include: true,
|
|
|
|
foldCase: runtime.GOOS == "darwin" || runtime.GOOS == "windows",
|
|
|
|
}
|
|
|
|
|
2014-09-04 13:29:53 -07:00
|
|
|
if strings.HasPrefix(line, "!") {
|
|
|
|
line = line[1:]
|
2016-04-02 12:03:24 -07:00
|
|
|
pattern.include = false
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
|
|
|
|
2015-09-02 11:54:18 -07:00
|
|
|
if strings.HasPrefix(line, "(?i)") {
|
2016-04-02 12:03:24 -07:00
|
|
|
pattern.foldCase = true
|
2016-04-04 23:35:51 -07:00
|
|
|
line = line[4:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if pattern.foldCase {
|
|
|
|
line = strings.ToLower(line)
|
2015-09-02 11:54:18 -07:00
|
|
|
}
|
|
|
|
|
2016-04-02 12:03:24 -07:00
|
|
|
var err error
|
2014-09-04 13:29:53 -07:00
|
|
|
if strings.HasPrefix(line, "/") {
|
|
|
|
// Pattern is rooted in the current dir only
|
2016-04-02 12:03:24 -07:00
|
|
|
pattern.match, err = glob.Compile(line[1:])
|
2014-09-04 13:29:53 -07:00
|
|
|
if err != nil {
|
2015-09-29 09:01:19 -07:00
|
|
|
return fmt.Errorf("invalid pattern %q in ignore file", line)
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
2016-04-02 12:03:24 -07:00
|
|
|
patterns = append(patterns, pattern)
|
2014-09-04 13:29:53 -07:00
|
|
|
} else if strings.HasPrefix(line, "**/") {
|
|
|
|
// Add the pattern as is, and without **/ so it matches in current dir
|
2016-04-02 12:03:24 -07:00
|
|
|
pattern.match, err = glob.Compile(line)
|
2014-09-04 13:29:53 -07:00
|
|
|
if err != nil {
|
2015-09-29 09:01:19 -07:00
|
|
|
return fmt.Errorf("invalid pattern %q in ignore file", line)
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
2016-04-02 12:03:24 -07:00
|
|
|
patterns = append(patterns, pattern)
|
2014-09-04 13:29:53 -07:00
|
|
|
|
2016-04-04 05:22:25 -07:00
|
|
|
line = line[3:]
|
|
|
|
pattern.pattern = line
|
|
|
|
pattern.match, err = glob.Compile(line)
|
2014-09-04 13:29:53 -07:00
|
|
|
if err != nil {
|
2015-09-29 09:01:19 -07:00
|
|
|
return fmt.Errorf("invalid pattern %q in ignore file", line)
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
2016-04-02 12:03:24 -07:00
|
|
|
patterns = append(patterns, pattern)
|
2014-09-04 13:29:53 -07:00
|
|
|
} else if strings.HasPrefix(line, "#include ") {
|
2015-09-29 09:01:19 -07:00
|
|
|
includeRel := line[len("#include "):]
|
|
|
|
includeFile := filepath.Join(filepath.Dir(currentFile), includeRel)
|
2014-09-04 13:29:53 -07:00
|
|
|
includes, err := loadIgnoreFile(includeFile, seen)
|
|
|
|
if err != nil {
|
2015-09-29 09:01:19 -07:00
|
|
|
return fmt.Errorf("include of %q: %v", includeRel, err)
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
2014-12-23 02:05:08 -07:00
|
|
|
patterns = append(patterns, includes...)
|
2014-09-04 13:29:53 -07:00
|
|
|
} else {
|
|
|
|
// Path name or pattern, add it so it matches files both in
|
|
|
|
// current directory and subdirs.
|
2016-04-02 12:03:24 -07:00
|
|
|
pattern.match, err = glob.Compile(line)
|
2014-09-04 13:29:53 -07:00
|
|
|
if err != nil {
|
2015-09-29 09:01:19 -07:00
|
|
|
return fmt.Errorf("invalid pattern %q in ignore file", line)
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
2016-04-02 12:03:24 -07:00
|
|
|
patterns = append(patterns, pattern)
|
2014-09-04 13:29:53 -07:00
|
|
|
|
2016-04-04 05:22:25 -07:00
|
|
|
line := "**/" + line
|
|
|
|
pattern.pattern = line
|
|
|
|
pattern.match, err = glob.Compile(line)
|
2014-09-04 13:29:53 -07:00
|
|
|
if err != nil {
|
2015-09-29 09:01:19 -07:00
|
|
|
return fmt.Errorf("invalid pattern %q in ignore file", line)
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
2016-04-02 12:03:24 -07:00
|
|
|
patterns = append(patterns, pattern)
|
2014-09-04 13:29:53 -07: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 14:22:21 -07:00
|
|
|
case strings.HasPrefix(line, "//"):
|
|
|
|
continue
|
2016-04-02 12:03:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
line = filepath.ToSlash(line)
|
|
|
|
switch {
|
2014-09-04 13:29:53 -07:00
|
|
|
case strings.HasPrefix(line, "#"):
|
|
|
|
err = addPattern(line)
|
|
|
|
case strings.HasSuffix(line, "/**"):
|
|
|
|
err = addPattern(line)
|
|
|
|
case strings.HasSuffix(line, "/"):
|
|
|
|
err = addPattern(line)
|
|
|
|
default:
|
|
|
|
err = addPattern(line)
|
|
|
|
if err == nil {
|
|
|
|
err = addPattern(line + "/**")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-10-14 13:30:00 -07:00
|
|
|
return nil, err
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 02:05:08 -07:00
|
|
|
return patterns, nil
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|