mirror of
https://github.com/mjl-/mox.git
synced 2025-07-13 13:34:37 +03:00
move "link or copy" functionality to moxio
and add a bit more logging for unexpected failures when closing files. and make tests pass with a TMPDIR on a different filesystem than the testdata directory.
This commit is contained in:
@ -240,14 +240,9 @@ func Add(ctx context.Context, log *mlog.Log, senderAccount string, mailFrom, rcp
|
||||
// Could be due to cross-filesystem rename. Users shouldn't configure their systems that way.
|
||||
return 0, fmt.Errorf("move message into queue dir: %w", err)
|
||||
}
|
||||
} else if err := os.Link(msgFile.Name(), dst); err != nil {
|
||||
// Assume file system does not support hardlinks. Copy it instead.
|
||||
if err := writeFile(dst, &moxio.AtReader{R: msgFile}); err != nil {
|
||||
return 0, fmt.Errorf("copying message to new file: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := moxio.SyncDir(dstDir); err != nil {
|
||||
} else if err := moxio.LinkOrCopy(log, dst, msgFile.Name(), nil, true); err != nil {
|
||||
return 0, fmt.Errorf("linking/copying message to new file: %s", err)
|
||||
} else if err := moxio.SyncDir(dstDir); err != nil {
|
||||
return 0, fmt.Errorf("sync directory: %v", err)
|
||||
}
|
||||
|
||||
@ -261,29 +256,6 @@ func Add(ctx context.Context, log *mlog.Log, senderAccount string, mailFrom, rcp
|
||||
return qm.ID, nil
|
||||
}
|
||||
|
||||
// write contents of r to new file dst, for delivering a message.
|
||||
func writeFile(dst string, r io.Reader) error {
|
||||
df, err := os.OpenFile(dst, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
if df != nil {
|
||||
err := df.Close()
|
||||
xlog.Check(err, "closing file after failed write")
|
||||
}
|
||||
}()
|
||||
if _, err := io.Copy(df, r); err != nil {
|
||||
return fmt.Errorf("copy: %s", err)
|
||||
} else if err := df.Sync(); err != nil {
|
||||
return fmt.Errorf("sync: %s", err)
|
||||
} else if err := df.Close(); err != nil {
|
||||
return fmt.Errorf("close: %s", err)
|
||||
}
|
||||
df = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func formatIPDomain(d dns.IPDomain) string {
|
||||
if len(d.IP) > 0 {
|
||||
return "[" + d.IP.String() + "]"
|
||||
|
@ -491,20 +491,6 @@ func TestQueueStart(t *testing.T) {
|
||||
time.Sleep(100 * time.Millisecond) // Racy... we won't get notified when work is done...
|
||||
}
|
||||
|
||||
func TestWriteFile(t *testing.T) {
|
||||
name := "../testdata/queue.test"
|
||||
os.Remove(name)
|
||||
defer os.Remove(name)
|
||||
err := writeFile(name, strings.NewReader("test"))
|
||||
if err != nil {
|
||||
t.Fatalf("writeFile, unexpected error %v", err)
|
||||
}
|
||||
buf, err := os.ReadFile(name)
|
||||
if err != nil || string(buf) != "test" {
|
||||
t.Fatalf("writeFile, read file, got err %v, data %q", err, buf)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGatherHosts(t *testing.T) {
|
||||
mox.Context = ctxbg
|
||||
|
||||
|
Reference in New Issue
Block a user