aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAudrius Butkevicius <audrius.butkevicius@gmail.com>2014-08-30 22:32:17 +0100
committerAudrius Butkevicius <audrius.butkevicius@gmail.com>2014-08-31 22:33:49 +0100
commiteebe0eeb715b3bb33caee05473f614676f5349b2 (patch)
treee9049e3edea613a1de5d2489911ed9c62a40c1fb
parentc2daedbd11293e74f40f6d63b5b6e06197479b11 (diff)
downloadsyncthing-eebe0eeb715b3bb33caee05473f614676f5349b2.tar.gz
syncthing-eebe0eeb715b3bb33caee05473f614676f5349b2.zip
Handle recursive includes
-rw-r--r--scanner/walk.go28
1 files changed, 21 insertions, 7 deletions
diff --git a/scanner/walk.go b/scanner/walk.go
index 6008d22e8..1278490c0 100644
--- a/scanner/walk.go
+++ b/scanner/walk.go
@@ -99,7 +99,8 @@ func (w *Walker) loadIgnoreFiles(dir string, ignores *[]*regexp.Regexp) filepath
if pn, sn := filepath.Split(rn); sn == w.IgnoreFile {
pn := filepath.Clean(pn)
- dirIgnores := loadIgnoreFile(p, pn)
+ filesSeen := make(map[string]map[string]bool)
+ dirIgnores := loadIgnoreFile(p, pn, filesSeen)
*ignores = append(*ignores, dirIgnores...)
}
@@ -107,16 +108,16 @@ func (w *Walker) loadIgnoreFiles(dir string, ignores *[]*regexp.Regexp) filepath
}
}
-func loadIgnoreFile(ignFile, base string) []*regexp.Regexp {
+func loadIgnoreFile(ignFile, base string, filesSeen map[string]map[string]bool) []*regexp.Regexp {
fd, err := os.Open(ignFile)
if err != nil {
return nil
}
defer fd.Close()
- return parseIgnoreFile(fd, base, filepath.Dir(ignFile))
+ return parseIgnoreFile(fd, base, ignFile, filesSeen)
}
-func parseIgnoreFile(fd io.Reader, base, dir string) []*regexp.Regexp {
+func parseIgnoreFile(fd io.Reader, base, currentFile string, filesSeen map[string]map[string]bool) []*regexp.Regexp {
var exps []*regexp.Regexp
scanner := bufio.NewScanner(fd)
for scanner.Scan() {
@@ -149,12 +150,25 @@ func parseIgnoreFile(fd io.Reader, base, dir string) []*regexp.Regexp {
}
exps = append(exps, exp)
} else if strings.HasPrefix(line, "#include ") {
- includeFile := filepath.Join(dir, strings.Replace(line, "#include ", "", 1))
+ includeFile := filepath.Join(filepath.Dir(currentFile), strings.Replace(line, "#include ", "", 1))
if _, err := os.Stat(includeFile); os.IsNotExist(err) {
l.Infoln("Could not open ignore include file", includeFile)
} else {
- includes := loadIgnoreFile(includeFile, base)
- exps = append(exps, includes...)
+ seen := false
+ if seenByCurrent, ok := filesSeen[currentFile]; ok {
+ _, seen = seenByCurrent[includeFile]
+ }
+
+ if seen {
+ l.Warnf("Recursion detected while including %s from %s", includeFile, currentFile)
+ } else {
+ if filesSeen[currentFile] == nil {
+ filesSeen[currentFile] = make(map[string]bool)
+ }
+ filesSeen[currentFile][includeFile] = true
+ includes := loadIgnoreFile(includeFile, base, filesSeen)
+ exps = append(exps, includes...)
+ }
}
} else {
// Path name or pattern, add it so it matches files both in