ignore.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package ignore
  7. import (
  8. "bufio"
  9. "bytes"
  10. "crypto/md5"
  11. "fmt"
  12. "io"
  13. "os"
  14. "path/filepath"
  15. "regexp"
  16. "strings"
  17. "sync"
  18. "time"
  19. "github.com/syncthing/syncthing/internal/fnmatch"
  20. )
  21. type Pattern struct {
  22. match *regexp.Regexp
  23. include bool
  24. }
  25. func (p Pattern) String() string {
  26. if p.include {
  27. return p.match.String()
  28. } else {
  29. return "(?exclude)" + p.match.String()
  30. }
  31. }
  32. type Matcher struct {
  33. patterns []Pattern
  34. withCache bool
  35. matches *cache
  36. curHash string
  37. stop chan struct{}
  38. mut sync.Mutex
  39. }
  40. func New(withCache bool) *Matcher {
  41. m := &Matcher{
  42. withCache: withCache,
  43. stop: make(chan struct{}),
  44. }
  45. if withCache {
  46. go m.clean(2 * time.Hour)
  47. }
  48. return m
  49. }
  50. func (m *Matcher) Load(file string) error {
  51. // No locking, Parse() does the locking
  52. fd, err := os.Open(file)
  53. if err != nil {
  54. // We do a parse with empty patterns to clear out the hash, cache etc.
  55. m.Parse(&bytes.Buffer{}, file)
  56. return err
  57. }
  58. defer fd.Close()
  59. return m.Parse(fd, file)
  60. }
  61. func (m *Matcher) Parse(r io.Reader, file string) error {
  62. m.mut.Lock()
  63. defer m.mut.Unlock()
  64. seen := map[string]bool{file: true}
  65. patterns, err := parseIgnoreFile(r, file, seen)
  66. // Error is saved and returned at the end. We process the patterns
  67. // (possibly blank) anyway.
  68. newHash := hashPatterns(patterns)
  69. if newHash == m.curHash {
  70. // We've already loaded exactly these patterns.
  71. return err
  72. }
  73. m.curHash = newHash
  74. m.patterns = patterns
  75. if m.withCache {
  76. m.matches = newCache(patterns)
  77. }
  78. return err
  79. }
  80. func (m *Matcher) Match(file string) (result bool) {
  81. m.mut.Lock()
  82. defer m.mut.Unlock()
  83. if len(m.patterns) == 0 {
  84. return false
  85. }
  86. if m.matches != nil {
  87. // Check the cache for a known result.
  88. res, ok := m.matches.get(file)
  89. if ok {
  90. return res
  91. }
  92. // Update the cache with the result at return time
  93. defer func() {
  94. m.matches.set(file, result)
  95. }()
  96. }
  97. // Check all the patterns for a match.
  98. for _, pattern := range m.patterns {
  99. if pattern.match.MatchString(file) {
  100. return pattern.include
  101. }
  102. }
  103. // Default to false.
  104. return false
  105. }
  106. // Patterns return a list of the loaded regexp patterns, as strings
  107. func (m *Matcher) Patterns() []string {
  108. m.mut.Lock()
  109. defer m.mut.Unlock()
  110. patterns := make([]string, len(m.patterns))
  111. for i, pat := range m.patterns {
  112. patterns[i] = pat.String()
  113. }
  114. return patterns
  115. }
  116. func (m *Matcher) Hash() string {
  117. m.mut.Lock()
  118. defer m.mut.Unlock()
  119. return m.curHash
  120. }
  121. func (m *Matcher) Stop() {
  122. close(m.stop)
  123. }
  124. func (m *Matcher) clean(d time.Duration) {
  125. t := time.NewTimer(d / 2)
  126. for {
  127. select {
  128. case <-m.stop:
  129. return
  130. case <-t.C:
  131. m.mut.Lock()
  132. if m.matches != nil {
  133. m.matches.clean(d)
  134. }
  135. t.Reset(d / 2)
  136. m.mut.Unlock()
  137. }
  138. }
  139. }
  140. func hashPatterns(patterns []Pattern) string {
  141. h := md5.New()
  142. for _, pat := range patterns {
  143. h.Write([]byte(pat.String()))
  144. h.Write([]byte("\n"))
  145. }
  146. return fmt.Sprintf("%x", h.Sum(nil))
  147. }
  148. func loadIgnoreFile(file string, seen map[string]bool) ([]Pattern, error) {
  149. if seen[file] {
  150. return nil, fmt.Errorf("Multiple include of ignore file %q", file)
  151. }
  152. seen[file] = true
  153. fd, err := os.Open(file)
  154. if err != nil {
  155. return nil, err
  156. }
  157. defer fd.Close()
  158. return parseIgnoreFile(fd, file, seen)
  159. }
  160. func parseIgnoreFile(fd io.Reader, currentFile string, seen map[string]bool) ([]Pattern, error) {
  161. var patterns []Pattern
  162. addPattern := func(line string) error {
  163. include := true
  164. if strings.HasPrefix(line, "!") {
  165. line = line[1:]
  166. include = false
  167. }
  168. if strings.HasPrefix(line, "/") {
  169. // Pattern is rooted in the current dir only
  170. exp, err := fnmatch.Convert(line[1:], fnmatch.FNM_PATHNAME)
  171. if err != nil {
  172. return fmt.Errorf("Invalid pattern %q in ignore file", line)
  173. }
  174. patterns = append(patterns, Pattern{exp, include})
  175. } else if strings.HasPrefix(line, "**/") {
  176. // Add the pattern as is, and without **/ so it matches in current dir
  177. exp, err := fnmatch.Convert(line, fnmatch.FNM_PATHNAME)
  178. if err != nil {
  179. return fmt.Errorf("Invalid pattern %q in ignore file", line)
  180. }
  181. patterns = append(patterns, Pattern{exp, include})
  182. exp, err = fnmatch.Convert(line[3:], fnmatch.FNM_PATHNAME)
  183. if err != nil {
  184. return fmt.Errorf("Invalid pattern %q in ignore file", line)
  185. }
  186. patterns = append(patterns, Pattern{exp, include})
  187. } else if strings.HasPrefix(line, "#include ") {
  188. includeFile := filepath.Join(filepath.Dir(currentFile), line[len("#include "):])
  189. includes, err := loadIgnoreFile(includeFile, seen)
  190. if err != nil {
  191. return err
  192. }
  193. patterns = append(patterns, includes...)
  194. } else {
  195. // Path name or pattern, add it so it matches files both in
  196. // current directory and subdirs.
  197. exp, err := fnmatch.Convert(line, fnmatch.FNM_PATHNAME)
  198. if err != nil {
  199. return fmt.Errorf("Invalid pattern %q in ignore file", line)
  200. }
  201. patterns = append(patterns, Pattern{exp, include})
  202. exp, err = fnmatch.Convert("**/"+line, fnmatch.FNM_PATHNAME)
  203. if err != nil {
  204. return fmt.Errorf("Invalid pattern %q in ignore file", line)
  205. }
  206. patterns = append(patterns, Pattern{exp, include})
  207. }
  208. return nil
  209. }
  210. scanner := bufio.NewScanner(fd)
  211. var err error
  212. for scanner.Scan() {
  213. line := strings.TrimSpace(scanner.Text())
  214. switch {
  215. case line == "":
  216. continue
  217. case strings.HasPrefix(line, "//"):
  218. continue
  219. case strings.HasPrefix(line, "#"):
  220. err = addPattern(line)
  221. case strings.HasSuffix(line, "/**"):
  222. err = addPattern(line)
  223. case strings.HasSuffix(line, "/"):
  224. err = addPattern(line)
  225. if err == nil {
  226. err = addPattern(line + "**")
  227. }
  228. default:
  229. err = addPattern(line)
  230. if err == nil {
  231. err = addPattern(line + "/**")
  232. }
  233. }
  234. if err != nil {
  235. return nil, err
  236. }
  237. }
  238. return patterns, nil
  239. }