-
Notifications
You must be signed in to change notification settings - Fork 6
/
pickup.go
55 lines (47 loc) · 1.07 KB
/
pickup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"crypto/rand"
"fmt"
"os"
"path/filepath"
)
type pickup string
func (p pickup) submit(msg *message) (err error) {
file, err := newTempFile()
if err != nil {
return
}
encode(file, msg)
err = file.Close()
if err != nil {
return
}
// move to pickup directory
// Instead of creating the file elsewhere and then moving to the pickup directory,
// it is also possible to create the file directly in it. However, since PowerMTA
// must repeatedly attempt to lock the file for exclusive access, it is less efficient
// to do so.
name := file.Name()
dest := filepath.Join(string(p), filepath.Base(name))
err = os.Rename(name, dest)
if err != nil {
return
}
return
}
var tempDir = os.TempDir()
func newTempFile() (file *os.File, err error) {
for n := 0; n < 10; n++ {
name := filepath.Join(tempDir, randName())
file, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err == nil || !os.IsExist(err) {
return
}
}
return
}
func randName() string {
b := make([]byte, 8)
rand.Read(b)
return fmt.Sprintf("%x.eml", b)
}