/
ip.go
231 lines (201 loc) · 6.46 KB
/
ip.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package scipipe
import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"time"
)
// ======= IP ========
type IP interface {
Id() string
LocalPath() string
ContentHash() string
UpdateContentHash()
ReadyToWrite() bool
Content() []byte // Good for params, etc
MakeReadyToWrite() // Whatever needs to be done before opening/writing to the file
MakeReadyToRead()
FixStuffAfterWriting() // Whatever needs to be done after a file is finished writing to
FixStuffAfterReading() // Whatever needs to be done after a file is finished writing to
}
// LocalFileIP (Short for "Information Packet" in Flow-Based Programming terminology)
// contains information and helper methods for a physical file on a normal disk.
type LocalFileIP struct {
BaseIP
path string
}
// NewLocalFileIP creates a new IP, to represent a file on the local file system
func NewLocalFileIP(path string) *LocalFileIP {
ip := &LocalFileIP{
BaseIP: NewBaseIP(path),
path: path,
}
return ip
}
// Path returns the (final) path of the physical file
func (ip *LocalFileIP) Path() string {
return ip.path
}
// TempPath returns the temporary path of the physical file
func (ip *LocalFileIP) TempPath() string {
return ip.path + ".tmp"
}
// FifoPath returns the path to use when a FIFO file is used instead of a
// normal file
func (ip *LocalFileIP) FifoPath() string {
return ip.path + ".fifo"
}
// Size returns the size of an existing file, in bytes
func (ip *LocalFileIP) Size() int64 {
fi, err := os.Stat(ip.path)
Check(err)
return fi.Size()
}
// Open opens the file and returns a file handle (*os.File)
func (ip *LocalFileIP) Open() *os.File {
f, err := os.Open(ip.Path())
CheckWithMsg(err, "Could not open file: "+ip.Path())
return f
}
// OpenTemp opens the temp file and returns a file handle (*os.File)
func (ip *LocalFileIP) OpenTemp() *os.File {
f, err := os.Open(ip.TempPath())
CheckWithMsg(err, "Could not open temp file: "+ip.TempPath())
return f
}
// OpenWriteTemp opens the file for writing, and returns a file handle (*os.File)
func (ip *LocalFileIP) OpenWriteTemp() *os.File {
f, err := os.Create(ip.TempPath())
CheckWithMsg(err, "Could not open temp file for writing: "+ip.TempPath())
return f
}
// Read reads the whole content of the file and returns the content as a byte
// array
func (ip *LocalFileIP) Read() []byte {
dat, err := ioutil.ReadFile(ip.Path())
CheckWithMsg(err, "Could not open file for reading: "+ip.Path())
return dat
}
// ReadAuditFile reads the content of the audit file and return it as a byte array
func (ip *LocalFileIP) ReadAuditFile() []byte {
dat, err := ioutil.ReadFile(ip.AuditFilePath())
CheckWithMsg(err, "Could not open file for reading: "+ip.AuditFilePath())
return dat
}
// WriteTempFile writes a byte array ([]byte) to the file's temp path
func (ip *LocalFileIP) WriteTempFile(dat []byte) {
err := ioutil.WriteFile(ip.TempPath(), dat, 0644)
CheckWithMsg(err, "Could not write to temp file: "+ip.TempPath())
}
const (
sleepDurationSec = 1
)
// Atomize renames the temporary file name to the final file name, thus enabling
// to separate unfinished, and finished files
func (ip *LocalFileIP) Atomize() {
Debug.Println("IP: Atomizing", ip.TempPath(), "->", ip.Path())
doneAtomizing := false
for !doneAtomizing {
if ip.TempFileExists() {
ip.lock.Lock()
err := os.Rename(ip.TempPath(), ip.path)
CheckWithMsg(err, "Could not rename file: "+ip.TempPath())
ip.lock.Unlock()
doneAtomizing = true
Debug.Println("IP: Done atomizing", ip.TempPath(), "->", ip.Path())
} else {
Debug.Printf("Sleeping for %d seconds before atomizing ...\n", sleepDurationSec)
time.Sleep(time.Duration(sleepDurationSec) * time.Second)
}
}
}
// CreateFifo creates a FIFO file for the IP
func (ip *LocalFileIP) CreateFifo() {
ip.lock.Lock()
cmd := "mkfifo " + ip.FifoPath()
Debug.Println("Now creating FIFO with command:", cmd)
if _, err := os.Stat(ip.FifoPath()); err == nil {
Warning.Println("FIFO already exists, so not creating a new one:", ip.FifoPath())
} else {
_, err := exec.Command("bash", "-c", cmd).Output()
CheckWithMsg(err, "Could not execute command: "+cmd)
}
ip.lock.Unlock()
}
// RemoveFifo removes the FIFO file, if it exists
func (ip *LocalFileIP) RemoveFifo() {
// FIXME: Shouldn't we check first whether the fifo exists?
ip.lock.Lock()
output, err := exec.Command("bash", "-c", "rm "+ip.FifoPath()).Output()
CheckWithMsg(err, "Could not delete fifo file: "+ip.FifoPath())
Debug.Println("Removed FIFO output: ", output)
ip.lock.Unlock()
}
// Exists checks if the file exists (at its final file name)
func (ip *LocalFileIP) Exists() bool {
exists := false
ip.lock.Lock()
if _, err := os.Stat(ip.Path()); err == nil {
exists = true
}
ip.lock.Unlock()
return exists
}
// TempFileExists checks if the temp-file exists
func (ip *LocalFileIP) TempFileExists() bool {
tempFileExists := false
ip.lock.Lock()
if _, err := os.Stat(ip.TempPath()); err == nil {
tempFileExists = true
}
ip.lock.Unlock()
return tempFileExists
}
// FifoFileExists checks if the FIFO-file (named pipe file) exists
func (ip *LocalFileIP) FifoFileExists() bool {
fifoFileExists := false
ip.lock.Lock()
if _, err := os.Stat(ip.FifoPath()); err == nil {
fifoFileExists = true
}
ip.lock.Unlock()
return fifoFileExists
}
// --------------------------------------------------------------------------------
// IPGenerator helper process
// --------------------------------------------------------------------------------
// IPGenerator is initialized by a set of strings with file paths, and from that will
// return instantiated (generated) IP on its Out-port, when run.
type IPGenerator struct {
BaseProcess
FilePaths []string
}
// NewIPGenerator initializes a new IPGenerator component from a list of file paths
func NewIPGenerator(wf *Workflow, name string, filePaths ...string) (p *IPGenerator) {
p = &IPGenerator{
BaseProcess: NewBaseProcess(wf, name),
FilePaths: filePaths,
}
p.InitOutPort(p, "out")
wf.AddProc(p)
return p
}
// Out returns the out-port of the IPGenerator
func (p *IPGenerator) Out() *OutPort {
return p.OutPort("out")
}
// Run runs the IPGenerator process, returning instantiated IP
func (p *IPGenerator) Run() {
defer p.Out().Close()
for _, fp := range p.FilePaths {
p.Out().Send(NewLocalFileIP(fp))
}
}
// UnMarshalJSON is a helper function to unmarshal the content of the IPs file
// to the interface v
func (ip *LocalFileIP) UnMarshalJSON(v interface{}) {
d := ip.Read()
err := json.Unmarshal(d, v)
CheckWithMsg(err, "Could not unmarshal content"+ip.Path())
}