Repository navigation
Expand file tree
/
Copy pathutil.go
More file actions
99 lines (85 loc) · 2.3 KB
/
Copy pathutil.go
File metadata and controls
99 lines (85 loc) · 2.3 KB
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
package tftp
import "io"
func sliceTo0(p []byte) ([]byte, []byte) {
for i, b := range p {
if b == 0 {
return p[0:i], p[i+1:]
}
}
return p[:], nil
}
// ErrorCountWriter embeds an io.Writer and counts the total number of bytes written to it over its lifetime.
// It also tracks the error state, which is defined as the first write error encountered.
type ErrorCountWriter struct {
io.Writer
nn int64
err error
}
// NewErrorCountWriter returns a new ErrorCountWriter
func NewErrorCountWriter(writer io.Writer) *ErrorCountWriter {
return &ErrorCountWriter{
Writer: writer,
}
}
// Write writes the contents of p if no previous write errors occurred.
// It returns the number of bytes written in the current write operation
// as well as the error condition on the writer
func (e *ErrorCountWriter) Write(p []byte) (n int, err error) {
// If no write errors encountered, Write
if e.err == nil {
n, err = e.Writer.Write(p)
e.nn += int64(n)
e.err = err
}
return n, e.err
}
// Count returns the cumulated number of bytes written
func (e *ErrorCountWriter) Count() int64 {
return e.nn
}
// Err returns the error state
func (e *ErrorCountWriter) Err() error {
return e.err
}
// Reset resets the count and error state
func (e *ErrorCountWriter) Reset() {
e.err = nil
e.nn = 0
}
// ErrorCountReader embeds a io.Reader and counts the total number of bytes written to it over its lifetime
type ErrorCountReader struct {
io.Reader
nn int64
err error
}
// NewErrorCountReader returns a new ErrorCountReader
func NewErrorCountReader(reader io.Reader) *ErrorCountReader {
return &ErrorCountReader{
Reader: reader,
}
}
// Read reads the next len(p) bytes from the buffer or until the buffer
// is drained. The return value n is the number of bytes read. If the
// buffer has no data to return, err is io.EOF (unless len(p) is zero);
// otherwise it is nil.
func (e *ErrorCountReader) Read(p []byte) (n int, err error) {
if e.err == nil {
n, err = e.Reader.Read(p)
e.nn += int64(n)
e.err = err
}
return n, e.err
}
// Count returns the cumulated number of bytes read
func (e *ErrorCountReader) Count() int64 {
return e.nn
}
// Err returns the error state
func (e *ErrorCountReader) Err() error {
return e.err
}
// Reset resets the count and error state
func (e *ErrorCountReader) Reset() {
e.err = nil
e.nn = 0
}