Documentation
¶
Index ¶
- Constants
- Variables
- func CalculateDirSize(dirPath string) (int64, error)
- func CopyDir(src, dst string) error
- func CopyFile(src, dst string) error
- func CreateZipArchive(paths []string, basePath string, destPath string, compress bool, ...) (count int, err error)
- func CreateZipToWriter(paths []string, basePath string, w io.Writer, compress bool, ...) (count int, err error)
- func ExtractArchive(ctx context.Context, archivePath string, destPath string, ...) (int, error)
- func ExtractArchiveAtomic(ctx context.Context, archivePath, targetRoot string) (int, error)
- func ExtractArchiveLimit(ctx context.Context, archivePath string, destPath string, ...) (int, error)
- func ExtractTarGz(r io.Reader, dest string) error
- func FindWorldDir(dataDir string) (string, error)
- func FindWorldDirs(dataDir string) ([]string, error)
- func GetDiskSpace(path string) (int64, error)
- func IsTextFile(path string) bool
- func PathExcluded(rel string, patterns []string) bool
- func ResolveInRoot(root, rel string) (string, error)
- func SHA256File(path string) (string, error)
- func SanitizePathName(name string) string
- func VerifyZipArchive(path string) error
- func Within(root, target string) bool
- func WithinReal(root, target string) bool
- type AtomicWriter
Constants ¶
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 ¶
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.
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 ¶
calculateDirSize calculates the total size of a directory in bytes, including all nested files.
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
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
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 FindWorldDirs ¶
Returns the server's world directory along with any sibling world dirs
func GetDiskSpace ¶
GetDiskSpace returns the total disk space available in bytes for the given path
func IsTextFile ¶
func PathExcluded ¶ added in v1.0.39
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
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 ¶
SHA256File calculates and returns the hex-encoded SHA256 hash of a file on disk.
func SanitizePathName ¶
func VerifyZipArchive ¶
VerifyZipArchive opens the zip archive and reads every entry so that stored file CRCs are verified.
func Within ¶
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 ¶
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.