diff --git a/e2e/multiroot_test.go b/e2e/multiroot_test.go index 271f591f..fb0d4958 100644 --- a/e2e/multiroot_test.go +++ b/e2e/multiroot_test.go @@ -47,44 +47,12 @@ func readIn(dir, rel string) string { return string(b) } -// addRoot attaches an extra save location through the real API. -// -// A newly paired game may still have an accepted initial sync reading or -// writing its original root. Native topology admission must return 409 rather -// than remap a path underneath that work. A user can retry once that native -// work drains; this helper exercises exactly that public retry contract. +// addRoot attaches an extra save location through the API, the way the app +// will. func addRoot(t *testing.T, td *testutil.TestDaemon, gameID, name, path string) { t.Helper() - endpoint := "/api/games/" + gameID + "/roots" - payload := map[string]string{"name": name, "path": path} - deadline := time.Now().Add(45 * time.Second) - for attempts := 1; ; attempts++ { - status := td.APIStatus(http.MethodPost, endpoint, payload, nil) - switch status { - case http.StatusOK: - return - case http.StatusConflict: - // A refused request must not silently apply the requested mapping. - // Peer-learned, unmapped named roots are permitted to appear while - // the initial sync is still running. - var roots []struct { - Name string `json:"name"` - Path string `json:"path"` - } - td.API(http.MethodGet, endpoint, nil, &roots) - for _, root := range roots { - if root.Name == name && root.Path == path { - t.Fatalf("AddRoot returned 409 but applied the requested mapping %q -> %q", name, path) - } - } - default: - t.Fatalf("AddRoot %q returned HTTP %d: %s", name, status, td.LastError()) - } - if time.Now().After(deadline) { - t.Fatalf("AddRoot %q remained busy after %d attempts (45s); native work did not drain", name, attempts) - } - time.Sleep(100 * time.Millisecond) - } + td.API(http.MethodPost, "/api/games/"+gameID+"/roots", + map[string]string{"name": name, "path": path}, nil) } // Two devices, one game, two folders each. A file written to the second diff --git a/internal/api/root_topology_admission_test.go b/internal/api/root_topology_admission_test.go index b7da553e..d964a926 100644 --- a/internal/api/root_topology_admission_test.go +++ b/internal/api/root_topology_admission_test.go @@ -5,12 +5,14 @@ import ( "os" "path/filepath" "testing" + "time" ) // Root API mutations must observe native admission for the duration of the // Store write, not merely sample an active-writer count. func TestRootTopologyRefusesActiveNativeWriter(t *testing.T) { ts := startTestServer(t) + ts.server.topologyWaitFor = 25 * time.Millisecond id := trackProbeGame(t, ts) dir := filepath.Join(t.TempDir(), "config") if err := os.MkdirAll(dir, 0700); err != nil { diff --git a/internal/api/root_topology_wait_test.go b/internal/api/root_topology_wait_test.go new file mode 100644 index 00000000..a1f0edc4 --- /dev/null +++ b/internal/api/root_topology_wait_test.go @@ -0,0 +1,43 @@ +package api + +import ( + "net/http" + "os" + "path/filepath" + "testing" + "time" +) + +// One actual request waits for the in-flight writer and then succeeds. +// This does not rely on an E2E helper repeatedly retrying a refused request. +func TestRootTopologySingleRequestWaitsForWriterAndPreservesFiles(t *testing.T) { + ts := startTestServer(t) + ts.server.topologyWaitFor = time.Second + id := trackProbeGame(t, ts) + dir := filepath.Join(t.TempDir(), "config") + if err := os.Mkdir(dir, 0700); err != nil { t.Fatal(err) } + path := filepath.Join(dir, "slot.sav") + if err := os.WriteFile(path, []byte("ORIGINAL"), 0600); err != nil { t.Fatal(err) } + resp, body := ts.do(t, http.MethodPost, "/api/games/"+id+"/roots", map[string]string{"name":"config","path":dir}) + if resp.StatusCode != http.StatusOK { t.Fatalf("setup failed HTTP %d: %v",resp.StatusCode,body) } + writer := ts.daemon.P2P.Sync.Writing(id) + result := make(chan int,1) + go func() { + response, _ := ts.do(t, http.MethodDelete, "/api/games/"+id+"/roots/config", nil) + result <- response.StatusCode + }() + select { + case status := <-result: t.Fatalf("mutated while writer active: HTTP %d",status) + case <-time.After(60*time.Millisecond): + } + roots, err := ts.daemon.Store.GameRootPaths(id) + if err != nil || roots["config"] != dir { t.Fatalf("mapping moved during writer: %v %v",roots,err) } + writer() + select { + case status := <-result: + if status != http.StatusOK { t.Fatalf("after drain HTTP %d",status) } + case <-time.After(3*time.Second): t.Fatal("single API request never resumed") + } + bytes, err := os.ReadFile(path) + if err != nil || string(bytes)!="ORIGINAL" { t.Fatalf("physical save modified: %s %v",bytes,err) } +} diff --git a/internal/api/routes.go b/internal/api/routes.go index 3606fc88..84ef1b7e 100644 --- a/internal/api/routes.go +++ b/internal/api/routes.go @@ -434,6 +434,13 @@ func (s *Server) handleUpdateGame(w http.ResponseWriter, r *http.Request) { } game.SavePath = abs } + // A combined path+exclusion change would rebase agreement from a new + // path before admission. Refuse rather than claim old lineage for new + // save bytes; a safe combined transition needs a separate explicit design. + if game.SavePath != oldSavePath && s.Daemon.IgnoreText(game) != oldIgnore { + writeError(w, http.StatusConflict, "Change save location and sync exclusions separately.") + return + } // Cover art: a user-set custom URL is always kept. An empty cover, or // a previously auto-generated Steam cover, is (re)derived from the // AppID — so changing the AppID refreshes the art. @@ -483,11 +490,8 @@ func (s *Server) handleUpdateGame(w http.ResponseWriter, r *http.Request) { } if game.SavePath != oldSavePath { - done, ok := s.tryRootTopology(gameID) - if !ok { - writeError(w, http.StatusConflict, "cannot move a save location while native work is active or admission is unavailable") - return - } + done, ok := s.acquireRootTopology(w, r, gameID) + if !ok { return } defer done() } if err := s.Daemon.Store.UpdateGame(game); err != nil { diff --git a/internal/api/routes_roots.go b/internal/api/routes_roots.go index a8ed6ddd..68756985 100644 --- a/internal/api/routes_roots.go +++ b/internal/api/routes_roots.go @@ -1,6 +1,9 @@ package api import ( + "context" + "errors" + "time" "net/http" "net/url" "strings" @@ -71,11 +74,8 @@ func (s *Server) handleAddGameRoot(w http.ResponseWriter, r *http.Request) { } // A root that is currently being written by an in-flight transfer must // not be remapped out from under that transfer (issue #60). - done, ok := s.tryRootTopology(gameID) - if !ok { - writeError(w, http.StatusConflict, "cannot change root while native work is active or admission is unavailable") - return - } + done, ok := s.acquireRootTopology(w, r, gameID) + if !ok { return } defer done() // Rejections here are the overlap and naming rules, which are the user's // to hear about rather than a server fault: "that folder is already @@ -102,11 +102,8 @@ func (s *Server) handleRemoveGameRoot(w http.ResponseWriter, r *http.Request) { } // Refuse while any transfer still holds a captured path for this game // (issue #60). The mapping must stay until the last write finishes. - done, ok := s.tryRootTopology(gameID) - if !ok { - writeError(w, http.StatusConflict, "cannot remove root while native work is active or admission is unavailable") - return - } + done, ok := s.acquireRootTopology(w, r, gameID) + if !ok { return } defer done() oldRoots, _ := s.Daemon.Store.GameRootPaths(gameID) if err := s.Daemon.Store.RemoveGameRoot(gameID, name); err != nil { @@ -121,11 +118,23 @@ func (s *Server) handleRemoveGameRoot(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, map[string]any{"ok": true}) } -// tryRootTopology acquires native admission for the entire root mutation, -// not merely a snapshot of whether a writer happened to be active. -func (s *Server) tryRootTopology(gameID string) (func(), bool) { +// acquireRootTopology waits for interactive edits only. The 30-second bound +// covers acquiring the reservation; the database/watcher phase may take longer. +func (s *Server) acquireRootTopology(w http.ResponseWriter, r *http.Request, gameID string) (func(), bool) { if s.Daemon == nil || s.Daemon.P2P == nil || s.Daemon.P2P.Sync == nil { + writeError(w, http.StatusServiceUnavailable, "Save-location admission is unavailable.") return nil, false } - return s.Daemon.P2P.Sync.TryTopologyMutation(gameID) + limit := s.topologyWaitFor + if limit <= 0 { limit = 30 * time.Second } + ctx, cancel := context.WithTimeout(r.Context(), limit) + defer cancel() + done, err := s.Daemon.P2P.Sync.WaitTopologyMutation(ctx, gameID, 100*time.Millisecond) + if err == nil { return done, true } + if errors.Is(err, context.DeadlineExceeded) && r.Context().Err() == nil { + writeError(w, http.StatusConflict, "A sync of this game is in progress. Try again in a moment.") + } else if r.Context().Err() == nil { + writeError(w, http.StatusServiceUnavailable, "Save-location admission was cancelled or is unavailable.") + } + return nil, false } diff --git a/internal/api/server.go b/internal/api/server.go index f9d73fe7..b3239324 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -21,7 +21,6 @@ import ( "github.com/go-chi/chi/v5" cloudsvc "github.com/opensave/opensave/internal/cloud" "github.com/opensave/opensave/internal/daemon" - "github.com/opensave/opensave/internal/fsx" "github.com/opensave/opensave/internal/logging" "github.com/opensave/opensave/internal/p2p" "github.com/opensave/opensave/internal/p2p/syncengine" @@ -38,7 +37,7 @@ type Server struct { httpServer *http.Server listener net.Listener - presenceUnlock func() + topologyWaitFor time.Duration // test-injectable admission limit; defaults to 30s // instance marks every response from this process (instanceHeader), so // Start can tell whether 127.0.0.1 at its port is really answered by it. instance string @@ -236,15 +235,6 @@ func (s *Server) Start(port int) (string, error) { if err != nil { return "", fmt.Errorf("listen: %w", err) } - unlock, available, lockErr := fsx.TryLock(filepath.Join(s.Daemon.Paths.HomeDir, "daemon-presence.lock")) - if lockErr != nil || !available { - ln.Close() - if lockErr != nil { - return "", fmt.Errorf("daemon presence lock: %w", lockErr) - } - return "", fmt.Errorf("another OpenSave daemon owns this home") - } - s.presenceUnlock = unlock s.listener = ln s.httpServer = &http.Server{Handler: r} @@ -255,8 +245,6 @@ func (s *Server) Start(port int) (string, error) { }() if err := s.answersLoopback(ln.Addr()); err != nil { _ = s.httpServer.Close() - s.presenceUnlock() - s.presenceUnlock = nil s.httpServer, s.listener = nil, nil s.Daemon.Log.Log("warn", err.Error()) return "", err @@ -386,10 +374,6 @@ func (s *Server) Stop() { defer cancel() _ = s.httpServer.Shutdown(ctx) } - if s.presenceUnlock != nil { - s.presenceUnlock() - s.presenceUnlock = nil - } } // corsLocalhost adds permissive CORS headers for local requests and diff --git a/internal/api/server_presence_test.go b/internal/api/server_presence_test.go deleted file mode 100644 index 79401499..00000000 --- a/internal/api/server_presence_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package api - -import ( - "path/filepath" - "testing" - - "github.com/opensave/opensave/internal/fsx" -) - -// The address file is advisory, but the running HTTP daemon must hold a -// stable cross-process owner lock even when its address file is absent. -func TestServerPresenceLockLivesUntilShutdown(t *testing.T) { - ts := startTestServer(t) - path := filepath.Join(ts.daemon.Paths.HomeDir, "daemon-presence.lock") - unlock, ok, err := fsx.TryLock(path) - if err != nil { - t.Fatal(err) - } - if ok { - unlock() - t.Fatal("running native HTTP server did not hold the presence lock") - } - ts.server.Stop() - unlock, ok, err = fsx.TryLock(path) - if err != nil { - t.Fatal(err) - } - if !ok { - t.Fatal("daemon-presence lock remained held after server shutdown") - } - unlock() -} diff --git a/internal/cliapp/client.go b/internal/cliapp/client.go index 6ed58921..70193426 100644 --- a/internal/cliapp/client.go +++ b/internal/cliapp/client.go @@ -14,7 +14,6 @@ import ( "time" "github.com/opensave/opensave/internal/config" - "github.com/opensave/opensave/internal/fsx" ) // Commands that touch peers, pairing or syncing need the *running* daemon: @@ -47,55 +46,6 @@ func daemonBaseURL() (string, error) { return "http://127.0.0.1:8383", nil } -// daemonPublished reports whether a daemon has published its address. -// An absent file is not proof that no daemon exists. A published daemon that does not answer -// is fail-closed: the CLI must not write the database beside a live pull. -func daemonPublished() bool { - paths, err := config.Resolve() - if err != nil { - return false - } - _, err = os.Stat(filepath.Join(paths.HomeDir, "daemon.addr")) - return err == nil -} - -func useRunningDaemon() (bool, error) { - if !daemonPublished() { - // Older daemons and interrupted startup may listen without an - // address file. Do not silently write SQLite beside such a daemon. - probe, err := net.DialTimeout("tcp", "127.0.0.1:8383", 200*time.Millisecond) - if err != nil { - return false, nil - } - probe.Close() - } - if _, err := daemonRequest("GET", "/api/games", nil); err != nil { - // An advertised or listening daemon that cannot answer is NOT - // evidence that offline direct Store mutation is safe. - return false, err - } - return true, nil -} - -// withOfflineRootAdmission prevents a CLI process from rewriting root mappings -// beside a live daemon when its daemon.addr file is missing or stale. Hold the -// lease through the Store write so server startup cannot interleave. -func withOfflineRootAdmission(write func() error) error { - paths, err := config.Resolve() - if err != nil { - return err - } - unlock, ok, err := fsx.TryLock(filepath.Join(paths.HomeDir, "daemon-presence.lock")) - if err != nil { - return fmt.Errorf("check daemon ownership: %w", err) - } - if !ok { - return fmt.Errorf("cannot change root: a running OpenSave daemon owns this home; restore daemon.addr or use its API") - } - defer unlock() - return write() -} - // daemonRequest calls the running daemon. The error text is deliberately // actionable: "connection refused" on its own sends people hunting. func daemonRequest(method, path string, body any) ([]byte, error) { diff --git a/internal/cliapp/commands_games.go b/internal/cliapp/commands_games.go index 9c26dce6..95be9ed1 100644 --- a/internal/cliapp/commands_games.go +++ b/internal/cliapp/commands_games.go @@ -3,7 +3,6 @@ package cliapp import ( "encoding/json" "fmt" - "net/url" "os" "path/filepath" "strconv" @@ -75,30 +74,12 @@ func cmdGame(d *daemon.Daemon, args []string) int { return fail(asJSON, err) } game.SavePath = abs - if running, runErr := useRunningDaemon(); runErr != nil { - return fail(asJSON, runErr) - } else if running { - if _, err := daemonRequest("PATCH", "/api/games/"+url.PathEscape(gameID), game); err != nil { - return fail(asJSON, err) - } - if asJSON { - return emitJSON(map[string]any{"game": game.ID, "set": key, "value": value}) - } - success("%s %s = %s", bold(game.Name), faint(key), accent(value)) - return 0 - } default: return fail(asJSON, fmt.Errorf("unknown setting %q\n\n%s", key, gameUsage)) } - var updateErr error - if key == "path" { - updateErr = withOfflineRootAdmission(func() error { return d.Store.UpdateGame(game) }) - } else { - updateErr = d.Store.UpdateGame(game) - } - if updateErr != nil { - return fail(asJSON, updateErr) + if err := d.Store.UpdateGame(game); err != nil { + return fail(asJSON, err) } // Re-watch so a path or auto-sync change takes effect immediately rather // than at the next restart. diff --git a/internal/cliapp/commands_locations.go b/internal/cliapp/commands_locations.go index 1735ac4a..671cae3a 100644 --- a/internal/cliapp/commands_locations.go +++ b/internal/cliapp/commands_locations.go @@ -2,7 +2,6 @@ package cliapp import ( "fmt" - "net/url" "os" "github.com/opensave/opensave/internal/daemon" @@ -31,15 +30,7 @@ func cmdLocations(d *daemon.Daemon, args []string) int { return fail(asJSON, err) } oldRoots, _ := d.Store.GameRootPaths(gameID) - if running, err := useRunningDaemon(); err != nil { - return fail(asJSON, err) - } else if running { - if _, err := daemonRequest("POST", "/api/games/"+url.PathEscape(gameID)+"/roots", map[string]string{"name": name, "path": abs}); err != nil { - return fail(asJSON, err) - } - } else if err := withOfflineRootAdmission(func() error { - return d.Store.AddGameRoot(gameID, name, abs) - }); err != nil { + if err := d.Store.AddGameRoot(gameID, name, abs); err != nil { return fail(asJSON, err) } d.LocationsChanged(gameID, oldRoots) @@ -56,15 +47,7 @@ func cmdLocations(d *daemon.Daemon, args []string) int { return 1 } oldRoots, _ := d.Store.GameRootPaths(args[1]) - if running, err := useRunningDaemon(); err != nil { - return fail(asJSON, err) - } else if running { - if _, err := daemonRequest("DELETE", "/api/games/"+url.PathEscape(args[1])+"/roots/"+url.PathEscape(args[2]), nil); err != nil { - return fail(asJSON, err) - } - } else if err := withOfflineRootAdmission(func() error { - return d.Store.RemoveGameRoot(args[1], args[2]) - }); err != nil { + if err := d.Store.RemoveGameRoot(args[1], args[2]); err != nil { return fail(asJSON, err) } d.LocationsChanged(args[1], oldRoots) diff --git a/internal/p2p/syncengine/settle.go b/internal/p2p/syncengine/settle.go index b3add8dc..20b32ff4 100644 --- a/internal/p2p/syncengine/settle.go +++ b/internal/p2p/syncengine/settle.go @@ -318,6 +318,28 @@ func (e *Engine) TryTopologyMutation(gameID string) (done func(), ok bool) { }, true } +// WaitTopologyMutation waits for per-game admission, without holding either +// sync mutex while polling. The caller supplies a single overall deadline. +func (e *Engine) WaitTopologyMutation(ctx context.Context, gameID string, interval time.Duration) (release func(), err error) { + if interval <= 0 { interval = 100 * time.Millisecond } + for { + if err := ctx.Err(); err != nil { return nil, err } + done, ok := e.TryTopologyMutation(gameID) + if ok { + if err := ctx.Err(); err != nil { done(); return nil, err } + return done, nil + } + timer := time.NewTimer(interval) + select { + case <-ctx.Done(): + // Private timer: do not drain an already-consumed channel. + timer.Stop() + return nil, ctx.Err() + case <-timer.C: + } + } +} + // IsWriting reports whether any native writer currently holds this game's // save locations. Root topology mutations (AddRoot / RemoveRoot) must refuse // while true so an in-flight pull cannot write into a path that has already