fileio

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrPathValidation = errors.New("path validation failed")

ErrPathValidation indicates the path failed security validation (traversal, absolute, control chars, symlink escape, etc.).

Functions

func Register

func Register(p Provider)

Register registers a FileIO Provider. Later registrations override earlier ones (last-write-wins). Unlike credential.Register which appends to a chain (multiple credential sources are tried in order), FileIO uses a single active provider because only one file I/O backend is active at a time (local vs server mode). Typically called from init() via blank import.

Types

type File

type File interface {
	io.Reader
	io.ReaderAt
	io.Closer
}

File is the interface returned by FileIO.Open. It covers the subset of *os.File methods actually used by CLI commands. *os.File satisfies this interface without adaptation.

type FileIO

type FileIO interface {
	// Open opens a file for reading (upload, attachment, template scenarios).
	// The default implementation validates the path via SafeInputPath.
	Open(name string) (File, error)

	// Stat returns file metadata (size validation, existence checks).
	// The default implementation validates the path via SafeInputPath.
	// Use os.IsNotExist(err) to distinguish "file not found" from "invalid path".
	Stat(name string) (FileInfo, error)

	// ResolvePath returns the validated, absolute path for the given output path.
	// The default implementation delegates to SafeOutputPath.
	// Use this to obtain the canonical saved path for user-facing output.
	ResolvePath(path string) (string, error)

	// Save writes content to the target path and returns a SaveResult.
	// The default implementation validates via SafeOutputPath, creates
	// parent directories, and writes atomically.
	Save(path string, opts SaveOptions, body io.Reader) (SaveResult, error)
}

FileIO abstracts file transfer operations for CLI commands. The default implementation operates on the local filesystem with path validation, directory creation, and atomic writes. Inject a custom implementation via Factory.FileIOProvider to replace file transfer behavior (e.g. streaming in server mode).

type FileInfo

type FileInfo interface {
	Size() int64
	IsDir() bool
}

FileInfo is a minimal subset of os.FileInfo covering actual CLI usage. os.FileInfo satisfies this interface.

type MkdirError

type MkdirError struct {
	Err error
}

MkdirError indicates parent directory creation failed. Use errors.As(err, &fileio.MkdirError{}) to match.

func (*MkdirError) Error

func (e *MkdirError) Error() string

func (*MkdirError) Unwrap

func (e *MkdirError) Unwrap() error

type PathValidationError

type PathValidationError struct {
	Err error // original error
}

PathValidationError wraps a path validation error. errors.Is(err, ErrPathValidation) returns true. errors.Is(err, <original OS error>) also works via the chain.

func (*PathValidationError) Error

func (e *PathValidationError) Error() string

func (*PathValidationError) Unwrap

func (e *PathValidationError) Unwrap() []error

type Provider

type Provider interface {
	Name() string
	ResolveFileIO(ctx context.Context) FileIO
}

Provider creates FileIO instances. Follows the same API style as extension/credential.Provider.

func GetProvider

func GetProvider() Provider

GetProvider returns the currently registered Provider. Returns nil if no provider has been registered.

type SaveOptions

type SaveOptions struct {
	ContentType   string // MIME type
	ContentLength int64  // content length; -1 if unknown
}

SaveOptions carries metadata for Save. The default (local) implementation ignores these fields; server-mode implementations use them to construct streaming response frames.

type SaveResult

type SaveResult interface {
	Size() int64 // actual bytes written
}

SaveResult holds the outcome of a Save operation.

type WriteError

type WriteError struct {
	Err error
}

WriteError indicates file write failed. Use errors.As(err, &fileio.WriteError{}) to match.

func (*WriteError) Error

func (e *WriteError) Error() string

func (*WriteError) Unwrap

func (e *WriteError) Unwrap() error

Jump to

Keyboard shortcuts

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