maildir: replace ListFolder method with FolderMap

Replace ListFolder with a new method that returns a map indexed by
folder names instead of a list of folder names. A map is simpler to use
and more efficient in case we only want to check the presence of a
specific folder in the Maildir store.

Signed-off-by: Julian Pidancet <julian.pidancet@oracle.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
This commit is contained in:
Julian Pidancet 2022-10-26 22:29:03 +02:00 committed by Robin Jarry
parent f021bfd1c7
commit ea10b329dd
2 changed files with 10 additions and 11 deletions

View file

@ -34,12 +34,11 @@ func NewMaildirStore(root string, maildirpp bool) (*MaildirStore, error) {
}, nil }, nil
} }
// ListFolders returns a list of maildir folders in the container func (s *MaildirStore) FolderMap() (map[string]maildir.Dir, error) {
func (s *MaildirStore) ListFolders() ([]string, error) { folders := make(map[string]maildir.Dir)
folders := []string{}
if s.maildirpp { if s.maildirpp {
// In Maildir++ layout, INBOX is the root folder // In Maildir++ layout, INBOX is the root folder
folders = append(folders, "INBOX") folders["INBOX"] = maildir.Dir(s.root)
} }
err := filepath.Walk(s.root, func(path string, info os.FileInfo, err error) error { err := filepath.Walk(s.root, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
@ -81,14 +80,14 @@ func (s *MaildirStore) ListFolders() ([]string, error) {
} }
dirPath = strings.TrimPrefix(dirPath, ".") dirPath = strings.TrimPrefix(dirPath, ".")
dirPath = strings.ReplaceAll(dirPath, ".", "/") dirPath = strings.ReplaceAll(dirPath, ".", "/")
folders = append(folders, dirPath) folders[dirPath] = maildir.Dir(path)
// Since all mailboxes are stored in a single directory, don't // Since all mailboxes are stored in a single directory, don't
// recurse into subdirectories // recurse into subdirectories
return filepath.SkipDir return filepath.SkipDir
} }
folders = append(folders, dirPath) folders[dirPath] = maildir.Dir(path)
return nil return nil
}) })
return folders, err return folders, err

View file

@ -329,12 +329,12 @@ func (w *Worker) handleListDirectories(msg *types.ListDirectories) error {
if w.c == nil { if w.c == nil {
return errors.New("Incorrect maildir directory") return errors.New("Incorrect maildir directory")
} }
dirs, err := w.c.Store.ListFolders() dirs, err := w.c.Store.FolderMap()
if err != nil { if err != nil {
logging.Errorf("failed listing directories: %v", err) logging.Errorf("failed listing directories: %v", err)
return err return err
} }
for _, name := range dirs { for name := range dirs {
w.worker.PostMessage(&types.Directory{ w.worker.PostMessage(&types.Directory{
Message: types.RespondTo(msg), Message: types.RespondTo(msg),
Dir: &models.Directory{ Dir: &models.Directory{
@ -733,12 +733,12 @@ func (w *Worker) handleCheckMail(msg *types.CheckMail) {
if err != nil { if err != nil {
w.err(msg, fmt.Errorf("checkmail: error running command: %w", err)) w.err(msg, fmt.Errorf("checkmail: error running command: %w", err))
} else { } else {
dirs, err := w.c.Store.ListFolders() dirs, err := w.c.Store.FolderMap()
if err != nil { if err != nil {
w.err(msg, fmt.Errorf("failed listing directories: %w", err)) w.err(msg, fmt.Errorf("failed listing directories: %w", err))
} }
for _, name := range dirs { for name, dir := range dirs {
err := w.c.SyncNewMail(w.c.Store.Dir(name)) err := w.c.SyncNewMail(dir)
if err != nil { if err != nil {
w.err(msg, fmt.Errorf("could not sync new mail: %w", err)) w.err(msg, fmt.Errorf("could not sync new mail: %w", err))
} }