jinpwng (#6428, on X) syncs two computers and wants only Library and memory to go between them: each keeps its own providers, settings and models. They also want to see what a sync would change before it applies, and to roll the last one back. What syncs: providers, settings and profiles now have switches like agents' models, library and memory (Config.Providers/Settings/Profiles, *bool, nil = synced as before, so an existing setup syncs exactly what it did). The sync form shows Providers (and its API keys), Settings and Agents' models up front; Profiles, usage and sign-ins sit under Advanced, closed unless one is set away from its default. The form sends a part's choice only when it is or was off. A part left out stays as it is here and on the server; turning it back on joins it like a first sync (st.Off, rejoin). The sync's line says which parts each computer keeps ("Stays as it is on each computer: ..."). The setup text and the CLI carry it: magpie <webdav|s3|github> set providers=no settings=no profiles=no. Preview: davsync.Preview (plan.go) says, per part, what syncing now would bring in, send up, or leave; library items and memory files by name, sign-ins and plugins gone. Preview... in Settings > Sync shows it, and Sync now from there runs SyncPreviewed(token): if the server or this computer changed since, it says so and shows the new plan instead of syncing something else. CLI: magpie <kind> preview. Undo: a sync that brings parts in records them (st.Applied) and keeps a sealed copy of this computer first (keepSealed, as Restore does). Undo the last sync puts those parts back from that copy, on this computer only (st.Local, so the next sync sends it up rather than fetching the server's again). If one of them changed here since, it asks first (409 changed, Undo anyway). CLI: magpie <kind> undo-sync [anyway]. Restore's own Undo is unchanged. docs/subsystems/sync-backup.md and the site's sync docs (en, zh, ja) have a section on it. i18n: zh, zh-TW, ja, de. Run (worktree at base48e394146): - go test -count=1 . ./internal/davsync ./internal/cli: ok (mote). Broken by hand, each went red: syncs() forced true (scope tests), rejoin's again forced false, UndoSync without the st.Local update. - go test -count=1 ./internal/gui: TestCoreScriptBudget was over by 85 bytes; the sync-left line moved into syncfiles.js, now ok. - go vet ./internal/gui, GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok. gofmt -l: nothing. - node --check app.js, syncfiles.js, sync-scope.test.cjs: ok. - GUI (mica), every test file that opens Settings > Sync or calls davsync (22 files) plus i18n-keys, Chromium and WebKit, 1100 and 420px, en/zh/zh-TW/ja/de: 453 of 454 pass. The red is s3-sync webkit en "picking S3 must not scroll the page" (0 !== 98), the same on origin/main52c9880cb(#3413, #2448). An earlier run also had library-new-skill chromium en 420 red, the same on main (#6539). sync-scope.test.cjs is new: on the old code there is no Providers switch, Preview or Undo, so it fails at once. - Rebased on origin/main7eecbcc56(app.js and i18n.js changed there): TestCoreScriptBudget, go test . ./internal/davsync, node --check, and i18n-keys, sync-scope, webdav-settings-refresh (both engines): ok. - The full go test ./... was started through mote's suite lock when this was pushed; its result goes on #6428. Not tried: a real WebDAV, S3 or GitHub server (the tests use fakes); the real app on Linux WebKitGTK. "Save and sync" from the form, after a preview, syncs without the preview's token check.
396 lines
12 KiB
Go
396 lines
12 KiB
Go
package davsync
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/backup"
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// ErrOff is a Restore, Undo or SetAuto asked for with sync off.
|
|
var ErrOff = errors.New("sync is off")
|
|
|
|
// errNothingToFollow is a sync of a computer following the server when
|
|
// the server has no setup yet: nothing goes up from one that follows.
|
|
var errNothingToFollow = errors.New("there is no setup on the server to follow yet: sync the computer the others follow first")
|
|
|
|
// SetFollowServer sets whether this computer follows the setup on the
|
|
// server (Config.FollowServer) or syncs both ways.
|
|
func SetFollowServer(on bool) error {
|
|
return locked(func() error {
|
|
c, ok := Load()
|
|
if !ok {
|
|
return ErrOff
|
|
}
|
|
c.FollowServer = on
|
|
b, err := json.MarshalIndent(c, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := edit.WriteAtomic(path("sync.json"), b); err != nil {
|
|
return err
|
|
}
|
|
return os.Chmod(path("sync.json"), 0o600)
|
|
})
|
|
}
|
|
|
|
// SetAuto sets how many minutes apart the gateway's magpie syncs by
|
|
// itself, one of AutoChoices; Manual: never, only when asked (#847).
|
|
func SetAuto(minutes int) error {
|
|
if !slices.Contains(AutoChoices, minutes) {
|
|
return fmt.Errorf("sync by itself every 3, 15, 30 or 60 minutes, or never, not %d", minutes)
|
|
}
|
|
return locked(func() error {
|
|
c, ok := Load()
|
|
if !ok {
|
|
return ErrOff
|
|
}
|
|
c.AutoEvery = minutes
|
|
if minutes == int(Every/time.Minute) {
|
|
c.AutoEvery = 0
|
|
}
|
|
b, err := json.MarshalIndent(c, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := edit.WriteAtomic(path("sync.json"), b); err != nil {
|
|
return err
|
|
}
|
|
return os.Chmod(path("sync.json"), 0o600)
|
|
})
|
|
}
|
|
|
|
// Restore makes this computer's setup the server's, whatever changed here:
|
|
// each part synced that differs is brought in, as a sync brings in a part
|
|
// changed only there. What was here first is sealed into the sync folder,
|
|
// with the API keys, and Undo brings it back. It is the parts brought in.
|
|
func Restore(ctx context.Context) ([]string, error) {
|
|
var brought []string
|
|
err := locked(func() error {
|
|
c, ok := Load()
|
|
if !ok {
|
|
return ErrOff
|
|
}
|
|
d, err := newRemote(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
data, ver, err := d.get(ctx, version{}) // the file as it is now, never the cached copy
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if data == nil {
|
|
return errors.New("there is no magpie setup on the server yet: nothing to restore")
|
|
}
|
|
remote, err := backup.Open(data, c.Passphrase)
|
|
if errors.Is(err, backup.ErrPassphrase) {
|
|
return errors.New("the passphrase doesn't open the file on the server: it was sealed with another one; use the passphrase set on your other computers")
|
|
}
|
|
if err != nil {
|
|
return described(c, data, err)
|
|
}
|
|
local, err := collect(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
here := withHere(nil, hereOf(c, local)) // an agent's picks kept here are no change
|
|
L, R := hashesOwn(local, here), hashesOwn(remote, here)
|
|
for _, p := range Parts {
|
|
if L[p] == R[p] || !c.syncs(p) || (p == "library" && remote.Library == nil) {
|
|
continue
|
|
}
|
|
brought = append(brought, p)
|
|
}
|
|
st := loadState()
|
|
if st.Key != stateKey(c) {
|
|
st = state{Key: stateKey(c), Brought: st.Brought, Replaced: st.Replaced, Own: st.Own, Here: st.Here}
|
|
}
|
|
if len(brought) > 0 {
|
|
kept, err := keepHere(c)
|
|
if err != nil {
|
|
return fmt.Errorf("keeping this computer's setup before restoring: %w", err)
|
|
}
|
|
for _, p := range brought {
|
|
if p == "agents" {
|
|
// the server's whole, the fields kept here too: they
|
|
// follow the others again, as Follow leaves them
|
|
got, err := bringAgents(remote.Agents, here)
|
|
broughtIn(&st, got)
|
|
st.Own, st.Follow = nil, false
|
|
if err != nil {
|
|
return fmt.Errorf("bringing in the %s: %w", p, err)
|
|
}
|
|
continue
|
|
}
|
|
if err := bring(remote, p); err != nil {
|
|
return fmt.Errorf("bringing in the %s: %w", p, err)
|
|
}
|
|
}
|
|
if local, err = collect(c); err != nil {
|
|
return err
|
|
}
|
|
st.Undo = kept
|
|
st.Notice = &Notice{At: time.Now(), Here: brought, Saved: filepath.Dir(kept), Restored: true}
|
|
}
|
|
// the server's sign-ins, when they go: put in as a backup's are,
|
|
// nothing renewed later here written over
|
|
if c.SignIns && remote.SignIns != nil {
|
|
if _, err := provider.RestoreSignIns(remote.SignIns); err != nil {
|
|
return fmt.Errorf("bringing in the sign-ins: %w", err)
|
|
}
|
|
if local, err = collect(c); err != nil {
|
|
return err
|
|
}
|
|
st.SignIns = nil
|
|
}
|
|
// as a sync that brought them in leaves it: the next one has
|
|
// nothing to push back
|
|
st.Here = hereOf(c, local)
|
|
own := withHere(st.Own, st.Here)
|
|
st.Local, st.Remote, st.Sum, st.Server = hashesOwn(local, own), hashesOwn(remote, own), sum(data), ver
|
|
st.Blind = nil // what is here now is what the server has
|
|
remember(data)
|
|
st.Last, st.Error = time.Now(), ""
|
|
saveState(st)
|
|
return nil
|
|
})
|
|
return brought, err
|
|
}
|
|
|
|
// Upload writes this computer's setup over the server's file when that
|
|
// file isn't a backup a sync can read — empty, cut short, another app's —
|
|
// so a sync stopped on it has a way on that the user chooses. A file that
|
|
// opens as a backup, one sealed with another passphrase, a newer magpie's,
|
|
// and a page the server answered with instead of a file are never written
|
|
// over. What the server held is kept in the sync folder first, and nothing
|
|
// is written when it can't be. The write is made over the version read, so
|
|
// a file another computer wrote meanwhile isn't.
|
|
func Upload(ctx context.Context) error {
|
|
return locked(func() error {
|
|
c, ok := Load()
|
|
if !ok {
|
|
return ErrOff
|
|
}
|
|
d, err := newRemote(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
data, ver, err := d.get(ctx, version{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if data != nil {
|
|
_, err := backup.Open(data, c.Passphrase)
|
|
switch {
|
|
case err == nil:
|
|
return errors.New("the file on the server is a magpie backup this computer can open: Sync now merges with it instead, and nothing was written over it")
|
|
case errors.Is(err, backup.ErrPassphrase):
|
|
return errors.New("the file on the server is a magpie backup sealed with another passphrase, so it wasn't written over: use the passphrase set on your other computers")
|
|
}
|
|
err = described(c, data, err)
|
|
var nb *notBackup
|
|
if !errors.As(err, &nb) || !nb.f.Replace {
|
|
return err
|
|
}
|
|
if len(data) > 0 {
|
|
if err := keepAside(data, "server-replaced", 5); err != nil {
|
|
return fmt.Errorf("keeping the server's file before writing over it, so nothing was written: %w", err)
|
|
}
|
|
}
|
|
}
|
|
local, err := collect(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// a part not synced goes as the server last had it, when this
|
|
// computer read it whole before, else unset: never this computer's,
|
|
// and never an empty one over what the others have when it can be helped
|
|
b := c.only(local)
|
|
if st := loadState(); st.Key == stateKey(c) && len(c.left()) > 0 {
|
|
if prev, err := backup.Open(cached(st.Sum), c.Passphrase); err == nil {
|
|
for _, p := range c.left() {
|
|
take(&b, prev, p)
|
|
}
|
|
}
|
|
}
|
|
b.Created, b.App = time.Now().UTC(), "magpie"
|
|
sealed, err := backup.Seal(b, c.Passphrase)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
v, err := d.put(ctx, sealed, ver.ETag)
|
|
if errors.Is(err, errChanged) {
|
|
return errors.New("the file on the server changed while it was being replaced, so it wasn't: sync now to see what it is")
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
st := loadState()
|
|
if st.Key != stateKey(c) {
|
|
st = state{Key: stateKey(c), Brought: st.Brought, Replaced: st.Replaced, Own: st.Own, Here: st.Here}
|
|
}
|
|
// as the first sync to an empty server leaves it
|
|
st.Here = hereOf(c, local)
|
|
own := withHere(st.Own, st.Here)
|
|
st.Local, st.Remote, st.Sum, st.Server = hashesOwn(local, own), hashesOwn(b, own), sum(sealed), v
|
|
st.Blind = nil // it replaced what wasn't a backup: nothing to merge again
|
|
remember(sealed)
|
|
st.Last, st.Error, st.File = time.Now(), "", nil
|
|
saveState(st)
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Undo puts back the setup the last Restore replaced. The next sync takes
|
|
// it to the server, as any change made here.
|
|
func Undo() ([]string, error) {
|
|
var back []string
|
|
err := locked(func() error {
|
|
c, ok := Load()
|
|
if !ok {
|
|
return ErrOff
|
|
}
|
|
st := loadState()
|
|
if st.Key != stateKey(c) || st.Undo == "" {
|
|
return errors.New("there is no restore to undo")
|
|
}
|
|
data, err := os.ReadFile(st.Undo)
|
|
if err != nil {
|
|
return fmt.Errorf("the copy kept before the restore is gone: %w", err)
|
|
}
|
|
kept, err := backup.Open(data, c.Passphrase)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
local, err := collect(withKeys(c))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
L, K := hashes(local), hashes(kept)
|
|
for _, p := range Parts {
|
|
if L[p] == K[p] || !c.syncs(p) || (p == "library" && kept.Library == nil) {
|
|
continue
|
|
}
|
|
if err := bring(kept, p); err != nil {
|
|
return fmt.Errorf("bringing back the %s: %w", p, err)
|
|
}
|
|
back = append(back, p)
|
|
}
|
|
st.Undo, st.Notice = "", nil
|
|
saveState(st)
|
|
return nil
|
|
})
|
|
return back, err
|
|
}
|
|
|
|
// syncs is whether part goes to the server with c. A part that doesn't
|
|
// stays as it is on each computer: never brought in, never taken up.
|
|
func (c Config) syncs(part string) bool {
|
|
switch part {
|
|
case "providers":
|
|
return yes(c.Providers)
|
|
case "settings":
|
|
return yes(c.Settings)
|
|
case "profiles":
|
|
return yes(c.Profiles)
|
|
case "agents":
|
|
return c.Agents
|
|
case "library":
|
|
return c.library()
|
|
case "memory":
|
|
return c.memoryOn()
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Synced is the parts c syncs, in Parts' order, memory last.
|
|
func (c Config) Synced() []string {
|
|
var out []string
|
|
for _, p := range append(slices.Clone(Parts), "memory") {
|
|
if c.syncs(p) {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// left is the parts of Parts c doesn't sync.
|
|
func (c Config) left() []string {
|
|
var out []string
|
|
for _, p := range Parts {
|
|
if !c.syncs(p) {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// only is b with the parts c doesn't sync as a new magpie has them
|
|
// (unset): what goes up to a server with none of its own. Another
|
|
// computer that syncs such a part then puts its own there on joining, as
|
|
// with any unset part (#5642), and none is brought down from this one.
|
|
// Agents, library and memory are left out by collect already.
|
|
func (c Config) only(b backup.Bundle) backup.Bundle {
|
|
if !c.syncs("providers") {
|
|
b.Providers, b.Groups, b.Icons, b.Order, b.GroupOrder = nil, nil, nil, nil, nil
|
|
b.Searches, b.Plugins, b.Proxies = nil, nil, nil
|
|
}
|
|
if !c.syncs("settings") {
|
|
s := settings.Defaults()
|
|
b.Settings, b.GatewayKeys, b.AccountGroups = &s, nil, nil
|
|
}
|
|
if !c.syncs("profiles") {
|
|
b.Profiles = nil
|
|
}
|
|
return b
|
|
}
|
|
|
|
func withKeys(c Config) Config { c.Keys = true; return c }
|
|
|
|
// keepHere seals this computer's setup, keys and all, into the sync
|
|
// folder before a restore replaces it; the path. The newest few are kept.
|
|
func keepHere(c Config) (string, error) { return keepSealed(c, "before-restore") }
|
|
|
|
// keepSealed seals this computer's setup, keys and all, into the sync
|
|
// folder as <time>-<tag>; the path. The newest 5 so tagged are kept.
|
|
func keepSealed(c Config, tag string) (string, error) {
|
|
b, err := collect(withKeys(c))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
sealed, err := backup.Seal(b, c.Passphrase)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
dir := path("sync")
|
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
|
return "", err
|
|
}
|
|
name := filepath.Join(dir, time.Now().Format("2006-01-02-150405.000")+"-"+tag+backup.Ext)
|
|
if err := edit.WriteAtomic(name, sealed); err != nil {
|
|
return "", err
|
|
}
|
|
os.Chmod(name, 0o600)
|
|
old, _ := filepath.Glob(filepath.Join(dir, "*-"+tag+backup.Ext))
|
|
slices.Sort(old)
|
|
for len(old) > 5 {
|
|
os.Remove(old[0])
|
|
old = old[1:]
|
|
}
|
|
return name, nil
|
|
}
|
|
|
|
func isFile(p string) bool {
|
|
fi, err := os.Stat(p)
|
|
return err == nil && fi.Mode().IsRegular()
|
|
}
|