walk.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. "bytes"
  7. "errors"
  8. "fmt"
  9. "io/ioutil"
  10. "os"
  11. "path/filepath"
  12. "runtime"
  13. "strings"
  14. "code.google.com/p/go.text/unicode/norm"
  15. "github.com/syncthing/syncthing/lamport"
  16. "github.com/syncthing/syncthing/protocol"
  17. )
  18. type Walker struct {
  19. // Dir is the base directory for the walk
  20. Dir string
  21. // Limit walking to this path within Dir, or no limit if Sub is blank
  22. Sub string
  23. // BlockSize controls the size of the block used when hashing.
  24. BlockSize int
  25. // If IgnoreFile is not empty, it is the name used for the file that holds ignore patterns.
  26. IgnoreFile string
  27. // If TempNamer is not nil, it is used to ignore tempory files when walking.
  28. TempNamer TempNamer
  29. // If CurrentFiler is not nil, it is queried for the current file before rescanning.
  30. CurrentFiler CurrentFiler
  31. // If IgnorePerms is true, changes to permission bits will not be
  32. // detected. Scanned files will get zero permission bits and the
  33. // NoPermissionBits flag set.
  34. IgnorePerms bool
  35. }
  36. type TempNamer interface {
  37. // Temporary returns a temporary name for the filed referred to by filepath.
  38. TempName(path string) string
  39. // IsTemporary returns true if path refers to the name of temporary file.
  40. IsTemporary(path string) bool
  41. }
  42. type CurrentFiler interface {
  43. // CurrentFile returns the file as seen at last scan.
  44. CurrentFile(name string) protocol.FileInfo
  45. }
  46. // Walk returns the list of files found in the local repository by scanning the
  47. // file system. Files are blockwise hashed.
  48. func (w *Walker) Walk() (chan protocol.FileInfo, map[string][]string, error) {
  49. if debug {
  50. l.Debugln("Walk", w.Dir, w.Sub, w.BlockSize, w.IgnoreFile)
  51. }
  52. err := checkDir(w.Dir)
  53. if err != nil {
  54. return nil, nil, err
  55. }
  56. ignore := make(map[string][]string)
  57. files := make(chan protocol.FileInfo)
  58. hashedFiles := make(chan protocol.FileInfo)
  59. newParallelHasher(w.Dir, w.BlockSize, runtime.NumCPU(), hashedFiles, files)
  60. hashFiles := w.walkAndHashFiles(files, ignore)
  61. go func() {
  62. filepath.Walk(w.Dir, w.loadIgnoreFiles(w.Dir, ignore))
  63. filepath.Walk(filepath.Join(w.Dir, w.Sub), hashFiles)
  64. close(files)
  65. }()
  66. return hashedFiles, ignore, nil
  67. }
  68. // CleanTempFiles removes all files that match the temporary filename pattern.
  69. func (w *Walker) CleanTempFiles() {
  70. filepath.Walk(w.Dir, w.cleanTempFile)
  71. }
  72. func (w *Walker) loadIgnoreFiles(dir string, ign map[string][]string) filepath.WalkFunc {
  73. return func(p string, info os.FileInfo, err error) error {
  74. if err != nil {
  75. return nil
  76. }
  77. rn, err := filepath.Rel(dir, p)
  78. if err != nil {
  79. return nil
  80. }
  81. if pn, sn := filepath.Split(rn); sn == w.IgnoreFile {
  82. pn := filepath.Clean(pn)
  83. bs, _ := ioutil.ReadFile(p)
  84. lines := bytes.Split(bs, []byte("\n"))
  85. var patterns []string
  86. for _, line := range lines {
  87. lineStr := strings.TrimSpace(string(line))
  88. if len(lineStr) > 0 {
  89. patterns = append(patterns, lineStr)
  90. }
  91. }
  92. ign[pn] = patterns
  93. }
  94. return nil
  95. }
  96. }
  97. func (w *Walker) walkAndHashFiles(fchan chan protocol.FileInfo, ign map[string][]string) filepath.WalkFunc {
  98. return func(p string, info os.FileInfo, err error) error {
  99. if err != nil {
  100. if debug {
  101. l.Debugln("error:", p, info, err)
  102. }
  103. return nil
  104. }
  105. rn, err := filepath.Rel(w.Dir, p)
  106. if err != nil {
  107. if debug {
  108. l.Debugln("rel error:", p, err)
  109. }
  110. return nil
  111. }
  112. if rn == "." {
  113. return nil
  114. }
  115. if w.TempNamer != nil && w.TempNamer.IsTemporary(rn) {
  116. // A temporary file
  117. if debug {
  118. l.Debugln("temporary:", rn)
  119. }
  120. return nil
  121. }
  122. if sn := filepath.Base(rn); sn == w.IgnoreFile || sn == ".stversions" || w.ignoreFile(ign, rn) {
  123. // An ignored file
  124. if debug {
  125. l.Debugln("ignored:", rn)
  126. }
  127. if info.IsDir() {
  128. return filepath.SkipDir
  129. }
  130. return nil
  131. }
  132. if (runtime.GOOS == "linux" || runtime.GOOS == "windows") && !norm.NFC.IsNormalString(rn) {
  133. l.Warnf("File %q contains non-NFC UTF-8 sequences and cannot be synced. Consider renaming.", rn)
  134. return nil
  135. }
  136. if info.Mode().IsDir() {
  137. if w.CurrentFiler != nil {
  138. cf := w.CurrentFiler.CurrentFile(rn)
  139. permUnchanged := w.IgnorePerms || !protocol.HasPermissionBits(cf.Flags) || PermsEqual(cf.Flags, uint32(info.Mode()))
  140. if !protocol.IsDeleted(cf.Flags) && protocol.IsDirectory(cf.Flags) && permUnchanged {
  141. return nil
  142. }
  143. }
  144. var flags uint32 = protocol.FlagDirectory
  145. if w.IgnorePerms {
  146. flags |= protocol.FlagNoPermBits | 0777
  147. } else {
  148. flags |= uint32(info.Mode() & os.ModePerm)
  149. }
  150. f := protocol.FileInfo{
  151. Name: rn,
  152. Version: lamport.Default.Tick(0),
  153. Flags: flags,
  154. Modified: info.ModTime().Unix(),
  155. }
  156. if debug {
  157. l.Debugln("dir:", f)
  158. }
  159. fchan <- f
  160. return nil
  161. }
  162. if info.Mode().IsRegular() {
  163. if w.CurrentFiler != nil {
  164. cf := w.CurrentFiler.CurrentFile(rn)
  165. permUnchanged := w.IgnorePerms || !protocol.HasPermissionBits(cf.Flags) || PermsEqual(cf.Flags, uint32(info.Mode()))
  166. if !protocol.IsDeleted(cf.Flags) && cf.Modified == info.ModTime().Unix() && permUnchanged {
  167. return nil
  168. }
  169. if debug {
  170. l.Debugln("rescan:", cf, info.ModTime().Unix(), info.Mode()&os.ModePerm)
  171. }
  172. }
  173. var flags = uint32(info.Mode() & os.ModePerm)
  174. if w.IgnorePerms {
  175. flags = protocol.FlagNoPermBits | 0666
  176. }
  177. fchan <- protocol.FileInfo{
  178. Name: rn,
  179. Version: lamport.Default.Tick(0),
  180. Flags: flags,
  181. Modified: info.ModTime().Unix(),
  182. }
  183. }
  184. return nil
  185. }
  186. }
  187. func (w *Walker) cleanTempFile(path string, info os.FileInfo, err error) error {
  188. if err != nil {
  189. return err
  190. }
  191. if info.Mode()&os.ModeType == 0 && w.TempNamer.IsTemporary(path) {
  192. os.Remove(path)
  193. }
  194. return nil
  195. }
  196. func (w *Walker) ignoreFile(patterns map[string][]string, file string) bool {
  197. first, last := filepath.Split(file)
  198. for prefix, pats := range patterns {
  199. if prefix == "." || prefix == first || strings.HasPrefix(first, fmt.Sprintf("%s%c", prefix, os.PathSeparator)) {
  200. for _, pattern := range pats {
  201. if match, _ := filepath.Match(pattern, last); match || pattern == last {
  202. return true
  203. }
  204. }
  205. }
  206. }
  207. return false
  208. }
  209. func checkDir(dir string) error {
  210. if info, err := os.Lstat(dir); err != nil {
  211. return err
  212. } else if !info.IsDir() {
  213. return errors.New(dir + ": not a directory")
  214. } else if debug {
  215. l.Debugln("checkDir", dir, info)
  216. }
  217. return nil
  218. }
  219. func PermsEqual(a, b uint32) bool {
  220. switch runtime.GOOS {
  221. case "windows":
  222. // There is only writeable and read only, represented for user, group
  223. // and other equally. We only compare against user.
  224. return a&0600 == b&0600
  225. default:
  226. // All bits count
  227. return a&0777 == b&0777
  228. }
  229. }