fsutil

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 16 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	TempFilePrefix     = ".genieterm-"
	TempBaseNameMaxLen = 64
	MaxSymlinkDepth    = 40
	DefaultFileMode    = os.FileMode(0644)
)
View Source
const (
	OverwriteRequiredError = "file already exists at %q, set overwrite flag to delete the existing file"
	MergeRequiredError     = "directory already exists at %q, set overwrite flag to delete the existing contents or set merge flag to merge the contents"
)

Variables

View Source
var ErrAtomicReplaceUnavailable = errors.New("atomic replace unavailable")

ErrAtomicReplaceUnavailable marks failures where the file could not be replaced via temp-file-and-rename without losing something (directory not writable, foreign owner, hard links). Callers that hold the data in memory may fall back to an in-place write.

Functions

func CleanPathPrefix

func CleanPathPrefix(path string) (string, error)

CleanPathPrefix corrects paths for prefix filesystems (i.e. ones that don't have directories)

func CopyTree added in v0.5.0

func CopyTree(ctx context.Context, src CopySource, srcEntry *CopyEntry, dest CopyDest, destPath string, opts CopyOpts) error

CopyTree copies srcEntry to destPath. Files replace atomically (overwrite required if the destination exists). Directories are copied into a staging directory and swapped in only on success; with Merge the source tree is copied into the existing directory, replacing files. Symlinks inside a directory tree are copied as their target file contents; symlinked directories and special files are skipped.

func GetParentPath

func GetParentPath(conn *connparse.Connection) string

func GetParentPathString

func GetParentPathString(hostAndPath string) string

func IncompleteTransferError added in v0.5.0

func IncompleteTransferError(target string, got int64, want int64) error

func IsLocalPathWithin added in v1.5.1

func IsLocalPathWithin(parent, child string) (bool, error)

IsLocalPathWithin compares filesystem identities so case-sensitive directories, junctions and alternate spellings cannot bypass the copy-into-itself guard. A missing destination inherits the relationship of its nearest existing ancestor.

func IsPathWithin added in v0.5.0

func IsPathWithin(parent string, child string, separator string) bool

IsPathWithin reports whether child is parent or lies under it (both must be cleaned, same separator).

func MakeTempName added in v0.5.0

func MakeTempName(baseName string) (string, error)

func PreservedMode added in v0.5.0

func PreservedMode(mode os.FileMode) os.FileMode

PreservedMode keeps permission and setuid/setgid/sticky bits of an existing file.

func ReadFileStream

func ReadFileStream(ctx context.Context, readCh <-chan wshrpc.RespOrErrorUnion[wshrpc.FileData], fileInfoCallback func(finfo wshrpc.FileInfo), dirCallback func(entries []*wshrpc.FileInfo) error, fileCallback func(data io.Reader) error) error

func ReadStreamToFileData

func ReadStreamToFileData(ctx context.Context, readCh <-chan wshrpc.RespOrErrorUnion[wshrpc.FileData]) (*wshrpc.FileData, error)

func ReplaceLocalFile added in v0.5.0

func ReplaceLocalFile(target string, r io.Reader, opts ReplaceOpts) (int64, error)

ReplaceLocalFile streams r into a temp file next to target (same filesystem), fsyncs it, copies the existing file's mode/owner and replaces target only after the write succeeds. Failures before replacement leave the original untouched and remove the temp file. A partial Windows replacement failure restores the original when possible, otherwise the error reports its retained backup path.

func ResolveCopyDestPath added in v0.5.0

func ResolveCopyDestPath(dest CopyDest, destPath string, srcBaseName string, destHasSlash bool) (string, error)

func ResolveLocalWriteTarget added in v0.5.0

func ResolveLocalWriteTarget(p string) (string, error)

ResolveLocalWriteTarget follows symlinks so a replace lands on the link target instead of swapping the link itself for a regular file. Dangling links resolve to their (missing) target.

func SaveLocalFile added in v0.5.0

func SaveLocalFile(p string, data []byte, createMode os.FileMode) error

SaveLocalFile replaces the contents of p (following symlinks) atomically when possible. When the directory is not writable or ownership/hard links can't be preserved it falls back to truncating and rewriting the existing file in place, which is what editors do in the same situation.

func WriteLocalFileInPlace added in v0.5.0

func WriteLocalFileInPlace(target string, data []byte) error

Types

type CopyDest added in v0.5.0

type CopyDest interface {
	Stat(p string) (*CopyEntry, error)
	Join(dir string, name string) string
	Dir(p string) string
	Base(p string) string
	Mkdir(p string, mode os.FileMode) error
	WriteFile(p string, r io.Reader, mode os.FileMode, expectedSize int64) error
	Rename(oldPath string, newPath string) error
	RemoveAll(p string) error
}

CopyDest is the write side of a copy. Stat follows symlinks and returns (nil, nil) for a missing path. WriteFile must replace atomically. RemoveAll must not follow a top-level symlink.

type CopyEntry added in v0.5.0

type CopyEntry struct {
	Path  string
	Name  string
	Size  int64
	Mode  os.FileMode
	IsDir bool
}

type CopyOpts added in v0.5.0

type CopyOpts struct {
	Overwrite bool
	Merge     bool
}

type CopySource added in v0.5.0

type CopySource interface {
	Stat(p string) (*CopyEntry, error)
	ReadDir(p string) ([]*CopyEntry, error)
	Open(p string) (io.ReadCloser, error)
}

CopySource is the read side of a copy. Stat follows symlinks; ReadDir entries do not.

type FileOwner added in v0.5.0

type FileOwner struct {
	Uid   int
	Gid   int
	Nlink uint64
}

func GetFileOwner added in v0.5.0

func GetFileOwner(info os.FileInfo) (FileOwner, bool)

type LocalCopyDest added in v0.5.0

type LocalCopyDest struct{}

func (LocalCopyDest) Base added in v0.5.0

func (LocalCopyDest) Base(p string) string

func (LocalCopyDest) Dir added in v0.5.0

func (LocalCopyDest) Dir(p string) string

func (LocalCopyDest) Join added in v0.5.0

func (LocalCopyDest) Join(dir string, name string) string

func (LocalCopyDest) Mkdir added in v0.5.0

func (LocalCopyDest) Mkdir(p string, mode os.FileMode) error

func (LocalCopyDest) RemoveAll added in v0.5.0

func (LocalCopyDest) RemoveAll(p string) error

func (LocalCopyDest) Rename added in v0.5.0

func (LocalCopyDest) Rename(oldPath string, newPath string) error

func (LocalCopyDest) Stat added in v0.5.0

func (LocalCopyDest) Stat(p string) (*CopyEntry, error)

func (LocalCopyDest) WriteFile added in v0.5.0

func (LocalCopyDest) WriteFile(p string, r io.Reader, mode os.FileMode, expectedSize int64) error

type LocalCopySource added in v0.5.0

type LocalCopySource struct{}

func (LocalCopySource) Open added in v0.5.0

func (LocalCopySource) ReadDir added in v0.5.0

func (LocalCopySource) ReadDir(p string) ([]*CopyEntry, error)

func (LocalCopySource) Stat added in v0.5.0

func (LocalCopySource) Stat(p string) (*CopyEntry, error)

type ReplaceOpts added in v0.5.0

type ReplaceOpts struct {
	CreateMode os.FileMode
	// ExpectedSize is the minimum number of bytes the reader must produce; -1 disables the check.
	ExpectedSize int64
	// StrictOwner makes ownership/hard-link loss an ErrAtomicReplaceUnavailable error instead of best-effort.
	StrictOwner bool
}

Jump to

Keyboard shortcuts

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