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:
Jakob Borg 2017-01-04 10:34:52 +00:00 committed by Audrius Butkevicius
parent 2ebd6ad77f
commit 920274bce4
2 changed files with 16 additions and 12 deletions

View File

@ -526,9 +526,9 @@ func (db *Instance) ListFolders() []string {
folderExists := make(map[string]bool)
for dbi.Next() {
folder := string(db.globalKeyFolder(dbi.Key()))
if !folderExists[folder] {
folderExists[folder] = true
folder, ok := db.globalKeyFolder(dbi.Key())
if ok && !folderExists[string(folder)] {
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
dbi = t.NewIterator(util.BytesPrefix([]byte{KeyTypeGlobal}), nil)
for dbi.Next() {
itemFolder := db.globalKeyFolder(dbi.Key())
if bytes.Equal(folder, itemFolder) {
itemFolder, ok := db.globalKeyFolder(dbi.Key())
if ok && bytes.Equal(folder, itemFolder) {
db.Delete(dbi.Key(), nil)
}
}
@ -680,12 +680,8 @@ func (db *Instance) globalKeyName(key []byte) []byte {
}
// globalKeyFolder returns the folder name from the key
func (db *Instance) globalKeyFolder(key []byte) []byte {
folder, ok := db.folderIdx.Val(binary.BigEndian.Uint32(key[keyPrefixLen:]))
if !ok {
panic("bug: lookup of nonexistent folder ID")
}
return folder
func (db *Instance) globalKeyFolder(key []byte) ([]byte, bool) {
return db.folderIdx.Val(binary.BigEndian.Uint32(key[keyPrefixLen:]))
}
func (db *Instance) getIndexID(device, folder []byte) protocol.IndexID {

View File

@ -45,7 +45,10 @@ func TestGlobalKey(t *testing.T) {
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) {
t.Errorf("wrong folder %q != %q", fld2, fld)
}
@ -53,4 +56,9 @@ func TestGlobalKey(t *testing.T) {
if !bytes.Equal(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")
}
}