-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-writers.go
66 lines (58 loc) · 1.21 KB
/
image-writers.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
package main
import (
. "fmt"
"os"
)
type imageWriters struct {
fileMode os.FileMode
writers []*imageWriter
}
func newImageWriters(fileMode os.FileMode) *imageWriters {
return &imageWriters{fileMode: fileMode}
}
func (iws *imageWriters) Close() error {
for _, iw := range iws.writers {
iw.Close()
}
return nil
}
func (iws *imageWriters) AbortAll() {
var err error
for _, iw := range iws.writers {
if err = iw.Abort(); err != nil {
log.Error("failed to abort image: %s", err)
}
}
return
}
func (iws *imageWriters) AddImageWriter(ip string, c int64) (err error) {
iw, e := newImageWriter(ip, c, iws.fileMode)
if e != nil {
err = Errorf("failed to allocate image file %s: %s", ip, e)
return
}
iws.writers = append(iws.writers, iw)
return
}
func (iws *imageWriters) Write(bs []byte) (n int, err error) {
for _, iw := range iws.writers {
n, err = iw.Write(bs)
if err != nil {
iws.AbortAll()
return
}
}
return
}
func (iws *imageWriters) DumpReports(r []byte) {
for _, iw := range iws.writers {
// Create report file.
iif, err := os.Create(iw.file.Name() + ".report")
if err != nil {
log.Error("failed to create report file: %s", err)
continue
}
iif.Write(r)
iif.Close()
}
}