-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipes.go
More file actions
68 lines (62 loc) · 1.42 KB
/
pipes.go
File metadata and controls
68 lines (62 loc) · 1.42 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
package cont
import (
"github.com/google/uuid"
"golang.org/x/sys/unix"
"os"
"path/filepath"
)
func PipePath(id uuid.UUID) string {
dir := os.TempDir()
return filepath.Join(dir, id.String())
}
func pipeNames(path string) (inPath, outPath, errPath string) {
inPath = path + ".in"
outPath = path + ".out"
errPath = path + ".err"
return inPath, outPath, errPath
}
func CreatePipes(path string) error {
inPath, outPath, errPath := pipeNames(path)
if err := unix.Mkfifo(outPath, 0666); err != nil {
return err
}
if err := unix.Mkfifo(inPath, 0666); err != nil {
return err
}
if err := unix.Mkfifo(errPath, 0666); err != nil {
return err
}
return nil
}
func RemovePipes(path string) error {
inPath, outPath, errPath := pipeNames(path)
if err := os.Remove(inPath); err != nil {
return err
}
if err := os.Remove(outPath); err != nil {
return err
}
if err := os.Remove(errPath); err != nil {
return err
}
return nil
}
func OpenPipes(path string) (files [3]*os.File, err error) {
inPath, outPath, errPath := pipeNames(path)
inPipe, err := os.OpenFile(inPath, os.O_RDWR, os.ModeNamedPipe)
if err != nil {
return files, err
}
outPipe, err := os.OpenFile(outPath, os.O_RDWR, os.ModeNamedPipe)
if err != nil {
return files, err
}
errPipe, err := os.OpenFile(errPath, os.O_RDWR, os.ModeNamedPipe)
if err != nil {
return files, err
}
files[0] = inPipe
files[1] = outPipe
files[2] = errPipe
return files, err
}