mirror of
https://github.com/syncthing/syncthing.git
synced 2024-11-16 10:28:49 -07:00
lib/db: Deduplicate getFile* code (#5468)
This commit is contained in:
parent
96bd691f55
commit
1d99db9bc6
@ -40,20 +40,13 @@ func (db *instance) updateFiles(folder, device []byte, fs []protocol.FileInfo, m
|
|||||||
name := []byte(f.Name)
|
name := []byte(f.Name)
|
||||||
fk = db.keyer.GenerateDeviceFileKey(fk, folder, device, name)
|
fk = db.keyer.GenerateDeviceFileKey(fk, folder, device, name)
|
||||||
|
|
||||||
// Get and unmarshal the file entry. If it doesn't exist or can't be
|
ef, ok := t.getFileTrunc(fk, true)
|
||||||
// unmarshalled we'll add it as a new entry.
|
if ok && unchanged(f, ef) {
|
||||||
bs, err := t.Get(fk, nil)
|
|
||||||
var ef FileInfoTruncated
|
|
||||||
if err == nil {
|
|
||||||
err = ef.Unmarshal(bs)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err == nil && unchanged(f, ef) {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
devID := protocol.DeviceIDFromBytes(device)
|
devID := protocol.DeviceIDFromBytes(device)
|
||||||
if err == nil {
|
if ok {
|
||||||
meta.removeFile(devID, ef)
|
meta.removeFile(devID, ef)
|
||||||
}
|
}
|
||||||
meta.addFile(devID, f)
|
meta.addFile(devID, f)
|
||||||
@ -204,22 +197,9 @@ func (db *instance) withAllFolderTruncated(folder []byte, fn func(device []byte,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *instance) getFileDirty(folder, device, file []byte) (protocol.FileInfo, bool) {
|
func (db *instance) getFileDirty(folder, device, file []byte) (protocol.FileInfo, bool) {
|
||||||
key := db.keyer.GenerateDeviceFileKey(nil, folder, device, file)
|
t := db.newReadOnlyTransaction()
|
||||||
bs, err := db.Get(key, nil)
|
defer t.close()
|
||||||
if err == leveldb.ErrNotFound {
|
return t.getFile(folder, device, file)
|
||||||
return protocol.FileInfo{}, false
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
l.Debugln("surprise error:", err)
|
|
||||||
return protocol.FileInfo{}, false
|
|
||||||
}
|
|
||||||
|
|
||||||
var f protocol.FileInfo
|
|
||||||
if err := f.Unmarshal(bs); err != nil {
|
|
||||||
l.Debugln("unmarshal error:", err)
|
|
||||||
return protocol.FileInfo{}, false
|
|
||||||
}
|
|
||||||
return f, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *instance) getGlobalDirty(folder, file []byte, truncate bool) (FileIntf, bool) {
|
func (db *instance) getGlobalDirty(folder, file []byte, truncate bool) (FileIntf, bool) {
|
||||||
|
Loading…
Reference in New Issue
Block a user