* cli: magpie webdav — WebDAV sync from the terminal, what Settings › WebDAV sync does, for a machine with no window (WSL, a server over SSH) magpie webdav shows it; on <address> [user=…] [password=…] [keys=no] [agents=no] [library=no] turns it on and syncs at once, asking for the passphrase (and the password, when a user has none); set k=v… changes what is given and keeps the rest; now, dismiss and off are the page's Sync now, OK and Turn off. Through the same davsync.Configure, Now, Dismiss and Off. A sync, Off and Dismiss hold a lock the system keeps on sync.lock (flock; LockFileEx on Windows): the gateway's sync every 3 minutes and one from a terminal — or from the app's window while magpie serve has the gateway — would each save sync-state.json over the other's. The system lets it go when the magpie holding it ends, however it ends. Configure keeps the saved password only for the same server (scheme and host) and user: an address moved to another server, or another user, no longer takes it there — in the app too, where a new address with the password left empty was sent the old one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * davsync, cli: from the review of #127 — the setup read again under the lock, Configure locked too, the password never an argument, one left behind asked for, a wait that runs out said in the status Now reads the setup again once it has the lock: one that waited while another magpie turned sync off went on with the setup it had read, and saved sync-state.json back after the Off. Configure takes mu and the lock too, after a sync in progress. magpie webdav takes neither the password nor the passphrase as an argument: password= asks for it, as passphrase= does. Configure with the saved password left behind — another server or user, the password empty — says to type it and saves nothing, where the Settings page dropped it and the sync failed with 401 under a field that still said saved. A wait for the lock that runs out is davsync.ErrBusy. Dismiss returns it, and the davsync routes put ErrBusy from a sync in the status they answer with, where the page shows a sync's failures; Save's own errors stay in its form, what was typed in it kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * davsync, cli: the lock before mu, a wait bounded under the page's, one called off not called busy, the password rule in one place, a bare address with = in it The lock comes before mu everywhere, so a wait for another magpie never holds mu, and Configure, Off and Dismiss wait for it 30 seconds at most, less than the Settings page gives a request: before, a request could hang on mu and then on the lock for minutes. A wait for the lock called off, as when the page is closed or the gateway stops, is its context's error, not ErrBusy: nothing said another magpie was syncing. What becomes of the saved password is decided in one place, davsync.SavedPassword, which Configure and magpie webdav both go by, where the two had rules of their own that had come apart: an account with a password and no user name, moved to another server, had its password dropped with nothing asked, and the sync failed with 401. It is asked for now, as for an account with a user. magpie webdav takes a bare argument with :// in it as the address, = in it or not: https://host/dav?token=… was read as a field. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
2.1 KiB
Go
69 lines
2.1 KiB
Go
package davsync
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// ErrBusy is a wait for another magpie's sync that ran out of time.
|
|
var ErrBusy = errors.New("another magpie is syncing: try again in a moment")
|
|
|
|
// lock keeps what reads or writes sync's files — a sync, Configure, Off,
|
|
// Dismiss — to one at a time across magpies: the gateway's syncs every
|
|
// Every, and magpie webdav from a terminal meanwhile, would each save
|
|
// sync-state.json over the other's. It keeps them apart within one magpie
|
|
// too, each taking it through a descriptor of its own, and is taken before
|
|
// mu, so a wait for another magpie never holds mu. The system holds it on
|
|
// sync.lock and lets it go when the magpie holding it ends, however it
|
|
// ends, so none is ever left behind. The file stays: taken away, one
|
|
// magpie could hold the lock on it while another took one on a new file.
|
|
func lock(ctx context.Context) (unlock func(), err error) {
|
|
os.MkdirAll(settings.Dir(), 0o755)
|
|
f, err := os.OpenFile(path("sync.lock"), os.O_CREATE|os.O_RDWR, 0o600)
|
|
if err != nil {
|
|
return func() {}, nil // no lock to be had here: sync as before there was one
|
|
}
|
|
for {
|
|
got, err := tryLock(f)
|
|
if err != nil { // a file system without locks, as an NFS home with no lockd
|
|
f.Close()
|
|
return func() {}, nil
|
|
}
|
|
if got {
|
|
return func() { unlockFile(f); f.Close() }, nil
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
f.Close()
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
|
return nil, ErrBusy
|
|
}
|
|
return nil, ctx.Err()
|
|
case <-time.After(200 * time.Millisecond):
|
|
}
|
|
}
|
|
}
|
|
|
|
// wait is how long Configure, Off and Dismiss wait for a sync in progress:
|
|
// less than the Settings page gives a request.
|
|
const wait = 30 * time.Second
|
|
|
|
// locked runs fn holding the lock, then mu, after a sync in progress: it
|
|
// would save its state over what fn does.
|
|
func locked(fn func() error) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), wait)
|
|
defer cancel()
|
|
unlock, err := lock(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer unlock()
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
return fn()
|
|
}
|