From aa1961927c073db4ccb85798f8259a0b2e98777c Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 17:36:38 +0000 Subject: [PATCH 1/6] refactor(store): share history adoption within caller-owned SQL transaction --- internal/store/adopt.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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 } From b0eac70551c310defa1412e7a7cd8bf81639c72f Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 17:36:59 +0000 Subject: [PATCH 2/6] fix(store): commit alias, roots, snapshots and deletion as one transition --- internal/store/link_atomic.go | 91 +++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 internal/store/link_atomic.go 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 +} From 7d074120105bf2c720e81109bc21a012c87ade44 Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 17:37:15 +0000 Subject: [PATCH 3/6] fix(native): reserve both linking games and abort on atomic transition failure --- internal/daemon/daemon.go | 91 ++++++++++----------------------------- 1 file changed, 23 insertions(+), 68 deletions(-) diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index e39efa84..9b17c7ae 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "strconv" + "sort" "strings" "sync" "time" @@ -1232,80 +1233,34 @@ func (d *Daemon) UntrackGame(gameID string) error { // matching — for titles tracked under different names on two PCs. If aliasID // 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. -func (d *Daemon) LinkGames(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) - } - 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)) - } - } - } - - // 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)) - 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()) - } - +// disk is left untouched, exactly like a normal ufunc (d *Daemon) LinkGames(canonicalID, aliasID string) error { + if canonicalID == "" || aliasID == "" || canonicalID == aliasID { return fmt.Errorf("invalid game link") } + if d.P2P == nil || d.P2P.Sync == nil { return fmt.Errorf("native root admission unavailable") } + // Acquire admission for BOTH affected games in a stable order. + ids := []string{canonicalID, aliasID} + sort.Strings(ids) + releases := make([]func(), 0, 2) + defer func() { for i := len(releases)-1; i >= 0; i-- { releases[i]() } }() + for _, id := range ids { + release, ok := d.P2P.Sync.TryTopologyMutation(id) + if !ok { return fmt.Errorf("cannot link games while native sync is active for %s", id) } + 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 } + nil +} + // UnlinkGame removes an alias link and, if that alias was a game merged in via // LinkGames, brings it back as its own tracked entry. Its save files on disk // were never touched. Its snapshots stay where linking put them — on their own From acc9c28c60871dea836e8134057060a6a0e6ffe7 Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 17:37:39 +0000 Subject: [PATCH 4/6] test(store): link rollback after second root carry fails --- internal/store/link_atomic_test.go | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 internal/store/link_atomic_test.go 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)} +} From 8ff5896058bab08df9fb03955889c36f3fc8aff9 Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 17:37:44 +0000 Subject: [PATCH 5/6] test(native): busy linking preserves alias and mapped root --- internal/daemon/link_atomic_admission_test.go | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 internal/daemon/link_atomic_admission_test.go diff --git a/internal/daemon/link_atomic_admission_test.go b/internal/daemon/link_atomic_admission_test.go new file mode 100644 index 00000000..cf8087e7 --- /dev/null +++ b/internal/daemon/link_atomic_admission_test.go @@ -0,0 +1,30 @@ +package daemon + +import ( + "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() + if err:=d.LinkGames("canonical","alias");err==nil{t.Fatal("expected busy refusal")} + 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)} +} From 3d59e84a897c32e042d9cf5e92d3bf8f0ad95fbb Mon Sep 17 00:00:00 2001 From: Seagulls Date: Sat, 31 Oct 2026 19:25:36 +0000 Subject: [PATCH 6/6] fix(native): repair malformed LinkGames declaration and stray return --- internal/daemon/daemon.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 9b17c7ae..67ca3e19 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -1233,21 +1233,34 @@ func (d *Daemon) UntrackGame(gameID string) error { // matching — for titles tracked under different names on two PCs. If aliasID // 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 ufunc (d *Daemon) LinkGames(canonicalID, aliasID string) error { - if canonicalID == "" || aliasID == "" || canonicalID == aliasID { return fmt.Errorf("invalid game link") } - if d.P2P == nil || d.P2P.Sync == nil { return fmt.Errorf("native root admission unavailable") } - // Acquire admission for BOTH affected games in a stable order. +// disk is left untouched, exactly like a normal untrack. +func (d *Daemon) LinkGames(canonicalID, aliasID string) error { + if canonicalID == "" || aliasID == "" || canonicalID == aliasID { + return fmt.Errorf("invalid game link") + } + 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]() } }() + defer func() { + for i := len(releases) - 1; i >= 0; i-- { + releases[i]() + } + }() for _, id := range ids { release, ok := d.P2P.Sync.TryTopologyMutation(id) - if !ok { return fmt.Errorf("cannot link games while native sync is active for %s", id) } + if !ok { + return fmt.Errorf("cannot link games while native sync is active for %s", id) + } releases = append(releases, release) } branches, merged, err := d.Store.LinkGamesAtomic(canonicalID, aliasID, snapshot.CleanBranchName(aliasID)) - if err != nil { return err } + 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, ", "))) } @@ -1258,9 +1271,6 @@ func (d *Daemon) UntrackGame(gameID string) error { return nil } - nil -} - // UnlinkGame removes an alias link and, if that alias was a game merged in via // LinkGames, brings it back as its own tracked entry. Its save files on disk // were never touched. Its snapshots stay where linking put them — on their own