mirror of
https://github.com/mjl-/mox.git
synced 2025-07-12 22:14:40 +03:00
When opening an account, check for unexpected message files in the file system, and adjust the next message ID autoincrement sequence in the database to prevent future message delivery failures.
Just to be cautious. This hasn't happened yet in practice that I'm aware of. But in theory, mox could crash after it has written the message file during delivery, but before the database transaction was committed. In that case, a message file for the "next message id" is already present. Any future delivery attempts will get assigned the same message id by the database, but writing the file will fail because there already is one, causing delivery to fail (until the file is moved away). When opening an account, we now check in the file system if newer files exist than we expect based on the last existing message in the database. If so, we adjust the message ID the database will assign next.
This commit is contained in:
@ -312,3 +312,90 @@ test
|
||||
|
||||
// todo: test the SMTPMailFrom and VerifiedDomains rule.
|
||||
}
|
||||
|
||||
// Check that opening an account forwards the Message.ID used for new additions if
|
||||
// message files already exist in the file system.
|
||||
func TestNextMessageID(t *testing.T) {
|
||||
log := mlog.New("store", nil)
|
||||
os.RemoveAll("../testdata/store/data")
|
||||
mox.ConfigStaticPath = filepath.FromSlash("../testdata/store/mox.conf")
|
||||
mox.MustLoadConfig(true, false)
|
||||
|
||||
// Ensure account exists.
|
||||
acc, err := OpenAccount(log, "mjl", false)
|
||||
tcheck(t, err, "open account")
|
||||
err = acc.Close()
|
||||
tcheck(t, err, "closing account")
|
||||
acc = nil
|
||||
|
||||
// Create file on disk to occupy the first Message.ID that would otherwise be used for deliveries..
|
||||
msgData := []byte("a: b\r\n\r\ntest\r\n")
|
||||
msgDir := filepath.FromSlash("../testdata/store/data/accounts/mjl/msg")
|
||||
os.MkdirAll(filepath.Join(msgDir, "a"), 0700)
|
||||
msgPath := filepath.Join(msgDir, "a", "1")
|
||||
err = os.WriteFile(msgPath, msgData, 0700)
|
||||
tcheck(t, err, "write message file")
|
||||
|
||||
msgPathBogus := filepath.Join(msgDir, "a", "bogus")
|
||||
err = os.WriteFile(msgPathBogus, []byte("test"), 0700)
|
||||
msgPathBadID := filepath.Join(msgDir, "a", "10000") // Out of range.
|
||||
err = os.WriteFile(msgPathBadID, []byte("test"), 0700)
|
||||
|
||||
// Open account. This should increase the next message ID.
|
||||
acc, err = OpenAccount(log, "mjl", false)
|
||||
tcheck(t, err, "open account")
|
||||
defer Switchboard()()
|
||||
|
||||
// Deliver a message. It should get ID 2.
|
||||
mf, err := CreateMessageTemp(log, "account-test")
|
||||
tcheck(t, err, "creating temp message file")
|
||||
_, err = mf.Write(msgData)
|
||||
tcheck(t, err, "write file")
|
||||
defer CloseRemoveTempFile(log, mf, "temp message file")
|
||||
m := Message{
|
||||
Size: int64(len(msgData)),
|
||||
}
|
||||
err = acc.DeliverMailbox(log, "Inbox", &m, mf)
|
||||
tcheck(t, err, "deliver mailbox")
|
||||
if m.ID != 2 {
|
||||
t.Fatalf("got message id %d, expected 2", m.ID)
|
||||
}
|
||||
|
||||
// Ensure account consistency check won't complain.
|
||||
err = os.Remove(msgPath)
|
||||
tcheck(t, err, "removing message path")
|
||||
err = os.Remove(msgPathBogus)
|
||||
tcheck(t, err, "removing message path")
|
||||
err = os.Remove(msgPathBadID)
|
||||
tcheck(t, err, "removing message path")
|
||||
|
||||
err = acc.Close()
|
||||
tcheck(t, err, "closing account")
|
||||
|
||||
// Try again, but also create next message directory, but no file.
|
||||
os.MkdirAll(filepath.Join(msgDir, "b"), 0700)
|
||||
os.MkdirAll(filepath.Join(msgDir, "d"), 0700) // Not used.
|
||||
|
||||
// Open account again, increasing next message ID.
|
||||
acc, err = OpenAccount(log, "mjl", false)
|
||||
tcheck(t, err, "open account")
|
||||
|
||||
// Deliver a message. It should get ID 8*1024+1.
|
||||
mf, err = CreateMessageTemp(log, "account-test")
|
||||
tcheck(t, err, "creating temp message file")
|
||||
_, err = mf.Write(msgData)
|
||||
tcheck(t, err, "write file")
|
||||
defer CloseRemoveTempFile(log, mf, "temp message file")
|
||||
m = Message{
|
||||
Size: int64(len(msgData)),
|
||||
}
|
||||
err = acc.DeliverMailbox(log, "Inbox", &m, mf)
|
||||
tcheck(t, err, "deliver mailbox")
|
||||
if m.ID != 8*1024+1 {
|
||||
t.Fatalf("got message id %d, expected 8*1024+1", m.ID)
|
||||
}
|
||||
|
||||
err = acc.Close()
|
||||
tcheck(t, err, "closing account")
|
||||
acc.WaitClosed()
|
||||
}
|
||||
|
Reference in New Issue
Block a user