mirror of
https://github.com/mjl-/mox.git
synced 2024-12-27 08:53:48 +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")
|
msgFile, err := store.CreateMessageTemp("ctl-deliver")
|
||||||
ctl.xcheck(err, "creating temporary message file")
|
ctl.xcheck(err, "creating temporary message file")
|
||||||
defer func() {
|
defer store.CloseRemoveTempFile(log, msgFile, "deliver message")
|
||||||
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))
|
|
||||||
}()
|
|
||||||
mw := message.NewWriter(msgFile)
|
mw := message.NewWriter(msgFile)
|
||||||
ctl.xwriteok()
|
ctl.xwriteok()
|
||||||
|
|
||||||
|
|
|
@ -311,13 +311,7 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||||
conf, _ := a.Conf()
|
conf, _ := a.Conf()
|
||||||
|
|
||||||
process := func(m *store.Message, msgf *os.File, origPath string) {
|
process := func(m *store.Message, msgf *os.File, origPath string) {
|
||||||
defer func() {
|
defer store.CloseRemoveTempFile(ctl.log, msgf, "message to import")
|
||||||
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))
|
|
||||||
}()
|
|
||||||
|
|
||||||
for _, kw := range m.Keywords {
|
for _, kw := range m.Keywords {
|
||||||
mailboxKeywords[kw] = true
|
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)
|
qlog("creating temporary message file", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
defer store.CloseRemoveTempFile(log, msgFile, "dsn message")
|
||||||
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))
|
|
||||||
}()
|
|
||||||
|
|
||||||
msgWriter := message.NewWriter(msgFile)
|
msgWriter := message.NewWriter(msgFile)
|
||||||
if _, err := msgWriter.Write(msgData); err != nil {
|
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)
|
log.Infox("making temporary message file for changelog delivery", err)
|
||||||
return next
|
return next
|
||||||
}
|
}
|
||||||
defer func() {
|
defer store.CloseRemoveTempFile(log, f, "message for changelog delivery")
|
||||||
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))
|
|
||||||
}()
|
|
||||||
m := &store.Message{
|
m := &store.Message{
|
||||||
Received: time.Now(),
|
Received: time.Now(),
|
||||||
Flags: store.Flags{Flagged: true},
|
Flags: store.Flags{Flagged: true},
|
||||||
|
|
|
@ -3,10 +3,8 @@ package smtpserver
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/mjl-/mox/dsn"
|
"github.com/mjl-/mox/dsn"
|
||||||
"github.com/mjl-/mox/mlog"
|
|
||||||
"github.com/mjl-/mox/queue"
|
"github.com/mjl-/mox/queue"
|
||||||
"github.com/mjl-/mox/smtp"
|
"github.com/mjl-/mox/smtp"
|
||||||
"github.com/mjl-/mox/store"
|
"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 {
|
if err != nil {
|
||||||
return fmt.Errorf("creating temp file: %w", err)
|
return fmt.Errorf("creating temp file: %w", err)
|
||||||
}
|
}
|
||||||
defer func() {
|
defer store.CloseRemoveTempFile(c.log, f, "smtpserver dsn message")
|
||||||
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))
|
|
||||||
}()
|
|
||||||
if _, err := f.Write([]byte(buf)); err != nil {
|
if _, err := f.Write([]byte(buf)); err != nil {
|
||||||
return fmt.Errorf("writing dsn file: %w", err)
|
return fmt.Errorf("writing dsn file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1532,13 +1532,7 @@ func (c *conn) cmdData(p *parser) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
xsmtpServerErrorf(errCodes(smtp.C451LocalErr, smtp.SeSys3Other0, err), "creating temporary file for message: %s", err)
|
xsmtpServerErrorf(errCodes(smtp.C451LocalErr, smtp.SeSys3Other0, err), "creating temporary file for message: %s", err)
|
||||||
}
|
}
|
||||||
defer func() {
|
defer store.CloseRemoveTempFile(c.log, dataFile, "smtpserver delivered message")
|
||||||
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))
|
|
||||||
}()
|
|
||||||
msgWriter := message.NewWriter(dataFile)
|
msgWriter := message.NewWriter(dataFile)
|
||||||
dr := smtp.NewDataReader(c.r)
|
dr := smtp.NewDataReader(c.r)
|
||||||
n, err := io.Copy(&limitWriter{maxSize: c.maxMessageSize, w: msgWriter}, dr)
|
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
|
var mboxwriter *bufio.Writer
|
||||||
defer func() {
|
defer func() {
|
||||||
if mboxtmp != nil {
|
if mboxtmp != nil {
|
||||||
name := mboxtmp.Name()
|
CloseRemoveTempFile(log, mboxtmp, "mbox")
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
@ -84,11 +84,7 @@ func (mr *MboxReader) Next() (*Message, *os.File, string, error) {
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if f != nil {
|
if f != nil {
|
||||||
name := f.Name()
|
CloseRemoveTempFile(mr.log, f, "message after mbox read error")
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
@ -299,11 +299,7 @@ func Handle(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if tmpf != nil {
|
if tmpf != nil {
|
||||||
name := tmpf.Name()
|
store.CloseRemoveTempFile(log, tmpf, "upload")
|
||||||
err := tmpf.Close()
|
|
||||||
log.Check(err, "closing uploaded file")
|
|
||||||
err = os.Remove(name)
|
|
||||||
log.Check(err, "removing temporary file", mlog.Field("path", name))
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if _, err := io.Copy(tmpf, f); err != nil {
|
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) {
|
func importStart(log *mlog.Log, accName string, f *os.File, skipMailboxPrefix string) (string, error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if f != nil {
|
if f != nil {
|
||||||
name := f.Name()
|
store.CloseRemoveTempFile(log, f, "upload for import")
|
||||||
err := f.Close()
|
|
||||||
log.Check(err, "closing uploaded file")
|
|
||||||
err = os.Remove(name)
|
|
||||||
log.Check(err, "removing uploaded file", mlog.Field("name", name))
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -316,11 +312,7 @@ func importMessages(ctx context.Context, log *mlog.Log, token string, acc *store
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
name := f.Name()
|
store.CloseRemoveTempFile(log, f, "uploaded messages")
|
||||||
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))
|
|
||||||
|
|
||||||
for _, id := range deliveredIDs {
|
for _, id := range deliveredIDs {
|
||||||
p := acc.MessagePath(id)
|
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")
|
ximportcheckf(err, "creating temp message")
|
||||||
defer func() {
|
defer func() {
|
||||||
if f != nil {
|
if f != nil {
|
||||||
name := f.Name()
|
store.CloseRemoveTempFile(log, f, "message to import")
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
@ -359,13 +359,7 @@ func (w Webmail) MessageSubmit(ctx context.Context, m SubmitMessage) {
|
||||||
// Create file to compose message into.
|
// Create file to compose message into.
|
||||||
dataFile, err := store.CreateMessageTemp("webmail-submit")
|
dataFile, err := store.CreateMessageTemp("webmail-submit")
|
||||||
xcheckf(ctx, err, "creating temporary file for message")
|
xcheckf(ctx, err, "creating temporary file for message")
|
||||||
defer func() {
|
defer store.CloseRemoveTempFile(log, dataFile, "message to submit")
|
||||||
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))
|
|
||||||
}()
|
|
||||||
|
|
||||||
// If writing to the message file fails, we abort immediately.
|
// If writing to the message file fails, we abort immediately.
|
||||||
xmsgw := &xerrWriter{ctx, bufio.NewWriter(dataFile), 0, w.maxMessageSize}
|
xmsgw := &xerrWriter{ctx, bufio.NewWriter(dataFile), 0, w.maxMessageSize}
|
||||||
|
|
Loading…
Reference in a new issue