walk.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package scanner
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "path"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "github.com/calmh/syncthing/protocol"
  12. )
  13. type Walker struct {
  14. // Dir is the base directory for the walk
  15. Dir string
  16. // If FollowSymlinks is true, symbolic links directly under Dir will be followed.
  17. // Symbolic links at deeper levels are never followed regardless of this flag.
  18. FollowSymlinks bool
  19. // BlockSize controls the size of the block used when hashing.
  20. BlockSize int
  21. // If IgnoreFile is not empty, it is the name used for the file that holds ignore patterns.
  22. IgnoreFile string
  23. // If TempNamer is not nil, it is used to ignore tempory files when walking.
  24. TempNamer TempNamer
  25. // If Suppressor is not nil, it is queried for supression of modified files.
  26. Suppressor Suppressor
  27. previous map[string]File // file name -> last seen file state
  28. suppressed map[string]bool // file name -> suppression status
  29. }
  30. type TempNamer interface {
  31. // Temporary returns a temporary name for the filed referred to by path.
  32. TempName(path string) string
  33. // IsTemporary returns true if path refers to the name of temporary file.
  34. IsTemporary(path string) bool
  35. }
  36. type Suppressor interface {
  37. // Supress returns true if the update to the named file should be ignored.
  38. Suppress(name string, fi os.FileInfo) bool
  39. }
  40. // Walk returns the list of files found in the local repository by scanning the
  41. // file system. Files are blockwise hashed.
  42. func (w *Walker) Walk() (files []File, ignore map[string][]string) {
  43. w.lazyInit()
  44. if debug {
  45. dlog.Println("Walk", w.Dir, w.FollowSymlinks, w.BlockSize, w.IgnoreFile)
  46. }
  47. t0 := time.Now()
  48. ignore = make(map[string][]string)
  49. hashFiles := w.walkAndHashFiles(&files, ignore)
  50. filepath.Walk(w.Dir, w.loadIgnoreFiles(w.Dir, ignore))
  51. filepath.Walk(w.Dir, hashFiles)
  52. if w.FollowSymlinks {
  53. d, err := os.Open(w.Dir)
  54. if err != nil {
  55. return
  56. }
  57. defer d.Close()
  58. fis, err := d.Readdir(-1)
  59. if err != nil {
  60. return
  61. }
  62. for _, info := range fis {
  63. if info.Mode()&os.ModeSymlink != 0 {
  64. dir := path.Join(w.Dir, info.Name()) + "/"
  65. filepath.Walk(dir, w.loadIgnoreFiles(dir, ignore))
  66. filepath.Walk(dir, hashFiles)
  67. }
  68. }
  69. }
  70. if debug {
  71. t1 := time.Now()
  72. d := t1.Sub(t0).Seconds()
  73. dlog.Printf("Walk in %.02f ms, %.0f files/s", d*1000, float64(len(files))/d)
  74. }
  75. return
  76. }
  77. // CleanTempFiles removes all files that match the temporary filename pattern.
  78. func (w *Walker) CleanTempFiles() {
  79. filepath.Walk(w.Dir, w.cleanTempFile)
  80. }
  81. func (w *Walker) lazyInit() {
  82. if w.previous == nil {
  83. w.previous = make(map[string]File)
  84. w.suppressed = make(map[string]bool)
  85. }
  86. }
  87. func (w *Walker) loadIgnoreFiles(dir string, ign map[string][]string) filepath.WalkFunc {
  88. return func(p string, info os.FileInfo, err error) error {
  89. if err != nil {
  90. return nil
  91. }
  92. rn, err := filepath.Rel(dir, p)
  93. if err != nil {
  94. return nil
  95. }
  96. if pn, sn := path.Split(rn); sn == w.IgnoreFile {
  97. pn := strings.Trim(pn, "/")
  98. bs, _ := ioutil.ReadFile(p)
  99. lines := bytes.Split(bs, []byte("\n"))
  100. var patterns []string
  101. for _, line := range lines {
  102. if len(line) > 0 {
  103. patterns = append(patterns, string(line))
  104. }
  105. }
  106. ign[pn] = patterns
  107. }
  108. return nil
  109. }
  110. }
  111. func (w *Walker) walkAndHashFiles(res *[]File, ign map[string][]string) filepath.WalkFunc {
  112. return func(p string, info os.FileInfo, err error) error {
  113. if err != nil {
  114. if debug {
  115. dlog.Println("error:", p, info, err)
  116. }
  117. return nil
  118. }
  119. rn, err := filepath.Rel(w.Dir, p)
  120. if err != nil {
  121. if debug {
  122. dlog.Println("rel error:", p, err)
  123. }
  124. return nil
  125. }
  126. if w.TempNamer != nil && w.TempNamer.IsTemporary(rn) {
  127. if debug {
  128. dlog.Println("temporary:", rn)
  129. }
  130. return nil
  131. }
  132. if _, sn := path.Split(rn); sn == w.IgnoreFile {
  133. if debug {
  134. dlog.Println("ignorefile:", rn)
  135. }
  136. return nil
  137. }
  138. if w.ignoreFile(ign, rn) {
  139. if debug {
  140. dlog.Println("ignored:", rn)
  141. }
  142. return nil
  143. }
  144. if info.Mode()&os.ModeType == 0 {
  145. modified := info.ModTime().Unix()
  146. pf := w.previous[rn]
  147. if pf.Modified == modified {
  148. if nf := uint32(info.Mode()); nf != pf.Flags {
  149. if debug {
  150. dlog.Println("new flags:", rn)
  151. }
  152. pf.Flags = nf
  153. pf.Version++
  154. w.previous[rn] = pf
  155. } else if debug {
  156. dlog.Println("unchanged:", rn)
  157. }
  158. *res = append(*res, pf)
  159. return nil
  160. }
  161. if w.Suppressor != nil && w.Suppressor.Suppress(rn, info) {
  162. if debug {
  163. dlog.Println("suppressed:", rn)
  164. }
  165. if !w.suppressed[rn] {
  166. w.suppressed[rn] = true
  167. log.Printf("INFO: Changes to %q are being temporarily suppressed because it changes too frequently.", p)
  168. }
  169. f := pf
  170. f.Flags = protocol.FlagInvalid
  171. f.Blocks = nil
  172. *res = append(*res, f)
  173. return nil
  174. } else if w.suppressed[rn] {
  175. log.Printf("INFO: Changes to %q are no longer suppressed.", p)
  176. delete(w.suppressed, rn)
  177. }
  178. fd, err := os.Open(p)
  179. if err != nil {
  180. if debug {
  181. dlog.Println("open:", p, err)
  182. }
  183. return nil
  184. }
  185. defer fd.Close()
  186. t0 := time.Now()
  187. blocks, err := Blocks(fd, w.BlockSize)
  188. if err != nil {
  189. if debug {
  190. dlog.Println("hash error:", rn, err)
  191. }
  192. return nil
  193. }
  194. if debug {
  195. t1 := time.Now()
  196. dlog.Println("hashed:", rn, ";", len(blocks), "blocks;", info.Size(), "bytes;", int(float64(info.Size())/1024/t1.Sub(t0).Seconds()), "KB/s")
  197. }
  198. f := File{
  199. Name: rn,
  200. Size: info.Size(),
  201. Flags: uint32(info.Mode()),
  202. Modified: modified,
  203. Blocks: blocks,
  204. }
  205. w.previous[rn] = f
  206. *res = append(*res, f)
  207. }
  208. return nil
  209. }
  210. }
  211. func (w *Walker) cleanTempFile(path string, info os.FileInfo, err error) error {
  212. if err != nil {
  213. return err
  214. }
  215. if info.Mode()&os.ModeType == 0 && w.TempNamer.IsTemporary(path) {
  216. os.Remove(path)
  217. }
  218. return nil
  219. }
  220. func (w *Walker) ignoreFile(patterns map[string][]string, file string) bool {
  221. first, last := path.Split(file)
  222. for prefix, pats := range patterns {
  223. if len(prefix) == 0 || prefix == first || strings.HasPrefix(first, prefix+"/") {
  224. for _, pattern := range pats {
  225. if match, _ := path.Match(pattern, last); match {
  226. return true
  227. }
  228. }
  229. }
  230. }
  231. return false
  232. }