-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.go
More file actions
186 lines (163 loc) · 3.37 KB
/
buffer.go
File metadata and controls
186 lines (163 loc) · 3.37 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package fs
import (
"context"
"io"
"sync"
)
// OpenBuffer returns a lazy-executing reader for the file at name.
// The file is opened on first Read(), not when OpenBuffer is called.
//
// Example:
//
// io.Copy(dst, fs.OpenBuffer(ctx, fsys, "input.txt"))
func OpenBuffer(ctx context.Context, fsys FS, name string) io.ReadCloser {
return &bufferReader{ctx: ctx, fsys: fsys, name: name}
}
type bufferReader struct {
sync.Mutex
ctx context.Context
fsys FS
name string
r io.ReadCloser
err error
started bool
closed bool
}
func (br *bufferReader) init() {
br.r, br.err = Open(br.ctx, br.fsys, br.name)
}
func (br *bufferReader) Read(p []byte) (n int, err error) {
br.Lock()
if br.closed {
br.Unlock()
return 0, ErrClosed
}
if !br.started {
br.started = true
br.init()
}
br.Unlock()
if br.err != nil {
return 0, br.err
}
return br.r.Read(p)
}
func (br *bufferReader) Close() error {
br.Lock()
if br.closed {
br.Unlock()
return nil
}
br.closed = true
started := br.started
br.Unlock()
if started && br.r != nil {
return br.r.Close()
}
return nil
}
// CreateBuffer returns a lazy-executing writer for the file at name.
// The file is created on first Write(), not when CreateBuffer is called.
//
// Example:
//
// io.Copy(fs.CreateBuffer(ctx, fsys, "output.txt"), src)
func CreateBuffer(
ctx context.Context, fsys FS, name string,
) io.WriteCloser {
return &bufferWriter{ctx: ctx, fsys: fsys, name: name, create: true}
}
// AppendBuffer returns a lazy-executing writer for appending to the file at
// name. The file is opened for appending on first Write(), not when
// AppendBuffer is called.
//
// Example:
//
// io.Copy(fs.AppendBuffer(ctx, fsys, "log.txt"), src)
func AppendBuffer(
ctx context.Context, fsys FS, name string,
) io.WriteCloser {
return &bufferWriter{ctx: ctx, fsys: fsys, name: name, create: false}
}
type bufferWriter struct {
sync.Mutex
ctx context.Context
fsys FS
name string
create bool // true for Create, false for Append
w io.WriteCloser
err error
started bool
closed bool
}
func (bw *bufferWriter) init() {
if bw.create {
cfs, ok := bw.fsys.(CreateFS)
if !ok {
bw.err = ErrUnsupported
return
}
bw.w, bw.err = cfs.Create(bw.ctx, bw.name)
} else {
afs, ok := bw.fsys.(AppendFS)
if !ok {
bw.err = ErrUnsupported
return
}
bw.w, bw.err = afs.Append(bw.ctx, bw.name)
}
}
func (bw *bufferWriter) Write(p []byte) (n int, err error) {
bw.Lock()
if bw.closed {
bw.Unlock()
return 0, ErrClosed
}
if !bw.started {
bw.started = true
bw.init()
}
bw.Unlock()
if bw.err != nil {
return 0, bw.err
}
return bw.w.Write(p)
}
func (bw *bufferWriter) Close() error {
bw.Lock()
if bw.closed {
bw.Unlock()
return nil
}
bw.closed = true
started := bw.started
bw.Unlock()
if started && bw.w != nil {
return bw.w.Close()
}
return nil
}
// ReadFrom implements io.ReaderFrom for optimized copying that auto-closes
// the writer when the source reaches EOF.
// This allows io.Copy to automatically close the writer in pipeline stages.
func (bw *bufferWriter) ReadFrom(src io.Reader) (n int64, err error) {
bw.Lock()
if bw.closed {
bw.Unlock()
return 0, ErrClosed
}
if !bw.started {
bw.started = true
bw.init()
}
bw.Unlock()
if bw.err != nil {
return 0, bw.err
}
n, err = io.Copy(bw.w, src)
closeErr := bw.Close()
if err == nil {
err = closeErr
}
return n, err
}