fix(db): shareID is not removed

This commit is contained in:
hexxa 2022-01-23 13:54:25 +08:00 committed by Hexxa
parent 2904d03bdb
commit 958872731a
2 changed files with 28 additions and 13 deletions

View file

@ -139,11 +139,6 @@ func NewFileInfoStore(store kvstore.IKVStore) (*FileInfoStore, error) {
} }
} }
// err = store.SetStringIn(InitNs, SchemaVerKey, SchemaV1)
// if err != nil {
// return nil, err
// }
fi := &FileInfoStore{ fi := &FileInfoStore{
store: store, store: store,
mtx: &sync.RWMutex{}, mtx: &sync.RWMutex{},
@ -191,17 +186,29 @@ func (fi *FileInfoStore) DelSharing(dirPath string) error {
if err != nil { if err != nil {
return err return err
} }
info.Shared = false
info.ShareID = ""
// TODO: ensure Atomicity // TODO: ensure Atomicity
// In the bolt, if the key does not exist // In the bolt, if the key does not exist
// then nothing is done and a nil error is returned // then nothing is done and a nil error is returned
err = fi.store.DelStringIn(ShareIDNs, info.ShareID)
// because before this version, shareIDs are not removed correctly
// so it iterates all shareIDs and cleans remaining entries
shareIDtoDir, err := fi.store.ListStringsIn(ShareIDNs)
if err != nil { if err != nil {
return err return err
} }
for shareID, shareDir := range shareIDtoDir {
if shareDir == dirPath {
err = fi.store.DelStringIn(ShareIDNs, shareID)
if err != nil {
return err
}
}
}
info.ShareID = ""
info.Shared = false
return fi.SetInfo(dirPath, info) return fi.SetInfo(dirPath, info)
} }
@ -231,7 +238,6 @@ func (fi *FileInfoStore) ListSharings(prefix string) (map[string]string, error)
return nil, fmt.Errorf("list sharing error: %w", err) return nil, fmt.Errorf("list sharing error: %w", err)
} }
fmt.Println(infoStr)
if info.IsDir && info.Shared { if info.IsDir && info.Shared {
sharings[itemPath] = info.ShareID sharings[itemPath] = info.ShareID
} }
@ -319,7 +325,10 @@ func (fi *FileInfoStore) getShareID(payload string) (string, error) {
} }
shareID := fmt.Sprintf("%x", h.Sum(nil))[:7] shareID := fmt.Sprintf("%x", h.Sum(nil))[:7]
if _, ok := fi.store.GetStringIn(ShareIDNs, shareID); !ok { shareDir, ok := fi.store.GetStringIn(ShareIDNs, shareID)
if !ok {
return shareID, nil
} else if ok && shareDir == payload {
return shareID, nil return shareID, nil
} }
} }

View file

@ -60,12 +60,12 @@ func TestUserStores(t *testing.T) {
} }
} }
dirToID, err = store.ListSharings(prefix) dirToIDAfterDel, err := store.ListSharings(prefix)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
for _, dirPath := range dirPaths { for _, dirPath := range dirPaths {
if _, ok := dirToID[dirPath]; ok { if _, ok := dirToIDAfterDel[dirPath]; ok {
t.Fatalf("sharing(%s) should not exist", dirPath) t.Fatalf("sharing(%s) should not exist", dirPath)
} }
shared, exist := store.GetSharing(dirPath) shared, exist := store.GetSharing(dirPath)
@ -80,7 +80,13 @@ func TestUserStores(t *testing.T) {
t.Fatalf("ShareID should be empty %s", info.ShareID) t.Fatalf("ShareID should be empty %s", info.ShareID)
} }
_, err = store.GetSharingDir(info.ShareID) // shareIDs are removed, use original dirToID to get shareID
originalShareID, ok := dirToID[dirPath]
if !ok {
t.Fatalf("dir (%s) should exist in originalShareID", dirPath)
}
_, err = store.GetSharingDir(originalShareID)
if err != ErrSharingNotFound { if err != ErrSharingNotFound {
t.Fatal("should return ErrSharingNotFound") t.Fatal("should return ErrSharingNotFound")
} }