fileutil

package
v0.1.0-p8a.2 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package fileutil provides file manipulation utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyFile

func CopyFile(src, dst string, perm os.FileMode) error

func ExpandHome

func ExpandHome(path string) string

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

func IsCommittedWriteError(err error) bool

IsCommittedWriteError distinguishes post-rename failures from failures that leave the original target unchanged.

func MkdirAllDurable

func MkdirAllDurable(root, relativePath string, perm os.FileMode) error

MkdirAllDurable creates a directory below an existing durable root and syncs every parent entry between the root and target.

func SyncDirectory

func SyncDirectory(path string) error

SyncDirectory flushes directory metadata where supported by the platform.

func WriteFileAtomic

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

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

Jump to

Keyboard shortcuts

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