filesync

package
v0.16.200 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 27 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 filesync keeps one machine's folder in step with a collection of the store (internal/filestore): local edits go up with the revision they were made on, other machines' edits come down and land atomically, and an edit made on a stale copy is kept (revision.go) instead of being lost. It is the agent of design-sync, carried into harnessly: a runtime runs one for its Sinopia library.

Index

Constants

View Source
const (
	KindKept    = "kept"    // a file that is not a board: the losing contents are kept
	KindReapply = "reapply" // a small edit of a board: re-apply it on the fresh version
	KindVariant = "variant" // a bigger edit of a board: saved as the project Variant
)

Notice kinds.

View Source
const KeptDir = ".conflicts"

KeptDir is where a losing edit is kept, relative to the folder's root; hidden, so it is never synced.

View Source
const VariantFile = "variant.json"

VariantFile is the link record of a variant project, at its root.

Variables

View Source
var ErrNoBlob = errors.New("the store does not hold these contents")

ErrNoBlob is contents the collection does not hold.

View Source
var ErrUnauthorized = errors.New("the store did not accept this machine")

ErrUnauthorized is a store that refused this machine even with a fresh token.

Functions

func BoardOf

func BoardOf(rel string) (project, board string, ok bool)

BoardOf splits a path into project and board file when it is a board.

func ConflictName

func ConflictName(rel, machine string, at time.Time) string

ConflictName is where a conflicting local edit of rel is kept: `Main (конфликт vm-b 2026-10-18 2141).dc.html`, a board of its own in the same project, so the person and the agents see it.

func SmallEdit

func SmallEdit(a, b []byte) bool

SmallEdit: the two versions differ only in a short stretch, found by cutting the common start and end away.

func VariantID

func VariantID(project, machine string, at time.Time) string

VariantID names the project a losing edit is saved as; at most 64 characters.

Types

type Agent

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

Agent syncs one folder.

func New

func New(cfg Config) (*Agent, error)

New opens the folder and the agent's state.

func (*Agent) Apply

func (a *Agent) Apply(ctx context.Context, e filestore.Entry) error

Apply brings one revision from the hub into the folder.

func (*Agent) Close

func (a *Agent) Close()

Close releases the folder and the state.

func (*Agent) Pull

func (a *Agent) Pull(ctx context.Context, wait time.Duration) (bool, error)

Pull asks the hub for everything after the agent's cursor and applies it; with wait, it holds until something changes or the wait passes. It returns whether the hub had more.

func (*Agent) Quarantined

func (a *Agent) Quarantined() ([]Quarantined, error)

Quarantined lists the files set aside because the agent had no rights on them.

func (*Agent) RetryQuarantine

func (a *Agent) RetryQuarantine(ctx context.Context) error

RetryQuarantine tries the files in quarantine again once their rights may be back: it reads the hub's history from the oldest revision set aside and applies the latest one of each such file. A file still without rights stays.

func (*Agent) Run

func (a *Agent) Run(ctx context.Context) error

Run syncs until ctx ends: a first full sync, then the watcher, the long poll and a periodic rescan.

func (*Agent) Scan

func (a *Agent) Scan(ctx context.Context, rel string) error

Scan looks at one path (a file, a folder or something gone) and sends what changed to the hub.

func (*Agent) ScanAll

func (a *Agent) ScanAll(ctx context.Context) error

ScanAll walks the whole folder and every file the agent believes in: catches edits made while it was not running and events the watcher missed.

func (*Agent) SyncOnce

func (a *Agent) SyncOnce(ctx context.Context) error

SyncOnce pulls everything, then sends everything local: what the agent does at start.

type Client

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

Client speaks the store's API for one collection: its base is the store's root (`https://hub.example.org/store`, or anything an in-process transport answers), the collection one of filestore.Kinds.

func NewClient

func NewClient(base, collection string, token Token, hc *http.Client) *Client

NewClient makes a client of a collection. hc nil is a plain client with a timeout long enough for a long poll.

func (*Client) Changes

func (c *Client) Changes(ctx context.Context, since int64, wait time.Duration) (filestore.Changes, error)

Changes is every file changed after since; with wait, the store holds the request until something changes.

func (*Client) Collection

func (c *Client) Collection() string

Collection is the client's collection kind.

func (*Client) Commit

Commit asks for a new revision; a stale base answers Conflict with the current revision.

func (*Client) Current

func (c *Client) Current(ctx context.Context, path string) (*filestore.Entry, error)

Current is the latest revision of a path, nil when the path never existed.

func (*Client) GetBlob

func (c *Client) GetBlob(ctx context.Context, sha string, w io.Writer) error

GetBlob writes the contents with this sha256 to w; ErrNoBlob when the collection does not hold them.

func (*Client) HasBlob

func (c *Client) HasBlob(ctx context.Context, sha string) (bool, error)

HasBlob tells whether the collection holds the contents with this sha256.

func (*Client) Put

