package fd
import "gvisor.dev/gvisor/pkg/fd"
Package fd provides types for working with file descriptors.
Index ¶
- type FD
- func New(fd int) *FD
- func NewFromFile(file *os.File) (*FD, error)
- func NewFromFiles(files []*os.File) ([]*FD, error)
- func Open(path string, openmode int, perm uint32) (*FD, error)
- func OpenAt(dir *FD, path string, flags int, mode uint32) (*FD, error)
- func (f *FD) Close() error
- func (f *FD) File() (*os.File, error)
- func (f *FD) Release() int
- func (f *FD) ReleaseToFile(name string) *os.File
- type ReadWriter
- func NewReadWriter(fd int) *ReadWriter
- func (r *ReadWriter) FD() int
- func (r *ReadWriter) Read(b []byte) (int, error)
- func (r *ReadWriter) ReadAt(b []byte, off int64) (c int, err error)
- func (r *ReadWriter) String() string
- func (r *ReadWriter) Write(b []byte) (int, error)
- func (r *ReadWriter) WriteAt(b []byte, off int64) (c int, err error)
Types ¶
type FD ¶
type FD struct { ReadWriter }
FD owns a host file descriptor.
It is similar to os.File, with a few important distinctions:
FD provides a Release() method which relinquishes ownership. Like os.File, FD adds a finalizer to close the backing FD. However, the finalizer cannot be removed from os.File, forever pinning the lifetime of an FD to its os.File.
FD supports both blocking and non-blocking operation. os.File only supports blocking operation.
func New ¶
New creates a new FD.
New takes ownership of fd.
func NewFromFile ¶
NewFromFile creates a new FD from an os.File.
NewFromFile does not transfer ownership of the file descriptor (it will be duplicated, so both the os.File and FD will eventually need to be closed and some (but not all) changes made to the FD will be applied to the os.File as well).
The returned FD is always blocking (Go 1.9+).
func NewFromFiles ¶
NewFromFiles creates new FDs for each file in the slice.
func Open ¶
Open is equivalent to open(2).
func OpenAt ¶
OpenAt is equivalent to openat(2).
func (*FD) Close ¶
Close closes the file descriptor contained in the FD.
Close is safe to call multiple times, but will return an error after the first call.
Concurrently calling Close and any other method is undefined.
func (*FD) File ¶
File converts the FD to an os.File.
FD does not transfer ownership of the file descriptor (it will be duplicated, so both the FD and os.File will eventually need to be closed and some (but not all) changes made to the os.File will be applied to the FD as well).
This operation is somewhat expensive, so care should be taken to minimize its use.
func (*FD) Release ¶
Release relinquishes ownership of the contained file descriptor.
Concurrently calling Release and any other method is undefined.
func (*FD) ReleaseToFile ¶
ReleaseToFile returns an os.File that takes ownership of the FD.
name is passed to os.NewFile.
type ReadWriter ¶
type ReadWriter struct {
// contains filtered or unexported fields
}
ReadWriter implements io.ReadWriter, io.ReaderAt, and io.WriterAt for fd. It does not take ownership of fd.
func NewReadWriter ¶
func NewReadWriter(fd int) *ReadWriter
NewReadWriter creates a ReadWriter for fd.
func (*ReadWriter) FD ¶
func (r *ReadWriter) FD() int
FD returns the owned file descriptor. Ownership remains unchanged.
func (*ReadWriter) Read ¶
func (r *ReadWriter) Read(b []byte) (int, error)
Read implements io.Reader.
func (*ReadWriter) ReadAt ¶
func (r *ReadWriter) ReadAt(b []byte, off int64) (c int, err error)
ReadAt implements io.ReaderAt.
ReadAt always returns a non-nil error when c < len(b).
func (*ReadWriter) String ¶
func (r *ReadWriter) String() string
String implements Stringer.String().
func (*ReadWriter) Write ¶
func (r *ReadWriter) Write(b []byte) (int, error)
Write implements io.Writer.
func (*ReadWriter) WriteAt ¶
func (r *ReadWriter) WriteAt(b []byte, off int64) (c int, err error)
WriteAt implements io.WriterAt.
Source Files ¶
fd.go
- Version
- v0.0.0-20250605235530-a6711d1e1dc6 (latest)
- Published
- Jun 5, 2025
- Platform
- linux/amd64
- Imports
- 6 packages
- Last checked
- 4 hours ago –
Tools for package owners.