journal

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 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 journal makes every write hopsesh does undoable. Before a file is created, replaced, appended to or moved, the journal records how to reverse it (and keeps a copy of what is replaced), on disk, so an interrupted move can still be undone.

Index

Constants

View Source
const (
	KindMove     = "move"     // a session brought here in its own agent
	KindContinue = "continue" // a session continued in another agent
	KindPush     = "push"     // a session sent to another machine (its mark and lineage here)
	KindMark     = "mark"     // a mark owed to a copy left behind
)

Kinds of operations.

Variables

View Source
var ErrChanged = errors.New("changed since")

ErrChanged means a file the operation wrote changed since: undoing it would lose that later work.

Functions

func Dir

func Dir(stateDir string) string

Dir is where journals live under the state folder.

Types

type Entry

type Entry struct {
	Op        Op        `json:"op"`
	Machine   string    `json:"machine"`
	Path      string    `json:"path"`
	From      string    `json:"from,omitempty"`   // OpRename
	Size      int64     `json:"size,omitempty"`   // OpAppend: size before
	Len       int64     `json:"len,omitempty"`    // OpAppend: bytes appended
	Sum       string    `json:"sum,omitempty"`    // OpAppend: their SHA-256
	Mtime     time.Time `json:"mtime,omitempty"`  // OpAppend: time before
	Backup    string    `json:"backup,omitempty"` // OpReplace: copy of the old file
	KeepMtime bool      `json:"keepMtime,omitempty"`
	Created   bool      `json:"created,omitempty"` // OpAppend: the append created the file
	// OpAppend: lines written later do not build on these bytes (AppendOptions.Standalone).
	Standalone bool `json:"standalone,omitempty"`
}

Entry is one journaled write.

type Journal

type Journal struct {
	ID      string             `json:"id"`
	Kind    string             `json:"kind"` // what kind of operation (Kind*)
	Title   string             `json:"title"`
	Time    time.Time          `json:"time"`
	Keys    []agent.SessionKey `json:"keys"` // the sessions it created or changed
	Entries []Entry            `json:"entries"`
	Undone  bool               `json:"undone,omitempty"`
	// Remote are journals of the same operation kept by hopsesh on other machines (a push):
	// undoing this one undoes them too.
	Remote []Remote `json:"remote,omitempty"`
	// After is the state the operation left each file it created or replaced in (Seal), so
	// undo can tell when one was used afterwards and refuse to lose that work.
	After []State `json:"after,omitempty"`
	// contains filtered or unexported fields
}

Journal is the undo record of one operation (a move, a continuation, a mark).

func List

func List(stateDir string) ([]*Journal, error)

List returns the journals, newest first.

func Load

func Load(stateDir, id string) (*Journal, error)

Load reads one journal.

func New

func New(stateDir, kind, title string) (*Journal, error)

New starts a journal of one operation.

func (*Journal) AddKey

func (j *Journal) AddKey(k agent.SessionKey)

AddKey records a session the journal concerns.

func (*Journal) AddRemote

func (j *Journal) AddRemote(machine, id string) error

AddRemote records a journal the same operation left on another machine.

func (*Journal) Adopt

func (j *Journal) Adopt(machine, p string) error

Adopt records a file another program just created for this operation (an agent's own importer), so undo removes it.

func (*Journal) Append

func (j *Journal) Append(fsys host.FS, machine, p string, b []byte, o agent.AppendOptions) error

Append adds to a file, remembering where and what, so undo can take out exactly these bytes, even from between lines written later when they are Standalone.

func (*Journal) Changed

func (j *Journal) Changed(fsFor func(machine string) (host.FS, error)) error

Changed reports the first file that changed since the operation (ErrChanged with what changed), or nil: one it created or replaced, or one it appended to (not Standalone) that has more after its bytes. A file that is gone, or a machine fsFor cannot reach, does not count.

func (*Journal) Forget

func (j *Journal) Forget(machine string) error

Forget drops the entries for a machine whose own hopsesh journals those writes (the sender of a push replays and journals them there).

func (*Journal) Place

func (j *Journal) Place(machine, staged, dst string, perm fs.FileMode) error

Place installs a staged local file at dst on this machine (moving, not copying, when both are on one volume). An existing dst is kept in the journal.

func (*Journal) Rename

func (j *Journal) Rename(fsys host.FS, machine, from, to string) error

Rename moves a file, remembering where from.

func (*Journal) Seal

func (j *Journal) Seal(fsFor func(machine string) (host.FS, error)) error

Seal records the state the operation left each file it created or replaced in. Files on machines fsFor cannot reach are left out (their undo is not checked).

func (*Journal) SetAside

func (j *Journal) SetAside(machine, p string) error

SetAside moves a local file into the journal (it comes back on undo).

func (*Journal) Undo

func (j *Journal) Undo(fsFor func(machine string) (host.FS, error), force bool) error

Undo reverses a journal's entries, newest first. fsFor returns the filesystem of a machine by name (an error when it cannot be reached; such entries are reported). Unless force, it refuses (ErrChanged) when a file the operation wrote changed since: undoing would lose that later work.

func (*Journal) WriteFile

func (j *Journal) WriteFile(fsys host.FS, machine, p string, b []byte, perm fs.FileMode) error

WriteFile replaces or creates a file, keeping a copy of the old one.

type Op

type Op string

Op is a journaled operation.

const (
	OpCreate  Op = "create"  // undo: remove
	OpReplace Op = "replace" // undo: put the backup back
	OpAppend  Op = "append"  // undo: cut out the appended bytes (Size, Len, Sum)
	OpRename  Op = "rename"  // undo: move back
)

type Remote

type Remote struct {
	Machine string `json:"machine"` // as this machine's configuration names it
	ID      string `json:"id"`
}

Remote names a journal on another machine's hopsesh.

type State

type State struct {
	Machine string `json:"machine"`
	Path    string `json:"path"`
	Size    int64  `json:"size"`
	Sum     string `json:"sum"`
}

State is a file's size and SHA-256.

Jump to

Keyboard shortcuts

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