ignore.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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 https://mozilla.org/MPL/2.0/.
  6. package ignore
  7. import (
  8. "bufio"
  9. "bytes"
  10. "errors"
  11. "fmt"
  12. "io"
  13. "path/filepath"
  14. "strings"
  15. "time"
  16. "github.com/gobwas/glob"
  17. "github.com/syncthing/syncthing/lib/build"
  18. "github.com/syncthing/syncthing/lib/fs"
  19. "github.com/syncthing/syncthing/lib/osutil"
  20. "github.com/syncthing/syncthing/lib/sha256"
  21. "github.com/syncthing/syncthing/lib/sync"
  22. )
  23. const (
  24. resultNotMatched Result = 0
  25. resultInclude Result = 1 << iota
  26. resultDeletable = 1 << iota
  27. resultFoldCase = 1 << iota
  28. )
  29. var defaultResult Result = resultInclude
  30. func init() {
  31. if build.IsDarwin || build.IsWindows {
  32. defaultResult |= resultFoldCase
  33. }
  34. }
  35. // A ParseError signifies an error with contents of an ignore file,
  36. // including I/O errors on included files. An I/O error on the root level
  37. // ignore file is not a ParseError.
  38. type ParseError struct {
  39. inner error
  40. }
  41. func (e *ParseError) Error() string {
  42. return fmt.Sprintf("parse error: %v", e.inner)
  43. }
  44. func (e *ParseError) Unwrap() error {
  45. return e.inner
  46. }
  47. func IsParseError(err error) bool {
  48. var e *ParseError
  49. return errors.As(err, &e)
  50. }
  51. func parseError(err error) error {
  52. if err == nil {
  53. return nil
  54. }
  55. return &ParseError{err}
  56. }
  57. type Pattern struct {
  58. pattern string
  59. match glob.Glob
  60. result Result
  61. }
  62. func (p Pattern) String() string {
  63. ret := p.pattern
  64. if p.result&resultInclude != resultInclude {
  65. ret = "!" + ret
  66. }
  67. if p.result&resultFoldCase == resultFoldCase {
  68. ret = "(?i)" + ret
  69. }
  70. if p.result&resultDeletable == resultDeletable {
  71. ret = "(?d)" + ret
  72. }
  73. return ret
  74. }
  75. func (p Pattern) allowsSkippingIgnoredDirs() bool {
  76. if p.result.IsIgnored() {
  77. return true
  78. }
  79. if p.pattern[0] != '/' {
  80. return false
  81. }
  82. if strings.Contains(p.pattern[1:], "/") {
  83. return false
  84. }
  85. // Double asterisk everywhere in the path except at the end is bad
  86. return !strings.Contains(strings.TrimSuffix(p.pattern, "**"), "**")
  87. }
  88. type Result uint8
  89. func (r Result) IsIgnored() bool {
  90. return r&resultInclude == resultInclude
  91. }
  92. func (r Result) IsDeletable() bool {
  93. return r.IsIgnored() && r&resultDeletable == resultDeletable
  94. }
  95. func (r Result) IsCaseFolded() bool {
  96. return r&resultFoldCase == resultFoldCase
  97. }
  98. // The ChangeDetector is responsible for determining if files have changed
  99. // on disk. It gets told to Remember() files (name and modtime) and will
  100. // then get asked if a file has been Seen() (i.e., Remember() has been
  101. // called on it) and if any of the files have Changed(). To forget all
  102. // files, call Reset().
  103. type ChangeDetector interface {
  104. Remember(fs fs.Filesystem, name string, modtime time.Time)
  105. Seen(fs fs.Filesystem, name string) bool
  106. Changed() bool
  107. Reset()
  108. }
  109. type Matcher struct {
  110. fs fs.Filesystem
  111. lines []string // exact lines read from .stignore
  112. patterns []Pattern // patterns including those from included files
  113. withCache bool
  114. matches *cache
  115. curHash string
  116. stop chan struct{}
  117. changeDetector ChangeDetector
  118. skipIgnoredDirs bool
  119. mut sync.Mutex
  120. }
  121. // An Option can be passed to New()
  122. type Option func(*Matcher)
  123. // WithCache enables or disables lookup caching. The default is disabled.
  124. func WithCache(v bool) Option {
  125. return func(m *Matcher) {
  126. m.withCache = v
  127. }
  128. }
  129. // WithChangeDetector sets a custom ChangeDetector. The default is to simply
  130. // use the on disk modtime for comparison.
  131. func WithChangeDetector(cd ChangeDetector) Option {
  132. return func(m *Matcher) {
  133. m.changeDetector = cd
  134. }
  135. }
  136. func New(fs fs.Filesystem, opts ...Option) *Matcher {
  137. m := &Matcher{
  138. fs: fs,
  139. stop: make(chan struct{}),
  140. mut: sync.NewMutex(),
  141. skipIgnoredDirs: true,
  142. }
  143. for _, opt := range opts {
  144. opt(m)
  145. }
  146. if m.changeDetector == nil {
  147. m.changeDetector = newModtimeChecker()
  148. }
  149. if m.withCache {
  150. go m.clean(2 * time.Hour)
  151. }
  152. return m
  153. }
  154. // Load and parse a file. The returned error may be of type *ParseError in
  155. // which case a file was loaded from disk but the patterns could not be
  156. // parsed. In this case the contents of the file are nonetheless available
  157. // in the Lines() method.
  158. func (m *Matcher) Load(file string) error {
  159. m.mut.Lock()
  160. defer m.mut.Unlock()
  161. if m.changeDetector.Seen(m.fs, file) && !m.changeDetector.Changed() {
  162. return nil
  163. }
  164. fd, info, err := loadIgnoreFile(m.fs, file)
  165. if err != nil {
  166. m.parseLocked(&bytes.Buffer{}, file)
  167. return err
  168. }
  169. defer fd.Close()
  170. m.changeDetector.Reset()
  171. err = m.parseLocked(fd, file)
  172. // If we failed to parse, don't cache, as next time Load is called
  173. // we'll pretend it's all good.
  174. if err == nil {
  175. m.changeDetector.Remember(m.fs, file, info.ModTime())
  176. }
  177. return err
  178. }
  179. // Load and parse an io.Reader. See Load() for notes on the returned error.
  180. func (m *Matcher) Parse(r io.Reader, file string) error {
  181. m.mut.Lock()
  182. defer m.mut.Unlock()
  183. return m.parseLocked(r, file)
  184. }
  185. func (m *Matcher) parseLocked(r io.Reader, file string) error {
  186. lines, patterns, err := parseIgnoreFile(m.fs, r, file, m.changeDetector, make(map[string]struct{}))
  187. // Error is saved and returned at the end. We process the patterns
  188. // (possibly blank) anyway.
  189. m.lines = lines
  190. newHash := hashPatterns(patterns)
  191. if newHash == m.curHash {
  192. // We've already loaded exactly these patterns.
  193. return err
  194. }
  195. m.skipIgnoredDirs = true
  196. var previous string
  197. for _, p := range patterns {
  198. // We automatically add patterns with a /** suffix, which normally
  199. // means that we cannot skip directories. However if the same
  200. // pattern without the /** already exists (which is true for
  201. // automatically added patterns) we can skip.
  202. if l := len(p.pattern); l > 3 && p.pattern[:len(p.pattern)-3] == previous {
  203. continue
  204. }
  205. if !p.allowsSkippingIgnoredDirs() {
  206. m.skipIgnoredDirs = false
  207. break
  208. }
  209. previous = p.pattern
  210. }
  211. m.curHash = newHash
  212. m.patterns = patterns
  213. if m.withCache {
  214. m.matches = newCache(patterns)
  215. }
  216. return err
  217. }
  218. func (m *Matcher) Match(file string) (result Result) {
  219. if file == "." {
  220. return resultNotMatched
  221. }
  222. m.mut.Lock()
  223. defer m.mut.Unlock()
  224. if len(m.patterns) == 0 {
  225. return resultNotMatched
  226. }
  227. if m.matches != nil {
  228. // Check the cache for a known result.
  229. res, ok := m.matches.get(file)
  230. if ok {
  231. return res
  232. }
  233. // Update the cache with the result at return time
  234. defer func() {
  235. m.matches.set(file, result)
  236. }()
  237. }
  238. // Check all the patterns for a match.
  239. file = filepath.ToSlash(file)
  240. var lowercaseFile string
  241. for _, pattern := range m.patterns {
  242. if pattern.result.IsCaseFolded() {
  243. if lowercaseFile == "" {
  244. lowercaseFile = strings.ToLower(file)
  245. }
  246. if pattern.match.Match(lowercaseFile) {
  247. return pattern.result
  248. }
  249. } else if pattern.match.Match(file) {
  250. return pattern.result
  251. }
  252. }
  253. // Default to not matching.
  254. return resultNotMatched
  255. }
  256. // Lines return a list of the unprocessed lines in .stignore at last load
  257. func (m *Matcher) Lines() []string {
  258. m.mut.Lock()
  259. defer m.mut.Unlock()
  260. return m.lines
  261. }
  262. // Patterns return a list of the loaded patterns, as they've been parsed
  263. func (m *Matcher) Patterns() []string {
  264. m.mut.Lock()
  265. defer m.mut.Unlock()
  266. patterns := make([]string, len(m.patterns))
  267. for i, pat := range m.patterns {
  268. patterns[i] = pat.String()
  269. }
  270. return patterns
  271. }
  272. func (m *Matcher) String() string {
  273. return fmt.Sprintf("Matcher/%v@%p", m.Patterns(), m)
  274. }
  275. func (m *Matcher) Hash() string {
  276. m.mut.Lock()
  277. defer m.mut.Unlock()
  278. return m.curHash
  279. }
  280. func (m *Matcher) Stop() {
  281. close(m.stop)
  282. }
  283. func (m *Matcher) clean(d time.Duration) {
  284. t := time.NewTimer(d / 2)
  285. for {
  286. select {
  287. case <-m.stop:
  288. return
  289. case <-t.C:
  290. m.mut.Lock()
  291. if m.matches != nil {
  292. m.matches.clean(d)
  293. }
  294. t.Reset(d / 2)
  295. m.mut.Unlock()
  296. }
  297. }
  298. }
  299. // ShouldIgnore returns true when a file is temporary, internal or ignored
  300. func (m *Matcher) ShouldIgnore(filename string) bool {
  301. switch {
  302. case fs.IsTemporary(filename):
  303. return true
  304. case fs.IsInternal(filename):
  305. return true
  306. case m.Match(filename).IsIgnored():
  307. return true
  308. }
  309. return false
  310. }
  311. func (m *Matcher) SkipIgnoredDirs() bool {
  312. m.mut.Lock()
  313. defer m.mut.Unlock()
  314. return m.skipIgnoredDirs
  315. }
  316. func hashPatterns(patterns []Pattern) string {
  317. h := sha256.New()
  318. for _, pat := range patterns {
  319. h.Write([]byte(pat.String()))
  320. h.Write([]byte("\n"))
  321. }
  322. return fmt.Sprintf("%x", h.Sum(nil))
  323. }
  324. func loadIgnoreFile(fs fs.Filesystem, file string) (fs.File, fs.FileInfo, error) {
  325. fd, err := fs.Open(file)
  326. if err != nil {
  327. return fd, nil, err
  328. }
  329. info, err := fd.Stat()
  330. if err != nil {
  331. fd.Close()
  332. }
  333. return fd, info, err
  334. }
  335. func loadParseIncludeFile(filesystem fs.Filesystem, file string, cd ChangeDetector, linesSeen map[string]struct{}) ([]Pattern, error) {
  336. // Allow escaping the folders filesystem.
  337. // TODO: Deprecate, somehow?
  338. if filesystem.Type() == fs.FilesystemTypeBasic {
  339. uri := filesystem.URI()
  340. joined := filepath.Join(uri, file)
  341. if !fs.IsParent(joined, uri) {
  342. filesystem = fs.NewFilesystem(filesystem.Type(), filepath.Dir(joined))
  343. file = filepath.Base(joined)
  344. }
  345. }
  346. if cd.Seen(filesystem, file) {
  347. return nil, parseError(fmt.Errorf("multiple include of ignore file %q", file))
  348. }
  349. fd, info, err := loadIgnoreFile(filesystem, file)
  350. if err != nil {
  351. return nil, err
  352. }
  353. defer fd.Close()
  354. cd.Remember(filesystem, file, info.ModTime())
  355. _, patterns, err := parseIgnoreFile(filesystem, fd, file, cd, linesSeen)
  356. return patterns, err
  357. }
  358. func parseLine(line string) ([]Pattern, error) {
  359. pattern := Pattern{
  360. result: defaultResult,
  361. }
  362. // Allow prefixes to be specified in any order, but only once.
  363. var seenPrefix [3]bool
  364. for {
  365. if strings.HasPrefix(line, "!") && !seenPrefix[0] {
  366. seenPrefix[0] = true
  367. line = line[1:]
  368. pattern.result ^= resultInclude
  369. } else if strings.HasPrefix(line, "(?i)") && !seenPrefix[1] {
  370. seenPrefix[1] = true
  371. pattern.result |= resultFoldCase
  372. line = line[4:]
  373. } else if strings.HasPrefix(line, "(?d)") && !seenPrefix[2] {
  374. seenPrefix[2] = true
  375. pattern.result |= resultDeletable
  376. line = line[4:]
  377. } else {
  378. break
  379. }
  380. }
  381. if line == "" {
  382. return nil, parseError(errors.New("missing pattern"))
  383. }
  384. if pattern.result.IsCaseFolded() {
  385. line = strings.ToLower(line)
  386. }
  387. pattern.pattern = line
  388. var err error
  389. if strings.HasPrefix(line, "/") {
  390. // Pattern is rooted in the current dir only
  391. pattern.match, err = glob.Compile(line[1:], '/')
  392. return []Pattern{pattern}, parseError(err)
  393. }
  394. patterns := make([]Pattern, 2)
  395. if strings.HasPrefix(line, "**/") {
  396. // Add the pattern as is, and without **/ so it matches in current dir
  397. pattern.match, err = glob.Compile(line, '/')
  398. if err != nil {
  399. return nil, parseError(err)
  400. }
  401. patterns[0] = pattern
  402. line = line[3:]
  403. pattern.pattern = line
  404. pattern.match, err = glob.Compile(line, '/')
  405. if err != nil {
  406. return nil, parseError(err)
  407. }
  408. patterns[1] = pattern
  409. return patterns, nil
  410. }
  411. // Path name or pattern, add it so it matches files both in
  412. // current directory and subdirs.
  413. pattern.match, err = glob.Compile(line, '/')
  414. if err != nil {
  415. return nil, parseError(err)
  416. }
  417. patterns[0] = pattern
  418. line = "**/" + line
  419. pattern.pattern = line
  420. pattern.match, err = glob.Compile(line, '/')
  421. if err != nil {
  422. return nil, parseError(err)
  423. }
  424. patterns[1] = pattern
  425. return patterns, nil
  426. }
  427. func parseIgnoreFile(fs fs.Filesystem, fd io.Reader, currentFile string, cd ChangeDetector, linesSeen map[string]struct{}) ([]string, []Pattern, error) {
  428. var patterns []Pattern
  429. addPattern := func(line string) error {
  430. newPatterns, err := parseLine(line)
  431. if err != nil {
  432. return fmt.Errorf("invalid pattern %q in ignore file: %w", line, err)
  433. }
  434. patterns = append(patterns, newPatterns...)
  435. return nil
  436. }
  437. scanner := bufio.NewScanner(fd)
  438. var lines []string
  439. for scanner.Scan() {
  440. line := strings.TrimSpace(scanner.Text())
  441. lines = append(lines, line)
  442. }
  443. if err := scanner.Err(); err != nil {
  444. return nil, nil, err
  445. }
  446. var err error
  447. for _, line := range lines {
  448. if _, ok := linesSeen[line]; ok {
  449. continue
  450. }
  451. linesSeen[line] = struct{}{}
  452. switch {
  453. case line == "":
  454. continue
  455. case strings.HasPrefix(line, "//"):
  456. continue
  457. }
  458. line = filepath.ToSlash(line)
  459. switch {
  460. case strings.HasPrefix(line, "#include"):
  461. fields := strings.SplitN(line, " ", 2)
  462. if len(fields) != 2 {
  463. err = parseError(errors.New("failed to parse #include line: no file?"))
  464. break
  465. }
  466. includeRel := strings.TrimSpace(fields[1])
  467. if includeRel == "" {
  468. err = parseError(errors.New("failed to parse #include line: no file?"))
  469. break
  470. }
  471. includeFile := filepath.Join(filepath.Dir(currentFile), includeRel)
  472. var includePatterns []Pattern
  473. if includePatterns, err = loadParseIncludeFile(fs, includeFile, cd, linesSeen); err == nil {
  474. patterns = append(patterns, includePatterns...)
  475. } else {
  476. // Wrap the error, as if the include does not exist, we get a
  477. // IsNotExists(err) == true error, which we use to check
  478. // existence of the .stignore file, and just end up assuming
  479. // there is none, rather than a broken include.
  480. err = parseError(fmt.Errorf("failed to load include file %s: %w", includeFile, err))
  481. }
  482. case strings.HasSuffix(line, "/**"):
  483. err = addPattern(line)
  484. case strings.HasSuffix(line, "/"):
  485. err = addPattern(line + "**")
  486. default:
  487. err = addPattern(line)
  488. if err == nil {
  489. err = addPattern(line + "/**")
  490. }
  491. }
  492. if err != nil {
  493. return lines, nil, err
  494. }
  495. }
  496. return lines, patterns, nil
  497. }
  498. // WriteIgnores is a convenience function to avoid code duplication
  499. func WriteIgnores(filesystem fs.Filesystem, path string, content []string) error {
  500. if len(content) == 0 {
  501. err := filesystem.Remove(path)
  502. if fs.IsNotExist(err) {
  503. return nil
  504. }
  505. return err
  506. }
  507. fd, err := osutil.CreateAtomicFilesystem(filesystem, path)
  508. if err != nil {
  509. return err
  510. }
  511. wr := osutil.LineEndingsWriter(fd)
  512. for _, line := range content {
  513. fmt.Fprintln(wr, line)
  514. }
  515. if err := fd.Close(); err != nil {
  516. return err
  517. }
  518. filesystem.Hide(path)
  519. return nil
  520. }
  521. type modtimeCheckerKey struct {
  522. fs fs.Filesystem
  523. name string
  524. }
  525. // modtimeChecker is the default implementation of ChangeDetector
  526. type modtimeChecker struct {
  527. modtimes map[modtimeCheckerKey]time.Time
  528. }
  529. func newModtimeChecker() *modtimeChecker {
  530. return &modtimeChecker{
  531. modtimes: map[modtimeCheckerKey]time.Time{},
  532. }
  533. }
  534. func (c *modtimeChecker) Remember(fs fs.Filesystem, name string, modtime time.Time) {
  535. c.modtimes[modtimeCheckerKey{fs, name}] = modtime
  536. }
  537. func (c *modtimeChecker) Seen(fs fs.Filesystem, name string) bool {
  538. _, ok := c.modtimes[modtimeCheckerKey{fs, name}]
  539. return ok
  540. }
  541. func (c *modtimeChecker) Reset() {
  542. c.modtimes = map[modtimeCheckerKey]time.Time{}
  543. }
  544. func (c *modtimeChecker) Changed() bool {
  545. for key, modtime := range c.modtimes {
  546. info, err := key.fs.Stat(key.name)
  547. if err != nil {
  548. return true
  549. }
  550. if !info.ModTime().Equal(modtime) {
  551. return true
  552. }
  553. }
  554. return false
  555. }