From 46d4f7be1df7effd46c7c59701b2d10eba27547f Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sun, 13 Oct 2013 21:03:51 -0700 Subject: [PATCH] Message size calculation changes Message sizes are now calculated as the message is written out to disk, and saved into the index.gob file --- smtpd/filestore.go | 8 +++----- smtpd/filestore_test.go | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/smtpd/filestore.go b/smtpd/filestore.go index dde3add..4368de5 100644 --- a/smtpd/filestore.go +++ b/smtpd/filestore.go @@ -275,6 +275,7 @@ type FileMessage struct { Fdate time.Time Ffrom string Fsubject string + Fsize int64 // These are for creating new messages only writable bool writerFile *os.File @@ -310,11 +311,7 @@ func (m *FileMessage) String() string { } func (m *FileMessage) Size() int64 { - fi, err := os.Stat(m.rawPath()) - if err != nil { - return 0 - } - return fi.Size() + return m.Fsize } func (m *FileMessage) rawPath() string { @@ -399,6 +396,7 @@ func (m *FileMessage) Append(data []byte) error { m.writer = bufio.NewWriter(file) } _, err := m.writer.Write(data) + m.Fsize += int64(len(data)) return err } diff --git a/smtpd/filestore_test.go b/smtpd/filestore_test.go index 45e631e..85f7fae 100644 --- a/smtpd/filestore_test.go +++ b/smtpd/filestore_test.go @@ -44,7 +44,7 @@ func TestFSDirStructure(t *testing.T) { mbPath := expect expect = filepath.Join(mbPath, "index.gob") assert.True(t, isFile(expect), "Expected %q to be a file", expect) - expect = filepath.Join(mbPath, id1 + ".raw") + expect = filepath.Join(mbPath, id1+".raw") assert.True(t, isFile(expect), "Expected %q to be a file", expect) // Deliver second test message @@ -53,7 +53,7 @@ func TestFSDirStructure(t *testing.T) { // Check files expect = filepath.Join(mbPath, "index.gob") assert.True(t, isFile(expect), "Expected %q to be a file", expect) - expect = filepath.Join(mbPath, id2 + ".raw") + expect = filepath.Join(mbPath, id2+".raw") assert.True(t, isFile(expect), "Expected %q to be a file", expect) // Delete message @@ -65,7 +65,7 @@ func TestFSDirStructure(t *testing.T) { assert.Nil(t, err) // Message should be removed - expect = filepath.Join(mbPath, id1 + ".raw") + expect = filepath.Join(mbPath, id1+".raw") assert.False(t, isPresent(expect), "Did not expect %q to exist", expect) expect = filepath.Join(mbPath, "index.gob") assert.True(t, isFile(expect), "Expected %q to be a file", expect) @@ -77,7 +77,7 @@ func TestFSDirStructure(t *testing.T) { assert.Nil(t, err) // Message should be removed - expect = filepath.Join(mbPath, id2 + ".raw") + expect = filepath.Join(mbPath, id2+".raw") assert.False(t, isPresent(expect), "Did not expect %q to exist", expect) // No messages, index & maildir should be removed @@ -87,7 +87,6 @@ func TestFSDirStructure(t *testing.T) { assert.False(t, isPresent(expect), "Did not expect %q to exist", expect) } - // Test FileDataStore.AllMailboxes() func TestFSAllMailboxes(t *testing.T) { ds := setupDataStore() @@ -219,6 +218,37 @@ func TestFSDelete(t *testing.T) { } +// Test message size calculation +func TestFSSize(t *testing.T) { + ds := setupDataStore() + defer teardownDataStore(ds) + + mbName := "fred" + subjects := []string{"a", "br", "much longer than the others"} + sentIds := make([]string, len(subjects)) + sentSizes := make([]int, len(subjects)) + + for i, subj := range subjects { + // Add a message + id, size := deliverMessage(ds, mbName, subj, time.Now()) + sentIds[i] = id + sentSizes[i] = size + } + + mb, err := ds.MailboxFor(mbName) + if err != nil { + panic(err) + } + for i, id := range sentIds { + msg, err := mb.GetMessage(id) + assert.Nil(t, err) + + expect := sentSizes[i] + size := msg.Size() + assert.Equal(t, expect, size, "Expected size of %v, got %v", expect, size) + } +} + // setupDataStore creates a new FileDataStore in a temporary directory func setupDataStore() *FileDataStore { path, err := ioutil.TempDir("", "inbucket") @@ -283,4 +313,3 @@ func isDir(path string) bool { } return false } -