walk.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package scanner
  2. import (
  3. "bytes"
  4. "errors"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "time"
  11. "github.com/calmh/syncthing/lamport"
  12. "github.com/calmh/syncthing/protocol"
  13. )
  14. type Walker struct {
  15. // Dir is the base directory for the walk
  16. Dir string
  17. // BlockSize controls the size of the block used when hashing.
  18. BlockSize int
  19. // If IgnoreFile is not empty, it is the name used for the file that holds ignore patterns.
  20. IgnoreFile string
  21. // If TempNamer is not nil, it is used to ignore tempory files when walking.
  22. TempNamer TempNamer
  23. // If CurrentFiler is not nil, it is queried for the current file before rescanning.
  24. CurrentFiler CurrentFiler
  25. // If Suppressor is not nil, it is queried for supression of modified files.
  26. // Suppressed files will be returned with empty metadata and the Suppressed flag set.
  27. // Requires CurrentFiler to be set.
  28. Suppressor Suppressor
  29. // If IgnorePerms is true, changes to permission bits will not be
  30. // detected. Scanned files will get zero permission bits and the
  31. // NoPermissionBits flag set.
  32. IgnorePerms bool
  33. }
  34. type TempNamer interface {
  35. // Temporary returns a temporary name for the filed referred to by filepath.
  36. TempName(path string) string
  37. // IsTemporary returns true if path refers to the name of temporary file.
  38. IsTemporary(path string) bool
  39. }
  40. type Suppressor interface {
  41. // Supress returns true if the update to the named file should be ignored.
  42. Suppress(name string, fi os.FileInfo) (bool, bool)
  43. }
  44. type CurrentFiler interface {
  45. // CurrentFile returns the file as seen at last scan.
  46. CurrentFile(name string) File
  47. }
  48. // Walk returns the list of files found in the local repository by scanning the
  49. // file system. Files are blockwise hashed.
  50. func (w *Walker) Walk() (files []File, ignore map[string][]string, err error) {
  51. if debug {
  52. l.Debugln("Walk", w.Dir, w.BlockSize, w.IgnoreFile)
  53. }
  54. err = checkDir(w.Dir)
  55. if err != nil {
  56. return
  57. }
  58. t0 := time.Now()
  59. ignore = make(map[string][]string)
  60. hashFiles := w.walkAndHashFiles(&files, ignore)
  61. filepath.Walk(w.Dir, w.loadIgnoreFiles(w.Dir, ignore))
  62. filepath.Walk(w.Dir, hashFiles)
  63. if debug {
  64. t1 := time.Now()
  65. d := t1.Sub(t0).Seconds()
  66. l.Debugf("Walk in %.02f ms, %.0f files/s", d*1000, float64(len(files))/d)
  67. }
  68. err = checkDir(w.Dir)
  69. return
  70. }
  71. // CleanTempFiles removes all files that match the temporary filename pattern.
  72. func (w *Walker) CleanTempFiles() {
  73. filepath.Walk(w.Dir, w.cleanTempFile)
  74. }
  75. func (w *Walker) loadIgnoreFiles(dir string, ign map[string][]string) filepath.WalkFunc {
  76. return func(p string, info os.FileInfo, err error) error {
  77. if err != nil {
  78. return nil
  79. }
  80. rn, err := filepath.Rel(dir, p)
  81. if err != nil {
  82. return nil
  83. }
  84. if pn, sn := filepath.Split(rn); sn == w.IgnoreFile {
  85. pn := strings.Trim(pn, "/")
  86. bs, _ := ioutil.ReadFile(p)
  87. lines := bytes.Split(bs, []byte("\n"))
  88. var patterns []string
  89. for _, line := range lines {
  90. if len(line) > 0 {
  91. patterns = append(patterns, string(line))
  92. }
  93. }
  94. ign[pn] = patterns
  95. }
  96. return nil
  97. }
  98. }
  99. func (w *Walker) walkAndHashFiles(res *[]File, ign map[string][]string) filepath.WalkFunc {
  100. return func(p string, info os.FileInfo, err error) error {
  101. if err != nil {
  102. if debug {
  103. l.Debugln("error:", p, info, err)
  104. }
  105. return nil
  106. }
  107. rn, err := filepath.Rel(w.Dir, p)
  108. if err != nil {
  109. if debug {
  110. l.Debugln("rel error:", p, err)
  111. }
  112. return nil
  113. }
  114. if rn == "." {
  115. return nil
  116. }
  117. if w.TempNamer != nil && w.TempNamer.IsTemporary(rn) {
  118. // A temporary file
  119. if debug {
  120. l.Debugln("temporary:", rn)
  121. }
  122. return nil
  123. }
  124. if _, sn := filepath.Split(rn); sn == w.IgnoreFile {
  125. // An ignore-file; these are ignored themselves
  126. if debug {
  127. l.Debugln("ignorefile:", rn)
  128. }
  129. return nil
  130. }
  131. if w.ignoreFile(ign, rn) {
  132. // An ignored file
  133. if debug {
  134. l.Debugln("ignored:", rn)
  135. }
  136. if info.IsDir() {
  137. return filepath.SkipDir
  138. }
  139. return nil
  140. }
  141. if info.Mode().IsDir() {
  142. if w.CurrentFiler != nil {
  143. cf := w.CurrentFiler.CurrentFile(rn)
  144. permUnchanged := w.IgnorePerms || !protocol.HasPermissionBits(cf.Flags) || PermsEqual(cf.Flags, uint32(info.Mode()))
  145. if cf.Modified == info.ModTime().Unix() && protocol.IsDirectory(cf.Flags) && permUnchanged {
  146. if debug {
  147. l.Debugln("unchanged:", cf)
  148. }
  149. *res = append(*res, cf)
  150. } else {
  151. var flags uint32 = protocol.FlagDirectory
  152. if w.IgnorePerms {
  153. flags |= protocol.FlagNoPermBits
  154. } else {
  155. flags |= uint32(info.Mode() & os.ModePerm)
  156. }
  157. f := File{
  158. Name: rn,
  159. Version: lamport.Default.Tick(0),
  160. Flags: flags,
  161. Modified: info.ModTime().Unix(),
  162. }
  163. if debug {
  164. l.Debugln("dir:", cf, f)
  165. }
  166. *res = append(*res, f)
  167. }
  168. return nil
  169. }
  170. }
  171. if info.Mode().IsRegular() {
  172. if w.CurrentFiler != nil {
  173. cf := w.CurrentFiler.CurrentFile(rn)
  174. permUnchanged := w.IgnorePerms || !protocol.HasPermissionBits(cf.Flags) || PermsEqual(cf.Flags, uint32(info.Mode()))
  175. if !protocol.IsDeleted(cf.Flags) && cf.Modified == info.ModTime().Unix() && permUnchanged {
  176. if debug {
  177. l.Debugln("unchanged:", cf)
  178. }
  179. *res = append(*res, cf)
  180. return nil
  181. }
  182. if w.Suppressor != nil {
  183. if cur, prev := w.Suppressor.Suppress(rn, info); cur && !prev {
  184. l.Infof("Changes to %q are being temporarily suppressed because it changes too frequently.", p)
  185. cf.Suppressed = true
  186. cf.Version++
  187. if debug {
  188. l.Debugln("suppressed:", cf)
  189. }
  190. *res = append(*res, cf)
  191. return nil
  192. } else if prev && !cur {
  193. l.Infof("Changes to %q are no longer suppressed.", p)
  194. }
  195. }
  196. if debug {
  197. l.Debugln("rescan:", cf, info.ModTime().Unix(), info.Mode()&os.ModePerm)
  198. }
  199. }
  200. fd, err := os.Open(p)
  201. if err != nil {
  202. if debug {
  203. l.Debugln("open:", p, err)
  204. }
  205. return nil
  206. }
  207. defer fd.Close()
  208. t0 := time.Now()
  209. blocks, err := Blocks(fd, w.BlockSize)
  210. if err != nil {
  211. if debug {
  212. l.Debugln("hash error:", rn, err)
  213. }
  214. return nil
  215. }
  216. if debug {
  217. t1 := time.Now()
  218. l.Debugln("hashed:", rn, ";", len(blocks), "blocks;", info.Size(), "bytes;", int(float64(info.Size())/1024/t1.Sub(t0).Seconds()), "KB/s")
  219. }
  220. var flags = uint32(info.Mode() & os.ModePerm)
  221. if w.IgnorePerms {
  222. flags = protocol.FlagNoPermBits | 0666
  223. }
  224. f := File{
  225. Name: rn,
  226. Version: lamport.Default.Tick(0),
  227. Size: info.Size(),
  228. Flags: flags,
  229. Modified: info.ModTime().Unix(),
  230. Blocks: blocks,
  231. }
  232. *res = append(*res, f)
  233. }
  234. return nil
  235. }
  236. }
  237. func (w *Walker) cleanTempFile(path string, info os.FileInfo, err error) error {
  238. if err != nil {
  239. return err
  240. }
  241. if info.Mode()&os.ModeType == 0 && w.TempNamer.IsTemporary(path) {
  242. os.Remove(path)
  243. }
  244. return nil
  245. }
  246. func (w *Walker) ignoreFile(patterns map[string][]string, file string) bool {
  247. first, last := filepath.Split(file)
  248. for prefix, pats := range patterns {
  249. if len(prefix) == 0 || prefix == first || strings.HasPrefix(first, prefix+"/") {
  250. for _, pattern := range pats {
  251. if match, _ := filepath.Match(pattern, last); match {
  252. return true
  253. }
  254. }
  255. }
  256. }
  257. return false
  258. }
  259. func checkDir(dir string) error {
  260. if info, err := os.Stat(dir); err != nil {
  261. return err
  262. } else if !info.IsDir() {
  263. return errors.New(dir + ": not a directory")
  264. }
  265. return nil
  266. }
  267. func PermsEqual(a, b uint32) bool {
  268. switch runtime.GOOS {
  269. case "windows":
  270. // There is only writeable and read only, represented for user, group
  271. // and other equally. We only compare against user.
  272. return a&0600 == b&0600
  273. default:
  274. // All bits count
  275. return a&0777 == b&0777
  276. }
  277. }