github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/vfs/fd.go (about) 1 // Copyright 2021 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package vfs 6 7 // fdGetter is an interface for a file with an Fd() method. A lot of 8 // File related optimizations (eg. Prefetch(), WAL recycling) rely on the 9 // existence of the Fd method to return a raw file descriptor. 10 type fdGetter interface { 11 Fd() uintptr 12 } 13 14 // fdFileWrapper is a File wrapper that also exposes an Fd() method. Used to 15 // wrap outer (wrapped) Files that could unintentionally hide the Fd() method 16 // exposed by the inner (unwrapped) File. It effectively lets the Fd() method 17 // bypass the outer File and go to the inner File. 18 type fdFileWrapper struct { 19 File 20 21 // All methods usually pass through to File above, except for Fd(), which 22 // bypasses it and gets called directly on the inner file. 23 inner fdGetter 24 } 25 26 func (f *fdFileWrapper) Fd() uintptr { 27 return f.inner.Fd() 28 } 29 30 // WithFd takes an inner (unwrapped) and an outer (wrapped) vfs.File, 31 // and returns an fdFileWrapper if the inner file has an Fd() method. Use this 32 // method to fix the hiding of the Fd() method and the subsequent unintentional 33 // disabling of Fd-related file optimizations. 34 func WithFd(inner, outer File) File { 35 if f, ok := inner.(fdGetter); ok { 36 return &fdFileWrapper{ 37 File: outer, 38 inner: f, 39 } 40 } 41 return outer 42 }