mirror of
https://github.com/mjl-/mox.git
synced 2024-12-26 16:33:47 +03:00
refactor common pattern of close & remove temporary file into calling the new store.CloseRemoveTempFile
This commit is contained in:
parent
b6897d1837
commit
ef50f4abf0
11 changed files with 15 additions and 81 deletions
8
ctl.go
8
ctl.go
|
@ -319,13 +319,7 @@ func servectlcmd(ctx context.Context, ctl *ctl, shutdown func()) {
|
|||
|
||||
msgFile, err := store.CreateMessageTemp("ctl-deliver")
|
||||
ctl.xcheck(err, "creating temporary message file")
|
||||
defer func() {
|
||||
name := msgFile.Name()
|
||||
err := msgFile.Close()
|
||||
log.Check(err, "closing temporary message file")
|
||||
err = os.Remove(name)
|
||||
log.Check(err, "removing temporary message file", mlog.Field("path", name))
|
||||
}()
|
||||
defer store.CloseRemoveTempFile(log, msgFile, "deliver message")
|
||||
mw := message.NewWriter(msgFile)
|
||||
ctl.xwriteok()
|
||||
|
||||
|
|
|
@ -311,13 +311,7 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
|||
conf, _ := a.Conf()
|
||||
|
||||
process := func(m *store.Message, msgf *os.File, origPath string) {
|
||||
defer func() {
|
||||
name := msgf.Name()
|
||||
err := msgf.Close()
|
||||
ctl.log.Check(err, "closing temporary message after failing to import")
|
||||
err = os.Remove(name)
|
||||
ctl.log.Check(err, "removing temporary message after failing to import", mlog.Field("path", name))
|
||||
}()
|
||||
defer store.CloseRemoveTempFile(ctl.log, msgf, "message to import")
|
||||
|
||||
for _, kw := range m.Keywords {
|
||||
mailboxKeywords[kw] = true
|
||||
|
|
|
@ -175,13 +175,7 @@ func deliverDSN(log *mlog.Log, m Msg, remoteMTA dsn.NameIP, secodeOpt, errmsg st
|
|||
qlog("creating temporary message file", err)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
name := msgFile.Name()
|
||||
err := msgFile.Close()
|
||||
log.Check(err, "closing message file")
|
||||
err = os.Remove(name)
|
||||
log.Check(err, "removing message file", mlog.Field("path", name))
|
||||
}()
|
||||
defer store.CloseRemoveTempFile(log, msgFile, "dsn message")
|
||||
|
||||
msgWriter := message.NewWriter(msgFile)
|
||||
if _, err := msgWriter.Write(msgData); err != nil {
|
||||
|
|
|
@ -290,13 +290,8 @@ Only implemented on unix systems, not Windows.
|
|||
log.Infox("making temporary message file for changelog delivery", err)
|
||||
return next
|
||||
}
|
||||
defer func() {
|
||||
name := f.Name()
|
||||
err = f.Close()
|
||||
log.Check(err, "closing temp changelog file")
|
||||
err := os.Remove(name)
|
||||
log.Check(err, "removing temp changelog file", mlog.Field("path", name))
|
||||
}()
|
||||
defer store.CloseRemoveTempFile(log, f, "message for changelog delivery")
|
||||
|
||||
m := &store.Message{
|
||||
Received: time.Now(),
|
||||
Flags: store.Flags{Flagged: true},
|
||||
|
|
|
@ -3,10 +3,8 @@ package smtpserver
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/mjl-/mox/dsn"
|
||||
"github.com/mjl-/mox/mlog"
|
||||
"github.com/mjl-/mox/queue"
|
||||
"github.com/mjl-/mox/smtp"
|
||||
"github.com/mjl-/mox/store"
|
||||
|
@ -30,13 +28,8 @@ func queueDSN(ctx context.Context, c *conn, rcptTo smtp.Path, m dsn.Message, req
|
|||
if err != nil {
|
||||
return fmt.Errorf("creating temp file: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
name := f.Name()
|
||||
err = f.Close()
|
||||
c.log.Check(err, "closing temporary dsn message file")
|
||||
err := os.Remove(name)
|
||||
c.log.Check(err, "removing temporary dsn message file", mlog.Field("path", name))
|
||||
}()
|
||||
defer store.CloseRemoveTempFile(c.log, f, "smtpserver dsn message")
|
||||
|
||||
if _, err := f.Write([]byte(buf)); err != nil {
|
||||
return fmt.Errorf("writing dsn file: %w", err)
|
||||
}
|
||||
|
|
|
@ -1532,13 +1532,7 @@ func (c *conn) cmdData(p *parser) {
|
|||
if err != nil {
|
||||
xsmtpServerErrorf(errCodes(smtp.C451LocalErr, smtp.SeSys3Other0, err), "creating temporary file for message: %s", err)
|
||||
}
|
||||
defer func() {
|
||||
name := dataFile.Name()
|
||||
err := dataFile.Close()
|
||||
c.log.Check(err, "removing temporary message file")
|
||||
err = os.Remove(name)
|
||||
c.log.Check(err, "removing temporary message file", mlog.Field("path", name))
|
||||
}()
|
||||
defer store.CloseRemoveTempFile(c.log, dataFile, "smtpserver delivered message")
|
||||
msgWriter := message.NewWriter(dataFile)
|
||||
dr := smtp.NewDataReader(c.r)
|
||||
n, err := io.Copy(&limitWriter{maxSize: c.maxMessageSize, w: msgWriter}, dr)
|
||||
|
|
|
@ -214,11 +214,7 @@ func ExportMessages(ctx context.Context, log *mlog.Log, db *bstore.DB, accountDi
|
|||
var mboxwriter *bufio.Writer
|
||||
defer func() {
|
||||
if mboxtmp != nil {
|
||||
name := mboxtmp.Name()
|
||||
err := mboxtmp.Close()
|
||||
log.Check(err, "closing mbox temp file")
|
||||
err = os.Remove(name)
|
||||
log.Check(err, "removing mbox temp file", mlog.Field("name", name))
|
||||
CloseRemoveTempFile(log, mboxtmp, "mbox")
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
@ -84,11 +84,7 @@ func (mr *MboxReader) Next() (*Message, *os.File, string, error) {
|
|||
}
|
||||
defer func() {
|
||||
if f != nil {
|
||||
name := f.Name()
|
||||
err := f.Close()
|
||||
mr.log.Check(err, "closing temporary message file after mbox read error")
|
||||
err = os.Remove(name)
|
||||
mr.log.Check(err, "removing temporary message file after mbox read error", mlog.Field("path", name))
|
||||
CloseRemoveTempFile(mr.log, f, "message after mbox read error")
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
@ -299,11 +299,7 @@ func Handle(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
defer func() {
|
||||
if tmpf != nil {
|
||||
name := tmpf.Name()
|
||||
err := tmpf.Close()
|
||||
log.Check(err, "closing uploaded file")
|
||||
err = os.Remove(name)
|
||||
log.Check(err, "removing temporary file", mlog.Field("path", name))
|
||||
store.CloseRemoveTempFile(log, tmpf, "upload")
|
||||
}
|
||||
}()
|
||||
if _, err := io.Copy(tmpf, f); err != nil {
|
||||
|
|
|
@ -202,11 +202,7 @@ type importStep struct {
|
|||
func importStart(log *mlog.Log, accName string, f *os.File, skipMailboxPrefix string) (string, error) {
|
||||
defer func() {
|
||||
if f != nil {
|
||||
name := f.Name()
|
||||
err := f.Close()
|
||||
log.Check(err, "closing uploaded file")
|
||||
err = os.Remove(name)
|
||||
log.Check(err, "removing uploaded file", mlog.Field("name", name))
|
||||
store.CloseRemoveTempFile(log, f, "upload for import")
|
||||
}
|
||||
}()
|
||||
|
||||
|
@ -316,11 +312,7 @@ func importMessages(ctx context.Context, log *mlog.Log, token string, acc *store
|
|||
}
|
||||
|
||||
defer func() {
|
||||
name := f.Name()
|
||||
err := f.Close()
|
||||
log.Check(err, "closing uploaded messages file")
|
||||
err = os.Remove(name)
|
||||
log.Check(err, "removing uploaded messages file", mlog.Field("path", name))
|
||||
store.CloseRemoveTempFile(log, f, "uploaded messages")
|
||||
|
||||
for _, id := range deliveredIDs {
|
||||
p := acc.MessagePath(id)
|
||||
|
@ -594,11 +586,7 @@ func importMessages(ctx context.Context, log *mlog.Log, token string, acc *store
|
|||
ximportcheckf(err, "creating temp message")
|
||||
defer func() {
|
||||
if f != nil {
|
||||
name := f.Name()
|
||||
err = f.Close()
|
||||
log.Check(err, "closing temporary file for delivery")
|
||||
err := os.Remove(name)
|
||||
log.Check(err, "removing temporary file for delivery", mlog.Field("path", name))
|
||||
store.CloseRemoveTempFile(log, f, "message to import")
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
@ -359,13 +359,7 @@ func (w Webmail) MessageSubmit(ctx context.Context, m SubmitMessage) {
|
|||
// Create file to compose message into.
|
||||
dataFile, err := store.CreateMessageTemp("webmail-submit")
|
||||
xcheckf(ctx, err, "creating temporary file for message")
|
||||
defer func() {
|
||||
name := dataFile.Name()
|
||||
err := dataFile.Close()
|
||||
log.Check(err, "closing submit message file")
|
||||
err = os.Remove(name)
|
||||
log.Check(err, "removing temporary submit message file", mlog.Field("name", name))
|
||||
}()
|
||||
defer store.CloseRemoveTempFile(log, dataFile, "message to submit")
|
||||
|
||||
// If writing to the message file fails, we abort immediately.
|
||||
xmsgw := &xerrWriter{ctx, bufio.NewWriter(dataFile), 0, w.maxMessageSize}
|
||||
|
|
Loading…
Reference in a new issue