files

package
v1.0.39 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxExtractedBytes int64 = 20 << 30 // 20 GiB

Extract archive to destPath DefaultMaxExtractedBytes caps the total uncompressed bytes an archive may expand to during extraction. A small compressed upload can otherwise expand to fill the disk (a decompression bomb).

Variables

View Source
var (
	ErrFileTooLarge = errors.New("files: file exceeds declared size")
	ErrClosed       = errors.New("files: writer closed")
)

ErrFileTooLarge is returned when a write would pass the declared size. ErrClosed is returned by a second Close or by Write after Close or Abort.

View Source
var (
	ErrEmptyRoot    = errors.New("files: empty root")
	ErrAbsolutePath = errors.New("files: absolute path")
	ErrPathEscapes  = errors.New("files: path escapes root")
)

ErrEmptyRoot, ErrAbsolutePath, and ErrPathEscapes are returned by ResolveInRoot.

Functions

func CalculateDirSize

func CalculateDirSize(dirPath string) (int64, error)

calculateDirSize calculates the total size of a directory in bytes, including all nested files.

func CopyDir

func CopyDir(src, dst string) error

CopyDir recursively copies a directory tree from src to dst.

func CopyFile

func CopyFile(src, dst string) error

func CreateZipArchive

func CreateZipArchive(paths []string, basePath string, destPath string, compress bool, skip ...func(string) bool) (count int, err error)

CreateZipArchive creates a zip archive file on disk from the given paths.

func CreateZipToWriter

func CreateZipToWriter(paths []string, basePath string, w io.Writer, compress bool, skip ...func(string) bool) (count int, err error)

CreateZipToWriter writes a zip archive of the given paths to the writer. basePath is the root directory used to calculate relative paths in the archive. When compress is false all entries are stored uncompressed. Returns the number of files archived.

func ExtractArchive

func ExtractArchive(ctx context.Context, archivePath string, destPath string, counter *atomic.Int32) (int, error)

ExtractArchive extracts archivePath into destPath, bounded by DefaultMaxExtractedBytes.

func ExtractArchiveAtomic added in v1.0.39

func ExtractArchiveAtomic(ctx context.Context, archivePath, targetRoot string) (int, error)

ExtractArchiveAtomic extracts an archive into a clean staging directory beside targetRoot and swaps it in only after extraction fully succeeds (OSS-235). A restore never merges into the live tree: stale files cannot survive a successful restore, and a failed extraction leaves the current tree untouched. The previous tree is moved aside for the duration of the swap and removed only after the new tree is in place; an interrupted swap leaves both trees on disk for recovery.

func ExtractArchiveLimit added in v1.0.39

func ExtractArchiveLimit(ctx context.Context, archivePath string, destPath string, counter *atomic.Int32, maxBytes int64) (int, error)

ExtractArchiveLimit is ExtractArchive with an explicit aggregate uncompressed budget. It fails rather than writing past maxBytes.

func ExtractTarGz added in v1.0.39

func ExtractTarGz(r io.Reader, dest string) error

ExtractTarGz unpacks a gzipped tar into dest. Only directories and regular files are written. Symlink, hard-link, and device entries are rejected. Every target must pass ResolveInRoot.

func FindWorldDir

func FindWorldDir(dataDir string) (string, error)

func FindWorldDirs

func FindWorldDirs(dataDir string) ([]string, error)

Returns the server's world directory along with any sibling world dirs

func GetDiskSpace

func GetDiskSpace(path string) (int64, error)

GetDiskSpace returns the total disk space available in bytes for the given path

func IsTextFile

func IsTextFile(path string) bool

func PathExcluded added in v1.0.39

func PathExcluded(rel string, patterns []string) bool

PathExcluded reports whether a relative archive path matches a glob or sits under an excluded directory. Patterns use forward slashes.

func ResolveInRoot added in v1.0.39

func ResolveInRoot(root, rel string) (string, error)

ResolveInRoot cleans rel against root and returns the absolute path. Callers use this instead of a string prefix. An empty rel names the root. An absolute rel is rejected. The path is returned only when WithinReal accepts it, so a symlink that leaves root is an error.

func SHA256File

func SHA256File(path string) (string, error)

SHA256File calculates and returns the hex-encoded SHA256 hash of a file on disk.

func SanitizePathName

func SanitizePathName(name string) string

func VerifyZipArchive

func VerifyZipArchive(path string) error

VerifyZipArchive opens the zip archive and reads every entry so that stored file CRCs are verified.

func Within

func Within(root, target string) bool

Within reports whether target resolves to root itself or a path inside root.

It is a boundary check, not a string prefix check: a sibling directory whose name merely shares root's prefix (root=/data/srv, target=/data/srv-evil/x) is NOT within root. Absolute targets outside root and targets that escape via ".." are rejected.

func WithinReal

func WithinReal(root, target string) bool

WithinReal reports whether target (when existing, or its closest existing ancestor if target does not yet exist) resolves strictly to root itself or within root after evaluating all symbolic links.

It first performs a lexical boundary check via Within. Then it canonicalizes root and target (or target's closest existing parent) via filepath.EvalSymlinks to ensure no symlink points outside root.

Types

type AtomicWriter added in v1.0.39

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

AtomicWriter writes to a temp file beside dest and renames it into place on Close. The caller resolves dest, including ResolveInRoot, before calling NewAtomicWriter. Abort removes the temp file and leaves dest unchanged.

func NewAtomicWriter added in v1.0.39

func NewAtomicWriter(dest string, maxBytes int64) (*AtomicWriter, error)

NewAtomicWriter creates a temp file in the destination directory. maxBytes is the most Write will accept. The destination directory must exist.

func (*AtomicWriter) Abort added in v1.0.39

func (w *AtomicWriter) Abort() error

Abort removes the temp file. It does not remove a destination already published by Close.

func (*AtomicWriter) Close added in v1.0.39

func (w *AtomicWriter) Close() error

Close publishes the temp file as dest, replacing an existing file.

func (*AtomicWriter) Write added in v1.0.39

func (w *AtomicWriter) Write(p []byte) (int, error)

Write rejects a chunk that would pass maxBytes and writes none of that chunk.

Jump to

Keyboard shortcuts

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