generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwrite.go
More file actions
43 lines (37 loc) · 706 Bytes
/
write.go
File metadata and controls
43 lines (37 loc) · 706 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
38
39
40
41
42
43
package main
import (
"io"
"os"
"log"
)
func writeFile(args []string, stdin io.Reader, stderr io.Writer, exit exitFunc) {
l := log.New(stderr, "", 0)
if len(args) < 1 {
exit(1)
}
file, err := os.OpenFile(args[0], os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
l.Printf("Error opening file: %+v", err)
exit(1)
}
err = file.Chmod(0600)
if err != nil {
l.Printf("Error chmod'ing file: %+v", err)
exit(1)
}
buf := make([]byte, 8192)
for {
nBytes, err := stdin.Read(buf)
if err != nil {
exit(0)
}
wBytes, err := file.Write(buf[0:nBytes])
if err != nil {
exit(1)
}
if wBytes != nBytes {
// Write should never fail without error
exit(1)
}
}
}