mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-08 22:31:04 +00:00
lib/db: Use need func in withNeed and simplify (#6412)
This commit is contained in:
parent
c101a04179
commit
a1cb1d70c4
@ -8,11 +8,20 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/syncthing/syncthing/lib/db/backend"
|
"github.com/syncthing/syncthing/lib/db/backend"
|
||||||
"github.com/syncthing/syncthing/lib/protocol"
|
"github.com/syncthing/syncthing/lib/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type errDeviceEntryMissing struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err errDeviceEntryMissing) Error() string {
|
||||||
|
return fmt.Sprintf("device present in global list but missing as device/fileinfo entry: %s", err.name)
|
||||||
|
}
|
||||||
|
|
||||||
// A readOnlyTransaction represents a database snapshot.
|
// A readOnlyTransaction represents a database snapshot.
|
||||||
type readOnlyTransaction struct {
|
type readOnlyTransaction struct {
|
||||||
backend.ReadTransaction
|
backend.ReadTransaction
|
||||||
@ -344,53 +353,25 @@ func (t *readOnlyTransaction) withNeed(folder, device []byte, truncate bool, fn
|
|||||||
}
|
}
|
||||||
|
|
||||||
haveFV, have := vl.Get(device)
|
haveFV, have := vl.Get(device)
|
||||||
// XXX: This marks Concurrent (i.e. conflicting) changes as
|
|
||||||
// needs. Maybe we should do that, but it needs special
|
|
||||||
// handling in the puller.
|
|
||||||
if have && haveFV.Version.GreaterEqual(vl.Versions[0].Version) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
name := t.keyer.NameFromGlobalVersionKey(dbi.Key())
|
name := t.keyer.NameFromGlobalVersionKey(dbi.Key())
|
||||||
needVersion := vl.Versions[0].Version
|
dk, err = t.keyer.GenerateDeviceFileKey(dk, folder, vl.Versions[0].Device, name)
|
||||||
needDevice := protocol.DeviceIDFromBytes(vl.Versions[0].Device)
|
if err != nil {
|
||||||
|
return err
|
||||||
for i := range vl.Versions {
|
}
|
||||||
if !vl.Versions[i].Version.Equal(needVersion) {
|
gf, ok, err := t.getFileTrunc(dk, truncate)
|
||||||
// We haven't found a valid copy of the file with the needed version.
|
if err != nil {
|
||||||
break
|
return err
|
||||||
}
|
}
|
||||||
|
if !ok {
|
||||||
if vl.Versions[i].Invalid {
|
return errDeviceEntryMissing{string(name)}
|
||||||
// The file is marked invalid, don't use it.
|
}
|
||||||
continue
|
if !need(gf, have, haveFV.Version) {
|
||||||
}
|
continue
|
||||||
|
}
|
||||||
dk, err = t.keyer.GenerateDeviceFileKey(dk, folder, vl.Versions[i].Device, name)
|
l.Debugf("need folder=%q device=%v name=%q have=%v invalid=%v haveV=%v globalV=%v globalDev=%v", folder, devID, name, have, haveFV.Invalid, haveFV.Version, vl.Versions[0].Version, vl.Versions[0].Device)
|
||||||
if err != nil {
|
if !fn(gf) {
|
||||||
return err
|
return dbi.Error()
|
||||||
}
|
|
||||||
gf, ok, err := t.getFileTrunc(dk, truncate)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if gf.IsDeleted() && !have {
|
|
||||||
// We don't need deleted files that we don't have
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
l.Debugf("need folder=%q device=%v name=%q have=%v invalid=%v haveV=%v globalV=%v globalDev=%v", folder, devID, name, have, haveFV.Invalid, haveFV.Version, needVersion, needDevice)
|
|
||||||
|
|
||||||
if !fn(gf) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// This file is handled, no need to look further in the version list
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dbi.Error()
|
return dbi.Error()
|
||||||
|
Loading…
Reference in New Issue
Block a user