walk.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package model
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "github.com/calmh/syncthing/protocol"
  13. )
  14. const BlockSize = 128 * 1024
  15. type File struct {
  16. Name string
  17. Flags uint32
  18. Modified int64
  19. Version uint32
  20. Blocks []Block
  21. }
  22. func (f File) Size() (bytes int) {
  23. for _, b := range f.Blocks {
  24. bytes += int(b.Length)
  25. }
  26. return
  27. }
  28. func (f File) Equals(o File) bool {
  29. return f.Modified == o.Modified && f.Version == o.Version
  30. }
  31. func (f File) NewerThan(o File) bool {
  32. return f.Modified > o.Modified || (f.Modified == o.Modified && f.Version > o.Version)
  33. }
  34. func isTempName(name string) bool {
  35. return strings.HasPrefix(path.Base(name), ".syncthing.")
  36. }
  37. func tempName(name string, modified int64) string {
  38. tdir := path.Dir(name)
  39. tname := fmt.Sprintf(".syncthing.%s.%d", path.Base(name), modified)
  40. return path.Join(tdir, tname)
  41. }
  42. func (m *Model) loadIgnoreFiles(ign map[string][]string) filepath.WalkFunc {
  43. return func(p string, info os.FileInfo, err error) error {
  44. if err != nil {
  45. return nil
  46. }
  47. rn, err := filepath.Rel(m.dir, p)
  48. if err != nil {
  49. return nil
  50. }
  51. if pn, sn := path.Split(rn); sn == ".stignore" {
  52. pn := strings.Trim(pn, "/")
  53. bs, _ := ioutil.ReadFile(p)
  54. lines := bytes.Split(bs, []byte("\n"))
  55. var patterns []string
  56. for _, line := range lines {
  57. if len(line) > 0 {
  58. patterns = append(patterns, string(line))
  59. }
  60. }
  61. ign[pn] = patterns
  62. }
  63. return nil
  64. }
  65. }
  66. func (m *Model) walkAndHashFiles(res *[]File, ign map[string][]string) filepath.WalkFunc {
  67. return func(p string, info os.FileInfo, err error) error {
  68. if err != nil {
  69. return nil
  70. }
  71. if isTempName(p) {
  72. return nil
  73. }
  74. rn, err := filepath.Rel(m.dir, p)
  75. if err != nil {
  76. return nil
  77. }
  78. if _, sn := path.Split(rn); sn == ".stignore" {
  79. // We never sync the .stignore files
  80. return nil
  81. }
  82. if ignoreFile(ign, rn) {
  83. if m.trace["file"] {
  84. log.Println("FILE: IGNORE:", rn)
  85. }
  86. return nil
  87. }
  88. if info.Mode()&os.ModeType == 0 {
  89. fi, err := os.Stat(p)
  90. if err != nil {
  91. return nil
  92. }
  93. modified := fi.ModTime().Unix()
  94. m.RLock()
  95. hf, ok := m.local[rn]
  96. m.RUnlock()
  97. if ok && hf.Modified == modified {
  98. if nf := uint32(info.Mode()); nf != hf.Flags {
  99. hf.Flags = nf
  100. hf.Version++
  101. }
  102. *res = append(*res, hf)
  103. } else {
  104. m.Lock()
  105. if m.shouldSuppressChange(rn) {
  106. if m.trace["file"] {
  107. log.Println("FILE: SUPPRESS:", rn, m.fileWasSuppressed[rn], time.Since(m.fileLastChanged[rn]))
  108. }
  109. if ok {
  110. hf.Flags = protocol.FlagInvalid
  111. hf.Version++
  112. *res = append(*res, hf)
  113. }
  114. m.Unlock()
  115. return nil
  116. }
  117. m.Unlock()
  118. if m.trace["file"] {
  119. log.Printf("FILE: Hash %q", p)
  120. }
  121. fd, err := os.Open(p)
  122. if err != nil {
  123. if m.trace["file"] {
  124. log.Printf("FILE: %q: %v", p, err)
  125. }
  126. return nil
  127. }
  128. defer fd.Close()
  129. blocks, err := Blocks(fd, BlockSize)
  130. if err != nil {
  131. if m.trace["file"] {
  132. log.Printf("FILE: %q: %v", p, err)
  133. }
  134. return nil
  135. }
  136. f := File{
  137. Name: rn,
  138. Flags: uint32(info.Mode()),
  139. Modified: modified,
  140. Blocks: blocks,
  141. }
  142. *res = append(*res, f)
  143. }
  144. }
  145. return nil
  146. }
  147. }
  148. // Walk returns the list of files found in the local repository by scanning the
  149. // file system. Files are blockwise hashed.
  150. func (m *Model) Walk(followSymlinks bool) (files []File, ignore map[string][]string) {
  151. ignore = make(map[string][]string)
  152. hashFiles := m.walkAndHashFiles(&files, ignore)
  153. filepath.Walk(m.dir, m.loadIgnoreFiles(ignore))
  154. filepath.Walk(m.dir, hashFiles)
  155. if followSymlinks {
  156. d, err := os.Open(m.dir)
  157. if err != nil {
  158. return
  159. }
  160. defer d.Close()
  161. fis, err := d.Readdir(-1)
  162. if err != nil {
  163. return
  164. }
  165. for _, fi := range fis {
  166. if fi.Mode()&os.ModeSymlink != 0 {
  167. dir := path.Join(m.dir, fi.Name()) + "/"
  168. filepath.Walk(dir, m.loadIgnoreFiles(ignore))
  169. filepath.Walk(dir, hashFiles)
  170. }
  171. }
  172. }
  173. return
  174. }
  175. func (m *Model) cleanTempFile(path string, info os.FileInfo, err error) error {
  176. if err != nil {
  177. return err
  178. }
  179. if info.Mode()&os.ModeType == 0 && isTempName(path) {
  180. if m.trace["file"] {
  181. log.Printf("FILE: Remove %q", path)
  182. }
  183. os.Remove(path)
  184. }
  185. return nil
  186. }
  187. func (m *Model) cleanTempFiles() {
  188. filepath.Walk(m.dir, m.cleanTempFile)
  189. }
  190. func ignoreFilter(patterns map[string][]string, files []File) (filtered []File) {
  191. for _, f := range files {
  192. if !ignoreFile(patterns, f.Name) {
  193. filtered = append(filtered, f)
  194. }
  195. }
  196. return filtered
  197. }
  198. func ignoreFile(patterns map[string][]string, file string) bool {
  199. first, last := path.Split(file)
  200. for prefix, pats := range patterns {
  201. if len(prefix) == 0 || prefix == first || strings.HasPrefix(first, prefix+"/") {
  202. for _, pattern := range pats {
  203. if match, _ := path.Match(pattern, last); match {
  204. return true
  205. }
  206. }
  207. }
  208. }
  209. return false
  210. }