fileutil

package
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package fileutil holds small file-system primitives shared across cc-fleet.

AtomicWrite is the single place that implements the create-tempfile + chmod + write + close + rename + cleanup discipline used by config / models / profile / fingerprint / secrets / spawn / userops for every 0o600 metadata or secret file, so a future hardening only changes one implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtomicWrite

func AtomicWrite(path string, data []byte, mode os.FileMode) error

AtomicWrite writes data to path via a SAME-DIRECTORY temp file + rename at mode. The parent directory must already exist — this helper deliberately does not mkdir so callers keep control of the directory mode.

Discipline:

  1. CreateTemp in filepath.Dir(path) so the rename stays within one filesystem (atomic at the kernel level).
  2. Chmod the temp to exactly mode regardless of umask. CreateTemp's 0600-before-umask is not enough.
  3. Write data, Close, Rename. Any failure jumps to cleanup.
  4. Defer os.Remove on the temp path so a failure before rename never leaves an orphan; after a successful rename the temp name no longer exists, so the deferred Remove no-ops.

A partial/failed write therefore never truncates an existing file at path — the old file stays intact until os.Rename swaps in the fully-written replacement in one step. This matters for in-place secret rotation: the previous key stays usable rather than clobbered.

The error wraps a short stage tag + path + the underlying error, never data.

Types

This section is empty.

Jump to

Keyboard shortcuts

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