walk.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. package scanner
  5. import (
  6. "errors"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "code.google.com/p/go.text/unicode/norm"
  11. "github.com/syncthing/syncthing/internal/ignore"
  12. "github.com/syncthing/syncthing/internal/lamport"
  13. "github.com/syncthing/syncthing/internal/protocol"
  14. )
  15. type Walker struct {
  16. // Dir is the base directory for the walk
  17. Dir string
  18. // Limit walking to this path within Dir, or no limit if Sub is blank
  19. Sub string
  20. // BlockSize controls the size of the block used when hashing.
  21. BlockSize int
  22. // List of patterns to ignore
  23. Ignores ignore.Patterns
  24. // If TempNamer is not nil, it is used to ignore tempory files when walking.
  25. TempNamer TempNamer
  26. // If CurrentFiler is not nil, it is queried for the current file before rescanning.
  27. CurrentFiler CurrentFiler
  28. // If IgnorePerms is true, changes to permission bits will not be
  29. // detected. Scanned files will get zero permission bits and the
  30. // NoPermissionBits flag set.
  31. IgnorePerms bool
  32. }
  33. type TempNamer interface {
  34. // Temporary returns a temporary name for the filed referred to by filepath.
  35. TempName(path string) string
  36. // IsTemporary returns true if path refers to the name of temporary file.
  37. IsTemporary(path string) bool
  38. }
  39. type CurrentFiler interface {
  40. // CurrentFile returns the file as seen at last scan.
  41. CurrentFile(name string) protocol.FileInfo
  42. }
  43. // Walk returns the list of files found in the local repository by scanning the
  44. // file system. Files are blockwise hashed.
  45. func (w *Walker) Walk() (chan protocol.FileInfo, error) {
  46. if debug {
  47. l.Debugln("Walk", w.Dir, w.Sub, w.BlockSize, w.Ignores)
  48. }
  49. err := checkDir(w.Dir)
  50. if err != nil {
  51. return nil, err
  52. }
  53. files := make(chan protocol.FileInfo)
  54. hashedFiles := make(chan protocol.FileInfo)
  55. newParallelHasher(w.Dir, w.BlockSize, runtime.NumCPU(), hashedFiles, files)
  56. go func() {
  57. hashFiles := w.walkAndHashFiles(files)
  58. filepath.Walk(filepath.Join(w.Dir, w.Sub), hashFiles)
  59. close(files)
  60. }()
  61. return hashedFiles, nil
  62. }
  63. // CleanTempFiles removes all files that match the temporary filename pattern.
  64. func (w *Walker) CleanTempFiles() {
  65. filepath.Walk(w.Dir, w.cleanTempFile)
  66. }
  67. func (w *Walker) walkAndHashFiles(fchan chan protocol.FileInfo) filepath.WalkFunc {
  68. return func(p string, info os.FileInfo, err error) error {
  69. if err != nil {
  70. if debug {
  71. l.Debugln("error:", p, info, err)
  72. }
  73. return nil
  74. }
  75. rn, err := filepath.Rel(w.Dir, p)
  76. if err != nil {
  77. if debug {
  78. l.Debugln("rel error:", p, err)
  79. }
  80. return nil
  81. }
  82. if rn == "." {
  83. return nil
  84. }
  85. if w.TempNamer != nil && w.TempNamer.IsTemporary(rn) {
  86. // A temporary file
  87. if debug {
  88. l.Debugln("temporary:", rn)
  89. }
  90. return nil
  91. }
  92. if sn := filepath.Base(rn); sn == ".stignore" || sn == ".stversions" || w.Ignores.Match(rn) {
  93. // An ignored file
  94. if debug {
  95. l.Debugln("ignored:", rn)
  96. }
  97. if info.IsDir() {
  98. return filepath.SkipDir
  99. }
  100. return nil
  101. }
  102. if (runtime.GOOS == "linux" || runtime.GOOS == "windows") && !norm.NFC.IsNormalString(rn) {
  103. l.Warnf("File %q contains non-NFC UTF-8 sequences and cannot be synced. Consider renaming.", rn)
  104. return nil
  105. }
  106. if info.Mode().IsDir() {
  107. if w.CurrentFiler != nil {
  108. cf := w.CurrentFiler.CurrentFile(rn)
  109. permUnchanged := w.IgnorePerms || !protocol.HasPermissionBits(cf.Flags) || PermsEqual(cf.Flags, uint32(info.Mode()))
  110. if !protocol.IsDeleted(cf.Flags) && protocol.IsDirectory(cf.Flags) && permUnchanged {
  111. return nil
  112. }
  113. }
  114. var flags uint32 = protocol.FlagDirectory
  115. if w.IgnorePerms {
  116. flags |= protocol.FlagNoPermBits | 0777
  117. } else {
  118. flags |= uint32(info.Mode() & os.ModePerm)
  119. }
  120. f := protocol.FileInfo{
  121. Name: rn,
  122. Version: lamport.Default.Tick(0),
  123. Flags: flags,
  124. Modified: info.ModTime().Unix(),
  125. }
  126. if debug {
  127. l.Debugln("dir:", f)
  128. }
  129. fchan <- f
  130. return nil
  131. }
  132. if info.Mode().IsRegular() {
  133. if w.CurrentFiler != nil {
  134. cf := w.CurrentFiler.CurrentFile(rn)
  135. permUnchanged := w.IgnorePerms || !protocol.HasPermissionBits(cf.Flags) || PermsEqual(cf.Flags, uint32(info.Mode()))
  136. if !protocol.IsDeleted(cf.Flags) && cf.Modified == info.ModTime().Unix() && permUnchanged {
  137. return nil
  138. }
  139. if debug {
  140. l.Debugln("rescan:", cf, info.ModTime().Unix(), info.Mode()&os.ModePerm)
  141. }
  142. }
  143. var flags = uint32(info.Mode() & os.ModePerm)
  144. if w.IgnorePerms {
  145. flags = protocol.FlagNoPermBits | 0666
  146. }
  147. fchan <- protocol.FileInfo{
  148. Name: rn,
  149. Version: lamport.Default.Tick(0),
  150. Flags: flags,
  151. Modified: info.ModTime().Unix(),
  152. }
  153. }
  154. return nil
  155. }
  156. }
  157. func (w *Walker) cleanTempFile(path string, info os.FileInfo, err error) error {
  158. if err != nil {
  159. return err
  160. }
  161. if info.Mode()&os.ModeType == 0 && w.TempNamer.IsTemporary(path) {
  162. os.Remove(path)
  163. }
  164. return nil
  165. }
  166. func checkDir(dir string) error {
  167. if info, err := os.Lstat(dir); err != nil {
  168. return err
  169. } else if !info.IsDir() {
  170. return errors.New(dir + ": not a directory")
  171. } else if debug {
  172. l.Debugln("checkDir", dir, info)
  173. }
  174. return nil
  175. }
  176. func PermsEqual(a, b uint32) bool {
  177. switch runtime.GOOS {
  178. case "windows":
  179. // There is only writeable and read only, represented for user, group
  180. // and other equally. We only compare against user.
  181. return a&0600 == b&0600
  182. default:
  183. // All bits count
  184. return a&0777 == b&0777
  185. }
  186. }