Documentation
¶
Overview ¶
Package fileutil provides file system utilities including atomic write operations.
Index ¶
- Constants
- Variables
- func AtomicWriteFile(path string, data []byte, perm os.FileMode) error
- func AtomicWriteJSON(path string, v any) error
- func AtomicWriteJSONWithPerm(path string, v any, perm os.FileMode) error
- func AtomicWriteYAML(path string, v any) (err error)
- func AtomicWriteYAMLWithPerm(path string, v any, perm os.FileMode) (err error)
- func ReadFileWithLimit(path string) ([]byte, error)
Constants ¶
const MaxFileSize = 1024 * 1024 // 1MB
MaxFileSize is the maximum file size we'll read (1MB). This prevents memory exhaustion from maliciously large files.
Variables ¶
var ErrFileTooLarge = errors.Newf("file exceeds maximum size of %d bytes", MaxFileSize)
ErrFileTooLarge indicates that a file exceeded MaxFileSize.
Functions ¶
func AtomicWriteFile ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
ReadFileWithLimit reads a file up to MaxFileSize. It returns an error if the file is larger than the limit.
Types ¶
This section is empty.