fileutil

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package fileutil provides file system utilities including atomic write operations.

Index

Constants

View Source
const MaxFileSize = 1024 * 1024 // 1MB

MaxFileSize is the maximum file size we'll read (1MB). This prevents memory exhaustion from maliciously large files.

Variables

View Source
var ErrFileTooLarge = errors.Newf("file exceeds maximum size of %d bytes", MaxFileSize)

ErrFileTooLarge indicates that a file exceeded MaxFileSize.

Functions

func AtomicWriteFile

func AtomicWriteFile(path string, data []byte, perm os.FileMode) error

AtomicWriteFile writes data to a file atomically using a temp file + rename pattern. This ensures interrupted writes leave the original file intact.

The caller is responsible for ensuring the parent directory exists. Permissions are applied to the final file via the perm parameter.

func AtomicWriteJSON

func AtomicWriteJSON(path string, v any) error

AtomicWriteJSON writes v as indented JSON to path atomically. Uses 2-space indentation and appends a trailing newline for POSIX compliance.

The caller is responsible for ensuring the parent directory exists. The file is created with 0600 permissions.

func AtomicWriteJSONWithPerm

func AtomicWriteJSONWithPerm(path string, v any, perm os.FileMode) error

AtomicWriteJSON writes v as indented JSON to path atomically with specified permissions. Uses 2-space indentation and appends a trailing newline for POSIX compliance.

The caller is responsible for ensuring the parent directory exists.

func AtomicWriteYAML

func AtomicWriteYAML(path string, v any) (err error)

AtomicWriteYAML writes v as YAML to path atomically. Appends a trailing newline for POSIX compliance.

The caller is responsible for ensuring the parent directory exists. The file is created with 0600 permissions.

func AtomicWriteYAMLWithPerm

func AtomicWriteYAMLWithPerm(path string, v any, perm os.FileMode) (err error)

AtomicWriteYAMLWithPerm writes v as YAML to path atomically with specified permissions. Appends a trailing newline for POSIX compliance.

The caller is responsible for ensuring the parent directory exists.

func ReadFileWithLimit added in v0.0.3

func ReadFileWithLimit(path string) ([]byte, error)

ReadFileWithLimit reads a file up to MaxFileSize. It returns an error if the file is larger than the limit.

Types

This section is empty.

Jump to

Keyboard shortcuts

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