fd

package
v0.0.0-...-0a9b7a7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2024 License: Apache-2.0, MIT Imports: 6 Imported by: 39

Documentation

Overview

Package fd provides types for working with file descriptors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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

func New(fd int) *FD

New creates a new FD.

New takes ownership of fd.

func NewFromFile

func NewFromFile(file *os.File) (*FD, error)

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

func NewFromFiles(files []*os.File) ([]*FD, error)

NewFromFiles creates new FDs for each file in the slice.

func Open

func Open(path string, openmode int, perm uint32) (*FD, error)

Open is equivalent to open(2).

func OpenAt

func OpenAt(dir *FD, path string, flags int, mode uint32) (*FD, error)

OpenAt is equivalent to openat(2).

func (*FD) Close

func (f *FD) Close() error

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

func (f *FD) File() (*os.File, error)

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

func (f *FD) Release() int

Release relinquishes ownership of the contained file descriptor.

Concurrently calling Release and any other method is undefined.

func (*FD) ReleaseToFile

func (f *FD) ReleaseToFile(name string) *os.File

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL