...
1
2
3
4 package memexec
5
6 import (
7 "os"
8 "runtime"
9 )
10
11 func open(b []byte) (*os.File, error) {
12 pattern := "go-memexec-"
13 if runtime.GOOS == "windows" {
14 pattern = "go-memexec-*.exe"
15 }
16 f, err := os.CreateTemp("", pattern)
17 if err != nil {
18 return nil, err
19 }
20 defer func() {
21 if err != nil {
22 _ = clean(f)
23 }
24 }()
25 if err = os.Chmod(f.Name(), 0o500); err != nil {
26 return nil, err
27 }
28 if _, err = f.Write(b); err != nil {
29 return nil, err
30 }
31 if err = f.Close(); err != nil {
32 return nil, err
33 }
34 return f, nil
35 }
36
37 func clean(f *os.File) error {
38 return os.Remove(f.Name())
39 }
40
View as plain text