-
Notifications
You must be signed in to change notification settings - Fork 480
/
Copy patherrorfs.go
552 lines (485 loc) · 14.1 KB
/
errorfs.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// Copyright 2020 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package errorfs
import (
"fmt"
"io"
"os"
"strings"
"sync"
"sync/atomic"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/errors/oserror"
"github.com/cockroachdb/pebble/internal/dsl"
"github.com/cockroachdb/pebble/vfs"
)
// ErrInjected is an error artificially injected for testing fs error paths.
var ErrInjected = LabelledError{
error: errors.New("injected error"),
Label: "ErrInjected",
}
// Op describes a filesystem operation.
type Op struct {
// Kind describes the particular kind of operation being performed.
Kind OpKind
// Path is the path of the file of the file being operated on.
Path string
// Offset is the offset of an operation. It's set for OpFileReadAt and
// OpFileWriteAt operations.
Offset int64
}
// OpKind is an enum describing the type of operation.
type OpKind int
const (
// OpCreate describes a create file operation.
OpCreate OpKind = iota
// OpLink describes a hardlink operation.
OpLink
// OpOpen describes a file open operation.
OpOpen
// OpOpenDir describes a directory open operation.
OpOpenDir
// OpRemove describes a remove file operation.
OpRemove
// OpRemoveAll describes a recursive remove operation.
OpRemoveAll
// OpRename describes a rename operation.
OpRename
// OpReuseForWrite describes a reuse for write operation.
OpReuseForWrite
// OpMkdirAll describes a make directory including parents operation.
OpMkdirAll
// OpLock describes a lock file operation.
OpLock
// OpList describes a list directory operation.
OpList
// OpFilePreallocate describes a file preallocate operation.
OpFilePreallocate
// OpStat describes a path-based stat operation.
OpStat
// OpGetDiskUsage describes a disk usage operation.
OpGetDiskUsage
// OpFileClose describes a close file operation.
OpFileClose
// OpFileRead describes a file read operation.
OpFileRead
// OpFileReadAt describes a file seek read operation.
OpFileReadAt
// OpFileWrite describes a file write operation.
OpFileWrite
// OpFileWriteAt describes a file seek write operation.
OpFileWriteAt
// OpFileStat describes a file stat operation.
OpFileStat
// OpFileSync describes a file sync operation.
OpFileSync
// OpFileSyncData describes a file sync operation.
OpFileSyncData
// OpFileSyncTo describes a file sync operation.
OpFileSyncTo
// OpFileFlush describes a file flush operation.
OpFileFlush
)
// ReadOrWrite returns the operation's kind.
func (o OpKind) ReadOrWrite() OpReadWrite {
switch o {
case OpOpen, OpOpenDir, OpList, OpStat, OpGetDiskUsage, OpFileRead, OpFileReadAt, OpFileStat:
return OpIsRead
case OpCreate, OpLink, OpRemove, OpRemoveAll, OpRename, OpReuseForWrite, OpMkdirAll, OpLock, OpFileClose, OpFileWrite, OpFileWriteAt, OpFileSync, OpFileSyncData, OpFileSyncTo, OpFileFlush, OpFilePreallocate:
return OpIsWrite
default:
panic(fmt.Sprintf("unrecognized op %v\n", o))
}
}
// OpReadWrite is an enum describing whether an operation is a read or write
// operation.
type OpReadWrite int
const (
// OpIsRead describes read operations.
OpIsRead OpReadWrite = iota
// OpIsWrite describes write operations.
OpIsWrite
)
// String implements fmt.Stringer.
func (kind OpReadWrite) String() string {
switch kind {
case OpIsRead:
return "Reads"
case OpIsWrite:
return "Writes"
default:
panic(fmt.Sprintf("unrecognized OpKind %d", kind))
}
}
// OnIndex is a convenience function for constructing a dsl.OnIndex for use with
// an error-injecting filesystem.
func OnIndex(index int32) *InjectIndex {
return &InjectIndex{dsl.OnIndex[Op](index)}
}
// InjectIndex implements Injector, injecting an error at a specific index.
type InjectIndex struct {
*dsl.Index[Op]
}
// MaybeError implements the Injector interface.
//
// TODO(jackson): Remove this implementation and update callers to compose it
// with other injectors.
func (ii *InjectIndex) MaybeError(op Op) error {
if !ii.Evaluate(op) {
return nil
}
return ErrInjected
}
// InjectorFunc implements the Injector interface for a function with
// MaybeError's signature.
type InjectorFunc func(Op) error
// String implements fmt.Stringer.
func (f InjectorFunc) String() string { return "<opaque func>" }
// MaybeError implements the Injector interface.
func (f InjectorFunc) MaybeError(op Op) error { return f(op) }
// Injector injects errors into FS operations.
type Injector interface {
fmt.Stringer
// MaybeError is invoked by an errorfs before an operation is executed. It
// is passed an enum indicating the type of operation and a path of the
// subject file or directory. If the operation takes two paths (eg,
// Rename, Link), the original source path is provided.
MaybeError(op Op) error
}
// Any returns an injector that injects an error if any of the provided
// injectors inject an error. The error returned by the first injector to return
// an error is used.
func Any(injectors ...Injector) Injector {
return anyInjector(injectors)
}
type anyInjector []Injector
func (a anyInjector) String() string {
var sb strings.Builder
sb.WriteString("(Any")
for _, inj := range a {
sb.WriteString(" ")
sb.WriteString(inj.String())
}
sb.WriteString(")")
return sb.String()
}
func (a anyInjector) MaybeError(op Op) error {
for _, inj := range a {
if err := inj.MaybeError(op); err != nil {
return err
}
}
return nil
}
// Counter wraps an Injector, counting the number of errors injected. It may be
// used in tests to ensure that when an error is injected, the error is
// surfaced through the user interface.
type Counter struct {
Injector
mu struct {
sync.Mutex
v uint64
lastErr error
}
}
// String implements fmt.Stringer.
func (c *Counter) String() string {
return c.Injector.String()
}
// MaybeError implements Injector.
func (c *Counter) MaybeError(op Op) error {
err := c.Injector.MaybeError(op)
if err != nil {
c.mu.Lock()
c.mu.v++
c.mu.lastErr = err
c.mu.Unlock()
}
return err
}
// Load returns the number of errors injected.
func (c *Counter) Load() uint64 {
c.mu.Lock()
defer c.mu.Unlock()
return c.mu.v
}
// LastError returns the last non-nil error injected.
func (c *Counter) LastError() error {
c.mu.Lock()
defer c.mu.Unlock()
return c.mu.lastErr
}
// Toggle wraps an Injector. By default, Toggle injects nothing. When toggled on
// through its On method, it begins injecting errors when the contained injector
// injects them. It may be returned to its original state through Off.
type Toggle struct {
Injector
on atomic.Bool
}
// String implements fmt.Stringer.
func (t *Toggle) String() string {
return t.Injector.String()
}
// MaybeError implements Injector.
func (t *Toggle) MaybeError(op Op) error {
if !t.on.Load() {
return nil
}
return t.Injector.MaybeError(op)
}
// On enables error injection.
func (t *Toggle) On() { t.on.Store(true) }
// Off disables error injection.
func (t *Toggle) Off() { t.on.Store(false) }
// FS implements vfs.FS, injecting errors into
// its operations.
type FS struct {
fs vfs.FS
inj Injector
}
// Wrap wraps an existing vfs.FS implementation, returning a new
// vfs.FS implementation that shadows operations to the provided FS.
// It uses the provided Injector for deciding when to inject errors.
// If an error is injected, FS propagates the error instead of
// shadowing the operation.
func Wrap(fs vfs.FS, inj Injector) *FS {
return &FS{
fs: fs,
inj: inj,
}
}
// WrapFile wraps an existing vfs.File, returning a new vfs.File that shadows
// operations to the provided vfs.File. It uses the provided Injector for
// deciding when to inject errors. If an error is injected, the file
// propagates the error instead of shadowing the operation.
func WrapFile(f vfs.File, inj Injector) vfs.File {
return &errorFile{file: f, inj: inj}
}
// Unwrap returns the FS implementation underlying fs.
// See pebble/vfs.Root.
func (fs *FS) Unwrap() vfs.FS {
return fs.fs
}
// Create implements FS.Create.
func (fs *FS) Create(name string, category vfs.DiskWriteCategory) (vfs.File, error) {
if err := fs.inj.MaybeError(Op{Kind: OpCreate, Path: name}); err != nil {
return nil, err
}
f, err := fs.fs.Create(name, category)
if err != nil {
return nil, err
}
return &errorFile{name, f, fs.inj}, nil
}
// Link implements FS.Link.
func (fs *FS) Link(oldname, newname string) error {
if err := fs.inj.MaybeError(Op{Kind: OpLink, Path: oldname}); err != nil {
return err
}
return fs.fs.Link(oldname, newname)
}
// Open implements FS.Open.
func (fs *FS) Open(name string, opts ...vfs.OpenOption) (vfs.File, error) {
if err := fs.inj.MaybeError(Op{Kind: OpOpen, Path: name}); err != nil {
return nil, err
}
f, err := fs.fs.Open(name)
if err != nil {
return nil, err
}
ef := &errorFile{name, f, fs.inj}
for _, opt := range opts {
opt.Apply(ef)
}
return ef, nil
}
// OpenReadWrite implements FS.OpenReadWrite.
func (fs *FS) OpenReadWrite(
name string, category vfs.DiskWriteCategory, opts ...vfs.OpenOption,
) (vfs.File, error) {
if err := fs.inj.MaybeError(Op{Kind: OpOpen, Path: name}); err != nil {
return nil, err
}
f, err := fs.fs.OpenReadWrite(name, category)
if err != nil {
return nil, err
}
ef := &errorFile{name, f, fs.inj}
for _, opt := range opts {
opt.Apply(ef)
}
return ef, nil
}
// OpenDir implements FS.OpenDir.
func (fs *FS) OpenDir(name string) (vfs.File, error) {
if err := fs.inj.MaybeError(Op{Kind: OpOpenDir, Path: name}); err != nil {
return nil, err
}
f, err := fs.fs.OpenDir(name)
if err != nil {
return nil, err
}
return &errorFile{name, f, fs.inj}, nil
}
// GetDiskUsage implements FS.GetDiskUsage.
func (fs *FS) GetDiskUsage(path string) (vfs.DiskUsage, error) {
if err := fs.inj.MaybeError(Op{Kind: OpGetDiskUsage, Path: path}); err != nil {
return vfs.DiskUsage{}, err
}
return fs.fs.GetDiskUsage(path)
}
// PathBase implements FS.PathBase.
func (fs *FS) PathBase(p string) string {
return fs.fs.PathBase(p)
}
// PathDir implements FS.PathDir.
func (fs *FS) PathDir(p string) string {
return fs.fs.PathDir(p)
}
// PathJoin implements FS.PathJoin.
func (fs *FS) PathJoin(elem ...string) string {
return fs.fs.PathJoin(elem...)
}
// Remove implements FS.Remove.
func (fs *FS) Remove(name string) error {
if _, err := fs.fs.Stat(name); oserror.IsNotExist(err) {
return nil
}
if err := fs.inj.MaybeError(Op{Kind: OpRemove, Path: name}); err != nil {
return err
}
return fs.fs.Remove(name)
}
// RemoveAll implements FS.RemoveAll.
func (fs *FS) RemoveAll(fullname string) error {
if err := fs.inj.MaybeError(Op{Kind: OpRemoveAll, Path: fullname}); err != nil {
return err
}
return fs.fs.RemoveAll(fullname)
}
// Rename implements FS.Rename.
func (fs *FS) Rename(oldname, newname string) error {
if err := fs.inj.MaybeError(Op{Kind: OpRename, Path: oldname}); err != nil {
return err
}
return fs.fs.Rename(oldname, newname)
}
// ReuseForWrite implements FS.ReuseForWrite.
func (fs *FS) ReuseForWrite(
oldname, newname string, category vfs.DiskWriteCategory,
) (vfs.File, error) {
if err := fs.inj.MaybeError(Op{Kind: OpReuseForWrite, Path: oldname}); err != nil {
return nil, err
}
return fs.fs.ReuseForWrite(oldname, newname, category)
}
// MkdirAll implements FS.MkdirAll.
func (fs *FS) MkdirAll(dir string, perm os.FileMode) error {
if err := fs.inj.MaybeError(Op{Kind: OpMkdirAll, Path: dir}); err != nil {
return err
}
return fs.fs.MkdirAll(dir, perm)
}
// Lock implements FS.Lock.
func (fs *FS) Lock(name string) (io.Closer, error) {
if err := fs.inj.MaybeError(Op{Kind: OpLock, Path: name}); err != nil {
return nil, err
}
return fs.fs.Lock(name)
}
// List implements FS.List.
func (fs *FS) List(dir string) ([]string, error) {
if err := fs.inj.MaybeError(Op{Kind: OpList, Path: dir}); err != nil {
return nil, err
}
return fs.fs.List(dir)
}
// Stat implements FS.Stat.
func (fs *FS) Stat(name string) (vfs.FileInfo, error) {
if err := fs.inj.MaybeError(Op{Kind: OpStat, Path: name}); err != nil {
return nil, err
}
return fs.fs.Stat(name)
}
// errorFile implements vfs.File. The interface is implemented on the pointer
// type to allow pointer equality comparisons.
type errorFile struct {
path string
file vfs.File
inj Injector
}
func (f *errorFile) Close() error {
// We don't inject errors during close as those calls should never fail in
// practice.
return f.file.Close()
}
func (f *errorFile) Read(p []byte) (int, error) {
if err := f.inj.MaybeError(Op{Kind: OpFileRead, Path: f.path}); err != nil {
return 0, err
}
return f.file.Read(p)
}
func (f *errorFile) ReadAt(p []byte, off int64) (int, error) {
if err := f.inj.MaybeError(Op{
Kind: OpFileReadAt,
Path: f.path,
Offset: off,
}); err != nil {
return 0, err
}
return f.file.ReadAt(p, off)
}
func (f *errorFile) Write(p []byte) (int, error) {
if err := f.inj.MaybeError(Op{Kind: OpFileWrite, Path: f.path}); err != nil {
return 0, err
}
return f.file.Write(p)
}
func (f *errorFile) WriteAt(p []byte, off int64) (int, error) {
if err := f.inj.MaybeError(Op{
Kind: OpFileWriteAt,
Path: f.path,
Offset: off,
}); err != nil {
return 0, err
}
return f.file.WriteAt(p, off)
}
func (f *errorFile) Stat() (vfs.FileInfo, error) {
if err := f.inj.MaybeError(Op{Kind: OpFileStat, Path: f.path}); err != nil {
return nil, err
}
return f.file.Stat()
}
func (f *errorFile) Prefetch(offset, length int64) error {
// TODO(radu): Consider error injection.
return f.file.Prefetch(offset, length)
}
func (f *errorFile) Preallocate(offset, length int64) error {
if err := f.inj.MaybeError(Op{Kind: OpFilePreallocate, Path: f.path}); err != nil {
return err
}
return f.file.Preallocate(offset, length)
}
func (f *errorFile) Sync() error {
if err := f.inj.MaybeError(Op{Kind: OpFileSync, Path: f.path}); err != nil {
return err
}
return f.file.Sync()
}
func (f *errorFile) SyncData() error {
if err := f.inj.MaybeError(Op{Kind: OpFileSyncData, Path: f.path}); err != nil {
return err
}
return f.file.SyncData()
}
func (f *errorFile) SyncTo(length int64) (fullSync bool, err error) {
if err := f.inj.MaybeError(Op{Kind: OpFileSyncTo, Path: f.path}); err != nil {
return false, err
}
return f.file.SyncTo(length)
}
func (f *errorFile) Fd() uintptr {
return f.file.Fd()
}