mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-13 00:36:28 +00:00
lib/db: Don't panic on unknown folder in ListFolders (fixes #3584)
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3869
This commit is contained in:
parent
2ebd6ad77f
commit
920274bce4
@ -526,9 +526,9 @@ func (db *Instance) ListFolders() []string {
|
|||||||
|
|
||||||
folderExists := make(map[string]bool)
|
folderExists := make(map[string]bool)
|
||||||
for dbi.Next() {
|
for dbi.Next() {
|
||||||
folder := string(db.globalKeyFolder(dbi.Key()))
|
folder, ok := db.globalKeyFolder(dbi.Key())
|
||||||
if !folderExists[folder] {
|
if ok && !folderExists[string(folder)] {
|
||||||
folderExists[folder] = true
|
folderExists[string(folder)] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -558,8 +558,8 @@ func (db *Instance) dropFolder(folder []byte) {
|
|||||||
// Remove all items related to the given folder from the global bucket
|
// Remove all items related to the given folder from the global bucket
|
||||||
dbi = t.NewIterator(util.BytesPrefix([]byte{KeyTypeGlobal}), nil)
|
dbi = t.NewIterator(util.BytesPrefix([]byte{KeyTypeGlobal}), nil)
|
||||||
for dbi.Next() {
|
for dbi.Next() {
|
||||||
itemFolder := db.globalKeyFolder(dbi.Key())
|
itemFolder, ok := db.globalKeyFolder(dbi.Key())
|
||||||
if bytes.Equal(folder, itemFolder) {
|
if ok && bytes.Equal(folder, itemFolder) {
|
||||||
db.Delete(dbi.Key(), nil)
|
db.Delete(dbi.Key(), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -680,12 +680,8 @@ func (db *Instance) globalKeyName(key []byte) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// globalKeyFolder returns the folder name from the key
|
// globalKeyFolder returns the folder name from the key
|
||||||
func (db *Instance) globalKeyFolder(key []byte) []byte {
|
func (db *Instance) globalKeyFolder(key []byte) ([]byte, bool) {
|
||||||
folder, ok := db.folderIdx.Val(binary.BigEndian.Uint32(key[keyPrefixLen:]))
|
return db.folderIdx.Val(binary.BigEndian.Uint32(key[keyPrefixLen:]))
|
||||||
if !ok {
|
|
||||||
panic("bug: lookup of nonexistent folder ID")
|
|
||||||
}
|
|
||||||
return folder
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *Instance) getIndexID(device, folder []byte) protocol.IndexID {
|
func (db *Instance) getIndexID(device, folder []byte) protocol.IndexID {
|
||||||
|
@ -45,7 +45,10 @@ func TestGlobalKey(t *testing.T) {
|
|||||||
|
|
||||||
key := db.globalKey(fld, name)
|
key := db.globalKey(fld, name)
|
||||||
|
|
||||||
fld2 := db.globalKeyFolder(key)
|
fld2, ok := db.globalKeyFolder(key)
|
||||||
|
if !ok {
|
||||||
|
t.Error("should have been found")
|
||||||
|
}
|
||||||
if !bytes.Equal(fld2, fld) {
|
if !bytes.Equal(fld2, fld) {
|
||||||
t.Errorf("wrong folder %q != %q", fld2, fld)
|
t.Errorf("wrong folder %q != %q", fld2, fld)
|
||||||
}
|
}
|
||||||
@ -53,4 +56,9 @@ func TestGlobalKey(t *testing.T) {
|
|||||||
if !bytes.Equal(name2, name) {
|
if !bytes.Equal(name2, name) {
|
||||||
t.Errorf("wrong name %q != %q", name2, name)
|
t.Errorf("wrong name %q != %q", name2, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, ok = db.globalKeyFolder([]byte{1, 2, 3, 4, 5})
|
||||||
|
if ok {
|
||||||
|
t.Error("should not have been found")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user