1package store
2
3import (
4 "os"
5
6 "github.com/mjl-/mox/mlog"
7 "github.com/mjl-/mox/mox-"
8)
9
10// CreateMessageTemp creates a temporary file, e.g. for delivery. The is created in
11// subdirectory tmp of the data directory, so the file is on the same file system
12// as the accounts directory, so renaming files can succeed. The caller is
13// responsible for closing and possibly removing the file. The caller should ensure
14// the contents of the file are synced to disk before attempting to deliver the
15// message.
16func CreateMessageTemp(log mlog.Log, pattern string) (*os.File, error) {
17 dir := mox.DataDirPath("tmp")
18 os.MkdirAll(dir, 0770)
19 f, err := os.CreateTemp(dir, pattern)
20 if err != nil {
21 return nil, err
22 }
23 err = f.Chmod(0660)
24 if err != nil {
25 xerr := f.Close()
26 log.Check(xerr, "closing temp message file after chmod error")
27 return nil, err
28 }
29 return f, err
30}
31