ignore.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. "crypto/sha256"
  11. "errors"
  12. "fmt"
  13. "io"
  14. "os"
  15. "path/filepath"
  16. "strings"
  17. "time"
  18. "unicode/utf8"
  19. "github.com/gobwas/glob"
  20. "golang.org/x/text/unicode/norm"
  21. "github.com/syncthing/syncthing/lib/build"
  22. "github.com/syncthing/syncthing/lib/fs"
  23. "github.com/syncthing/syncthing/lib/ignore/ignoreresult"
  24. "github.com/syncthing/syncthing/lib/osutil"
  25. "github.com/syncthing/syncthing/lib/sync"
  26. )
  27. const escapePrefix = "#escape"
  28. var defaultEscapeChar = '\\'
  29. func init() {
  30. if os.PathSeparator == defaultEscapeChar {
  31. // The pipe character (|) is not allowed in filenames on Windows
  32. defaultEscapeChar = '|'
  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 ignoreresult.R
  61. }
  62. func (p Pattern) String() string {
  63. ret := p.pattern
  64. if !p.result.IsIgnored() {
  65. ret = "!" + ret
  66. }
  67. if p.result.IsCaseFolded() {
  68. ret = "(?i)" + ret
  69. }
  70. if p.result.IsDeletable() {
  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. // A "/**" at the end is allowed and doesn't have any bearing on the
  83. // below checks; remove it before checking.
  84. pattern := strings.TrimSuffix(p.pattern, "/**")
  85. if len(pattern) == 0 {
  86. return true
  87. }
  88. if strings.Contains(pattern[1:], "/") {
  89. return false
  90. }
  91. // Double asterisk everywhere in the path except at the end is bad
  92. return !strings.Contains(strings.TrimSuffix(pattern, "**"), "**")
  93. }
  94. // The ChangeDetector is responsible for determining if files have changed
  95. // on disk. It gets told to Remember() files (name and modtime) and will
  96. // then get asked if a file has been Seen() (i.e., Remember() has been
  97. // called on it) and if any of the files have Changed(). To forget all
  98. // files, call Reset().
  99. type ChangeDetector interface {
  100. Remember(fs fs.Filesystem, name string, modtime time.Time)
  101. Seen(fs fs.Filesystem, name string) bool
  102. Changed() bool
  103. Reset()
  104. }
  105. type Matcher struct {
  106. fs fs.Filesystem
  107. lines []string // exact lines read from .stignore
  108. patterns []Pattern // patterns including those from included files
  109. withCache bool
  110. matches *cache
  111. curHash string
  112. stop chan struct{}
  113. changeDetector ChangeDetector
  114. mut sync.Mutex
  115. }
  116. // An Option can be passed to New()
  117. type Option func(*Matcher)
  118. // WithCache enables or disables lookup caching. The default is disabled.
  119. func WithCache(v bool) Option {
  120. return func(m *Matcher) {
  121. m.withCache = v
  122. }
  123. }
  124. // WithChangeDetector sets a custom ChangeDetector. The default is to simply
  125. // use the on disk modtime for comparison.
  126. func WithChangeDetector(cd ChangeDetector) Option {
  127. return func(m *Matcher) {
  128. m.changeDetector = cd
  129. }
  130. }
  131. func New(fs fs.Filesystem, opts ...Option) *Matcher {
  132. m := &Matcher{
  133. fs: fs,
  134. stop: make(chan struct{}),
  135. mut: sync.NewMutex(),
  136. }
  137. for _, opt := range opts {
  138. opt(m)
  139. }
  140. if m.changeDetector == nil {
  141. m.changeDetector = newModtimeChecker()
  142. }
  143. if m.withCache {
  144. go m.clean(2 * time.Hour)
  145. }
  146. return m
  147. }
  148. // Load and parse a file. The returned error may be of type *ParseError in
  149. // which case a file was loaded from disk but the patterns could not be
  150. // parsed. In this case the contents of the file are nonetheless available
  151. // in the Lines() method.
  152. func (m *Matcher) Load(file string) error {
  153. m.mut.Lock()
  154. defer m.mut.Unlock()
  155. if m.changeDetector.Seen(m.fs, file) && !m.changeDetector.Changed() {
  156. return nil
  157. }
  158. fd, info, err := loadIgnoreFile(m.fs, file)
  159. if err != nil {
  160. m.parseLocked(&bytes.Buffer{}, file)
  161. return err
  162. }
  163. defer fd.Close()
  164. m.changeDetector.Reset()
  165. err = m.parseLocked(fd, file)
  166. // If we failed to parse, don't cache, as next time Load is called
  167. // we'll pretend it's all good.
  168. if err == nil {
  169. m.changeDetector.Remember(m.fs, file, info.ModTime())
  170. }
  171. return err
  172. }
  173. // Load and parse an io.Reader. See Load() for notes on the returned error.
  174. func (m *Matcher) Parse(r io.Reader, file string) error {
  175. m.mut.Lock()
  176. defer m.mut.Unlock()
  177. return m.parseLocked(r, file)
  178. }
  179. func (m *Matcher) parseLocked(r io.Reader, file string) error {
  180. lines, patterns, err := parseIgnoreFile(m.fs, r, file, m.changeDetector, make(map[string]struct{}))
  181. // Error is saved and returned at the end. We process the patterns
  182. // (possibly blank) anyway.
  183. m.lines = lines
  184. newHash := hashPatterns(patterns)
  185. if newHash == m.curHash {
  186. // We've already loaded exactly these patterns.
  187. return err
  188. }
  189. m.curHash = newHash
  190. m.patterns = patterns
  191. if m.withCache {
  192. m.matches = newCache()
  193. }
  194. return err
  195. }
  196. // Match matches the patterns plus temporary and internal files.
  197. //
  198. // The "file" parameter must be in the OS' native unicode format (NFD on macos,
  199. // NFC everywhere else). This is always the case in real usage in syncthing, as
  200. // we ensure native unicode normalisation on all entry points (scanning and from
  201. // protocol) - so no need to normalize when calling this, except e.g. in tests.
  202. func (m *Matcher) Match(file string) (result ignoreresult.R) {
  203. switch {
  204. case fs.IsTemporary(file):
  205. return ignoreresult.IgnoreAndSkip
  206. case fs.IsInternal(file):
  207. return ignoreresult.IgnoreAndSkip
  208. case file == ".":
  209. return ignoreresult.NotIgnored
  210. }
  211. m.mut.Lock()
  212. defer m.mut.Unlock()
  213. if len(m.patterns) == 0 {
  214. return ignoreresult.NotIgnored
  215. }
  216. // Change backslashes to slashes (on Windows only)
  217. file = filepath.ToSlash(file)
  218. if m.matches != nil {
  219. // Check the cache for a known result.
  220. res, ok := m.matches.get(file)
  221. if ok {
  222. return res
  223. }
  224. // Update the cache with the result at return time
  225. defer func() {
  226. m.matches.set(file, result)
  227. }()
  228. }
  229. // Check all the patterns for a match. Track whether the patterns so far
  230. // allow skipping matched directories or not. As soon as we hit an
  231. // exclude pattern (with some exceptions), we can't skip directories
  232. // anymore.
  233. var lowercaseFile string
  234. canSkipDir := true
  235. for _, pattern := range m.patterns {
  236. if canSkipDir && !pattern.allowsSkippingIgnoredDirs() {
  237. canSkipDir = false
  238. }
  239. res := pattern.result
  240. if canSkipDir {
  241. res = res.WithSkipDir()
  242. }
  243. if pattern.result.IsCaseFolded() {
  244. if lowercaseFile == "" {
  245. lowercaseFile = strings.ToLower(file)
  246. }
  247. if pattern.match.Match(lowercaseFile) {
  248. return res
  249. }
  250. } else if pattern.match.Match(file) {
  251. return res
  252. }
  253. }
  254. // Default to not matching.
  255. return ignoreresult.NotIgnored
  256. }
  257. // Lines return a list of the unprocessed lines in .stignore at last load
  258. func (m *Matcher) Lines() []string {
  259. m.mut.Lock()
  260. defer m.mut.Unlock()
  261. return m.lines
  262. }
  263. // Patterns return a list of the loaded patterns, as they've been parsed
  264. func (m *Matcher) Patterns() []string {
  265. m.mut.Lock()
  266. defer m.mut.Unlock()
  267. patterns := make([]string, len(m.patterns))
  268. for i, pat := range m.patterns {
  269. patterns[i] = pat.String()
  270. }
  271. return patterns
  272. }
  273. func (m *Matcher) String() string {
  274. return fmt.Sprintf("Matcher/%v@%p", m.Patterns(), m)
  275. }
  276. func (m *Matcher) Hash() string {
  277. m.mut.Lock()
  278. defer m.mut.Unlock()
  279. return m.curHash
  280. }
  281. func (m *Matcher) Stop() {
  282. close(m.stop)
  283. }
  284. func (m *Matcher) clean(d time.Duration) {
  285. t := time.NewTimer(d / 2)
  286. for {
  287. select {
  288. case <-m.stop:
  289. return
  290. case <-t.C:
  291. m.mut.Lock()
  292. if m.matches != nil {
  293. m.matches.clean(d)
  294. }
  295. t.Reset(d / 2)
  296. m.mut.Unlock()
  297. }
  298. }
  299. }
  300. func hashPatterns(patterns []Pattern) string {
  301. h := sha256.New()
  302. for _, pat := range patterns {
  303. h.Write([]byte(pat.String()))
  304. h.Write([]byte("\n"))
  305. }
  306. return fmt.Sprintf("%x", h.Sum(nil))
  307. }
  308. func loadIgnoreFile(fs fs.Filesystem, file string) (fs.File, fs.FileInfo, error) {
  309. fd, err := fs.Open(file)
  310. if err != nil {
  311. return fd, nil, err
  312. }
  313. info, err := fd.Stat()
  314. if err != nil {
  315. fd.Close()
  316. }
  317. return fd, info, err
  318. }
  319. func loadParseIncludeFile(filesystem fs.Filesystem, file string, cd ChangeDetector, linesSeen map[string]struct{}) ([]Pattern, error) {
  320. // Allow escaping the folders filesystem.
  321. // TODO: Deprecate, somehow?
  322. if filesystem.Type() == fs.FilesystemTypeBasic {
  323. uri := filesystem.URI()
  324. joined := filepath.Join(uri, file)
  325. if !fs.IsParent(joined, uri) {
  326. filesystem = fs.NewFilesystem(filesystem.Type(), filepath.Dir(joined))
  327. file = filepath.Base(joined)
  328. }
  329. }
  330. if cd.Seen(filesystem, file) {
  331. return nil, errors.New("multiple include")
  332. }
  333. fd, info, err := loadIgnoreFile(filesystem, file)
  334. if err != nil {
  335. // isNotExist is considered "ok" in a sense of that a folder doesn't have to act
  336. // upon it. This is because it is allowed for .stignore to not exist. However,
  337. // included ignore files are not allowed to be missing and these errors should be
  338. // acted upon on. So we don't preserve the error chain here and manually set an
  339. // error instead, if the file is missing.
  340. if fs.IsNotExist(err) {
  341. err = errors.New("file not found")
  342. }
  343. return nil, err
  344. }
  345. defer fd.Close()
  346. cd.Remember(filesystem, file, info.ModTime())
  347. _, patterns, err := parseIgnoreFile(filesystem, fd, file, cd, linesSeen)
  348. return patterns, err
  349. }
  350. func parseLine(line string) ([]Pattern, error) {
  351. // We use native normalization internally, thus the patterns must match
  352. // that to avoid false negative matches.
  353. line = nativeUnicodeNorm(line)
  354. pattern := Pattern{
  355. result: ignoreresult.Ignored,
  356. }
  357. // Allow prefixes to be specified in any order, but only once.
  358. var seenPrefix [3]bool
  359. for {
  360. if strings.HasPrefix(line, "!") && !seenPrefix[0] {
  361. seenPrefix[0] = true
  362. line = line[1:]
  363. pattern.result = pattern.result.ToggleIgnored()
  364. } else if strings.HasPrefix(line, "(?i)") && !seenPrefix[1] {
  365. seenPrefix[1] = true
  366. pattern.result = pattern.result.WithFoldCase()
  367. line = line[4:]
  368. } else if strings.HasPrefix(line, "(?d)") && !seenPrefix[2] {
  369. seenPrefix[2] = true
  370. pattern.result = pattern.result.WithDeletable()
  371. line = line[4:]
  372. } else {
  373. break
  374. }
  375. }
  376. if line == "" {
  377. return nil, parseError(errors.New("missing pattern"))
  378. }
  379. if pattern.result.IsCaseFolded() {
  380. line = strings.ToLower(line)
  381. }
  382. pattern.pattern = line
  383. var err error
  384. if strings.HasPrefix(line, "/") {
  385. // Pattern is rooted in the current dir only
  386. pattern.match, err = glob.Compile(line[1:], '/')
  387. return []Pattern{pattern}, parseError(err)
  388. }
  389. patterns := make([]Pattern, 2)
  390. if strings.HasPrefix(line, "**/") {
  391. // Add the pattern as is, and without **/ so it matches in current dir
  392. pattern.match, err = glob.Compile(line, '/')
  393. if err != nil {
  394. return nil, parseError(err)
  395. }
  396. patterns[0] = pattern
  397. line = line[3:]
  398. pattern.pattern = line
  399. pattern.match, err = glob.Compile(line, '/')
  400. if err != nil {
  401. return nil, parseError(err)
  402. }
  403. patterns[1] = pattern
  404. return patterns, nil
  405. }
  406. // Path name or pattern, add it so it matches files both in
  407. // current directory and subdirs.
  408. pattern.match, err = glob.Compile(line, '/')
  409. if err != nil {
  410. return nil, parseError(err)
  411. }
  412. patterns[0] = pattern
  413. line = "**/" + line
  414. pattern.pattern = line
  415. pattern.match, err = glob.Compile(line, '/')
  416. if err != nil {
  417. return nil, parseError(err)
  418. }
  419. patterns[1] = pattern
  420. return patterns, nil
  421. }
  422. func nativeUnicodeNorm(s string) string {
  423. if build.IsDarwin || build.IsIOS {
  424. return norm.NFD.String(s)
  425. }
  426. return norm.NFC.String(s)
  427. }
  428. func parseIgnoreFile(fs fs.Filesystem, fd io.Reader, currentFile string, cd ChangeDetector, linesSeen map[string]struct{}) ([]string, []Pattern, error) {
  429. var patterns []Pattern
  430. addPattern := func(line string) error {
  431. newPatterns, err := parseLine(line)
  432. if err != nil {
  433. return fmt.Errorf("invalid pattern %q in ignore file: %w", line, err)
  434. }
  435. patterns = append(patterns, newPatterns...)
  436. return nil
  437. }
  438. scanner := bufio.NewScanner(fd)
  439. var lines []string
  440. for scanner.Scan() {
  441. line := strings.TrimSpace(scanner.Text())
  442. lines = append(lines, line)
  443. }
  444. if err := scanner.Err(); err != nil {
  445. return nil, nil, err
  446. }
  447. escapeChar := defaultEscapeChar
  448. var err error
  449. escapePrefixSeen := false
  450. includedPatterns := 0
  451. for _, line := range lines {
  452. if strings.HasPrefix(line, escapePrefix) {
  453. if escapePrefixSeen {
  454. return nil, nil, errors.New("mutiple #escape= lines found in ignore file")
  455. }
  456. if len(patterns)-includedPatterns > 0 {
  457. return nil, nil, errors.New("#escape= line found after patterns in ignore file")
  458. }
  459. escapePrefixSeen = true
  460. trimmed := strings.TrimSpace(strings.TrimPrefix(line, escapePrefix))
  461. before, esc, ok := strings.Cut(trimmed, "=")
  462. if ok && before == "" {
  463. esc = strings.TrimSpace(esc)
  464. // avoids allocation of a new slice.
  465. if utf8.RuneCountInString(esc) == 1 {
  466. escapeChar, _ = utf8.DecodeRuneInString(esc)
  467. continue
  468. }
  469. }
  470. return nil, nil, fmt.Errorf("failed to parse #escape= line in ignore file: %q", line)
  471. }
  472. if _, ok := linesSeen[line]; ok {
  473. continue
  474. }
  475. linesSeen[line] = struct{}{}
  476. switch {
  477. case line == "":
  478. continue
  479. case strings.HasPrefix(line, "//"):
  480. continue
  481. }
  482. if escapeChar != '\\' {
  483. // ToSlash changes backslashes to forward slashes on Windows only,
  484. // so we only need to do this, if escapeChar is not a backslash.
  485. // If escapeChar is a backslash, then the user is using forward
  486. // slashes for path separators, and we leave backslashes alone.
  487. line = filepath.ToSlash(line)
  488. // Replace all escapeChars with backslashes
  489. line = strings.ReplaceAll(line, string(escapeChar), `\`)
  490. // Now restore double escapeChars to actually escape the escapeChar.
  491. line = strings.ReplaceAll(line, `\\`, `\`+string(escapeChar))
  492. }
  493. switch {
  494. case strings.HasPrefix(line, "#include"):
  495. fields := strings.SplitN(line, " ", 2)
  496. if len(fields) != 2 {
  497. err = parseError(errors.New("failed to parse #include line: no file?"))
  498. break
  499. }
  500. includeRel := strings.TrimSpace(fields[1])
  501. if includeRel == "" {
  502. err = parseError(errors.New("failed to parse #include line: no file?"))
  503. break
  504. }
  505. includeFile := filepath.Join(filepath.Dir(currentFile), includeRel)
  506. var includePatterns []Pattern
  507. if includePatterns, err = loadParseIncludeFile(fs, includeFile, cd, linesSeen); err == nil {
  508. patterns = append(patterns, includePatterns...)
  509. includedPatterns += len(includePatterns)
  510. } else {
  511. // Wrap the error, as if the include does not exist, we get a
  512. // IsNotExists(err) == true error, which we use to check
  513. // existence of the .stignore file, and just end up assuming
  514. // there is none, rather than a broken include.
  515. err = parseError(fmt.Errorf("failed to load include file %s: %w", includeFile, err))
  516. }
  517. case strings.HasSuffix(line, "/**"):
  518. err = addPattern(line)
  519. case strings.HasSuffix(line, "/"):
  520. err = addPattern(line + "**")
  521. default:
  522. err = addPattern(line)
  523. if err == nil {
  524. err = addPattern(line + "/**")
  525. }
  526. }
  527. if err != nil {
  528. return lines, nil, err
  529. }
  530. }
  531. return lines, patterns, nil
  532. }
  533. // WriteIgnores is a convenience function to avoid code duplication
  534. func WriteIgnores(filesystem fs.Filesystem, path string, content []string) error {
  535. if len(content) == 0 {
  536. err := filesystem.Remove(path)
  537. if fs.IsNotExist(err) {
  538. return nil
  539. }
  540. return err
  541. }
  542. fd, err := osutil.CreateAtomicFilesystem(filesystem, path)
  543. if err != nil {
  544. return err
  545. }
  546. wr := osutil.LineEndingsWriter(fd)
  547. for _, line := range content {
  548. fmt.Fprintln(wr, line)
  549. }
  550. if err := fd.Close(); err != nil {
  551. return err
  552. }
  553. filesystem.Hide(path)
  554. return nil
  555. }
  556. type modtimeCheckerKey struct {
  557. fs fs.Filesystem
  558. name string
  559. }
  560. // modtimeChecker is the default implementation of ChangeDetector
  561. type modtimeChecker struct {
  562. modtimes map[modtimeCheckerKey]time.Time
  563. }
  564. func newModtimeChecker() *modtimeChecker {
  565. return &modtimeChecker{
  566. modtimes: map[modtimeCheckerKey]time.Time{},
  567. }
  568. }
  569. func (c *modtimeChecker) Remember(fs fs.Filesystem, name string, modtime time.Time) {
  570. c.modtimes[modtimeCheckerKey{fs, name}] = modtime
  571. }
  572. func (c *modtimeChecker) Seen(fs fs.Filesystem, name string) bool {
  573. _, ok := c.modtimes[modtimeCheckerKey{fs, name}]
  574. return ok
  575. }
  576. func (c *modtimeChecker) Reset() {
  577. c.modtimes = map[modtimeCheckerKey]time.Time{}
  578. }
  579. func (c *modtimeChecker) Changed() bool {
  580. for key, modtime := range c.modtimes {
  581. info, err := key.fs.Stat(key.name)
  582. if err != nil {
  583. return true
  584. }
  585. if !info.ModTime().Equal(modtime) {
  586. return true
  587. }
  588. }
  589. return false
  590. }