Files
magpie/internal/davsync/restore.go
T
yetone be2037285a davsync, gui: a server file that isn't a backup is said as what it is, and can be replaced by this computer's setup (JasonLeeForOnly on Discord)
JasonLeeForOnly's WebDAV sync failed with only "not a magpie backup" for
several versions, and nothing on the page got past it. A WebDAV GET of the
backup can come back as a body that isn't the file in several ways; each is
now reproduced with an httptest server that answers the way the real one
does:

Unpacked (WebDAV and S3): gzip with or without Content-Encoding (a proxy
that drops the header), x-gzip, zstd with or without the header, deflate
(zlib or raw), and a header that says gzip over a plain body. Bodies are
told by magic number; a backup is JSON, so either magic is never the file.
Brotli (br) can't be unpacked and is said as itself, with what to turn off.

Said, with no upload offered: an HTML page answered with 200 (Nextcloud
login after a redirect, Cloudflare Access, an AList/OpenList web app, an
rclone directory listing, a captive portal), with its <title> and the
host/path a redirect ended at (query, fragment and user dropped), and
where the WebDAV address is for 坚果云 and Nextcloud; an XML answer, by its
root element.

Said, with Upload as the way out: an empty (0-byte) file, a backup cut
short or damaged (the automatic rebuild is unchanged and still goes first
when its rules allow), another app's file ("format": "…"), other JSON,
and any other file by its content type. Messages carry the size, kind and a
page title only, never the file's contents.

backup.Open now tells ErrNotBackup (doesn't begin like a backup) from
ErrCorrupt (begins like one, cut short). The status carries the kind as
serverFile; Settings › Sync and backup says it in the reader's language,
wraps it whole, and shows "Upload this computer's setup" only when the file
may be replaced. davsync.Upload (POST /api/davsync/upload, `magpie webdav
upload`, `magpie s3 upload`) asks first in the GUI, re-reads the file,
refuses a backup this computer opens, one sealed with another passphrase,
a newer magpie's, a page and an XML answer, keeps the server's copy in the
sync folder (0600, newest 5) and writes nothing if that fails, then writes
with If-Match on the version read. New strings in en, zh, zh-TW, ja, de.

Verified: internal/davsync/not_backup_test.go (TestReadCompressedBackup,
TestReadBrotliSaid, TestServerAnswerSaid, TestCutBackupSaidAndUploaded,
TestS3ReadCompressedObject; Upload-only parts stripped to compile) fails
on e4a90aa6 for every compressed case but "gzip said", Brotli, every
server-answer case, the cut case and S3 gzip, and passes here. Dropping
the Replace check in Upload turns the page/XML cases red ("Upload wrote
over a page"). gofmt -l clean; go vet darwin, GOOS=linux and GOOS=windows
-tags nogui on ., davsync, backup, gui; full go test -tags nogui ./... ok;
-race on davsync and backup ok. GUI: sync-not-backup.test.cjs (en, zh,
zh-TW, ja, de; 900 and 440px), sync-*, s3-sync, gui-de, gui-ja, gui-zh-tw,
click-scroll in Chromium and WebKit, 81 pass. Not tried against a real
WebDAV server or on the Windows box; the reporter's server is unknown.

Note: 2018882e on main carries this subject by mistake (another session read a shared message file); it holds only GTK DPI code. This commit is the actual WebDAV change.
2026-10-09 12:14:26 +08:00

270 lines
7.6 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"
)
// ErrOff is a Restore, Undo or SetAuto asked for with sync off.
var ErrOff = errors.New("sync is off")
// 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
}
L, R := hashes(local), hashes(remote)
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)}
}
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 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}
}
// as a sync that brought them in leaves it: the next one has
// nothing to push back
st.Local, st.Remote, st.Sum, st.Server = hashes(local), R, sum(data), ver
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
}
b := local
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)}
}
// as the first sync to an empty server leaves it
st.Local, st.Remote, st.Sum, st.Server = hashes(local), hashes(b), sum(sealed), v
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.
func (c Config) syncs(part string) bool {
return (part != "agents" || c.Agents) && (part != "library" || c.library())
}
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) {
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")+"-before-restore"+backup.Ext)
if err := edit.WriteAtomic(name, sealed); err != nil {
return "", err
}
os.Chmod(name, 0o600)
old, _ := filepath.Glob(filepath.Join(dir, "*-before-restore"+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()
}