update

package
v0.1.0-alpha.5 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package update implements the verification side of the Sweef auto-updater: a signed release manifest (ed25519), resumable checksum-verified downloads, and a staged binary replacement with rollback. It is deliberately pure Go, has no dependency on the rest of Sweef, and is not wired into the CLI yet. The design is described in docs/updates.md.

Index

Constants

View Source
const MaxManifestBytes = 1 << 20

MaxManifestBytes bounds manifest and signature downloads.

View Source
const SchemaVersion = 1

SchemaVersion is the only manifest schema this code understands.

Variables

View Source
var (
	ErrBadSignature = errors.New("update: manifest signature verification failed")
	ErrExpired      = errors.New("update: manifest has expired")
	ErrNoArtifact   = errors.New("update: no artifact for this platform")
	ErrNotNewer     = errors.New("update: manifest version is not newer than the running version")
	ErrChecksum     = errors.New("update: sha256 mismatch")
)

Errors returned by this package.

Functions

func Download

func Download(ctx context.Context, c *http.Client, a Artifact, dest string) error

Download fetches a.URL into dest, resuming a previous partial download (dest+".part") with an HTTP Range request when possible. The finished file is size- and SHA-256-verified against the (already signature-verified) artifact before being renamed to dest. On checksum failure the partial file is deleted so the next attempt starts clean.

func ExecutablePath

func ExecutablePath() (string, error)

ExecutablePath returns the resolved path of the running binary.

func Fetch

func Fetch(ctx context.Context, c *http.Client, url string) ([]byte, error)

Fetch GETs a small document (manifest or signature) with a size cap.

func IsNewer

func IsNewer(candidate, current string) bool

IsNewer reports whether candidate outranks current. Unparseable versions are never considered newer (fail closed).

func ParsePublicKey

func ParsePublicKey(b64 string) (ed25519.PublicKey, error)

ParsePublicKey decodes a base64 (std) ed25519 public key.

func Recover

func Recover(target string) error

Recover repairs an interrupted update at startup: if target is missing but target+".old" exists the swap was cut off between steps 2 and 3 (power loss), so restore it. Leftover .new/.bad files are removed.

func Sign

func Sign(priv ed25519.PrivateKey, data []byte) string

Sign returns the base64 detached signature of data.

func Verify

func Verify(data []byte, sigB64 string, keys []ed25519.PublicKey) error

Verify checks sigB64 over data against any of keys (several keys allow rotation: ship the new key in a release before retiring the old one).

Types

type Artifact

type Artifact struct {
	OS     string `json:"os"`   // GOOS: linux, darwin, windows
	Arch   string `json:"arch"` // GOARCH: amd64, arm64, or "universal" on darwin
	Kind   string `json:"kind"` // "binary" (raw executable, used by the updater), "archive", "installer"
	URL    string `json:"url"`
	SHA256 string `json:"sha256"` // lowercase hex
	Size   int64  `json:"size"`
}

Artifact is one downloadable file listed in a manifest.

type Manifest

type Manifest struct {
	Schema    int        `json:"schema"`
	Channel   string     `json:"channel"` // e.g. "alpha"
	Version   string     `json:"version"` // e.g. "0.1.0-alpha.7"
	Published time.Time  `json:"published"`
	Expires   time.Time  `json:"expires"` // freeze-attack protection
	NotesURL  string     `json:"notes_url,omitempty"`
	Artifacts []Artifact `json:"artifacts"`
}

Manifest is the document published as latest.json. It is signed as exact bytes: the detached signature (latest.json.sig, base64) covers the file content verbatim, so there is no canonicalisation to get wrong.

func Check

func Check(ctx context.Context, c *http.Client, manifestURL string, keys []ed25519.PublicKey, currentVersion string, now func() time.Time) (*Manifest, error)

Check downloads manifestURL and manifestURL+".sig", verifies the signature, and returns the manifest only when it is newer than currentVersion. It returns ErrNotNewer when already up to date.

func Parse

func Parse(data []byte) (*Manifest, error)

Parse decodes and validates a manifest. It does NOT check the signature; callers must call Verify on the same bytes first (see VerifyAndParse).

func VerifyAndParse

func VerifyAndParse(data []byte, sigB64 string, keys []ed25519.PublicKey, now time.Time) (*Manifest, error)

VerifyAndParse verifies the signature, then parses, then checks expiry.

func (*Manifest) Select

func (m *Manifest) Select(goos, goarch, kind string) (Artifact, error)

Select returns the artifact of the given kind for the platform. On darwin a "universal" artifact matches any arch.

func (*Manifest) Validate

func (m *Manifest) Validate() error

Validate checks structural invariants.

type Replacement

type Replacement struct {
	// contains filtered or unexported fields
}

Replacement is an installed-but-not-yet-committed binary swap.

func Replace

func Replace(target, newFile string) (*Replacement, error)

Replace atomically swaps newFile in as target, keeping the previous binary as target+".old" until Commit. Steps:

  1. copy newFile next to target (same filesystem) as target+".new", mode copied from target
  2. rename target -> target+".old" (works for a running executable on Linux, macOS and Windows)
  3. rename target+".new" -> target

If step 3 fails the old binary is put back. After the new version has started successfully and passed its self-check, call Commit; if it did not, call Rollback.

func (*Replacement) Commit

func (r *Replacement) Commit() error

Commit discards the backup. On Windows a running .old file may be locked; that is ignored and it is cleaned up by Recover on the next start.

func (*Replacement) Rollback

func (r *Replacement) Rollback() error

Rollback restores the previous binary. Safe to call after a failed health check.

type Version

type Version struct {
	Major, Minor, Patch int
	Pre                 []string // dot-separated pre-release identifiers
}

Version is a parsed semantic version (no build metadata).

func ParseVersion

func ParseVersion(s string) (Version, error)

ParseVersion parses "1.2.3" or "1.2.3-alpha.4" (a leading "v" is allowed).

func (Version) Compare

func (v Version) Compare(o Version) int

Compare returns -1, 0 or 1 following semver precedence rules.

Jump to

Keyboard shortcuts

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