gastownhall/beads · error

failed to write temp file: %w

Error message

failed to write temp file: %w

What it means

atomicWriteFile writes the payload to the staged temp file; this wraps a tmp.Write failure. On error the temp file is closed and removed, leaving the original file untouched.

Source

Thrown at cmd/bd/backup_export.go:107

//     entry; a crash between the rename and a subsequent directory fsync
//     can still lose the rename itself on some filesystems.
//   - os.Rename's atomic-replace guarantee is a POSIX/Unix property; it is
//     not guaranteed on Windows. It also does not follow a symlink at
//     path — it replaces whatever is there, symlink or not — so a caller
//     that must preserve a symlink's target should resolve path with
//     filepath.EvalSymlinks first (see cmd/bd/proxied_server.go).
func atomicWriteFile(path string, data []byte) error {
	dir := filepath.Dir(path)
	tmp, err := os.CreateTemp(dir, ".backup-tmp-*")
	if err != nil {
		return fmt.Errorf("failed to create temp file: %w", err)
	}
	tmpPath := tmp.Name()

	if _, err := tmp.Write(data); err != nil {
		_ = tmp.Close()
		_ = os.Remove(tmpPath)
		return fmt.Errorf("failed to write temp file: %w", err)
	}
	if err := tmp.Sync(); err != nil {
		_ = tmp.Close()
		_ = os.Remove(tmpPath)
		return fmt.Errorf("failed to sync temp file: %w", err)
	}
	if err := tmp.Close(); err != nil {
		_ = os.Remove(tmpPath)
		return fmt.Errorf("failed to close temp file: %w", err)
	}
	if err := os.Rename(tmpPath, path); err != nil {
		_ = os.Remove(tmpPath)
		return fmt.Errorf("failed to rename temp file: %w", err)
	}
	return nil
}

// runBackupExport performs a Dolt-native backup to .beads/backup/.

View on GitHub (pinned to 71377f2769)

Solutions

  1. Free disk space: df -h and clean up; retry the command.
  2. Check user/container storage quotas.
  3. If hardware I/O errors appear in dmesg, address the disk health issue.
  4. Retry — the atomic design means no partial/corrupt backup_state.json is left behind.

Example fix

// shell
df -h .   # verify free space, then retry
bd sync
Defensive patterns

Strategy: retry

Validate before calling

var st syscall.Statfs_t
if err := syscall.Statfs(".beads", &st); err == nil {
    freeBytes := st.Bavail * uint64(st.Bsize)
    if freeBytes < 1<<20 { // need at least ~1MB headroom
        return errors.New("insufficient disk space for backup state write")
    }
}

Try / catch

if err := saveBackupState(dir, state); err != nil {
    if strings.Contains(err.Error(), "write temp file") {
        // likely ENOSPC — surface clearly and let caller free space / retry
        return fmt.Errorf("disk may be full: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: tmp.Write(data) fails after a successful CreateTemp: disk filled up between create and write, quota exceeded, or the file descriptor became invalid (rare).

Common situations: Disk filling up mid-operation; user quota hit; container storage limit reached; I/O errors on failing hardware.

Related errors


AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30). Data as JSON: /api/errors/6e0fccc4ef76ce20. Report an issue: GitHub.