forked from sahib/brig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.go
270 lines (215 loc) · 5.42 KB
/
handle.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package catfs
import (
"errors"
"fmt"
"io"
"sync"
"github.com/sahib/brig/catfs/mio"
"github.com/sahib/brig/catfs/mio/overlay"
n "github.com/sahib/brig/catfs/nodes"
)
var (
// ErrIsClosed is returned when an operation is performed on an already
// closed file.
ErrIsClosed = errors.New("File handle is closed")
)
// Handle is a emulation of a os.File handle, as returned by os.Open()
// It supports the usual operations like open, read, write and seek.
// Take note that the flushing operation currently is quite expensive.
type Handle struct {
fs *FS
file *n.File
lock sync.Mutex
layer *overlay.Layer
stream mio.Stream
wasModified bool
isClosed bool
readOnly bool
}
func newHandle(fs *FS, file *n.File, readOnly bool) *Handle {
return &Handle{
fs: fs,
file: file,
readOnly: readOnly,
}
}
func (hdl *Handle) initStreamIfNeeded() error {
if hdl.stream != nil {
return nil
}
// Initialize the stream lazily to avoid I/O on open()
rawStream, err := hdl.fs.bk.Cat(hdl.file.BackendHash())
if err != nil {
return err
}
// Stack the mio stack on top:
hdl.stream, err = mio.NewOutStream(rawStream, hdl.file.Key())
if err != nil {
return err
}
hdl.layer = overlay.NewLayer(hdl.stream)
hdl.layer.Truncate(int64(hdl.file.Size()))
hdl.layer.SetSize(int64(hdl.file.Size()))
return nil
}
// Read will try to fill `buf` as much as possible.
// The seek pointer will be advanced by the number of bytes written.
// Take care, `buf` might still have contents, even if io.EOF was returned.
func (hdl *Handle) Read(buf []byte) (int, error) {
var err error
hdl.lock.Lock()
defer hdl.lock.Unlock()
if hdl.isClosed {
return 0, ErrIsClosed
}
if err := hdl.initStreamIfNeeded(); err != nil {
return 0, err
}
n, err := io.ReadFull(hdl.layer, buf)
isEOF := (err == io.ErrUnexpectedEOF || err == io.EOF)
if err != nil && !isEOF {
return 0, err
}
if isEOF {
return n, io.EOF
}
return n, nil
}
// Write will write the contents of `buf` to the current position.
// It will return the number of currently written bytes.
func (hdl *Handle) Write(buf []byte) (int, error) {
hdl.lock.Lock()
defer hdl.lock.Unlock()
if hdl.readOnly {
return 0, ErrReadOnly
}
if hdl.isClosed {
return 0, ErrIsClosed
}
if err := hdl.initStreamIfNeeded(); err != nil {
return 0, err
}
// Currently, we do not check if the file was actually modified
// (i.e. data changed compared to before)
hdl.wasModified = true
n, err := hdl.layer.Write(buf)
if err != nil {
return n, err
}
// Advance the write pointer when writing things to the buffer.
if _, err := hdl.stream.Seek(int64(n), io.SeekCurrent); err != nil && err != io.EOF {
return n, err
}
minSize := uint64(hdl.layer.MinSize())
if hdl.file.Size() < minSize {
hdl.fs.mu.Lock()
hdl.file.SetSize(minSize)
// Make sure to save the size change:
if err := hdl.fs.lkr.StageNode(hdl.file); err != nil {
hdl.fs.mu.Unlock()
return 0, err
}
hdl.fs.mu.Unlock()
// Also auto-truncate on every write.
hdl.layer.Truncate(int64(minSize))
}
return n, nil
}
// Seek will jump to the `offset` relative to `whence`.
// There next read and write operation will start from this point.
func (hdl *Handle) Seek(offset int64, whence int) (int64, error) {
hdl.lock.Lock()
defer hdl.lock.Unlock()
if hdl.isClosed {
return 0, ErrIsClosed
}
if err := hdl.initStreamIfNeeded(); err != nil {
return 0, err
}
n, err := hdl.layer.Seek(offset, whence)
if err != nil {
return 0, err
}
return n, nil
}
// Truncate truncates the file to a specific length.
func (hdl *Handle) Truncate(size uint64) error {
hdl.lock.Lock()
defer hdl.lock.Unlock()
if hdl.readOnly {
return ErrReadOnly
}
if hdl.isClosed {
return ErrIsClosed
}
if err := hdl.initStreamIfNeeded(); err != nil {
return err
}
hdl.fs.mu.Lock()
hdl.file.SetSize(size)
hdl.fs.mu.Unlock()
hdl.layer.Truncate(int64(size))
return nil
}
// unlocked version of Flush()
func (hdl *Handle) flush() error {
// flush unsets the layer, so we don't flush twice.
if hdl.layer == nil {
return nil
}
// No need to flush anything if no write calls happened.
if !hdl.wasModified {
return nil
}
// Make sure that hdl.layer is unset in any case.
// but only do that in case of real writes.
defer func() {
hdl.layer = nil
hdl.stream = nil
hdl.wasModified = false
}()
// Jump back to the beginning of the file, since fs.Stage()
// should read all content starting from there.
n, err := hdl.layer.Seek(0, io.SeekStart)
if err != nil {
return err
}
if n != 0 {
return fmt.Errorf("seek offset is not 0")
}
path := hdl.file.Path()
if err := hdl.fs.Stage(path, hdl.layer); err != nil {
return err
}
return hdl.layer.Close()
}
// Flush makes sure to write the current state to the backend.
// Please remember that this method is currently pretty expensive.
func (hdl *Handle) Flush() error {
hdl.lock.Lock()
defer hdl.lock.Unlock()
if hdl.readOnly {
return ErrReadOnly
}
if hdl.isClosed {
return ErrIsClosed
}
return hdl.flush()
}
// Close will finalize the file. It should not be used after.
// This will call flush if it did not happen yet.
func (hdl *Handle) Close() error {
hdl.lock.Lock()
defer hdl.lock.Unlock()
if hdl.isClosed {
return ErrIsClosed
}
hdl.isClosed = true
return hdl.flush()
}
// Path returns the absolute path of the file.
func (hdl *Handle) Path() string {
hdl.lock.Lock()
defer hdl.lock.Unlock()
return hdl.file.Path()
}