Binary Replacement Utility (platform/replace.go)

clawbench · internal/platform/replace.go · go · MIT
infra, files, lang:go, tag:updater, tag:atomic-write

Safely replaces running binaries with new versions using atomic rename or staged copy. Use for self-upgrading applications to prevent service interruption.

// Source: https://github.com/clawbench-dev/clawbench | License: MIT | Copyright 2025 xulongzhe
// Provided via justclaw.it.com; keep this notice with the code.
package platform

import (
	"io"
	"os"
	"path/filepath"
)

// renameFile is the rename ReplaceBinary uses. It is a variable so tests can
// force a cross-device failure (EXDEV on Unix, ERROR_NOT_SAME_DEVICE on
// Windows) and exercise the staged-copy fallback. Real deployments use
// os.Rename.
var renameFile = os.Rename

// ReplaceBinary replaces dst with the contents of src, leaving dst executable.
// It is how a self-upgrade swaps the running program's binary on disk.
//
// The defining property is that **dst is never removed or moved aside before
// its replacement already exists**: the old file stays valid until the instant
//

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key