Documentation
¶
Overview ¶
Package fileutil provides file manipulation utilities.
Index ¶
- func CopyFile(src, dst string, perm os.FileMode) error
- func ExpandHome(path string) string
- func IsCommittedWriteError(err error) bool
- func MkdirAllDurable(root, relativePath string, perm os.FileMode) error
- func SyncDirectory(path string) error
- func WriteFileAtomic(path string, data []byte, perm os.FileMode) error
- type CommittedWriteError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExpandHome ¶
ExpandHome expands a leading "~" (or "~/" / "~\" prefix) to the current user's home directory. "~user" forms and paths that do not start with "~" are returned unchanged. On any error resolving the home directory, path is returned unchanged.
func IsCommittedWriteError ¶
IsCommittedWriteError distinguishes post-rename failures from failures that leave the original target unchanged.
func MkdirAllDurable ¶
MkdirAllDurable creates a directory below an existing durable root and syncs every parent entry between the root and target.
func SyncDirectory ¶
SyncDirectory flushes directory metadata where supported by the platform.
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. Atomically renames temp file to target path 6. Syncs directory metadata where supported (ensures rename is durable)
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 ¶
type CommittedWriteError ¶
type CommittedWriteError struct {
Err error
}
CommittedWriteError reports that rename committed the new file, but the parent-directory sync failed, so crash durability could not be confirmed.
func (*CommittedWriteError) Error ¶
func (e *CommittedWriteError) Error() string
func (*CommittedWriteError) Unwrap ¶
func (e *CommittedWriteError) Unwrap() error