-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_writer.go
70 lines (66 loc) · 1.42 KB
/
split_writer.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
package pgbackup
import (
"fmt"
"os"
)
// SplitWriter splits data written to it into sequentially numbered files.
type SplitWriter struct {
// Base path and filename to which the generated suffix will be added.
Basepath string
// The size in bytes that each file should be.
SplitSize int
// The file suffix length.
SuffixLength int
//
fileNo int
remaining int
dfd *os.File
}
// Close closes the writer.
func (w *SplitWriter) Close() error {
if w.dfd != nil {
w.remaining = 0
return w.dfd.Close()
}
return nil
}
// Write writes a chunk of data to the splitter.
func (w *SplitWriter) Write(p []byte) (int, error) {
var wrote, total int
var err error
//
for size := len(p); size > 0; size = size - wrote {
//
// Current dest file may be full.
if w.remaining == 0 && w.dfd != nil {
if err = w.dfd.Close(); err != nil {
return total, err
}
w.dfd = nil
}
//
// If w.dfd is nil we need to open our dest file.
if w.dfd == nil {
dname := w.Basepath + fmt.Sprintf(".%0[1]*d", w.SuffixLength, w.fileNo)
if w.dfd, err = os.Create(dname); err != nil {
return total, err
}
w.fileNo++
w.remaining = w.SplitSize
}
//
if size > w.remaining {
wrote, err = w.dfd.Write(p[0:w.remaining])
p = p[w.remaining:]
} else {
wrote, err = w.dfd.Write(p)
}
total = total + wrote
w.remaining = w.remaining - wrote
if err != nil {
return total, err
}
}
//
return total, nil
}