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
- Variables
- func BoardOf(rel string) (project, board string, ok bool)
- func ConflictName(rel, machine string, at time.Time) string
- func SmallEdit(a, b []byte) bool
- func VariantID(project, machine string, at time.Time) string
- type Agent
- func (a *Agent) Apply(ctx context.Context, e filestore.Entry) error
- func (a *Agent) Close()
- func (a *Agent) Pull(ctx context.Context, wait time.Duration) (bool, error)
- func (a *Agent) Quarantined() ([]Quarantined, error)
- func (a *Agent) RetryQuarantine(ctx context.Context) error
- func (a *Agent) Run(ctx context.Context) error
- func (a *Agent) Scan(ctx context.Context, rel string) error
- func (a *Agent) ScanAll(ctx context.Context) error
- func (a *Agent) SyncOnce(ctx context.Context) error
- type Client
- func (c *Client) Changes(ctx context.Context, since int64, wait time.Duration) (filestore.Changes, error)
- func (c *Client) Collection() string
- func (c *Client) Commit(ctx context.Context, req filestore.CommitRequest) (filestore.CommitResponse, error)
- func (c *Client) Current(ctx context.Context, path string) (*filestore.Entry, error)
- func (c *Client) GetBlob(ctx context.Context, sha string, w io.Writer) error
- func (c *Client) HasBlob(ctx context.Context, sha string) (bool, error)
- func (c *Client) Put(ctx context.Context, path string, data []byte, author string) (filestore.Entry, error)
- func (c *Client) PutBlob(ctx context.Context, sha string, data []byte) error
- type Codec
- type Config
- type Notice
- type Plain
- func (Plain) Canonical(_ string, b []byte) []byte
- func (Plain) Local(_ string, c, _ []byte) []byte
- func (Plain) Merge(string, []byte, []byte, []byte, bool) ([]byte, bool)
- func (Plain) Open(_ string, b []byte) ([]byte, error)
- func (Plain) Ready() bool
- func (Plain) Seal(_ string, c []byte) ([]byte, error)
- func (Plain) Tag(c []byte) string
- type Quarantined
- type Token
- type VariantLink
Constants ¶
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.
const KeptDir = ".conflicts"
KeptDir is where a losing edit is kept, relative to the folder's root; hidden, so it is never synced.
const VariantFile = "variant.json"
VariantFile is the link record of a variant project, at its root.
Variables ¶
var ErrNoBlob = errors.New("the store does not hold these contents")
ErrNoBlob is contents the collection does not hold.
ErrUnauthorized is a store that refused this machine even with a fresh token.
Functions ¶
func ConflictName ¶
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.
Types ¶
type Agent ¶
type Agent struct {
// contains filtered or unexported fields
}
Agent syncs one folder.
func (*Agent) Pull ¶
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 ¶
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 ¶
Run syncs until ctx ends: a first full sync, then the watcher, the long poll and a periodic rescan.
func (*Agent) Scan ¶
Scan looks at one path (a file, a folder or something gone) and sends what changed to the hub.
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 ¶
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 ¶
Collection is the client's collection kind.
func (*Client) Commit ¶
func (c *Client) Commit(ctx context.Context, req filestore.CommitRequest) (filestore.CommitResponse, error)
Commit asks for a new revision; a stale base answers Conflict with the current revision.
func (*Client) GetBlob ¶
GetBlob writes the contents with this sha256 to w; ErrNoBlob when the collection does not hold them.
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.
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 Quarantined ¶
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 ¶
Token gives the bearer a request to the store carries. refresh asks for a new one: the store refused the last.
type VariantLink ¶
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.