mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
@@ -1,7 +1,6 @@
|
|||||||
package file
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"io"
|
"io"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
"os"
|
"os"
|
||||||
@@ -22,10 +21,6 @@ type Message struct {
|
|||||||
Fto []*mail.Address
|
Fto []*mail.Address
|
||||||
Fsubject string
|
Fsubject string
|
||||||
Fsize int64
|
Fsize int64
|
||||||
// These are for creating new messages only
|
|
||||||
writable bool
|
|
||||||
writerFile *os.File
|
|
||||||
writer *bufio.Writer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// newMessage creates a new FileMessage object and sets the Date and ID fields.
|
// newMessage creates a new FileMessage object and sets the Date and ID fields.
|
||||||
@@ -48,7 +43,7 @@ func (mb *mbox) newMessage() (*Message, error) {
|
|||||||
}
|
}
|
||||||
date := time.Now()
|
date := time.Now()
|
||||||
id := generateID(date)
|
id := generateID(date)
|
||||||
return &Message{mailbox: mb, Fid: id, Fdate: date, writable: true}, nil
|
return &Message{mailbox: mb, Fid: id, Fdate: date}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mailbox returns the name of the mailbox this message resides in.
|
// Mailbox returns the name of the mailbox this message resides in.
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jhillyerd/inbucket/pkg/config"
|
"github.com/jhillyerd/inbucket/pkg/config"
|
||||||
@@ -21,15 +20,6 @@ import (
|
|||||||
const indexFileName = "index.gob"
|
const indexFileName = "index.gob"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// indexMx is locked while reading/writing an index file
|
|
||||||
//
|
|
||||||
// NOTE: This is a bottleneck because it's a single lock even if we have a
|
|
||||||
// million index files
|
|
||||||
indexMx = new(sync.RWMutex)
|
|
||||||
|
|
||||||
// dirMx is locked while creating/removing directories
|
|
||||||
dirMx = new(sync.Mutex)
|
|
||||||
|
|
||||||
// countChannel is filled with a sequential numbers (0000..9999), which are
|
// countChannel is filled with a sequential numbers (0000..9999), which are
|
||||||
// used by generateID() to generate unique message IDs. It's global
|
// used by generateID() to generate unique message IDs. It's global
|
||||||
// because we only want one regardless of the number of DataStore objects
|
// because we only want one regardless of the number of DataStore objects
|
||||||
|
|||||||
@@ -101,9 +101,6 @@ func (mb *mbox) purge() error {
|
|||||||
func (mb *mbox) readIndex() error {
|
func (mb *mbox) readIndex() error {
|
||||||
// Clear message slice, open index
|
// Clear message slice, open index
|
||||||
mb.messages = mb.messages[:0]
|
mb.messages = mb.messages[:0]
|
||||||
// Lock for reading
|
|
||||||
indexMx.RLock()
|
|
||||||
defer indexMx.RUnlock()
|
|
||||||
// Check if index exists
|
// Check if index exists
|
||||||
if _, err := os.Stat(mb.indexPath); err != nil {
|
if _, err := os.Stat(mb.indexPath); err != nil {
|
||||||
// Does not exist, but that's not an error in our world
|
// Does not exist, but that's not an error in our world
|
||||||
@@ -146,8 +143,6 @@ func (mb *mbox) readIndex() error {
|
|||||||
// writeIndex overwrites the index on disk with the current mailbox data
|
// writeIndex overwrites the index on disk with the current mailbox data
|
||||||
func (mb *mbox) writeIndex() error {
|
func (mb *mbox) writeIndex() error {
|
||||||
// Lock for writing
|
// Lock for writing
|
||||||
indexMx.Lock()
|
|
||||||
defer indexMx.Unlock()
|
|
||||||
if len(mb.messages) > 0 {
|
if len(mb.messages) > 0 {
|
||||||
// Ensure mailbox directory exists
|
// Ensure mailbox directory exists
|
||||||
if err := mb.createDir(); err != nil {
|
if err := mb.createDir(); err != nil {
|
||||||
@@ -189,8 +184,6 @@ func (mb *mbox) writeIndex() error {
|
|||||||
|
|
||||||
// createDir checks for the presence of the path for this mailbox, creates it if needed
|
// createDir checks for the presence of the path for this mailbox, creates it if needed
|
||||||
func (mb *mbox) createDir() error {
|
func (mb *mbox) createDir() error {
|
||||||
dirMx.Lock()
|
|
||||||
defer dirMx.Unlock()
|
|
||||||
if _, err := os.Stat(mb.path); err != nil {
|
if _, err := os.Stat(mb.path); err != nil {
|
||||||
if err := os.MkdirAll(mb.path, 0770); err != nil {
|
if err := os.MkdirAll(mb.path, 0770); err != nil {
|
||||||
log.Errorf("Failed to create directory %v, %v", mb.path, err)
|
log.Errorf("Failed to create directory %v, %v", mb.path, err)
|
||||||
@@ -202,8 +195,6 @@ func (mb *mbox) createDir() error {
|
|||||||
|
|
||||||
// removeDir removes the mailbox, plus empty higher level directories
|
// removeDir removes the mailbox, plus empty higher level directories
|
||||||
func (mb *mbox) removeDir() error {
|
func (mb *mbox) removeDir() error {
|
||||||
dirMx.Lock()
|
|
||||||
defer dirMx.Unlock()
|
|
||||||
// remove mailbox dir, including index file
|
// remove mailbox dir, including index file
|
||||||
if err := os.RemoveAll(mb.path); err != nil {
|
if err := os.RemoveAll(mb.path); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -217,7 +208,7 @@ func (mb *mbox) removeDir() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// removeDirIfEmpty will remove the specified directory if it contains no files or directories.
|
// removeDirIfEmpty will remove the specified directory if it contains no files or directories.
|
||||||
// Caller should hold dirMx. Returns true if dir was removed.
|
// Returns true if dir was removed.
|
||||||
func removeDirIfEmpty(path string) (removed bool) {
|
func removeDirIfEmpty(path string) (removed bool) {
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user