Documentation
¶
Overview ¶
Package installtxn provides the cross-process filesystem transaction used by plugin and skill installation. Callers stage content before taking the lock, then commit the content swap and lockfile update together while holding it.
Index ¶
- func CommitDir(target string, staged string, publish func() error) error
- func Lock(dir string) (func(), error)
- func Recover(dir string, reconcile Reconciler) error
- func RemoveDir(target string, publish func() error) error
- func StageDir(dir string) (stage string, cleanup func(), err error)
- func WriteFileAtomically(path string, data []byte, perm os.FileMode) error
- type Phase
- type Reconciler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CommitDir ¶
CommitDir replaces target with staged and runs publish while retaining the previous target. If either the swap or publish fails, the previous target is restored (or the new target is removed for a first install).
The caller must hold the install-root lock returned by Lock.
func Lock ¶
Lock takes the per-install-root cross-process lock. It blocks until any other installer or remover using dir has completed.
func Recover ¶
func Recover(dir string, reconcile Reconciler) error
Recover resolves the workspaces an earlier run was killed inside, which is what a process killed mid-commit leaves: a tree retained in a workspace nothing else reads. How far that commit got cannot be read off the filesystem. CommitDir swaps the trees and only then publishes, so a kill between the two leaves exactly what a kill after both leaves, and a phase bit written after the publish only inverts which side of the write the ambiguity falls on. Only the caller knows what its published metadata records, so reconcile is asked which of the two trees that is, and its answer decides: the live target stands and the backup beside it is retired, or the backup is the truthful tree and goes back over the target. A nil reconciler means the caller publishes no metadata beside the tree, so a live target is the committed one. With nothing at the target there is no choice to make and the backup, the only copy in existence, is put back.
The returned error means an attributable transaction was left unresolved, never that there was nothing to do. Malformed, legacy, and unattributable workspaces are skipped without error, since they may be somebody's content or somebody else's transaction, but a workspace we own and could not resolve is reported. Every workspace is processed before returning, so one unresolved transaction does not strand the rest.
The caller must hold the install-root lock returned by Lock, and EVERY caller that takes that lock must call this first and abort on its error before it reads the lockfile, inspects the target, installs over it, or reports a successful removal. Recovering only on the install path is worse than not recovering at all: a removal would then report success while the backup it never saw stayed on disk, and the next install would publish it again, reinstating something the user deleted. Recovery is deliberately an explicit call rather than a side effect of Lock, matching how the other staged-swap transactions in this repo invoke their repair pass.
func RemoveDir ¶
RemoveDir removes target and runs publish while retaining the target until publish succeeds. A publish failure restores the directory.
The caller must hold the install-root lock returned by Lock.
Types ¶
type Phase ¶
type Phase int
Phase names which tree in an interrupted workspace the caller's published metadata records.
const ( // PhaseUnknown means the metadata matches neither tree. Recovery refuses to // act and reports the workspace as unresolved. PhaseUnknown Phase = iota // PhaseCommitted means the live target is what the metadata records: the // publish committed, and the backup beside it is superseded. PhaseCommitted // PhasePrePublish means the metadata still records the retained backup: the // tree swap landed but the publish never did. PhasePrePublish )
type Reconciler ¶
Reconciler classifies one interrupted workspace for the install named name. target is the live install path, backup the retained tree beside it. An error means the classification could not be made and recovery must not act.