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
21 changes: 20 additions & 1 deletion internal/daemon/missing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -87,7 +106,7 @@ func relocateCandidates(savePath string) []string {
continue
}
candidate := letter + ":" + rest
if !SaveFolderMissing(candidate) {
if saveFolderThere(candidate) {
found = append(found, candidate)
}
}
Expand Down
50 changes: 50 additions & 0 deletions internal/daemon/relocate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package daemon

import (
"errors"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -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)
}
}
Loading