Before moving message files in imapserver and webmail API, ensure the message destination directory for the newly assigned IDs exist.

Example symptom, when deleting a message in the webmail (which moves to Trash):

        l=error m="duplicating message in old mailbox for current sessions" err="link data/accounts/mjl/msg/I/368638 data/accounts/mjl/msg/J/368640: no such file or directory" pkg=webmail

Problem introduced a few weeks ago, where moving messages starting duplicating
the message first, and the copy is erased once all references (in IMAP
sessions) to the old mailbox have been removed.
This commit is contained in:
Mechiel Lukkien
2025-03-21 10:18:39 +01:00
parent 99f9eb438f
commit 75036c3a71
6 changed files with 61 additions and 9 deletions

View File

@ -713,14 +713,25 @@ func Add(ctx context.Context, log mlog.Log, senderAccount string, msgFile *os.Fi
}
}()
syncDirs := map[string]struct{}{}
for _, qm := range qml {
dst := qm.MessagePath()
paths = append(paths, dst)
dstDir := filepath.Dir(dst)
os.MkdirAll(dstDir, 0770)
if _, ok := syncDirs[dstDir]; !ok {
os.MkdirAll(dstDir, 0770)
syncDirs[dstDir] = struct{}{}
}
if err := moxio.LinkOrCopy(log, dst, msgFile.Name(), nil, true); err != nil {
return fmt.Errorf("linking/copying message to new file: %s", err)
} else if err := moxio.SyncDir(log, dstDir); err != nil {
}
}
for dir := range syncDirs {
if err := moxio.SyncDir(log, dir); err != nil {
return fmt.Errorf("sync directory: %v", err)
}
}