forked from lni/vfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fd.go
42 lines (36 loc) · 1.37 KB
/
fd.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
// Copyright 2021 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 vfs
// fdGetter is an interface for a file with an Fd() method. A lot of
// File related optimizations (eg. Prefetch(), WAL recycling) rely on the
// existence of the Fd method to return a raw file descriptor.
type fdGetter interface {
Fd() uintptr
}
// fdFileWrapper is a File wrapper that also exposes an Fd() method. Used to
// wrap outer (wrapped) Files that could unintentionally hide the Fd() method
// exposed by the inner (unwrapped) File. It effectively lets the Fd() method
// bypass the outer File and go to the inner File.
type fdFileWrapper struct {
File
// All methods usually pass through to File above, except for Fd(), which
// bypasses it and gets called directly on the inner file.
inner fdGetter
}
func (f *fdFileWrapper) Fd() uintptr {
return f.inner.Fd()
}
// WithFd takes an inner (unwrapped) and an outer (wrapped) vfs.File,
// and returns an fdFileWrapper if the inner file has an Fd() method. Use this
// method to fix the hiding of the Fd() method and the subsequent unintentional
// disabling of Fd-related file optimizations.
func WithFd(inner, outer File) File {
if f, ok := inner.(fdGetter); ok {
return &fdFileWrapper{
File: outer,
inner: f,
}
}
return outer
}