From 8322437acde91ce34498a924176dcded3aaf9457 Mon Sep 17 00:00:00 2001 From: Leander Beernaert Date: Fri, 7 Oct 2022 09:24:27 +0200 Subject: [PATCH] feat: Add Store.List method List all IDs of the data available in the store. --- store/disk.go | 34 ++++++++++++++++++++++++++++++++++ store/store.go | 1 + store/store_test.go | 12 ++++++++++++ 3 files changed, 47 insertions(+) diff --git a/store/disk.go b/store/disk.go index 641193dc..92d42d76 100644 --- a/store/disk.go +++ b/store/disk.go @@ -4,6 +4,8 @@ import ( "crypto/aes" "crypto/cipher" "crypto/rand" + "github.com/sirupsen/logrus" + "io/fs" "os" "path/filepath" @@ -115,6 +117,38 @@ func (c *onDiskStore) Delete(messageIDs ...imap.InternalMessageID) error { return nil } +func (c *onDiskStore) List() ([]imap.InternalMessageID, error) { + if c.sem != nil { + c.sem.Lock() + defer c.sem.Unlock() + } + + var ids []imap.InternalMessageID + + if err := filepath.Walk(c.path, func(path string, info fs.FileInfo, err error) error { + if err != nil { + return err + } + + if info.IsDir() { + return nil + } + + id, err := imap.InternalMessageIDFromString(info.Name()) + if err != nil { + logrus.WithError(err).Errorf("Invalid id file in cache: %v", info.Name()) + } + + ids = append(ids, id) + + return nil + }); err != nil { + return nil, err + } + + return ids, nil +} + func (c *onDiskStore) Close() error { return nil } diff --git a/store/store.go b/store/store.go index fd858b8c..e1d2413b 100644 --- a/store/store.go +++ b/store/store.go @@ -7,6 +7,7 @@ type Store interface { Set(messageID imap.InternalMessageID, literal []byte) error Delete(messageID ...imap.InternalMessageID) error Close() error + List() ([]imap.InternalMessageID, error) } type Builder interface { diff --git a/store/store_test.go b/store/store_test.go index 2dbc5265..5d93bbbe 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -1,6 +1,7 @@ package store_test import ( + "github.com/ProtonMail/gluon/imap" "runtime" "testing" @@ -24,6 +25,7 @@ func TestOnDiskStore(t *testing.T) { require.NoError(t, err) testStore(t, store) + testStoreList(t, store) }) } } @@ -38,6 +40,16 @@ func testStore(t *testing.T, store store.Store) { require.Equal(t, []byte("literal3"), must(store.Get(3))) } +func testStoreList(t *testing.T, store store.Store) { + require.NoError(t, store.Set(1, []byte("literal1"))) + require.NoError(t, store.Set(2, []byte("literal2"))) + require.NoError(t, store.Set(3, []byte("literal3"))) + + list, err := store.List() + require.NoError(t, err) + require.ElementsMatch(t, list, []imap.InternalMessageID{1, 2, 3}) +} + func must[T any](val T, err error) T { if err != nil { panic(err)