From 4881a6336f3348717cbddae272efe2d1a595d929 Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Tue, 23 Jun 2020 07:29:27 +0200 Subject: [PATCH] lib/db: Correct need accounting on reset (fixes #6784) (#6785) --- lib/db/meta.go | 10 +++++++--- lib/db/set_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/db/meta.go b/lib/db/meta.go index e7cabb7fa..5e9ef3a12 100644 --- a/lib/db/meta.go +++ b/lib/db/meta.go @@ -314,9 +314,13 @@ func (m *metadataTracker) resetAll(dev protocol.DeviceID) { m.dirty = true for i, c := range m.counts.Counts { if bytes.Equal(c.DeviceID, dev[:]) { - m.counts.Counts[i] = Counts{ - DeviceID: c.DeviceID, - LocalFlags: c.LocalFlags, + if c.LocalFlags != needFlag { + m.counts.Counts[i] = Counts{ + DeviceID: c.DeviceID, + LocalFlags: c.LocalFlags, + } + } else { + m.counts.Counts[i] = m.allNeededCounts(dev) } } } diff --git a/lib/db/set_test.go b/lib/db/set_test.go index d97a66bcc..0078887a9 100644 --- a/lib/db/set_test.go +++ b/lib/db/set_test.go @@ -1671,6 +1671,32 @@ func TestNeedRemoteOnly(t *testing.T) { } } +// https://github.com/syncthing/syncthing/issues/6784 +func TestNeedRemoteAfterReset(t *testing.T) { + ldb := db.NewLowlevel(backend.OpenMemory()) + defer ldb.Close() + + s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeFake, ""), ldb) + + files := fileList{ + protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)}, + } + s.Update(protocol.LocalDeviceID, files) + s.Update(remoteDevice0, files) + + need := needSize(s, remoteDevice0) + if !need.Equal(db.Counts{}) { + t.Error("Expected nothing needed, got", need) + } + + s.Drop(remoteDevice0) + + need = needSize(s, remoteDevice0) + if exp := (db.Counts{Files: 1}); !need.Equal(exp) { + t.Errorf("Expected %v, got %v", exp, need) + } +} + func replace(fs *db.FileSet, device protocol.DeviceID, files []protocol.FileInfo) { fs.Drop(device) fs.Update(device, files)