diff --git a/src/db/fileinfostore/file_info_store.go b/src/db/fileinfostore/file_info_store.go index 775fc30..1d046ef 100644 --- a/src/db/fileinfostore/file_info_store.go +++ b/src/db/fileinfostore/file_info_store.go @@ -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{ store: store, mtx: &sync.RWMutex{}, @@ -191,17 +186,29 @@ func (fi *FileInfoStore) DelSharing(dirPath string) error { if err != nil { return err } - info.Shared = false - info.ShareID = "" // TODO: ensure Atomicity // In the bolt, if the key does not exist // 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 { 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) } @@ -231,7 +238,6 @@ func (fi *FileInfoStore) ListSharings(prefix string) (map[string]string, error) return nil, fmt.Errorf("list sharing error: %w", err) } - fmt.Println(infoStr) if info.IsDir && info.Shared { sharings[itemPath] = info.ShareID } @@ -319,7 +325,10 @@ func (fi *FileInfoStore) getShareID(payload string) (string, error) { } 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 } } diff --git a/src/db/fileinfostore/file_info_store_test.go b/src/db/fileinfostore/file_info_store_test.go index 3419f08..80940a3 100644 --- a/src/db/fileinfostore/file_info_store_test.go +++ b/src/db/fileinfostore/file_info_store_test.go @@ -60,12 +60,12 @@ func TestUserStores(t *testing.T) { } } - dirToID, err = store.ListSharings(prefix) + dirToIDAfterDel, err := store.ListSharings(prefix) if err != nil { t.Fatal(err) } for _, dirPath := range dirPaths { - if _, ok := dirToID[dirPath]; ok { + if _, ok := dirToIDAfterDel[dirPath]; ok { t.Fatalf("sharing(%s) should not exist", dirPath) } shared, exist := store.GetSharing(dirPath) @@ -80,7 +80,13 @@ func TestUserStores(t *testing.T) { 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 { t.Fatal("should return ErrSharingNotFound") }