workdir

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package workdir is a repo folder as publish and pull see it: the files a publish carries, hashed into a tree, and the safe writing of a merge into them, with a backup to undo it. docs/designs/publish.md is its design.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(dir, backupDir string, changes []Change, begin func(before map[string]string) error) error

Apply writes changes into the folder at dir, as safely as it can:

  1. Every path is checked first (CheckPath). One unsafe path writes nothing.
  2. The current version of each path that will change is copied into backupDir, named by its blob id, never by its path: a backup named models/orders.aml would be read as part of the repo by anything that scans for .aml files.
  3. begin is called with each changed path's blob id before the merge, "" for one that didn't exist, for the caller to record before anything is written: a write cut short can then be undone (Restore).
  4. Each file is written through a temporary file and a rename, keeping an existing file's mode; deleted paths are removed.

Every file operation goes through an os.Root opened at dir or backupDir, so none can leave them, by ".." or a symlink, even one made after the check.

func CheckPath

func CheckPath(dir, p string) error

CheckPath refuses a path anfra won't write in the folder at dir: one that isn't a clean, relative, slash-separated path publish would send, or that leads through a symlink, or onto something other than a regular file.

func Files

func Files(dir string) (files []string, err error)

Files is the files a publish carries, slash-separated, relative to dir, sorted: the repo's own, as repoformat.Publishable says, the same on every machine, with or without git. A symlink, or anything but a regular file, among them fails with a NotRegularError.

func Restore

func Restore(dir, backupDir string, before map[string]string) error

Restore puts each path in before back as it was: its content from the backup, or deleted when it didn't exist (""). It undoes an Apply, whole or cut short, and takes a single path back for `--take local`.

Types

type Change

type Change struct {
	Path    string
	Content []byte
	Delete  bool
}

Change is a path's content after a merge: Content written, or the path deleted.

type NotRegularError

type NotRegularError struct {
	// Files are the paths, each with what it is: "models/a.aml (a symlink)".
	Files []string
}

NotRegularError names files of the repo that aren't regular files: a symlink or anything else. They're refused by name, never skipped, which would read as deleted, nor followed out of the repo.

func (*NotRegularError) Error

func (e *NotRegularError) Error() string

type Tree

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

Tree is a folder's files as a merge sees them: each selected file's path and blob id, as git names its content (repoformat.BlobID).

func Read

func Read(dir string) (Tree, error)

Read selects the folder's files (Files) and hashes each.

func (Tree) Files

func (t Tree) Files() map[string]string

Files is the tree's paths and blob ids. Callers mustn't change it.

func (Tree) Paths

func (t Tree) Paths() []string

Paths is the tree's paths, sorted.

func (Tree) ReadBlob

func (t Tree) ReadBlob(_ context.Context, id string) ([]byte, error)

ReadBlob reads a file's content by blob id, from a path holding it. A file changed since Read fails: the merge would otherwise work from content other than what it classified.

func (Tree) Size

func (t Tree) Size(path string) int

Size is a file's size in bytes, when it was read.

func (Tree) Unselected

func (t Tree) Unselected(paths []string) []string

Unselected is those of paths that exist in the folder but aren't among its selected files: a symlink, or a file this anfra doesn't publish, such as one an older anfra did. A merge would read each as deleted, and the next publish would delete it from the org, so callers stop and name them.

type UnsafePathError

type UnsafePathError struct {
	Path   string
	Reason string
}

UnsafePathError is a path a merge would write that anfra refuses: one that isn't a repo file publish would send, or that would reach outside the folder, through a symlink or otherwise. Nothing is written when there's one.

func (*UnsafePathError) Error

func (e *UnsafePathError) Error() string

Jump to

Keyboard shortcuts

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