func (c *Client) Put(ctx context.Context, path string, data []byte, author string) (filestore.Entry, error)

Put stores data as the newest revision of path whatever was there (a file of a collection that nobody edits in place: an upload, a file an agent sent) and returns the revision; the contents are then read by Entry.Sha.

func (*Client) PutBlob

func (c *Client) PutBlob(ctx context.Context, sha string, data []byte) error

PutBlob uploads contents under their sha256.

type Codec

type Codec interface {
	// Canonical is the shareable form of a local file.
	Canonical(rel string, local []byte) []byte
	// Local is the file this machine writes for canonical contents; current is what the file holds now (nil when
	// absent), so machine-only parts survive.
	Local(rel string, canonical, current []byte) []byte
	// Tag fingerprints canonical contents; equal tags mean equal contents.
	Tag(canonical []byte) string
	// Seal makes the blob the hub stores; Open reverses it.
	Seal(rel string, canonical []byte) ([]byte, error)
	Open(rel string, blob []byte) ([]byte, error)
	// Merge combines two edits of the same base, true when it could (settings merge key by key).
	Merge(rel string, base, local, remote []byte, localNewer bool) ([]byte, bool)
	// Ready tells whether the codec can work (a sealed collection waits for its key).
	Ready() bool
}

Codec turns a local file into the collection's canonical form and back. The canonical form is what machines compare and what the store keeps. A Sinopia library is Plain: files as they are. The seam stays so that a collection that carries machine paths or is sealed before it leaves the machine can be added without touching the sync.

type Config

type Config struct {
	Root     string // the folder: a Sinopia library
	StateDir string // where the sync keeps what it knows, outside the folder's synced paths
	// Store is the collection the folder syncs with.
	Store   *Client
	Machine string // shown in what a lost write leaves behind
	Policy  filestore.Policy
	Codec   Codec
	// TmpDir is where downloads are staged before the rename (relative to Root, same filesystem).
	TmpDir string
	// ConflictDir, when set, takes a losing local edit (relative to Root, never synced) instead of the handling by
	// revision (revision.go).
	ConflictDir string
	// AdoptRemote: on the very first sync, a local file the collection does not have goes to ConflictDir instead of
	// up. That is joining a collection whose truth lives on another machine: what only this machine had is kept
	// aside, not spread.
	AdoptRemote bool
	// OnApplied is called after another machine's revision lands (outside the agent's lock).
	OnApplied func(rel string)
	// Log is where the sync says what it did (nil: slog's default).
	Log *slog.Logger
}

Config is one folder.

type Notice

type Notice struct {
	V    int    `json:"v"`
	Kind string `json:"kind"`
	// Path is the file whose write lost, Kept where its contents are (relative to the root).
	Path string `json:"path"`
	Kept string `json:"kept"`
	// Project and Board name a board (<project>/project/<board>.dc.html); empty for another file.
	Project string `json:"project,omitempty"`
	Board   string `json:"board,omitempty"`
	// Variant is the project the edit was saved as (kind variant).
	Variant string `json:"variant,omitempty"`
	// CurrentRev, By and ByMachine tell whose version took the place.
	CurrentRev int64  `json:"current_rev"`
	By         string `json:"by,omitempty"`
	ByMachine  string `json:"by_machine,omitempty"`
	Machine    string `json:"machine"`
	At         string `json:"at"`
	Bytes      int    `json:"bytes"`
}

Notice is what the machine keeps about a losing edit, next to the contents.

type Plain

type Plain struct{}

Plain is the codec of files as they are.

func (Plain) Canonical

func (Plain) Canonical(_ string, b []byte) []byte

func (Plain) Local

func (Plain) Local(_ string, c, _ []byte) []byte

func (Plain) Merge

func (Plain) Merge(string, []byte, []byte, []byte, bool) ([]byte, bool)

func (Plain) Open

func (Plain) Open(_ string, b []byte) ([]byte, error)

func (Plain) Ready

func (Plain) Ready() bool

func (Plain) Seal

func (Plain) Seal(_ string, c []byte) ([]byte, error)

func (Plain) Tag

func (Plain) Tag(c []byte) string

type Quarantined

type Quarantined struct {
	Path   string
	Rev    int64
	Reason string
	Since  time.Time
}

Quarantined is a file the agent cannot apply (no rights on the local copy): it is set aside so the rest of the queue goes on, and tried again on every rescan and when a newer revision of it arrives.

type Token

type Token func(ctx context.Context, refresh bool) (string, error)

Token gives the bearer a request to the store carries. refresh asks for a new one: the store refused the last.

type VariantLink struct {
	V       int    `json:"v"`
	Of      string `json:"of"`
	Board   string `json:"board,omitempty"`
	Rev     int64  `json:"rev"`
	Machine string `json:"machine"`
	At      string `json:"at"`
	Reason  string `json:"reason"`
}

VariantLink is the record `variant.json` of a variant project: whose variant it is, of which board and revision.

Jump to

Keyboard shortcuts

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