-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditfile_linux.go
More file actions
37 lines (32 loc) · 891 Bytes
/
editfile_linux.go
File metadata and controls
37 lines (32 loc) · 891 Bytes
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
//go:build linux
package main
import (
"fmt"
"os"
)
// newEditorFile creates the editor temp file in /dev/shm (RAM-only on Linux).
// Falls back to os.TempDir() if /dev/shm is unavailable or unwritable.
func newEditorFile(content []byte) (*editorFile, error) {
dir := "/dev/shm"
if _, err := os.Stat(dir); err != nil {
dir = ""
}
tmp, err := os.CreateTemp(dir, "nillsec-edit-*.json")
if err != nil && dir != "" {
tmp, err = os.CreateTemp("", "nillsec-edit-*.json")
}
if err != nil {
return nil, fmt.Errorf("cannot create editor file: %w", err)
}
path := tmp.Name()
if _, err := tmp.Write(content); err != nil {
tmp.Close()
os.Remove(path)
return nil, fmt.Errorf("cannot write editor file: %w", err)
}
if err := tmp.Close(); err != nil {
os.Remove(path)
return nil, fmt.Errorf("cannot close editor file: %w", err)
}
return &editorFile{fpath: path}, nil
}