diff --git a/internal/api/routes.go b/internal/api/routes.go index 84ef1b7e..20c342dc 100644 --- a/internal/api/routes.go +++ b/internal/api/routes.go @@ -605,8 +605,12 @@ func (s *Server) handleLinkGame(w http.ResponseWriter, r *http.Request) { writeError(w, http.StatusBadRequest, "alias is required") return } - if err := s.Daemon.LinkGames(gameID, body.Alias); err != nil { - writeError(w, notFoundToStatus(err), err.Error()) + if err := s.Daemon.LinkGamesContext(r.Context(), gameID, body.Alias); err != nil { + if errors.Is(err, daemon.ErrLinkGamesBusy) { + writeError(w, http.StatusConflict, "A sync of this game is in progress. Try again in a moment.") + } else { + writeError(w, notFoundToStatus(err), err.Error()) + } return } s.BroadcastGamesUpdate() diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index e39efa84..6dd933ed 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "strconv" + "sort" "strings" "sync" "time" @@ -1233,74 +1234,53 @@ func (d *Daemon) UntrackGame(gameID string) error { // is itself a tracked game here, that entry is removed so the alias actually // takes effect (a direct id lookup would otherwise win); its save data on // disk is left untouched, exactly like a normal untrack. +// ErrLinkGamesBusy means sync activity did not drain within the bounded +// admission window. No link/alias/root changes were committed. +var ErrLinkGamesBusy = errors.New("a sync of this game is in progress") + func (d *Daemon) LinkGames(canonicalID, aliasID string) error { + return d.LinkGamesContext(context.Background(), canonicalID, aliasID) +} + +// LinkGamesContext reserves canonical and alias in stable order, then commits +// the entire link atomically. One bounded deadline covers both admissions. +func (d *Daemon) LinkGamesContext(parent context.Context, canonicalID, aliasID string) error { if canonicalID == "" || aliasID == "" || canonicalID == aliasID { - return fmt.Errorf("invalid link %q -> %q", aliasID, canonicalID) - } - if _, err := d.Store.GetGame(canonicalID); err != nil { - return fmt.Errorf("canonical game %q: %w", canonicalID, err) + return fmt.Errorf("invalid game link") } - if err := d.Store.AddGameAlias(aliasID, canonicalID); err != nil { - return err - } - // If the alias id is itself a tracked game, snapshot its identity (so - // unlink can restore it) and remove that entry so the alias takes effect. - if merged, err := d.Store.GetGame(aliasID); err == nil { - _ = d.Store.SetAliasSnapshot(aliasID, merged.Name, merged.SavePath, merged.AppID) - - // Carry the merged entry's extra save locations over to the canonical - // game. Linking says these are the same game, so its folders are that - // game's folders — and deleting the entry below cascades them away, - // which would silently stop covering folders the user had set up - // without saying anything. The files stay either way; what would be - // lost is OpenSave knowing about them. - // - // A name the canonical game already uses is left alone: it has its own - // folder for that name here, and the merged entry's path is no more - // authoritative than the one already chosen. - if mergedRoots, rootsErr := d.Store.ListGameRoots(aliasID); rootsErr == nil { - existing, _ := d.Store.GameRootPaths(canonicalID) - for _, root := range mergedRoots { - if _, taken := existing[root.Name]; taken { - continue - } - var addErr error - if root.Mapped() { - addErr = d.withRootTopology(canonicalID, func() error { - return d.Store.AddGameRoot(canonicalID, root.Name, root.Path) - }) - } else { - addErr = d.Store.NoteGameRoot(canonicalID, root.Name) - } - if addErr != nil { - d.Log.Log("warn", fmt.Sprintf( - "linking %q: could not carry over its %q save location: %v", aliasID, root.Name, addErr)) - } - } + if d.P2P == nil || d.P2P.Sync == nil { + return fmt.Errorf("native root admission unavailable") + } + // Acquiring in deterministic order avoids deadlock with concurrent links. + ids := []string{canonicalID, aliasID} + sort.Strings(ids) + releases := make([]func(), 0, 2) + defer func() { + for i := len(releases) - 1; i >= 0; i-- { + releases[i]() } - - // Its history comes too, onto branches named after it: deleting the - // entry below would otherwise take every snapshot row with it and - // leave the archives on disk where nothing lists, restores or prunes - // them. So would its place in Favourites and collections. - branches, err := d.Store.AdoptHistory(aliasID, canonicalID, snapshot.CleanBranchName(aliasID)) + }() + ctx, cancel := context.WithTimeout(parent, 30*time.Second) + defer cancel() + for _, id := range ids { + release, err := d.P2P.Sync.WaitTopologyMutation(ctx, id, 100*time.Millisecond) if err != nil { - return fmt.Errorf("keep %q's snapshots: %w", aliasID, err) - } - if len(branches) > 0 { - d.Log.Log("info", fmt.Sprintf("linked %q into %q: its snapshots are kept on the branch %s", - merged.Name, canonicalID, strings.Join(branches, ", "))) - } - if err := d.Store.AdoptCollections(aliasID, canonicalID); err != nil { - d.Log.Log("warn", err.Error()) + if errors.Is(err, context.DeadlineExceeded) && parent.Err() == nil { + return fmt.Errorf("%w for %s", ErrLinkGamesBusy, id) + } + return fmt.Errorf("game link admission for %s: %w", id, err) } - + releases = append(releases, release) + } + branches, merged, err := d.Store.LinkGamesAtomic(canonicalID, aliasID, snapshot.CleanBranchName(aliasID)) + if err != nil { + return err + } + if len(branches) > 0 { + d.Log.Log("info", fmt.Sprintf("linked %q into %q: snapshot branches %s", aliasID, canonicalID, strings.Join(branches, ", "))) + } + if merged { d.Watcher.Unwatch(aliasID) - if err := d.Store.DeleteGame(aliasID); err != nil { - return err - } - // The canonical game may have gained locations just now; its watch was - // set up before they existed. d.RewatchGame(canonicalID) } return nil diff --git a/internal/daemon/link_atomic_admission_test.go b/internal/daemon/link_atomic_admission_test.go new file mode 100644 index 00000000..e9dfc69c --- /dev/null +++ b/internal/daemon/link_atomic_admission_test.go @@ -0,0 +1,69 @@ +package daemon + +import ( + "context" + "errors" + "sync" + "time" + "os" + "path/filepath" + "testing" + + "github.com/opensave/opensave/internal/store" +) + +func TestLinkGamesRefusesBusyCanonicalWithoutLosingCoverage(t *testing.T) { + d, err := New(Options{HomeOverride:t.TempDir(),DisableDiscovery:true}) + if err!=nil{t.Fatal(err)} + t.Cleanup(d.Stop) + base:=t.TempDir() + for _,id:=range []string{"canonical","alias"}{ + dir:=filepath.Join(base,id) + if err:=os.Mkdir(dir,0700);err!=nil{t.Fatal(err)} + if err:=d.Store.CreateGame(store.Game{ID:id,Name:id,SavePath:dir});err!=nil{t.Fatal(err)} + } + extra:=filepath.Join(base,"extra") + if err:=os.Mkdir(extra,0700);err!=nil{t.Fatal(err)} + if err:=d.Store.AddGameRoot("alias","extra",extra);err!=nil{t.Fatal(err)} + writer:=d.P2P.Sync.Writing("canonical") + defer writer() + ctx, cancel := context.WithTimeout(context.Background(), 75*time.Millisecond) + defer cancel() + if err := d.LinkGamesContext(ctx, "canonical", "alias"); !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("expected bounded admission refusal, got %v", err) + } + if _,err:=d.Store.GetGame("alias");err!=nil{t.Fatalf("lost alias: %v",err)} + roots,err:=d.Store.GameRootPaths("alias") + if err!=nil||roots["extra"]!=extra{t.Fatalf("lost tracked root %v %v",roots,err)} +} + +// A short native writer should drain inside one user request. The complete +// link must carry mapped roots while keeping their physical bytes untouched. +func TestLinkGamesWaitsForWriterDrain(t *testing.T) { + d, err := New(Options{HomeOverride: t.TempDir(), DisableDiscovery: true}) + if err != nil { t.Fatal(err) } + t.Cleanup(d.Stop) + base := t.TempDir() + for _, id := range []string{"canonical", "alias"} { + path := filepath.Join(base, id) + if err := os.Mkdir(path, 0700); err != nil { t.Fatal(err) } + if err := d.Store.CreateGame(store.Game{ID: id, Name: id, SavePath: path}); err != nil { t.Fatal(err) } + } + extra := filepath.Join(base, "extra") + if err := os.Mkdir(extra, 0700); err != nil { t.Fatal(err) } + if err := os.WriteFile(filepath.Join(extra, "slot.sav"), []byte("ORIGINAL"), 0600); err != nil { t.Fatal(err) } + if err := d.Store.AddGameRoot("alias", "extra", extra); err != nil { t.Fatal(err) } + writer := d.P2P.Sync.Writing("canonical") + var once sync.Once + release := func() { once.Do(writer) } + defer release() + go func() { time.Sleep(100*time.Millisecond); release() }() + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + if err := d.LinkGamesContext(ctx, "canonical", "alias"); err != nil { t.Fatal(err) } + if _, err := d.Store.GetGame("alias"); err == nil { t.Fatal("alias remains independently tracked") } + roots, err := d.Store.GameRootPaths("canonical") + if err != nil || roots["extra"] != extra { t.Fatalf("root not carried: %v %v", roots, err) } + bytes, err := os.ReadFile(filepath.Join(extra, "slot.sav")) + if err != nil || string(bytes) != "ORIGINAL" { t.Fatalf("save changed: %q %v", bytes, err) } +} diff --git a/internal/store/adopt.go b/internal/store/adopt.go index 902ecb10..6bdd023c 100644 --- a/internal/store/adopt.go +++ b/internal/store/adopt.go @@ -3,6 +3,8 @@ package store import ( "fmt" "time" + + "github.com/jmoiron/sqlx" ) // AdoptHistory moves every snapshot of one game to another, onto branches of @@ -21,7 +23,13 @@ func (s *Store) AdoptHistory(fromID, toID, base string) ([]string, error) { return nil, err } defer tx.Rollback() + names, err := s.adoptHistoryTx(tx, fromID, toID, base) + if err != nil { return nil, err } + if err := tx.Commit(); err != nil { return nil, err } + return names, nil +} +func (s *Store) adoptHistoryTx(tx *sqlx.Tx, fromID, toID, base string) ([]string, error) { var from []string if err := tx.Select(&from, `SELECT name FROM branches WHERE game_id = ? ORDER BY name`, fromID); err != nil { return nil, fmt.Errorf("list %s's branches: %w", fromID, err) @@ -64,9 +72,6 @@ func (s *Store) AdoptHistory(fromID, toID, base string) ([]string, error) { } names = append(names, name) } - if err := tx.Commit(); err != nil { - return nil, err - } return names, nil } diff --git a/internal/store/link_atomic.go b/internal/store/link_atomic.go new file mode 100644 index 00000000..69a2ff99 --- /dev/null +++ b/internal/store/link_atomic.go @@ -0,0 +1,91 @@ +package store + +import ( + "database/sql" + "errors" + "fmt" + "time" +) + +// LinkGamesAtomic carries all mapped roots, aliases, snapshots and collection +// coverage in one transaction. No partial link can delete the final tracked +// owner of a root if a later carry or history adoption fails. +func (s *Store) LinkGamesAtomic(canonicalID, aliasID, base string) (names []string, merged bool, err error) { + if aliasID == "" || canonicalID == "" || aliasID == canonicalID { + return nil, false, fmt.Errorf("invalid game link") + } + tx, err := s.db.Beginx() + if err != nil { return nil, false, err } + defer tx.Rollback() + + var canonical, alias Game + if err = tx.Get(&canonical, `SELECT * FROM games WHERE id = ?`, canonicalID); err != nil { + return nil, false, fmt.Errorf("canonical game: %w", err) + } + aliasErr := tx.Get(&alias, `SELECT * FROM games WHERE id = ?`, aliasID) + if aliasErr != nil && !errors.Is(aliasErr, sql.ErrNoRows) { return nil, false, aliasErr } + merged = aliasErr == nil + + var carries []GameRoot + if merged { + var roots, existing []GameRoot + if err = tx.Select(&roots, `SELECT * FROM game_roots WHERE game_id = ? ORDER BY ordinal, name`, aliasID); err != nil { return nil, false, err } + if err = tx.Select(&existing, `SELECT * FROM game_roots WHERE game_id = ? ORDER BY ordinal, name`, canonicalID); err != nil { return nil, false, err } + existingNames := make(map[string]bool) + paths := []string{canonical.SavePath} + for _, root := range existing { + existingNames[root.Name] = true + if root.Mapped() { paths = append(paths, root.Path) } + } + for _, root := range roots { + if existingNames[root.Name] { continue } // canonical wins on same name + if root.Mapped() { + for _, path := range paths { + if overlaps(root.Path, path) { + return nil, false, fmt.Errorf("cannot link: %s overlaps an existing save location", root.Path) + } + } + paths = append(paths, root.Path) + } + carries = append(carries, root) + existingNames[root.Name] = true + } + } + + if _, err = tx.Exec(`INSERT INTO game_aliases (alias_id, game_id, created_at_ms) VALUES (?, ?, ?) + ON CONFLICT(alias_id) DO UPDATE SET game_id = excluded.game_id`, aliasID, canonicalID, time.Now().UnixMilli()); err != nil { + return nil, false, err + } + if merged { + if _, err = tx.Exec(`UPDATE game_aliases SET alias_name = ?, alias_save_path = ?, alias_app_id = ? WHERE alias_id = ?`, + alias.Name, alias.SavePath, alias.AppID, aliasID); err != nil { + return nil, false, err + } + for _, root := range carries { + if _, err = tx.Exec(`INSERT INTO game_roots (game_id, name, path, ordinal) + VALUES (?, ?, ?, COALESCE((SELECT MAX(ordinal) + 1 FROM game_roots WHERE game_id = ?), 0))`, + canonicalID, root.Name, root.Path, canonicalID); err != nil { + return nil, false, fmt.Errorf("carry root %q: %w", root.Name, err) + } + if root.Mapped() { + if _, err = tx.Exec(`INSERT INTO game_root_mapped (game_id, name, mapped_ms) VALUES (?, ?, ?)`, + canonicalID, root.Name, time.Now().UnixMilli()); err != nil { + return nil, false, fmt.Errorf("map root %q: %w", root.Name, err) + } + } + } + if names, err = s.adoptHistoryTx(tx, aliasID, canonicalID, base); err != nil { + return nil, false, err + } + if _, err = tx.Exec(`INSERT OR IGNORE INTO collection_games (collection_id, game_id, added_ms) + SELECT collection_id, ?, ? FROM collection_games WHERE game_id = ?`, + canonicalID, time.Now().UnixMilli(), aliasID); err != nil { + return nil, false, err + } + if _, err = tx.Exec(`DELETE FROM games WHERE id = ?`, aliasID); err != nil { + return nil, false, err + } + } + if err = tx.Commit(); err != nil { return nil, false, err } + return names, merged, nil +} diff --git a/internal/store/link_atomic_test.go b/internal/store/link_atomic_test.go new file mode 100644 index 00000000..fb716a0c --- /dev/null +++ b/internal/store/link_atomic_test.go @@ -0,0 +1,31 @@ +package store + +import "testing" + +func TestLinkGamesAtomicRollsBackAfterEarlierSuccessfulRootCarry(t *testing.T) { + s := openTestStore(t) + for _, id := range []string{"canonical", "alias"} { + if err := s.CreateGame(Game{ID:id, Name:id, SavePath:"/tmp/"+id}); err != nil { t.Fatal(err) } + } + if err:=s.AddGameRoot("alias","first","/tmp/first");err!=nil{t.Fatal(err)} + if err:=s.AddGameRoot("alias","second","/tmp/second");err!=nil{t.Fatal(err)} + if _,err:=s.db.Exec(`CREATE TRIGGER reject_second_root BEFORE INSERT ON game_roots WHEN NEW.game_id = 'canonical' AND NEW.name = 'second' BEGIN SELECT RAISE(ABORT,'inject second root failure'); END`);err!=nil{t.Fatal(err)} + if _,_,err:=s.LinkGamesAtomic("canonical","alias","alias");err==nil{t.Fatal("expected injected failure")} + if _,err:=s.GetGame("alias");err!=nil{t.Fatalf("alias registration deleted on failure: %v",err)} + if _,ok:=s.GetGameAlias("alias");ok{t.Fatal("alias created despite failure")} + aliasRoots,err:=s.GameRootPaths("alias");if err!=nil{t.Fatal(err)} + if aliasRoots["first"]!="/tmp/first"||aliasRoots["second"]!="/tmp/second"{t.Fatalf("source root coverage changed: %v",aliasRoots)} + canonRoots,err:=s.GameRootPaths("canonical");if err!=nil{t.Fatal(err)} + if len(canonRoots)!=0{t.Fatalf("partial canonical roots left behind: %v",canonRoots)} +} + +func TestLinkGamesAtomicSuccessCarriesRootsAndAlias(t *testing.T) { + s:=openTestStore(t) + for _,id:=range []string{"canonical","alias"}{if err:=s.CreateGame(Game{ID:id,Name:id,SavePath:"/tmp/"+id});err!=nil{t.Fatal(err)}} + if err:=s.AddGameRoot("alias","config","/tmp/config");err!=nil{t.Fatal(err)} + _,merged,err:=s.LinkGamesAtomic("canonical","alias","alias");if err!=nil{t.Fatal(err)} + if !merged{t.Fatal("expected merged alias")} + if _,err:=s.GetGame("alias");err==nil{t.Fatal("alias remains tracked")} + roots,err:=s.GameRootPaths("canonical");if err!=nil||roots["config"]!="/tmp/config"{t.Fatalf("missing carried root: %v, %v",roots,err)} + if id,ok:=s.ResolveGameAlias("alias");!ok||id!="canonical"{t.Fatalf("missing alias redirect: %q %v",id,ok)} +}