fileutil

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 4 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 added in v0.2.4

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

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. 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.

Jump to

Keyboard shortcuts

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