set_test.go 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package db_test
  7. import (
  8. "bytes"
  9. "fmt"
  10. "os"
  11. "path/filepath"
  12. "sort"
  13. "testing"
  14. "time"
  15. "github.com/d4l3k/messagediff"
  16. "github.com/syncthing/syncthing/lib/db"
  17. "github.com/syncthing/syncthing/lib/fs"
  18. "github.com/syncthing/syncthing/lib/protocol"
  19. )
  20. var remoteDevice0, remoteDevice1 protocol.DeviceID
  21. func init() {
  22. remoteDevice0, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  23. remoteDevice1, _ = protocol.DeviceIDFromString("I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU")
  24. }
  25. const myID = 1
  26. func genBlocks(n int) []protocol.BlockInfo {
  27. b := make([]protocol.BlockInfo, n)
  28. for i := range b {
  29. h := make([]byte, 32)
  30. for j := range h {
  31. h[j] = byte(i + j)
  32. }
  33. b[i].Size = int32(i)
  34. b[i].Hash = h
  35. }
  36. return b
  37. }
  38. func globalList(s *db.FileSet) []protocol.FileInfo {
  39. var fs []protocol.FileInfo
  40. s.WithGlobal(func(fi db.FileIntf) bool {
  41. f := fi.(protocol.FileInfo)
  42. fs = append(fs, f)
  43. return true
  44. })
  45. return fs
  46. }
  47. func globalListPrefixed(s *db.FileSet, prefix string) []db.FileInfoTruncated {
  48. var fs []db.FileInfoTruncated
  49. s.WithPrefixedGlobalTruncated(prefix, func(fi db.FileIntf) bool {
  50. f := fi.(db.FileInfoTruncated)
  51. fs = append(fs, f)
  52. return true
  53. })
  54. return fs
  55. }
  56. func haveList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  57. var fs []protocol.FileInfo
  58. s.WithHave(n, func(fi db.FileIntf) bool {
  59. f := fi.(protocol.FileInfo)
  60. fs = append(fs, f)
  61. return true
  62. })
  63. return fs
  64. }
  65. func haveListPrefixed(s *db.FileSet, n protocol.DeviceID, prefix string) []db.FileInfoTruncated {
  66. var fs []db.FileInfoTruncated
  67. s.WithPrefixedHaveTruncated(n, prefix, func(fi db.FileIntf) bool {
  68. f := fi.(db.FileInfoTruncated)
  69. fs = append(fs, f)
  70. return true
  71. })
  72. return fs
  73. }
  74. func needList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  75. var fs []protocol.FileInfo
  76. s.WithNeed(n, func(fi db.FileIntf) bool {
  77. f := fi.(protocol.FileInfo)
  78. fs = append(fs, f)
  79. return true
  80. })
  81. return fs
  82. }
  83. type fileList []protocol.FileInfo
  84. func (l fileList) Len() int {
  85. return len(l)
  86. }
  87. func (l fileList) Less(a, b int) bool {
  88. return l[a].Name < l[b].Name
  89. }
  90. func (l fileList) Swap(a, b int) {
  91. l[a], l[b] = l[b], l[a]
  92. }
  93. func (l fileList) String() string {
  94. var b bytes.Buffer
  95. b.WriteString("[]protocol.FileList{\n")
  96. for _, f := range l {
  97. fmt.Fprintf(&b, " %q: #%v, %d bytes, %d blocks, perms=%o\n", f.Name, f.Version, f.Size, len(f.Blocks), f.Permissions)
  98. }
  99. b.WriteString("}")
  100. return b.String()
  101. }
  102. func TestGlobalSet(t *testing.T) {
  103. ldb := db.OpenMemory()
  104. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  105. local0 := fileList{
  106. protocol.FileInfo{Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  107. protocol.FileInfo{Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  108. protocol.FileInfo{Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  109. protocol.FileInfo{Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  110. protocol.FileInfo{Name: "z", Sequence: 5, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  111. }
  112. local1 := fileList{
  113. protocol.FileInfo{Name: "a", Sequence: 6, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  114. protocol.FileInfo{Name: "b", Sequence: 7, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  115. protocol.FileInfo{Name: "c", Sequence: 8, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  116. protocol.FileInfo{Name: "d", Sequence: 9, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  117. protocol.FileInfo{Name: "z", Sequence: 10, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  118. }
  119. localTot := fileList{
  120. local1[0],
  121. local1[1],
  122. local1[2],
  123. local1[3],
  124. protocol.FileInfo{Name: "z", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  125. }
  126. remote0 := fileList{
  127. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  128. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  129. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  130. }
  131. remote1 := fileList{
  132. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(6)},
  133. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(7)},
  134. }
  135. remoteTot := fileList{
  136. remote0[0],
  137. remote1[0],
  138. remote0[2],
  139. remote1[1],
  140. }
  141. expectedGlobal := fileList{
  142. remote0[0], // a
  143. remote1[0], // b
  144. remote0[2], // c
  145. localTot[3], // d
  146. remote1[1], // e
  147. localTot[4], // z
  148. }
  149. expectedLocalNeed := fileList{
  150. remote1[0],
  151. remote0[2],
  152. remote1[1],
  153. }
  154. expectedRemoteNeed := fileList{
  155. local0[3],
  156. }
  157. replace(m, protocol.LocalDeviceID, local0)
  158. replace(m, protocol.LocalDeviceID, local1)
  159. replace(m, remoteDevice0, remote0)
  160. m.Update(remoteDevice0, remote1)
  161. g := fileList(globalList(m))
  162. sort.Sort(g)
  163. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal) {
  164. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal)
  165. }
  166. globalFiles, globalDirectories, globalDeleted, globalBytes := int32(0), int32(0), int32(0), int64(0)
  167. for _, f := range g {
  168. if f.IsInvalid() {
  169. continue
  170. }
  171. switch {
  172. case f.IsDeleted():
  173. globalDeleted++
  174. case f.IsDirectory():
  175. globalDirectories++
  176. default:
  177. globalFiles++
  178. }
  179. globalBytes += f.FileSize()
  180. }
  181. gs := m.GlobalSize()
  182. if gs.Files != globalFiles {
  183. t.Errorf("Incorrect GlobalSize files; %d != %d", gs.Files, globalFiles)
  184. }
  185. if gs.Directories != globalDirectories {
  186. t.Errorf("Incorrect GlobalSize directories; %d != %d", gs.Directories, globalDirectories)
  187. }
  188. if gs.Deleted != globalDeleted {
  189. t.Errorf("Incorrect GlobalSize deleted; %d != %d", gs.Deleted, globalDeleted)
  190. }
  191. if gs.Bytes != globalBytes {
  192. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  193. }
  194. h := fileList(haveList(m, protocol.LocalDeviceID))
  195. sort.Sort(h)
  196. if fmt.Sprint(h) != fmt.Sprint(localTot) {
  197. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, localTot)
  198. }
  199. haveFiles, haveDirectories, haveDeleted, haveBytes := int32(0), int32(0), int32(0), int64(0)
  200. for _, f := range h {
  201. if f.IsInvalid() {
  202. continue
  203. }
  204. switch {
  205. case f.IsDeleted():
  206. haveDeleted++
  207. case f.IsDirectory():
  208. haveDirectories++
  209. default:
  210. haveFiles++
  211. }
  212. haveBytes += f.FileSize()
  213. }
  214. ls := m.LocalSize()
  215. if ls.Files != haveFiles {
  216. t.Errorf("Incorrect LocalSize files; %d != %d", ls.Files, haveFiles)
  217. }
  218. if ls.Directories != haveDirectories {
  219. t.Errorf("Incorrect LocalSize directories; %d != %d", ls.Directories, haveDirectories)
  220. }
  221. if ls.Deleted != haveDeleted {
  222. t.Errorf("Incorrect LocalSize deleted; %d != %d", ls.Deleted, haveDeleted)
  223. }
  224. if ls.Bytes != haveBytes {
  225. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  226. }
  227. h = fileList(haveList(m, remoteDevice0))
  228. sort.Sort(h)
  229. if fmt.Sprint(h) != fmt.Sprint(remoteTot) {
  230. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, remoteTot)
  231. }
  232. n := fileList(needList(m, protocol.LocalDeviceID))
  233. sort.Sort(n)
  234. if fmt.Sprint(n) != fmt.Sprint(expectedLocalNeed) {
  235. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedLocalNeed)
  236. }
  237. n = fileList(needList(m, remoteDevice0))
  238. sort.Sort(n)
  239. if fmt.Sprint(n) != fmt.Sprint(expectedRemoteNeed) {
  240. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedRemoteNeed)
  241. }
  242. f, ok := m.Get(protocol.LocalDeviceID, "b")
  243. if !ok {
  244. t.Error("Unexpectedly not OK")
  245. }
  246. if fmt.Sprint(f) != fmt.Sprint(localTot[1]) {
  247. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, localTot[1])
  248. }
  249. f, ok = m.Get(remoteDevice0, "b")
  250. if !ok {
  251. t.Error("Unexpectedly not OK")
  252. }
  253. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  254. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  255. }
  256. f, ok = m.GetGlobal("b")
  257. if !ok {
  258. t.Error("Unexpectedly not OK")
  259. }
  260. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  261. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  262. }
  263. f, ok = m.Get(protocol.LocalDeviceID, "zz")
  264. if ok {
  265. t.Error("Unexpectedly OK")
  266. }
  267. if f.Name != "" {
  268. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  269. }
  270. f, ok = m.GetGlobal("zz")
  271. if ok {
  272. t.Error("Unexpectedly OK")
  273. }
  274. if f.Name != "" {
  275. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  276. }
  277. av := []protocol.DeviceID{protocol.LocalDeviceID, remoteDevice0}
  278. a := m.Availability("a")
  279. if !(len(a) == 2 && (a[0] == av[0] && a[1] == av[1] || a[0] == av[1] && a[1] == av[0])) {
  280. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, av)
  281. }
  282. a = m.Availability("b")
  283. if len(a) != 1 || a[0] != remoteDevice0 {
  284. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, remoteDevice0)
  285. }
  286. a = m.Availability("d")
  287. if len(a) != 1 || a[0] != protocol.LocalDeviceID {
  288. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, protocol.LocalDeviceID)
  289. }
  290. }
  291. func TestNeedWithInvalid(t *testing.T) {
  292. ldb := db.OpenMemory()
  293. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  294. localHave := fileList{
  295. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  296. }
  297. remote0Have := fileList{
  298. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  299. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  300. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  301. }
  302. remote1Have := fileList{
  303. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  304. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), RawInvalid: true},
  305. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  306. }
  307. expectedNeed := fileList{
  308. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  309. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  310. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  311. }
  312. replace(s, protocol.LocalDeviceID, localHave)
  313. replace(s, remoteDevice0, remote0Have)
  314. replace(s, remoteDevice1, remote1Have)
  315. need := fileList(needList(s, protocol.LocalDeviceID))
  316. sort.Sort(need)
  317. if fmt.Sprint(need) != fmt.Sprint(expectedNeed) {
  318. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, expectedNeed)
  319. }
  320. }
  321. func TestUpdateToInvalid(t *testing.T) {
  322. ldb := db.OpenMemory()
  323. folder := "test"
  324. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  325. f := db.NewBlockFinder(ldb)
  326. localHave := fileList{
  327. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  328. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  329. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), LocalFlags: protocol.FlagLocalIgnored},
  330. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  331. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, LocalFlags: protocol.FlagLocalIgnored},
  332. }
  333. replace(s, protocol.LocalDeviceID, localHave)
  334. have := fileList(haveList(s, protocol.LocalDeviceID))
  335. sort.Sort(have)
  336. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  337. t.Errorf("Have incorrect before invalidation;\n A: %v !=\n E: %v", have, localHave)
  338. }
  339. oldBlockHash := localHave[1].Blocks[0].Hash
  340. localHave[1].LocalFlags = protocol.FlagLocalIgnored
  341. localHave[1].Blocks = nil
  342. localHave[4].LocalFlags = 0
  343. localHave[4].Blocks = genBlocks(3)
  344. s.Update(protocol.LocalDeviceID, append(fileList{}, localHave[1], localHave[4]))
  345. have = fileList(haveList(s, protocol.LocalDeviceID))
  346. sort.Sort(have)
  347. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  348. t.Errorf("Have incorrect after invalidation;\n A: %v !=\n E: %v", have, localHave)
  349. }
  350. f.Iterate([]string{folder}, oldBlockHash, func(folder, file string, index int32) bool {
  351. if file == localHave[1].Name {
  352. t.Errorf("Found unexpected block in blockmap for invalidated file")
  353. return true
  354. }
  355. return false
  356. })
  357. if !f.Iterate([]string{folder}, localHave[4].Blocks[0].Hash, func(folder, file string, index int32) bool {
  358. return file == localHave[4].Name
  359. }) {
  360. t.Errorf("First block of un-invalidated file is missing from blockmap")
  361. }
  362. }
  363. func TestInvalidAvailability(t *testing.T) {
  364. ldb := db.OpenMemory()
  365. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  366. remote0Have := fileList{
  367. protocol.FileInfo{Name: "both", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  368. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  369. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  370. protocol.FileInfo{Name: "none", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  371. }
  372. remote1Have := fileList{
  373. protocol.FileInfo{Name: "both", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  374. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  375. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), RawInvalid: true},
  376. protocol.FileInfo{Name: "none", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  377. }
  378. replace(s, remoteDevice0, remote0Have)
  379. replace(s, remoteDevice1, remote1Have)
  380. if av := s.Availability("both"); len(av) != 2 {
  381. t.Error("Incorrect availability for 'both':", av)
  382. }
  383. if av := s.Availability("r0only"); len(av) != 1 || av[0] != remoteDevice0 {
  384. t.Error("Incorrect availability for 'r0only':", av)
  385. }
  386. if av := s.Availability("r1only"); len(av) != 1 || av[0] != remoteDevice1 {
  387. t.Error("Incorrect availability for 'r1only':", av)
  388. }
  389. if av := s.Availability("none"); len(av) != 0 {
  390. t.Error("Incorrect availability for 'none':", av)
  391. }
  392. }
  393. func TestGlobalReset(t *testing.T) {
  394. ldb := db.OpenMemory()
  395. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  396. local := []protocol.FileInfo{
  397. {Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  398. {Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  399. {Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  400. {Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  401. }
  402. remote := []protocol.FileInfo{
  403. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  404. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  405. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  406. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  407. }
  408. replace(m, protocol.LocalDeviceID, local)
  409. g := globalList(m)
  410. sort.Sort(fileList(g))
  411. if diff, equal := messagediff.PrettyDiff(local, g); !equal {
  412. t.Errorf("Global incorrect;\nglobal: %v\n!=\nlocal: %v\ndiff:\n%s", g, local, diff)
  413. }
  414. replace(m, remoteDevice0, remote)
  415. replace(m, remoteDevice0, nil)
  416. g = globalList(m)
  417. sort.Sort(fileList(g))
  418. if diff, equal := messagediff.PrettyDiff(local, g); !equal {
  419. t.Errorf("Global incorrect;\nglobal: %v\n!=\nlocal: %v\ndiff:\n%s", g, local, diff)
  420. }
  421. }
  422. func TestNeed(t *testing.T) {
  423. ldb := db.OpenMemory()
  424. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  425. local := []protocol.FileInfo{
  426. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  427. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  428. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  429. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  430. }
  431. remote := []protocol.FileInfo{
  432. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  433. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  434. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  435. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  436. }
  437. shouldNeed := []protocol.FileInfo{
  438. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  439. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  440. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  441. }
  442. replace(m, protocol.LocalDeviceID, local)
  443. replace(m, remoteDevice0, remote)
  444. need := needList(m, protocol.LocalDeviceID)
  445. sort.Sort(fileList(need))
  446. sort.Sort(fileList(shouldNeed))
  447. if fmt.Sprint(need) != fmt.Sprint(shouldNeed) {
  448. t.Errorf("Need incorrect;\n%v !=\n%v", need, shouldNeed)
  449. }
  450. }
  451. func TestSequence(t *testing.T) {
  452. ldb := db.OpenMemory()
  453. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  454. local1 := []protocol.FileInfo{
  455. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  456. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  457. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  458. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  459. }
  460. local2 := []protocol.FileInfo{
  461. local1[0],
  462. // [1] deleted
  463. local1[2],
  464. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  465. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  466. }
  467. replace(m, protocol.LocalDeviceID, local1)
  468. c0 := m.Sequence(protocol.LocalDeviceID)
  469. replace(m, protocol.LocalDeviceID, local2)
  470. c1 := m.Sequence(protocol.LocalDeviceID)
  471. if !(c1 > c0) {
  472. t.Fatal("Local version number should have incremented")
  473. }
  474. }
  475. func TestListDropFolder(t *testing.T) {
  476. ldb := db.OpenMemory()
  477. s0 := db.NewFileSet("test0", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  478. local1 := []protocol.FileInfo{
  479. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  480. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  481. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  482. }
  483. replace(s0, protocol.LocalDeviceID, local1)
  484. s1 := db.NewFileSet("test1", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  485. local2 := []protocol.FileInfo{
  486. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  487. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  488. {Name: "f", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  489. }
  490. replace(s1, remoteDevice0, local2)
  491. // Check that we have both folders and their data is in the global list
  492. expectedFolderList := []string{"test0", "test1"}
  493. actualFolderList := ldb.ListFolders()
  494. if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
  495. t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
  496. }
  497. if l := len(globalList(s0)); l != 3 {
  498. t.Errorf("Incorrect global length %d != 3 for s0", l)
  499. }
  500. if l := len(globalList(s1)); l != 3 {
  501. t.Errorf("Incorrect global length %d != 3 for s1", l)
  502. }
  503. // Drop one of them and check that it's gone.
  504. db.DropFolder(ldb, "test1")
  505. expectedFolderList = []string{"test0"}
  506. actualFolderList = ldb.ListFolders()
  507. if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
  508. t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
  509. }
  510. if l := len(globalList(s0)); l != 3 {
  511. t.Errorf("Incorrect global length %d != 3 for s0", l)
  512. }
  513. if l := len(globalList(s1)); l != 0 {
  514. t.Errorf("Incorrect global length %d != 0 for s1", l)
  515. }
  516. }
  517. func TestGlobalNeedWithInvalid(t *testing.T) {
  518. ldb := db.OpenMemory()
  519. s := db.NewFileSet("test1", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  520. rem0 := fileList{
  521. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  522. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true},
  523. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  524. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: remoteDevice0.Short(), Value: 1002}}}},
  525. }
  526. replace(s, remoteDevice0, rem0)
  527. rem1 := fileList{
  528. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  529. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  530. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true},
  531. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true, ModifiedS: 10},
  532. }
  533. replace(s, remoteDevice1, rem1)
  534. total := fileList{
  535. // There's a valid copy of each file, so it should be merged
  536. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  537. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  538. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  539. // in conflict and older, but still wins as the other is invalid
  540. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: remoteDevice0.Short(), Value: 1002}}}},
  541. }
  542. need := fileList(needList(s, protocol.LocalDeviceID))
  543. if fmt.Sprint(need) != fmt.Sprint(total) {
  544. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, total)
  545. }
  546. global := fileList(globalList(s))
  547. if fmt.Sprint(global) != fmt.Sprint(total) {
  548. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", global, total)
  549. }
  550. }
  551. func TestLongPath(t *testing.T) {
  552. ldb := db.OpenMemory()
  553. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  554. var b bytes.Buffer
  555. for i := 0; i < 100; i++ {
  556. b.WriteString("012345678901234567890123456789012345678901234567890")
  557. }
  558. name := b.String() // 5000 characters
  559. local := []protocol.FileInfo{
  560. {Name: string(name), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  561. }
  562. replace(s, protocol.LocalDeviceID, local)
  563. gf := globalList(s)
  564. if l := len(gf); l != 1 {
  565. t.Fatalf("Incorrect len %d != 1 for global list", l)
  566. }
  567. if gf[0].Name != local[0].Name {
  568. t.Errorf("Incorrect long filename;\n%q !=\n%q",
  569. gf[0].Name, local[0].Name)
  570. }
  571. }
  572. func TestCommitted(t *testing.T) {
  573. // Verify that the Committed counter increases when we change things and
  574. // doesn't increase when we don't.
  575. ldb := db.OpenMemory()
  576. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  577. local := []protocol.FileInfo{
  578. {Name: string("file"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  579. }
  580. // Adding a file should increase the counter
  581. c0 := ldb.Committed()
  582. replace(s, protocol.LocalDeviceID, local)
  583. c1 := ldb.Committed()
  584. if c1 <= c0 {
  585. t.Errorf("committed data didn't increase; %d <= %d", c1, c0)
  586. }
  587. // Updating with something identical should not do anything
  588. s.Update(protocol.LocalDeviceID, local)
  589. c2 := ldb.Committed()
  590. if c2 > c1 {
  591. t.Errorf("replace with same contents should do nothing but %d > %d", c2, c1)
  592. }
  593. }
  594. func BenchmarkUpdateOneFile(b *testing.B) {
  595. local0 := fileList{
  596. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  597. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  598. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  599. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  600. // A longer name is more realistic and causes more allocations
  601. protocol.FileInfo{Name: "zajksdhaskjdh/askjdhaskjdashkajshd/kasjdhaskjdhaskdjhaskdjash/dkjashdaksjdhaskdjahskdjh", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  602. }
  603. ldb, err := db.Open("testdata/benchmarkupdate.db")
  604. if err != nil {
  605. b.Fatal(err)
  606. }
  607. defer func() {
  608. ldb.Close()
  609. os.RemoveAll("testdata/benchmarkupdate.db")
  610. }()
  611. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  612. replace(m, protocol.LocalDeviceID, local0)
  613. l := local0[4:5]
  614. for i := 0; i < b.N; i++ {
  615. l[0].Version = l[0].Version.Update(myID)
  616. m.Update(protocol.LocalDeviceID, local0)
  617. }
  618. b.ReportAllocs()
  619. }
  620. func TestIndexID(t *testing.T) {
  621. ldb := db.OpenMemory()
  622. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  623. // The Index ID for some random device is zero by default.
  624. id := s.IndexID(remoteDevice0)
  625. if id != 0 {
  626. t.Errorf("index ID for remote device should default to zero, not %d", id)
  627. }
  628. // The Index ID for someone else should be settable
  629. s.SetIndexID(remoteDevice0, 42)
  630. id = s.IndexID(remoteDevice0)
  631. if id != 42 {
  632. t.Errorf("index ID for remote device should be remembered; got %d, expected %d", id, 42)
  633. }
  634. // Our own index ID should be generated randomly.
  635. id = s.IndexID(protocol.LocalDeviceID)
  636. if id == 0 {
  637. t.Errorf("index ID for local device should be random, not zero")
  638. }
  639. t.Logf("random index ID is 0x%016x", id)
  640. // But of course always the same after that.
  641. again := s.IndexID(protocol.LocalDeviceID)
  642. if again != id {
  643. t.Errorf("index ID changed; %d != %d", again, id)
  644. }
  645. }
  646. func TestDropFiles(t *testing.T) {
  647. ldb := db.OpenMemory()
  648. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  649. local0 := fileList{
  650. protocol.FileInfo{Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  651. protocol.FileInfo{Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  652. protocol.FileInfo{Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  653. protocol.FileInfo{Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  654. protocol.FileInfo{Name: "z", Sequence: 5, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  655. }
  656. remote0 := fileList{
  657. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  658. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  659. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  660. }
  661. // Insert files
  662. m.Update(protocol.LocalDeviceID, local0)
  663. m.Update(remoteDevice0, remote0)
  664. // Check that they're there
  665. h := haveList(m, protocol.LocalDeviceID)
  666. if len(h) != len(local0) {
  667. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  668. }
  669. h = haveList(m, remoteDevice0)
  670. if len(h) != len(remote0) {
  671. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  672. }
  673. g := globalList(m)
  674. if len(g) != len(local0) {
  675. // local0 covers all files
  676. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(local0))
  677. }
  678. // Drop the local files and recheck
  679. m.Drop(protocol.LocalDeviceID)
  680. h = haveList(m, protocol.LocalDeviceID)
  681. if len(h) != 0 {
  682. t.Errorf("Incorrect number of files after drop, %d != %d", len(h), 0)
  683. }
  684. h = haveList(m, remoteDevice0)
  685. if len(h) != len(remote0) {
  686. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  687. }
  688. g = globalList(m)
  689. if len(g) != len(remote0) {
  690. // the ones in remote0 remain
  691. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(remote0))
  692. }
  693. }
  694. func TestIssue4701(t *testing.T) {
  695. ldb := db.OpenMemory()
  696. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  697. localHave := fileList{
  698. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  699. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, LocalFlags: protocol.FlagLocalIgnored},
  700. }
  701. s.Update(protocol.LocalDeviceID, localHave)
  702. if c := s.LocalSize(); c.Files != 1 {
  703. t.Errorf("Expected 1 local file, got %v", c.Files)
  704. }
  705. if c := s.GlobalSize(); c.Files != 1 {
  706. t.Errorf("Expected 1 global file, got %v", c.Files)
  707. }
  708. localHave[1].LocalFlags = 0
  709. s.Update(protocol.LocalDeviceID, localHave)
  710. if c := s.LocalSize(); c.Files != 2 {
  711. t.Errorf("Expected 2 local files, got %v", c.Files)
  712. }
  713. if c := s.GlobalSize(); c.Files != 2 {
  714. t.Errorf("Expected 2 global files, got %v", c.Files)
  715. }
  716. localHave[0].LocalFlags = protocol.FlagLocalIgnored
  717. localHave[1].LocalFlags = protocol.FlagLocalIgnored
  718. s.Update(protocol.LocalDeviceID, localHave)
  719. if c := s.LocalSize(); c.Files != 0 {
  720. t.Errorf("Expected 0 local files, got %v", c.Files)
  721. }
  722. if c := s.GlobalSize(); c.Files != 0 {
  723. t.Errorf("Expected 0 global files, got %v", c.Files)
  724. }
  725. }
  726. func TestWithHaveSequence(t *testing.T) {
  727. ldb := db.OpenMemory()
  728. folder := "test"
  729. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  730. // The files must not be in alphabetical order
  731. localHave := fileList{
  732. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, RawInvalid: true},
  733. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  734. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  735. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  736. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  737. }
  738. replace(s, protocol.LocalDeviceID, localHave)
  739. i := 2
  740. s.WithHaveSequence(int64(i), func(fi db.FileIntf) bool {
  741. if f := fi.(protocol.FileInfo); !f.IsEquivalent(localHave[i-1], 0) {
  742. t.Fatalf("Got %v\nExpected %v", f, localHave[i-1])
  743. }
  744. i++
  745. return true
  746. })
  747. }
  748. func TestStressWithHaveSequence(t *testing.T) {
  749. // This races two loops against each other: one that contiously does
  750. // updates, and one that continously does sequence walks. The test fails
  751. // if the sequence walker sees a discontinuity.
  752. if testing.Short() {
  753. t.Skip("Takes a long time")
  754. }
  755. ldb := db.OpenMemory()
  756. folder := "test"
  757. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  758. var localHave []protocol.FileInfo
  759. for i := 0; i < 100; i++ {
  760. localHave = append(localHave, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Blocks: genBlocks(i * 10)})
  761. }
  762. done := make(chan struct{})
  763. t0 := time.Now()
  764. go func() {
  765. for time.Since(t0) < 10*time.Second {
  766. for j, f := range localHave {
  767. localHave[j].Version = f.Version.Update(42)
  768. }
  769. s.Update(protocol.LocalDeviceID, localHave)
  770. }
  771. close(done)
  772. }()
  773. var prevSeq int64 = 0
  774. loop:
  775. for {
  776. select {
  777. case <-done:
  778. break loop
  779. default:
  780. }
  781. s.WithHaveSequence(prevSeq+1, func(fi db.FileIntf) bool {
  782. if fi.SequenceNo() < prevSeq+1 {
  783. t.Fatal("Skipped ", prevSeq+1, fi.SequenceNo())
  784. }
  785. prevSeq = fi.SequenceNo()
  786. return true
  787. })
  788. }
  789. }
  790. func TestIssue4925(t *testing.T) {
  791. ldb := db.OpenMemory()
  792. folder := "test"
  793. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  794. localHave := fileList{
  795. protocol.FileInfo{Name: "dir"},
  796. protocol.FileInfo{Name: "dir.file"},
  797. protocol.FileInfo{Name: "dir/file"},
  798. }
  799. replace(s, protocol.LocalDeviceID, localHave)
  800. for _, prefix := range []string{"dir", "dir/"} {
  801. pl := haveListPrefixed(s, protocol.LocalDeviceID, prefix)
  802. if l := len(pl); l != 2 {
  803. t.Errorf("Expected 2, got %v local items below %v", l, prefix)
  804. }
  805. pl = globalListPrefixed(s, prefix)
  806. if l := len(pl); l != 2 {
  807. t.Errorf("Expected 2, got %v global items below %v", l, prefix)
  808. }
  809. }
  810. }
  811. func TestMoveGlobalBack(t *testing.T) {
  812. ldb := db.OpenMemory()
  813. folder := "test"
  814. file := "foo"
  815. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  816. localHave := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}, Blocks: genBlocks(1), ModifiedS: 10, Size: 1}}
  817. remote0Have := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}, {ID: remoteDevice0.Short(), Value: 1}}}, Blocks: genBlocks(2), ModifiedS: 0, Size: 2}}
  818. s.Update(protocol.LocalDeviceID, localHave)
  819. s.Update(remoteDevice0, remote0Have)
  820. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  821. t.Error("Expected 1 local need, got", need)
  822. } else if !need[0].IsEquivalent(remote0Have[0], 0) {
  823. t.Errorf("Local need incorrect;\n A: %v !=\n E: %v", need[0], remote0Have[0])
  824. }
  825. if need := needList(s, remoteDevice0); len(need) != 0 {
  826. t.Error("Expected no need for remote 0, got", need)
  827. }
  828. ls := s.LocalSize()
  829. if haveBytes := localHave[0].Size; ls.Bytes != haveBytes {
  830. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  831. }
  832. gs := s.GlobalSize()
  833. if globalBytes := remote0Have[0].Size; gs.Bytes != globalBytes {
  834. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  835. }
  836. // That's what happens when something becomes unignored or something.
  837. // In any case it will be moved back from first spot in the global list
  838. // which is the scenario to be tested here.
  839. remote0Have[0].Version = remote0Have[0].Version.Update(remoteDevice0.Short()).DropOthers(remoteDevice0.Short())
  840. s.Update(remoteDevice0, remote0Have)
  841. if need := needList(s, remoteDevice0); len(need) != 1 {
  842. t.Error("Expected 1 need for remote 0, got", need)
  843. } else if !need[0].IsEquivalent(localHave[0], 0) {
  844. t.Errorf("Need for remote 0 incorrect;\n A: %v !=\n E: %v", need[0], localHave[0])
  845. }
  846. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  847. t.Error("Expected no local need, got", need)
  848. }
  849. ls = s.LocalSize()
  850. if haveBytes := localHave[0].Size; ls.Bytes != haveBytes {
  851. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  852. }
  853. gs = s.GlobalSize()
  854. if globalBytes := localHave[0].Size; gs.Bytes != globalBytes {
  855. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  856. }
  857. }
  858. // TestIssue5007 checks, that updating the local device with an invalid file
  859. // info with the newest version does indeed remove that file from the list of
  860. // needed files.
  861. // https://github.com/syncthing/syncthing/issues/5007
  862. func TestIssue5007(t *testing.T) {
  863. ldb := db.OpenMemory()
  864. folder := "test"
  865. file := "foo"
  866. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  867. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}}}
  868. s.Update(remoteDevice0, fs)
  869. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  870. t.Fatal("Expected 1 local need, got", need)
  871. } else if !need[0].IsEquivalent(fs[0], 0) {
  872. t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
  873. }
  874. fs[0].LocalFlags = protocol.FlagLocalIgnored
  875. s.Update(protocol.LocalDeviceID, fs)
  876. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  877. t.Fatal("Expected no local need, got", need)
  878. }
  879. }
  880. // TestNeedDeleted checks that a file that doesn't exist locally isn't needed
  881. // when the global file is deleted.
  882. func TestNeedDeleted(t *testing.T) {
  883. ldb := db.OpenMemory()
  884. folder := "test"
  885. file := "foo"
  886. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  887. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}, Deleted: true}}
  888. s.Update(remoteDevice0, fs)
  889. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  890. t.Fatal("Expected no local need, got", need)
  891. }
  892. fs[0].Deleted = false
  893. fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
  894. s.Update(remoteDevice0, fs)
  895. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  896. t.Fatal("Expected 1 local need, got", need)
  897. } else if !need[0].IsEquivalent(fs[0], 0) {
  898. t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
  899. }
  900. fs[0].Deleted = true
  901. fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
  902. s.Update(remoteDevice0, fs)
  903. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  904. t.Fatal("Expected no local need, got", need)
  905. }
  906. }
  907. func TestReceiveOnlyAccounting(t *testing.T) {
  908. ldb := db.OpenMemory()
  909. folder := "test"
  910. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  911. local := protocol.DeviceID{1}
  912. remote := protocol.DeviceID{2}
  913. // Three files that have been created by the remote device
  914. version := protocol.Vector{Counters: []protocol.Counter{{ID: remote.Short(), Value: 1}}}
  915. files := fileList{
  916. protocol.FileInfo{Name: "f1", Size: 10, Sequence: 1, Version: version},
  917. protocol.FileInfo{Name: "f2", Size: 10, Sequence: 1, Version: version},
  918. protocol.FileInfo{Name: "f3", Size: 10, Sequence: 1, Version: version},
  919. }
  920. // We have synced them locally
  921. replace(s, protocol.LocalDeviceID, files)
  922. replace(s, remote, files)
  923. if n := s.LocalSize().Files; n != 3 {
  924. t.Fatal("expected 3 local files initially, not", n)
  925. }
  926. if n := s.LocalSize().Bytes; n != 30 {
  927. t.Fatal("expected 30 local bytes initially, not", n)
  928. }
  929. if n := s.GlobalSize().Files; n != 3 {
  930. t.Fatal("expected 3 global files initially, not", n)
  931. }
  932. if n := s.GlobalSize().Bytes; n != 30 {
  933. t.Fatal("expected 30 global bytes initially, not", n)
  934. }
  935. if n := s.ReceiveOnlyChangedSize().Files; n != 0 {
  936. t.Fatal("expected 0 receive only changed files initially, not", n)
  937. }
  938. if n := s.ReceiveOnlyChangedSize().Bytes; n != 0 {
  939. t.Fatal("expected 0 receive only changed bytes initially, not", n)
  940. }
  941. // Detected a local change in a receive only folder
  942. changed := files[0]
  943. changed.Version = changed.Version.Update(local.Short())
  944. changed.Size = 100
  945. changed.ModifiedBy = local.Short()
  946. changed.LocalFlags = protocol.FlagLocalReceiveOnly
  947. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{changed})
  948. // Check that we see the files
  949. if n := s.LocalSize().Files; n != 3 {
  950. t.Fatal("expected 3 local files after local change, not", n)
  951. }
  952. if n := s.LocalSize().Bytes; n != 120 {
  953. t.Fatal("expected 120 local bytes after local change, not", n)
  954. }
  955. if n := s.GlobalSize().Files; n != 3 {
  956. t.Fatal("expected 3 global files after local change, not", n)
  957. }
  958. if n := s.GlobalSize().Bytes; n != 30 {
  959. t.Fatal("expected 30 global files after local change, not", n)
  960. }
  961. if n := s.ReceiveOnlyChangedSize().Files; n != 1 {
  962. t.Fatal("expected 1 receive only changed file after local change, not", n)
  963. }
  964. if n := s.ReceiveOnlyChangedSize().Bytes; n != 100 {
  965. t.Fatal("expected 100 receive only changed btyes after local change, not", n)
  966. }
  967. // Fake a revert. That's a two step process, first converting our
  968. // changed file into a less preferred variant, then pulling down the old
  969. // version.
  970. changed.Version = protocol.Vector{}
  971. changed.LocalFlags &^= protocol.FlagLocalReceiveOnly
  972. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{changed})
  973. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{files[0]})
  974. // Check that we see the files, same data as initially
  975. if n := s.LocalSize().Files; n != 3 {
  976. t.Fatal("expected 3 local files after revert, not", n)
  977. }
  978. if n := s.LocalSize().Bytes; n != 30 {
  979. t.Fatal("expected 30 local bytes after revert, not", n)
  980. }
  981. if n := s.GlobalSize().Files; n != 3 {
  982. t.Fatal("expected 3 global files after revert, not", n)
  983. }
  984. if n := s.GlobalSize().Bytes; n != 30 {
  985. t.Fatal("expected 30 global bytes after revert, not", n)
  986. }
  987. if n := s.ReceiveOnlyChangedSize().Files; n != 0 {
  988. t.Fatal("expected 0 receive only changed files after revert, not", n)
  989. }
  990. if n := s.ReceiveOnlyChangedSize().Bytes; n != 0 {
  991. t.Fatal("expected 0 receive only changed bytes after revert, not", n)
  992. }
  993. }
  994. func TestNeedAfterUnignore(t *testing.T) {
  995. ldb := db.OpenMemory()
  996. folder := "test"
  997. file := "foo"
  998. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  999. remID := remoteDevice0.Short()
  1000. // Initial state: Devices in sync, locally ignored
  1001. local := protocol.FileInfo{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: remID, Value: 1}, {ID: myID, Value: 1}}}, ModifiedS: 10}
  1002. local.SetIgnored(myID)
  1003. remote := protocol.FileInfo{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: remID, Value: 1}, {ID: myID, Value: 1}}}, ModifiedS: 10}
  1004. s.Update(protocol.LocalDeviceID, fileList{local})
  1005. s.Update(remoteDevice0, fileList{remote})
  1006. // Unignore locally -> conflicting changes. Remote is newer, thus winning.
  1007. local.Version = local.Version.Update(myID)
  1008. local.Version = local.Version.DropOthers(myID)
  1009. local.LocalFlags = 0
  1010. local.ModifiedS = 0
  1011. s.Update(protocol.LocalDeviceID, fileList{local})
  1012. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  1013. t.Fatal("Expected one local need, got", need)
  1014. } else if !need[0].IsEquivalent(remote, 0) {
  1015. t.Fatalf("Got %v, expected %v", need[0], remote)
  1016. }
  1017. }
  1018. func TestRemoteInvalidNotAccounted(t *testing.T) {
  1019. // Remote files with the invalid bit should not count.
  1020. ldb := db.OpenMemory()
  1021. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1022. files := []protocol.FileInfo{
  1023. {Name: "a", Size: 1234, Sequence: 42, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}}, // valid, should count
  1024. {Name: "b", Size: 1234, Sequence: 43, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, RawInvalid: true}, // invalid, doesn't count
  1025. }
  1026. s.Update(remoteDevice0, files)
  1027. global := s.GlobalSize()
  1028. if global.Files != 1 {
  1029. t.Error("Expected one file in global size, not", global.Files)
  1030. }
  1031. if global.Bytes != 1234 {
  1032. t.Error("Expected 1234 bytes in global size, not", global.Bytes)
  1033. }
  1034. }
  1035. func TestNeedWithNewerInvalid(t *testing.T) {
  1036. ldb := db.OpenMemory()
  1037. s := db.NewFileSet("default", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1038. rem0ID := remoteDevice0.Short()
  1039. rem1ID := remoteDevice1.Short()
  1040. // Initial state: file present on rem0 and rem1, but not locally.
  1041. file := protocol.FileInfo{Name: "foo"}
  1042. file.Version = file.Version.Update(rem0ID)
  1043. s.Update(remoteDevice0, fileList{file})
  1044. s.Update(remoteDevice1, fileList{file})
  1045. need := needList(s, protocol.LocalDeviceID)
  1046. if len(need) != 1 {
  1047. t.Fatal("Locally missing file should be needed")
  1048. }
  1049. if !need[0].IsEquivalent(file, 0) {
  1050. t.Fatalf("Got needed file %v, expected %v", need[0], file)
  1051. }
  1052. // rem1 sends an invalid file with increased version
  1053. inv := file
  1054. inv.Version = inv.Version.Update(rem1ID)
  1055. inv.RawInvalid = true
  1056. s.Update(remoteDevice1, fileList{inv})
  1057. // We still have an old file, we need the newest valid file
  1058. need = needList(s, protocol.LocalDeviceID)
  1059. if len(need) != 1 {
  1060. t.Fatal("Locally missing file should be needed regardless of invalid files")
  1061. }
  1062. if !need[0].IsEquivalent(file, 0) {
  1063. t.Fatalf("Got needed file %v, expected %v", need[0], file)
  1064. }
  1065. }
  1066. func TestNeedAfterDeviceRemove(t *testing.T) {
  1067. ldb := db.OpenMemory()
  1068. file := "foo"
  1069. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1070. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}}}
  1071. s.Update(protocol.LocalDeviceID, fs)
  1072. fs[0].Version = fs[0].Version.Update(myID)
  1073. s.Update(remoteDevice0, fs)
  1074. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  1075. t.Fatal("Expected one local need, got", need)
  1076. }
  1077. s.Drop(remoteDevice0)
  1078. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  1079. t.Fatal("Expected no local need, got", need)
  1080. }
  1081. }
  1082. func TestCaseSensitive(t *testing.T) {
  1083. // Normal case sensitive lookup should work
  1084. ldb := db.OpenMemory()
  1085. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1086. local := []protocol.FileInfo{
  1087. {Name: filepath.FromSlash("D1/f1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1088. {Name: filepath.FromSlash("F1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1089. {Name: filepath.FromSlash("d1/F1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1090. {Name: filepath.FromSlash("d1/f1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1091. {Name: filepath.FromSlash("f1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1092. }
  1093. replace(s, protocol.LocalDeviceID, local)
  1094. gf := globalList(s)
  1095. if l := len(gf); l != len(local) {
  1096. t.Fatalf("Incorrect len %d != %d for global list", l, len(local))
  1097. }
  1098. for i := range local {
  1099. if gf[i].Name != local[i].Name {
  1100. t.Errorf("Incorrect filename;\n%q !=\n%q",
  1101. gf[i].Name, local[i].Name)
  1102. }
  1103. }
  1104. }
  1105. func TestSequenceIndex(t *testing.T) {
  1106. // This test attempts to verify correct operation of the sequence index.
  1107. // It's a stress test and needs to run for a long time, but we don't
  1108. // really have time for that in normal builds.
  1109. runtime := time.Minute
  1110. if testing.Short() {
  1111. runtime = time.Second
  1112. }
  1113. // Set up a db and a few files that we will manipulate.
  1114. ldb := db.OpenMemory()
  1115. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1116. local := []protocol.FileInfo{
  1117. {Name: filepath.FromSlash("banana"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1118. {Name: filepath.FromSlash("pineapple"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1119. {Name: filepath.FromSlash("orange"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1120. {Name: filepath.FromSlash("apple"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1121. {Name: filepath.FromSlash("jackfruit"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1122. }
  1123. // Start a background routine that makes updates to these files as fast
  1124. // as it can. We always update the same files in the same order.
  1125. done := make(chan struct{})
  1126. defer close(done)
  1127. go func() {
  1128. for {
  1129. select {
  1130. case <-done:
  1131. return
  1132. default:
  1133. }
  1134. for i := range local {
  1135. local[i].Version = local[i].Version.Update(42)
  1136. }
  1137. s.Update(protocol.LocalDeviceID, local)
  1138. }
  1139. }()
  1140. // Start a routine to walk the sequence index and inspect the result.
  1141. seen := make(map[string]db.FileIntf)
  1142. latest := make([]db.FileIntf, 0, len(local))
  1143. var seq int64
  1144. t0 := time.Now()
  1145. for time.Since(t0) < runtime {
  1146. // Walk the changes since our last iteration. This should give is
  1147. // one instance each of the files that are changed all the time, or
  1148. // a subset of those files if we manage to run before a complete
  1149. // update has happened since our last iteration.
  1150. latest = latest[:0]
  1151. s.WithHaveSequence(seq+1, func(f db.FileIntf) bool {
  1152. seen[f.FileName()] = f
  1153. latest = append(latest, f)
  1154. seq = f.SequenceNo()
  1155. return true
  1156. })
  1157. // Calculate the spread in sequence number.
  1158. var max, min int64
  1159. for _, v := range seen {
  1160. s := v.SequenceNo()
  1161. if max == 0 || max < s {
  1162. max = s
  1163. }
  1164. if min == 0 || min > s {
  1165. min = s
  1166. }
  1167. }
  1168. // We shouldn't see a spread larger than the number of files, as
  1169. // that would mean we have missed updates. For example, if we were
  1170. // to see the following:
  1171. //
  1172. // banana N
  1173. // pineapple N+1
  1174. // orange N+2
  1175. // apple N+10
  1176. // jackfruit N+11
  1177. //
  1178. // that would mean that there have been updates to banana, pineapple
  1179. // and orange that we didn't see in this pass. If those files aren't
  1180. // updated again, those updates are permanently lost.
  1181. if max-min > int64(len(local)) {
  1182. for _, v := range seen {
  1183. t.Log("seen", v.FileName(), v.SequenceNo())
  1184. }
  1185. for _, v := range latest {
  1186. t.Log("latest", v.FileName(), v.SequenceNo())
  1187. }
  1188. t.Fatal("large spread")
  1189. }
  1190. time.Sleep(time.Millisecond)
  1191. }
  1192. }
  1193. func replace(fs *db.FileSet, device protocol.DeviceID, files []protocol.FileInfo) {
  1194. fs.Drop(device)
  1195. fs.Update(device, files)
  1196. }