2023-01-30 16:27:06 +03:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2023-12-05 15:35:58 +03:00
|
|
|
"github.com/mjl-/mox/mlog"
|
2023-01-30 16:27:06 +03:00
|
|
|
"github.com/mjl-/mox/mox-"
|
|
|
|
)
|
|
|
|
|
2023-07-23 13:15:29 +03:00
|
|
|
// 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.
|
2023-12-05 15:35:58 +03:00
|
|
|
func CreateMessageTemp(log mlog.Log, pattern string) (*os.File, error) {
|
2023-01-30 16:27:06 +03:00
|
|
|
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 {
|
2023-02-16 15:22:00 +03:00
|
|
|
xerr := f.Close()
|
2023-12-05 15:35:58 +03:00
|
|
|
log.Check(xerr, "closing temp message file after chmod error")
|
2023-01-30 16:27:06 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f, err
|
|
|
|
}
|