mox/store/tmp.go
Mechiel Lukkien 3e9b4107fd
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.
2023-07-23 12:15:29 +02:00

29 lines
832 B
Go

package store
import (
"os"
"github.com/mjl-/mox/mox-"
)
// CreateMessageTemp creates a temporary file, e.g. for delivery. The is created in
// subdirectory tmp of the data directory, so the file is on the same file system
// as the accounts directory, so renaming files can succeed. The caller is
// responsible for closing and possibly removing the file. The caller should ensure
// the contents of the file are synced to disk before attempting to deliver the
// message.
func CreateMessageTemp(pattern string) (*os.File, error) {
dir := mox.DataDirPath("tmp")
os.MkdirAll(dir, 0770)
f, err := os.CreateTemp(dir, pattern)
if err != nil {
return nil, err
}
err = f.Chmod(0660)
if err != nil {
xerr := f.Close()
xlog.Check(xerr, "closing temp message file after chmod error")
return nil, err
}
return f, err
}