ctxvfs

package module
v0.0.0-...-8464d5d Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2024 License: MIT Imports: 13 Imported by: 4

README

ctxvfs Build Status

Package ctxvfs defines a virtual file system interface whose methods accept a context.Context parameter. It is otherwise similar to vfs.FileSystem.

This package is experimental until further notice.

Open the code in Nxpkg

Documentation

Overview

Package ctxvfs defines a virtual file system interface whose methods accept a context.Context parameter. It is otherwise similar to golang.org/x/tools/godoc/vfs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadAllFiles

func ReadAllFiles(ctx context.Context, fs FileSystem, root string, filter func(os.FileInfo) bool) (map[string][]byte, error)

func ReadFile

func ReadFile(ctx context.Context, fs FileSystem, path string) ([]byte, error)

ReadFile reads the file named by path from fs and returns the contents.

func StripContext

func StripContext(fs FileSystem) vfs.FileSystem

StripContext is an adapter for using a ctxvfs.FileSystem (whose interface methods take a context.Context parameter) as a vfs.FileSystem (whose interface methods DON'T take a context.Context parameter). A context.Background() value is passed to the underlying ctxvfs.FileSystem.

This should only be used temporarily while you are transitioning code to use ctxvfs.FileSystem.

Types

type BindMode

type BindMode int
const (
	BindReplace BindMode = iota
	BindBefore
	BindAfter
)

type FileSystem

type FileSystem interface {
	Open(ctx context.Context, name string) (ReadSeekCloser, error)
	Lstat(ctx context.Context, path string) (os.FileInfo, error)
	Stat(ctx context.Context, path string) (os.FileInfo, error)
	ReadDir(ctx context.Context, path string) ([]os.FileInfo, error)
	String() string
}

The FileSystem interface specifies the methods used to access the file system.

func Map

func Map(m map[string][]byte) FileSystem

Map returns a new FileSystem from the provided map. Map keys should be forward slash-separated pathnames and not contain a leading slash.

It differs from golang.org/x/tools/godoc/vfs/mapfs in the following ways:

  • It uses []byte instead of string to hold file contents. This means that callers must synchronize writes (if any) to the byte arrays.
  • It implements ctxvfs.FileSystem, not vfs.FileSystem.

func OS

func OS(root string) FileSystem

OS returns an implementation of FileSystem reading from the tree rooted at root. Recording a root is convenient everywhere but necessary on Windows, because the slash-separated path passed to Open has no way to specify a drive letter. Using a root lets code refer to OS(`c:\`), OS(`d:\`) and so on.

TODO(sqs): The ctx parameter in the FileSystem methods is currently ignored.

func SingleFileOverlay

func SingleFileOverlay(fs FileSystem, path string, contents []byte) FileSystem

func Sync

func Sync(mu *sync.Mutex, fs FileSystem) FileSystem

Sync creates a new file system wrapper around fs that locks a mutex during its operations.

The contents of files must be immutable, since it has no way of synchronizing access to the ReadSeekCloser from Open after Open returns.

type NameSpace

type NameSpace map[string][]mountedFS

A NameSpace is a file system made up of other file systems mounted at specific locations in the name space.

It is based on golang.org/x/tools/godoc/vfs Namespace and adds ctx method parameters and the ability to list ancestors of mount points. See that documentation for details.

func (NameSpace) Bind

func (ns NameSpace) Bind(old string, newfs FileSystem, new string, mode BindMode)

func (NameSpace) Fprint

func (ns NameSpace) Fprint(w io.Writer)

Fprint writes a text representation of the name space to w.

func (NameSpace) Lstat

func (ns NameSpace) Lstat(ctx context.Context, path string) (os.FileInfo, error)

func (NameSpace) Open

func (ns NameSpace) Open(ctx context.Context, path string) (ReadSeekCloser, error)

func (NameSpace) ReadDir

func (ns NameSpace) ReadDir(ctx context.Context, path string) ([]os.FileInfo, error)

func (NameSpace) Stat

func (ns NameSpace) Stat(ctx context.Context, path string) (os.FileInfo, error)

func (NameSpace) String

func (NameSpace) String() string

type ReadSeekCloser

type ReadSeekCloser interface {
	io.Reader
	io.Seeker
	io.Closer
}

A ReadSeekCloser can Read, Seek, and Close.

type Walker

type Walker struct {
	// contains filtered or unexported fields
}

Walker provides a convenient interface for iterating over the descendants of a filesystem path. Successive calls to the Step method will step through each file or directory in the tree, including the root. The files are walked in lexical order, which makes the output deterministic but means that for very large directories Walker can be inefficient. Walker does not follow symbolic links.

func Walk

func Walk(ctx context.Context, root string, fs FileSystem) *Walker

Walk returns a new Walker rooted at root on the FileSystem fs.

func (*Walker) Err

func (w *Walker) Err() error

Err returns the error, if any, for the most recent attempt by Step to visit a file or directory. If a directory has an error, w will not descend into that directory.

func (*Walker) Path

func (w *Walker) Path() string

Path returns the path to the most recent file or directory visited by a call to Step. It contains the argument to Walk as a prefix; that is, if Walk is called with "dir", which is a directory containing the file "a", Path will return "dir/a".

func (*Walker) SkipDir

func (w *Walker) SkipDir()

SkipDir causes the currently visited directory to be skipped. If w is not on a directory, SkipDir has no effect.

func (*Walker) Stat

func (w *Walker) Stat() os.FileInfo

Stat returns info for the most recent file or directory visited by a call to Step.

func (*Walker) Step

func (w *Walker) Step() bool

Step advances the Walker to the next file or directory, which will then be available through the Path, Stat, and Err methods. It returns false when the walk stops at the end of the tree.

Jump to

Keyboard shortcuts

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