stage

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 15 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 stage owns everything that touches disk before a swap: the single-updater lock, the state directory, free-space checks, and the streamed, resumable, hash-verified download.

Index

Constants

View Source
const LauncherLock = "launcher.lock"

LauncherLock is held by a resident launcher for its whole life, so another process can tell a launcher is running without any socket or pid file.

View Source
const Margin = 64 << 20

Margin is kept free beyond the download itself, so finishing an update never fills the disk the program is about to run from.

View Source
const PartPrefix = ".ota-"

PartPrefix marks every temporary file this package creates, so a sweep can tell ours from anything else in the directory.

Variables

This section is empty.

Functions

func CreateTemp

func CreateTemp(dir, suffix string) (*os.File, error)

CreateTemp makes a new, empty temp file owner-only. O_EXCL means a file planted at a predictable name can never be opened and written through.

func Download

func Download(ctx context.Context, dir string, a ota.Artifact, fetch Fetcher, progress Progress) (string, error)

Download streams an artifact into dir and returns the path of the complete, verified file (mode 0600). A partial left by an interrupted run for the same artifact is resumed, but only after its existing bytes are re-hashed, so a partial altered between runs can never be laundered by the final check.

On a transfer error the partial is kept for resume and ota.ErrDownload is returned. On a size overrun, hash mismatch or full disk the partial is deleted, since resuming it could only fail again.

func EnsureDir

func EnsureDir(dir string) error

EnsureDir creates the state directory owner-only. It holds partial downloads of the program and the record of which version is trusted, so nobody else on the machine gets to read or plant files in it.

func Free

func Free(dir string) (uint64, bool)

Free returns the bytes available to an unprivileged writer in dir.

func Held

func Held(stateDir, name string) bool

Held reports whether another process currently holds the named lock.

func Partial

func Partial(dir string) string

Partial returns the partial file named by an active download record, so the sweep keeps it.

func ReadJSON

func ReadJSON(path string, v any) error

ReadJSON loads path into v. A missing file reports fs.ErrNotExist so callers can treat "no record" as a state, not a failure.

func Remove

func Remove(path string) error

Remove deletes path, treating absence as success.

func Sweep

func Sweep(dir string, keep ...string) error

Sweep removes temp files from crashed runs. keep names files still in use (the partial named by an active download record). Call only while holding the lock, or a live download in another process would be deleted under it.

func SyncDir

func SyncDir(dir string) error

SyncDir makes a rename in dir durable. Without it a power cut can undo a rename that already returned, leaving a state file pointing at nothing.

func WriteJSON

func WriteJSON(path string, v any) error

WriteJSON replaces path atomically: temp file, fsync, rename, fsync the directory. A crash leaves either the old file or the new one, never half.

Types

type Fetcher

type Fetcher func(ctx context.Context, offset int64) (io.ReadCloser, int64, error)

Fetcher opens the artifact starting at offset. It returns the offset the body actually starts at: a source that ignores Range answers from 0, and the download then restarts rather than appending the wrong bytes.

type Lock

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

Lock is a held single-updater lock. The OS releases it if the process dies, so there is no stale-lock heuristic to get wrong.

func Acquire

func Acquire(stateDir string) (*Lock, error)

Acquire takes the lock for a state directory without blocking, returning ota.ErrLocked if another process holds it.

func AcquireNamed

func AcquireNamed(stateDir, name string) (*Lock, error)

AcquireNamed takes a named lock in a state directory without blocking.

func (*Lock) Release

func (l *Lock) Release()

Release drops the lock. Safe to call more than once.

type Progress

type Progress func(done, total int64)

Progress is called as bytes land, for events.

Jump to

Keyboard shortcuts

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