Documentation
¶
Overview ¶
Package fileutil provides file manipulation utilities.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WriteFileAtomic ¶
WriteFileAtomic atomically writes data to a file using a temp file + rename pattern.
This guarantees that the target file is either: - Completely written with the new data - Unchanged (if any step fails before rename)
The function: 1. Creates a temp file in the same directory (original untouched) 2. Writes data to temp file 3. Syncs data to disk (critical for SD cards/flash storage) 4. Sets file permissions 5. Syncs directory metadata (ensures rename is durable) 6. Atomically renames temp file to target path
Safety guarantees: - Original file is NEVER modified until successful rename - Temp file is always cleaned up on error - Data is flushed to physical storage before rename - Directory entry is synced to prevent orphaned inodes
Parameters:
- path: Target file path
- data: Data to write
- perm: File permission mode (e.g., 0o600 for secure, 0o644 for readable)
Returns:
- Error if any step fails, nil on success
Example:
// Secure config file (owner read/write only)
err := utils.WriteFileAtomic("config.json", data, 0o600)
// Public readable file
err := utils.WriteFileAtomic("public.txt", data, 0o644)
Types ¶
This section is empty.