set.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Package files provides a set type to track local/remote files with newness checks.
  2. package files
  3. import (
  4. "crypto/md5"
  5. "sync"
  6. "github.com/calmh/syncthing/cid"
  7. "github.com/calmh/syncthing/lamport"
  8. "github.com/calmh/syncthing/protocol"
  9. "github.com/calmh/syncthing/scanner"
  10. )
  11. type key struct {
  12. Name string
  13. Version uint64
  14. Modified int64
  15. Hash [md5.Size]byte
  16. }
  17. type fileRecord struct {
  18. Usage int
  19. File scanner.File
  20. }
  21. type bitset uint64
  22. func keyFor(f scanner.File) key {
  23. h := md5.New()
  24. for _, b := range f.Blocks {
  25. h.Write(b.Hash)
  26. }
  27. return key{
  28. Name: f.Name,
  29. Version: f.Version,
  30. Modified: f.Modified,
  31. Hash: md5.Sum(nil),
  32. }
  33. }
  34. func (a key) newerThan(b key) bool {
  35. if a.Version != b.Version {
  36. return a.Version > b.Version
  37. }
  38. if a.Modified != b.Modified {
  39. return a.Modified > b.Modified
  40. }
  41. for i := 0; i < md5.Size; i++ {
  42. if a.Hash[i] != b.Hash[i] {
  43. return a.Hash[i] > b.Hash[i]
  44. }
  45. }
  46. return false
  47. }
  48. type Set struct {
  49. sync.Mutex
  50. files map[key]fileRecord
  51. remoteKey [64]map[string]key
  52. changes [64]uint64
  53. globalAvailability map[string]bitset
  54. globalKey map[string]key
  55. }
  56. func NewSet() *Set {
  57. var m = Set{
  58. files: make(map[key]fileRecord),
  59. globalAvailability: make(map[string]bitset),
  60. globalKey: make(map[string]key),
  61. }
  62. return &m
  63. }
  64. func (m *Set) Replace(id uint, fs []scanner.File) {
  65. if debug {
  66. dlog.Printf("Replace(%d, [%d])", id, len(fs))
  67. }
  68. if id > 63 {
  69. panic("Connection ID must be in the range 0 - 63 inclusive")
  70. }
  71. m.Lock()
  72. if len(fs) == 0 || !m.equals(id, fs) {
  73. m.changes[id]++
  74. m.replace(id, fs)
  75. }
  76. m.Unlock()
  77. }
  78. func (m *Set) ReplaceWithDelete(id uint, fs []scanner.File) {
  79. if debug {
  80. dlog.Printf("ReplaceWithDelete(%d, [%d])", id, len(fs))
  81. }
  82. if id > 63 {
  83. panic("Connection ID must be in the range 0 - 63 inclusive")
  84. }
  85. m.Lock()
  86. if len(fs) == 0 || !m.equals(id, fs) {
  87. m.changes[id]++
  88. var nf = make(map[string]key, len(fs))
  89. for _, f := range fs {
  90. nf[f.Name] = keyFor(f)
  91. }
  92. // For previously existing files not in the list, add them to the list
  93. // with the relevant delete flags etc set. Previously existing files
  94. // with the delete bit already set are not modified.
  95. for _, ck := range m.remoteKey[cid.LocalID] {
  96. if _, ok := nf[ck.Name]; !ok {
  97. cf := m.files[ck].File
  98. if cf.Flags&protocol.FlagDeleted != protocol.FlagDeleted {
  99. cf.Flags |= protocol.FlagDeleted
  100. cf.Blocks = nil
  101. cf.Size = 0
  102. cf.Version = lamport.Default.Tick(cf.Version)
  103. }
  104. fs = append(fs, cf)
  105. if debug {
  106. dlog.Println("deleted:", ck.Name)
  107. }
  108. }
  109. }
  110. m.replace(id, fs)
  111. }
  112. m.Unlock()
  113. }
  114. func (m *Set) Update(id uint, fs []scanner.File) {
  115. if debug {
  116. dlog.Printf("Update(%d, [%d])", id, len(fs))
  117. }
  118. m.Lock()
  119. m.update(id, fs)
  120. m.changes[id]++
  121. m.Unlock()
  122. }
  123. func (m *Set) Need(id uint) []scanner.File {
  124. if debug {
  125. dlog.Printf("Need(%d)", id)
  126. }
  127. var fs []scanner.File
  128. m.Lock()
  129. rkID := m.remoteKey[id]
  130. for name, gk := range m.globalKey {
  131. if gk.newerThan(rkID[name]) {
  132. if m.files[gk].File.Flags&protocol.FlagDirectory == 0 || // Regular file
  133. m.files[gk].File.Flags&(protocol.FlagDirectory|protocol.FlagDeleted) == protocol.FlagDirectory { // Non-deleted directory
  134. fs = append(fs, m.files[gk].File)
  135. }
  136. }
  137. }
  138. m.Unlock()
  139. return fs
  140. }
  141. func (m *Set) Have(id uint) []scanner.File {
  142. if debug {
  143. dlog.Printf("Have(%d)", id)
  144. }
  145. var fs []scanner.File
  146. m.Lock()
  147. for _, rk := range m.remoteKey[id] {
  148. fs = append(fs, m.files[rk].File)
  149. }
  150. m.Unlock()
  151. return fs
  152. }
  153. func (m *Set) Global() []scanner.File {
  154. if debug {
  155. dlog.Printf("Global()")
  156. }
  157. var fs []scanner.File
  158. m.Lock()
  159. for _, rk := range m.globalKey {
  160. fs = append(fs, m.files[rk].File)
  161. }
  162. m.Unlock()
  163. return fs
  164. }
  165. func (m *Set) Get(id uint, file string) scanner.File {
  166. m.Lock()
  167. defer m.Unlock()
  168. if debug {
  169. dlog.Printf("Get(%d, %q)", id, file)
  170. }
  171. return m.files[m.remoteKey[id][file]].File
  172. }
  173. func (m *Set) GetGlobal(file string) scanner.File {
  174. m.Lock()
  175. defer m.Unlock()
  176. if debug {
  177. dlog.Printf("GetGlobal(%q)", file)
  178. }
  179. return m.files[m.globalKey[file]].File
  180. }
  181. func (m *Set) Availability(name string) bitset {
  182. m.Lock()
  183. defer m.Unlock()
  184. av := m.globalAvailability[name]
  185. if debug {
  186. dlog.Printf("Availability(%q) = %0x", name, av)
  187. }
  188. return av
  189. }
  190. func (m *Set) Changes(id uint) uint64 {
  191. m.Lock()
  192. defer m.Unlock()
  193. if debug {
  194. dlog.Printf("Changes(%d)", id)
  195. }
  196. return m.changes[id]
  197. }
  198. func (m *Set) equals(id uint, fs []scanner.File) bool {
  199. curWithoutDeleted := make(map[string]key)
  200. for _, k := range m.remoteKey[id] {
  201. f := m.files[k].File
  202. if f.Flags&protocol.FlagDeleted == 0 {
  203. curWithoutDeleted[f.Name] = k
  204. }
  205. }
  206. if len(curWithoutDeleted) != len(fs) {
  207. return false
  208. }
  209. for _, f := range fs {
  210. if curWithoutDeleted[f.Name] != keyFor(f) {
  211. return false
  212. }
  213. }
  214. return true
  215. }
  216. func (m *Set) update(cid uint, fs []scanner.File) {
  217. remFiles := m.remoteKey[cid]
  218. for _, f := range fs {
  219. n := f.Name
  220. fk := keyFor(f)
  221. if ck, ok := remFiles[n]; ok && ck == fk {
  222. // The remote already has exactly this file, skip it
  223. continue
  224. }
  225. remFiles[n] = fk
  226. // Keep the block list or increment the usage
  227. if br, ok := m.files[fk]; !ok {
  228. m.files[fk] = fileRecord{
  229. Usage: 1,
  230. File: f,
  231. }
  232. } else {
  233. br.Usage++
  234. m.files[fk] = br
  235. }
  236. // Update global view
  237. gk, ok := m.globalKey[n]
  238. switch {
  239. case ok && fk == gk:
  240. av := m.globalAvailability[n]
  241. av |= 1 << cid
  242. m.globalAvailability[n] = av
  243. case fk.newerThan(gk):
  244. m.globalKey[n] = fk
  245. m.globalAvailability[n] = 1 << cid
  246. }
  247. }
  248. }
  249. func (m *Set) replace(cid uint, fs []scanner.File) {
  250. // Decrement usage for all files belonging to this remote, and remove
  251. // those that are no longer needed.
  252. for _, fk := range m.remoteKey[cid] {
  253. br, ok := m.files[fk]
  254. switch {
  255. case ok && br.Usage == 1:
  256. delete(m.files, fk)
  257. case ok && br.Usage > 1:
  258. br.Usage--
  259. m.files[fk] = br
  260. }
  261. }
  262. // Clear existing remote remoteKey
  263. m.remoteKey[cid] = make(map[string]key)
  264. // Recalculate global based on all remaining remoteKey
  265. for n := range m.globalKey {
  266. var nk key // newest key
  267. var na bitset // newest availability
  268. for i, rem := range m.remoteKey {
  269. if rk, ok := rem[n]; ok {
  270. switch {
  271. case rk == nk:
  272. na |= 1 << uint(i)
  273. case rk.newerThan(nk):
  274. nk = rk
  275. na = 1 << uint(i)
  276. }
  277. }
  278. }
  279. if na != 0 {
  280. // Someone had the file
  281. m.globalKey[n] = nk
  282. m.globalAvailability[n] = na
  283. } else {
  284. // Noone had the file
  285. delete(m.globalKey, n)
  286. delete(m.globalAvailability, n)
  287. }
  288. }
  289. // Add new remote remoteKey to the mix
  290. m.update(cid, fs)
  291. }