diff options
author | Robin Jarry <robin@jarry.cc> | 2024-08-14 16:59:11 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2024-08-28 12:06:01 +0200 |
commit | 73dc39c6ee0827fc68b93af8dc438b0e1c14e929 (patch) | |
tree | aff067600ea6326ff179447ed968b6712013b889 /worker/maildir | |
parent | 2950d919a5c5a55bd0eb53d6c41f989d8b70bd55 (diff) | |
download | aerc-73dc39c6ee0827fc68b93af8dc438b0e1c14e929.tar.gz |
treewide: replace uint32 uids with opaque strings
Add a new models.UID type (an alias to string). Replace all occurrences
of uint32 being used as message UID or thread UID with models.UID.
Update all workers to only expose models.UID values and deal with the
conversion internally. Only IMAP needs to convert these to uint32. All
other backends already use plain strings as message identifiers, in
which case no conversion is even needed.
The directory tree implementation needed to be heavily refactored in
order to accommodate thread UID not being usable as a list index.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Inwit <inwit@sindominio.net>
Tested-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'worker/maildir')
-rw-r--r-- | worker/maildir/container.go | 73 | ||||
-rw-r--r-- | worker/maildir/message.go | 4 | ||||
-rw-r--r-- | worker/maildir/search.go | 9 | ||||
-rw-r--r-- | worker/maildir/worker.go | 16 |
4 files changed, 40 insertions, 62 deletions
diff --git a/worker/maildir/container.go b/worker/maildir/container.go index dea1ded6..c23825da 100644 --- a/worker/maildir/container.go +++ b/worker/maildir/container.go @@ -9,7 +9,7 @@ import ( "github.com/emersion/go-maildir" "git.sr.ht/~rjarry/aerc/lib/log" - "git.sr.ht/~rjarry/aerc/lib/uidstore" + "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/lib" ) @@ -17,8 +17,7 @@ import ( // the Maildir spec type Container struct { Store *lib.MaildirStore - uids *uidstore.Store - recentUIDS map[uint32]struct{} // used to set the recent flag + recentUIDS map[models.UID]struct{} // used to set the recent flag } // NewContainer creates a new container at the specified directory @@ -28,8 +27,8 @@ func NewContainer(dir string, maildirpp bool) (*Container, error) { return nil, err } return &Container{ - Store: store, uids: uidstore.NewStore(), - recentUIDS: make(map[uint32]struct{}), + Store: store, + recentUIDS: make(map[models.UID]struct{}), }, nil } @@ -40,8 +39,7 @@ func (c *Container) SyncNewMail(dir maildir.Dir) error { return err } for _, key := range keys { - uid := c.uids.GetOrInsert(key) - c.recentUIDS[uid] = struct{}{} + c.recentUIDS[models.UID(key)] = struct{}{} } return nil } @@ -57,18 +55,18 @@ func (c *Container) OpenDirectory(name string) (maildir.Dir, error) { } // IsRecent returns if a uid has the Recent flag set -func (c *Container) IsRecent(uid uint32) bool { +func (c *Container) IsRecent(uid models.UID) bool { _, ok := c.recentUIDS[uid] return ok } // ClearRecentFlag removes the Recent flag from the message with the given uid -func (c *Container) ClearRecentFlag(uid uint32) { +func (c *Container) ClearRecentFlag(uid models.UID) { delete(c.recentUIDS, uid) } // UIDs fetches the unique message identifiers for the maildir -func (c *Container) UIDs(d maildir.Dir) ([]uint32, error) { +func (c *Container) UIDs(d maildir.Dir) ([]models.UID, error) { keys, err := d.Keys() if err != nil && len(keys) == 0 { return nil, fmt.Errorf("could not get keys for %s: %w", d, err) @@ -77,39 +75,26 @@ func (c *Container) UIDs(d maildir.Dir) ([]uint32, error) { log.Errorf("could not get all keys for %s: %s", d, err.Error()) } sort.Strings(keys) - var uids []uint32 + var uids []models.UID for _, key := range keys { - uids = append(uids, c.uids.GetOrInsert(key)) + uids = append(uids, models.UID(key)) } return uids, err } // Message returns a Message struct for the given UID and maildir -func (c *Container) Message(d maildir.Dir, uid uint32) (*Message, error) { - if key, ok := c.uids.GetKey(uid); ok { - return &Message{ - dir: d, - uid: uid, - key: key, - }, nil - } - return nil, fmt.Errorf("could not find message with uid %d in maildir %s", - uid, d) -} - -func (c *Container) MessageFromKey(d maildir.Dir, key string) *Message { - uid := c.uids.GetOrInsert(key) +func (c *Container) Message(d maildir.Dir, uid models.UID) (*Message, error) { return &Message{ dir: d, uid: uid, - key: key, - } + key: string(uid), + }, nil } // DeleteAll deletes a set of messages by UID and returns the subset of UIDs // which were successfully deleted, stopping upon the first error. -func (c *Container) DeleteAll(d maildir.Dir, uids []uint32) ([]uint32, error) { - var success []uint32 +func (c *Container) DeleteAll(d maildir.Dir, uids []models.UID) ([]models.UID, error) { + var success []models.UID for _, uid := range uids { msg, err := c.Message(d, uid) if err != nil { @@ -124,46 +109,38 @@ func (c *Container) DeleteAll(d maildir.Dir, uids []uint32) ([]uint32, error) { } func (c *Container) CopyAll( - dest maildir.Dir, src maildir.Dir, uids []uint32, + dest maildir.Dir, src maildir.Dir, uids []models.UID, ) error { for _, uid := range uids { if err := c.copyMessage(dest, src, uid); err != nil { - return fmt.Errorf("could not copy message %d: %w", uid, err) + return fmt.Errorf("could not copy message %s: %w", uid, err) } } return nil } func (c *Container) copyMessage( - dest maildir.Dir, src maildir.Dir, uid uint32, + dest maildir.Dir, src maildir.Dir, uid models.UID, ) error { - key, ok := c.uids.GetKey(uid) - if !ok { - return fmt.Errorf("could not find key for message id %d", uid) - } - _, err := src.Copy(dest, key) + _, err := src.Copy(dest, string(uid)) return err } -func (c *Container) MoveAll(dest maildir.Dir, src maildir.Dir, uids []uint32) ([]uint32, error) { - var success []uint32 +func (c *Container) MoveAll(dest maildir.Dir, src maildir.Dir, uids []models.UID) ([]models.UID, error) { + var success []models.UID for _, uid := range uids { if err := c.moveMessage(dest, src, uid); err != nil { - return success, fmt.Errorf("could not move message %d: %w", uid, err) + return success, fmt.Errorf("could not move message %s: %w", uid, err) } success = append(success, uid) } return success, nil } -func (c *Container) moveMessage(dest maildir.Dir, src maildir.Dir, uid uint32) error { - key, ok := c.uids.GetKey(uid) - if !ok { - return fmt.Errorf("could not find key for message id %d", uid) - } - path, err := src.Filename(key) +func (c *Container) moveMessage(dest maildir.Dir, src maildir.Dir, uid models.UID) error { + path, err := src.Filename(string(uid)) if err != nil { - return fmt.Errorf("could not find path for message id %d", uid) + return fmt.Errorf("could not find path for message id %s: %w", uid, err) } // Remove encoded UID information from the key to prevent sync issues name := lib.StripUIDFromMessageFilename(filepath.Base(path)) diff --git a/worker/maildir/message.go b/worker/maildir/message.go index 1d8d26b9..14c83721 100644 --- a/worker/maildir/message.go +++ b/worker/maildir/message.go @@ -15,7 +15,7 @@ import ( // A Message is an individual email inside of a maildir.Dir. type Message struct { dir maildir.Dir - uid uint32 + uid models.UID key string } @@ -135,7 +135,7 @@ func (m Message) NewBodyPartReader(requestedParts []int) (io.Reader, error) { return rfc822.FetchEntityPartReader(msg, requestedParts) } -func (m Message) UID() uint32 { +func (m Message) UID() models.UID { return m.uid } diff --git a/worker/maildir/search.go b/worker/maildir/search.go index 90c84087..de2d0530 100644 --- a/worker/maildir/search.go +++ b/worker/maildir/search.go @@ -6,11 +6,12 @@ import ( "sync" "git.sr.ht/~rjarry/aerc/lib/log" + "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/lib" "git.sr.ht/~rjarry/aerc/worker/types" ) -func (w *Worker) search(ctx context.Context, criteria *types.SearchCriteria) ([]uint32, error) { +func (w *Worker) search(ctx context.Context, criteria *types.SearchCriteria) ([]models.UID, error) { criteria.PrepareHeader() requiredParts := lib.GetRequiredParts(criteria) w.worker.Debugf("Required parts bitmask for search: %b", requiredParts) @@ -20,7 +21,7 @@ func (w *Worker) search(ctx context.Context, criteria *types.SearchCriteria) ([] return nil, err } - matchedUids := []uint32{} + var matchedUids []models.UID mu := sync.Mutex{} wg := sync.WaitGroup{} // Hard limit at 2x CPU cores @@ -33,7 +34,7 @@ func (w *Worker) search(ctx context.Context, criteria *types.SearchCriteria) ([] default: limit <- struct{}{} wg.Add(1) - go func(key uint32) { + go func(key models.UID) { defer log.PanicHandler() defer wg.Done() success, err := w.searchKey(key, criteria, requiredParts) @@ -55,7 +56,7 @@ func (w *Worker) search(ctx context.Context, criteria *types.SearchCriteria) ([] } // Execute the search criteria for the given key, returns true if search succeeded -func (w *Worker) searchKey(key uint32, criteria *types.SearchCriteria, +func (w *Worker) searchKey(key models.UID, criteria *types.SearchCriteria, parts lib.MsgParts, ) (bool, error) { message, err := w.c.Message(*w.selected, key) diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go index 2c5bc893..74efdc3c 100644 --- a/worker/maildir/worker.go +++ b/worker/maildir/worker.go @@ -460,7 +460,7 @@ func (w *Worker) handleFetchDirectoryContents( msg *types.FetchDirectoryContents, ) error { var ( - uids []uint32 + uids []models.UID err error ) if msg.Filter != nil { @@ -494,7 +494,7 @@ func (w *Worker) handleFetchDirectoryContents( return nil } -func (w *Worker) sort(ctx context.Context, uids []uint32, criteria []*types.SortCriterion) ([]uint32, error) { +func (w *Worker) sort(ctx context.Context, uids []models.UID, criteria []*types.SortCriterion) ([]models.UID, error) { if len(criteria) == 0 { // At least sort by uid, parallel searching can create random // order @@ -516,7 +516,7 @@ func (w *Worker) sort(ctx context.Context, uids []uint32, criteria []*types.Sort default: limit <- struct{}{} wg.Add(1) - go func(uid uint32) { + go func(uid models.UID) { defer log.PanicHandler() defer wg.Done() info, err := w.msgHeadersFromUid(uid) @@ -546,7 +546,7 @@ func (w *Worker) handleFetchDirectoryThreaded( msg *types.FetchDirectoryThreaded, ) error { var ( - uids []uint32 + uids []models.UID err error ) if msg.Filter != nil { @@ -574,7 +574,7 @@ func (w *Worker) handleFetchDirectoryThreaded( return nil } -func (w *Worker) threads(ctx context.Context, uids []uint32, +func (w *Worker) threads(ctx context.Context, uids []models.UID, criteria []*types.SortCriterion, ) ([]*types.Thread, error) { builder := aercLib.NewThreadBuilder(iterator.NewFactory(false), false) @@ -590,7 +590,7 @@ func (w *Worker) threads(ctx context.Context, uids []uint32, default: limit <- struct{}{} wg.Add(1) - go func(uid uint32) { + go func(uid models.UID) { defer log.PanicHandler() defer wg.Done() info, err := w.msgHeadersFromUid(uid) @@ -903,7 +903,7 @@ func (w *Worker) handleSearchDirectory(msg *types.SearchDirectory) error { return nil } -func (w *Worker) msgInfoFromUid(uid uint32) (*models.MessageInfo, error) { +func (w *Worker) msgInfoFromUid(uid models.UID) (*models.MessageInfo, error) { m, err := w.c.Message(*w.selected, uid) if err != nil { return nil, err @@ -923,7 +923,7 @@ func (w *Worker) msgInfoFromUid(uid uint32) (*models.MessageInfo, error) { return info, nil } -func (w *Worker) msgHeadersFromUid(uid uint32) (*models.MessageInfo, error) { +func (w *Worker) msgHeadersFromUid(uid models.UID) (*models.MessageInfo, error) { m, err := w.c.Message(*w.selected, uid) if err != nil { return nil, err |