Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 4 additions & 36 deletions e2e/multiroot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/api/root_topology_admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 43 additions & 0 deletions internal/api/root_topology_wait_test.go
Original file line number Diff line number Diff line change
@@ -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) }
}
14 changes: 9 additions & 5 deletions internal/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
37 changes: 23 additions & 14 deletions internal/api/routes_roots.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package api

import (
"context"
"errors"
"time"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
18 changes: 1 addition & 17 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 0 additions & 32 deletions internal/api/server_presence_test.go

This file was deleted.

50 changes: 0 additions & 50 deletions internal/cliapp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 2 additions & 21 deletions internal/cliapp/commands_games.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cliapp
import (
"encoding/json"
"fmt"
"net/url"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -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.
Expand Down
Loading
Loading