浏览代码

lib/db: Correct need accounting on reset (fixes #6784) (#6785)

Simon Frei 5 年之前
父节点
当前提交
4881a6336f
共有 2 个文件被更改,包括 33 次插入3 次删除
  1. 7 3
      lib/db/meta.go
  2. 26 0
      lib/db/set_test.go

+ 7 - 3
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)
 			}
 		}
 	}

+ 26 - 0
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)