This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpipes.go
148 lines (141 loc) · 3.67 KB
/
pipes.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"errors"
"io"
"io/fs"
"os"
"path"
"syscall"
"time"
)
func createPipes(inst *instance) error {
var err error
inst.stdin, err = createOpenPipe(path.Join(inst.ConfDir, "stdin.pipe"), os.O_RDWR)
if err != nil {
inst.logger.Printf("Error opening stdin pipe: %s", err.Error())
return err
}
inst.stdout, err = createOpenPipe(path.Join(inst.ConfDir, "stdout.pipe"), os.O_RDWR)
if err != nil {
inst.logger.Printf("Error opening stdout pipe: %s", err.Error())
return err
}
inst.stderr, err = createOpenPipe(path.Join(inst.ConfDir, "stderr.pipe"), os.O_RDWR)
if err != nil {
inst.logger.Printf("Error opening stderr pipe: %s", err.Error())
return err
}
return nil
}
func createOpenPipe(instpath string, flag int) (*os.File, error) {
perm := fs.FileMode(cfg.GetDInt(644, "filePerms"))
err := syscall.Mkfifo(instpath, uint32(perm))
if err != nil {
return nil, err
}
return os.OpenFile(instpath, flag, os.ModeNamedPipe)
}
func openPipes(inst *instance) error {
var err error
inst.stdin, err = os.OpenFile(path.Join(inst.ConfDir, "stdin.pipe"), os.O_RDWR, os.ModeNamedPipe)
if err != nil {
inst.logger.Printf("Error opening stdin pipe: %s", err.Error())
return err
}
inst.stdout, err = os.OpenFile(path.Join(inst.ConfDir, "stdout.pipe"), os.O_RDWR, os.ModeNamedPipe)
if err != nil {
inst.logger.Printf("Error opening stdout pipe: %s", err.Error())
return err
}
inst.stderr, err = os.OpenFile(path.Join(inst.ConfDir, "stderr.pipe"), os.O_RDWR, os.ModeNamedPipe)
if err != nil {
inst.logger.Printf("Error opening stderr pipe: %s", err.Error())
return err
}
return nil
}
func closePipes(inst *instance) error {
err := inst.stdin.SetDeadline(time.Now().Add(1 * time.Second))
if err != nil {
inst.logger.Println("Failed to set deadline for stdin:", err)
return err
}
err = inst.stdin.Close()
if err != nil {
inst.logger.Println("Failed to close stdin:", err)
return err
}
err = inst.stderr.SetDeadline(time.Now().Add(1 * time.Second))
if err != nil {
inst.logger.Println("Failed to set deadline for stderr:", err)
return err
}
err = inst.stderr.Close()
if err != nil {
inst.logger.Println("Failed to close stderr:", err)
return err
}
err = inst.stdout.SetDeadline(time.Now().Add(1 * time.Second))
if err != nil {
inst.logger.Println("Failed to set deadline for stdout:", err)
return err
}
err = inst.stdout.Close()
if err != nil {
inst.logger.Println("Failed to close stdout:", err)
return err
}
return nil
}
func checkPipeContainsData(p string) (int64, error) {
fi, err := os.Stat(p)
if err != nil {
if os.IsNotExist(err) {
return 0, nil
}
return 0, err
}
if fi.Mode()&os.ModeNamedPipe == 0 {
return 0, errors.New("not a pipe")
}
return fi.Size(), nil
}
func drainRemovePipe(p string) error {
fi, err := os.Stat(p)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return errors.New("drain: " + err.Error())
}
if fi.Mode()&os.ModeNamedPipe == 0 {
return errors.New("not a pipe")
}
f, err := os.OpenFile(p, os.O_RDWR, os.ModeNamedPipe)
if err != nil {
return errors.New("drain: " + err.Error())
}
err = f.SetReadDeadline(time.Now().Add(1 * time.Second))
if err != nil {
return errors.New("drain: " + err.Error())
}
data, err := io.ReadAll(f)
if err != nil {
if !errors.Is(err, io.EOF) && !errors.Is(err, os.ErrDeadlineExceeded) {
return errors.New("drain: " + err.Error())
}
}
f.Close()
if len(data) > 0 {
perm := fs.FileMode(cfg.GetDInt(644, "filePerms"))
err := os.WriteFile(p+".txt", data, perm)
if err != nil {
return errors.New("drain: " + err.Error())
}
}
err = os.Remove(p)
if err != nil {
return errors.New("drain: " + err.Error())
}
return nil
}