-
Notifications
You must be signed in to change notification settings - Fork 1
/
ioutils.go
35 lines (32 loc) · 894 Bytes
/
ioutils.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
package main
import (
"log"
"os"
"syscall"
"time"
)
func openTrunc(fn string) (*os.File, error) {
return os.OpenFile(fn, os.O_WRONLY|syscall.O_NDELAY|os.O_TRUNC|os.O_CREATE, 0644)
}
func openAppend(fn string) (*os.File, error) {
return os.OpenFile(fn, os.O_WRONLY|syscall.O_NDELAY|os.O_APPEND|os.O_CREATE, 0600)
}
func openRead(fn string) (*os.File, error) {
// really, gosec is stupid.
// G304 (CWE-22): Potential file inclusion via variable
// now, tell me, why should this be a problem? Our user tells us where to work, anyway.
// #nosec
return os.Open(fn)
}
func closeOnExec(f *os.File) { syscall.CloseOnExec(int(f.Fd())) }
func writeLoop(target string, fd *os.File, buf []byte) {
for {
n, err := fd.Write(buf)
if err == nil { // all written
break
}
log.Printf("unable to write to %s, pausing: %v\n", target, err)
buf = buf[n:]
time.Sleep(time.Second)
}
}