From 24b29c8e6a2c795de19cc50032d93256e53d8bcb Mon Sep 17 00:00:00 2001 From: Omar Date: Thu, 5 Nov 2026 15:53:35 +0530 Subject: [PATCH] A drive that is not ready does not hold a save folder A missing save folder is looked for at the same path on the other drive letters, and exactly one match is offered. A candidate counted when it was not reported missing, but only a plain "not there" reads as missing: a drive with no disk in it, such as an empty card reader, answers "the device is not ready". It counted as holding the folder, made a second match, and nothing was ever offered. On its own it would have been offered as the folder. A candidate now counts only when its folder can be seen to exist. --- internal/daemon/missing.go | 21 +++++++++++++- internal/daemon/relocate_test.go | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/internal/daemon/missing.go b/internal/daemon/missing.go index e9aa0690..ce38d8f1 100644 --- a/internal/daemon/missing.go +++ b/internal/daemon/missing.go @@ -71,6 +71,25 @@ func SaveDriveMissing(savePath string) bool { // folders constantly. const relocateRecheck = time.Hour +// statSaveFolder is os.Stat, replaced by a test that needs a drive that is not +// ready. +var statSaveFolder = os.Stat + +// saveFolderThere reports whether a save path's folder can be seen to exist. +// It is not the opposite of SaveFolderMissing, which says "missing" only of a +// folder plainly not there. A drive with no disk in it, an empty card reader +// or an unreachable share answers with another error: not missing, and not +// there either. Taken as there, an empty card reader made a second match for +// every folder found on another drive, and nothing was ever offered. +func saveFolderThere(savePath string) bool { + folder := savePath + if isFile, err := delta.ResolveLocalSaveFilePath(savePath); err == nil && isFile { + folder = filepath.Dir(savePath) + } + _, err := statSaveFolder(folder) + return err == nil +} + // relocateCandidates lists the folders at savePath's exact path under the other // drive letters that exist here. Windows drive letters only. func relocateCandidates(savePath string) []string { @@ -87,7 +106,7 @@ func relocateCandidates(savePath string) []string { continue } candidate := letter + ":" + rest - if !SaveFolderMissing(candidate) { + if saveFolderThere(candidate) { found = append(found, candidate) } } diff --git a/internal/daemon/relocate_test.go b/internal/daemon/relocate_test.go index 603d2fc5..403f81ec 100644 --- a/internal/daemon/relocate_test.go +++ b/internal/daemon/relocate_test.go @@ -1,6 +1,7 @@ package daemon import ( + "errors" "os" "path/filepath" "runtime" @@ -178,3 +179,52 @@ func TestRelocateToOtherDrive_AnOfferIsStaleOnceTheFolderIsSetByHand(t *testing. t.Error("the stale offer is still offered") } } + +// A drive that is there but not ready, an empty card reader, does not hold the +// folder. It read as holding it: a second match, so nothing was ever offered. +func TestRelocateToOtherDrive_ADriveNotReadyIsNoMatch(t *testing.T) { + if runtime.GOOS != "windows" { + t.Skip("drive letters are a Windows thing") + } + free := freeDriveLetter() + if free == "" { + t.Skip("no free drive letter") + } + real := filepath.Join(t.TempDir(), "SteamLibrary", "save") + if err := os.MkdirAll(real, 0o777); err != nil { + t.Fatal(err) + } + statSaveFolder = func(p string) (os.FileInfo, error) { + if strings.HasPrefix(strings.ToUpper(p), free+":") { + return nil, errors.New("The device is not ready.") + } + return os.Stat(p) + } + t.Cleanup(func() { statSaveFolder = os.Stat }) + + // Looked for from a third letter, as a peer's path arrives. + gone := "?" + for c := 'Y'; c >= 'D'; c-- { + l := string(c) + if l == free || strings.EqualFold(l, real[:1]) { + continue + } + if _, err := os.Stat(l + ":" + string(filepath.Separator)); err != nil { + gone = l + break + } + } + if gone == "?" { + t.Skip("no second free drive letter") + } + found := relocateCandidates(gone + ":" + real[2:]) + var mine []string + for _, f := range found { + if strings.EqualFold(f, real) || strings.EqualFold(f[:1], free) { + mine = append(mine, f) + } + } + if len(mine) != 1 || !strings.EqualFold(mine[0], real) { + t.Errorf("found %v, want only %s", mine, real) + } +}