Skip to content

Commit 76eb7b3

Browse files
authored
fix(device): resolve pairing files under %APPDATA% on Windows (#237)
plumesign resolves its data directory through get_data_path: %APPDATA%\ PlumeImpactor on Windows and $XDG_CONFIG_HOME/PlumeImpactor (falling back to ~/.config/PlumeImpactor) elsewhere. atvloadly hard-coded ~/.config/PlumeImpactor, so on Windows it read a directory plumesign never writes to. The pairing file directory is the first thing CheckDevicePairedContext checks: an empty directory returns early, so a successful mDNS pairing produced no "add device" event and the device never appeared in the paired list. Manual (pairing-file) devices failed the same way through loadManualDevicesFromDir. Resolve SideloadDataDir exactly as plumesign does; RemotePairingDir and the accounts file follow it. Log the searched directory and the find-pairing error at debug level so a future path mismatch is diagnosable instead of silent. Fixes #236
1 parent 126bddd commit 76eb7b3

4 files changed

Lines changed: 100 additions & 11 deletions

File tree

‎internal/app/config.go‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package app
22

33
import (
4-
"fmt"
54
"os"
65
"path/filepath"
76
"runtime"
@@ -51,12 +50,31 @@ type Configuration struct {
5150
} `koanf:"secret" json:"secret"`
5251
}
5352

53+
// SideloadDataDir returns the PlumeImpactor data directory that the plumesign
54+
// engine uses. atvloadly reads the pairing files, accounts and anisette state
55+
// plumesign writes there, so this must resolve to exactly the same directory as
56+
// plumesign's get_data_path: %APPDATA%\PlumeImpactor on Windows, and
57+
// $XDG_CONFIG_HOME/PlumeImpactor (falling back to ~/.config/PlumeImpactor)
58+
// elsewhere. Hard-coding ~/.config broke Windows, where plumesign writes under
59+
// %APPDATA% instead.
5460
func SideloadDataDir() string {
55-
if home, err := os.UserHomeDir(); err != nil {
56-
return "~/.config/PlumeImpactor"
61+
var base string
62+
if runtime.GOOS == "windows" {
63+
base = os.Getenv("APPDATA")
64+
if base == "" {
65+
if home, err := os.UserHomeDir(); err == nil {
66+
base = filepath.Join(home, "AppData", "Roaming")
67+
}
68+
}
5769
} else {
58-
return fmt.Sprintf("%s/.config/PlumeImpactor", home)
70+
base = os.Getenv("XDG_CONFIG_HOME")
71+
if base == "" {
72+
if home, err := os.UserHomeDir(); err == nil {
73+
base = filepath.Join(home, ".config")
74+
}
75+
}
5976
}
77+
return filepath.Join(base, "PlumeImpactor")
6078
}
6179

6280
func LockdownDir() string {

‎internal/app/config_test.go‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package app
2+
3+
import (
4+
"path/filepath"
5+
"runtime"
6+
"testing"
7+
)
8+
9+
// TestSideloadDataDirMatchesPlumesign pins the directory to the one plumesign
10+
// itself resolves (get_data_path): %APPDATA%\PlumeImpactor on Windows and
11+
// $XDG_CONFIG_HOME/PlumeImpactor elsewhere. atvloadly reads the pairing files
12+
// plumesign writes, so a mismatch leaves paired devices undiscovered.
13+
func TestSideloadDataDirMatchesPlumesign(t *testing.T) {
14+
dir := t.TempDir()
15+
t.Setenv("HOME", dir)
16+
17+
if runtime.GOOS == "windows" {
18+
appData := filepath.Join(dir, "AppData", "Roaming")
19+
t.Setenv("APPDATA", appData)
20+
want := filepath.Join(appData, "PlumeImpactor")
21+
if got := SideloadDataDir(); got != want {
22+
t.Fatalf("SideloadDataDir() = %q, want %q", got, want)
23+
}
24+
return
25+
}
26+
27+
xdg := filepath.Join(dir, "xdg")
28+
t.Setenv("XDG_CONFIG_HOME", xdg)
29+
want := filepath.Join(xdg, "PlumeImpactor")
30+
if got := SideloadDataDir(); got != want {
31+
t.Fatalf("SideloadDataDir() = %q, want %q", got, want)
32+
}
33+
}
34+
35+
// TestSideloadDataDirFallsBackToHome covers the unset-variable path: plumesign
36+
// falls back to ~/.config on non-Windows, and to the user's Roaming profile on
37+
// Windows.
38+
func TestSideloadDataDirFallsBackToHome(t *testing.T) {
39+
dir := t.TempDir()
40+
t.Setenv("HOME", dir)
41+
t.Setenv("XDG_CONFIG_HOME", "")
42+
t.Setenv("APPDATA", "")
43+
44+
want := filepath.Join(dir, ".config", "PlumeImpactor")
45+
if runtime.GOOS == "windows" {
46+
want = filepath.Join(dir, "AppData", "Roaming", "PlumeImpactor")
47+
}
48+
if got := SideloadDataDir(); got != want {
49+
t.Fatalf("SideloadDataDir() = %q, want %q", got, want)
50+
}
51+
}

‎internal/manager/device_manager.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,13 @@ func (dm *DeviceManager) CheckDevicePaired(identifier string, authTag string) (*
792792
// context: when ctx is cancelled (e.g. the device disconnects), the
793793
// plumesign subprocess is killed and the goroutine returns promptly.
794794
func (dm *DeviceManager) CheckDevicePairedContext(ctx context.Context, identifier string, authTag string) (*model.RemoteDevice, error) {
795-
if !utils.ExistFiles(app.RemotePairingDir(), "*.plist") {
795+
pairingDir := app.RemotePairingDir()
796+
if !utils.ExistFiles(pairingDir, "*.plist") {
797+
// A remote-pairing device can only be added once plumesign has written
798+
// its pairing file here. Log the directory so a path mismatch (e.g. the
799+
// engine writing under %APPDATA% while atvloadly reads ~/.config) is
800+
// visible instead of silently leaving the device undiscovered.
801+
log.Debugf("no pairing files in %s, skipping pairing check for %s", pairingDir, identifier)
796802
return nil, nil
797803
}
798804

@@ -803,6 +809,7 @@ func (dm *DeviceManager) CheckDevicePairedContext(ctx context.Context, identifie
803809
if ctx.Err() != nil {
804810
return nil, ctx.Err()
805811
}
812+
log.Debugf("find-pairing failed for %s: %v: %s", identifier, err, strings.TrimSpace(string(data)))
806813
return nil, err
807814
}
808815

‎internal/manager/manual_device_test.go‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"os"
77
"path/filepath"
8+
"runtime"
89
"sync"
910
"sync/atomic"
1011
"testing"
@@ -13,6 +14,22 @@ import (
1314
"github.com/bitxeno/atvloadly/internal/model"
1415
)
1516

17+
// setSideloadDataDir points the PlumeImpactor data directory at a temp dir for
18+
// the current OS and returns its pairing_files directory. It mirrors the
19+
// engine's own resolution (%APPDATA% on Windows, $XDG_CONFIG_HOME elsewhere),
20+
// so the tests do not depend on the developer's real config directory.
21+
func setSideloadDataDir(t *testing.T) string {
22+
t.Helper()
23+
dir := t.TempDir()
24+
t.Setenv("HOME", dir)
25+
if runtime.GOOS == "windows" {
26+
t.Setenv("APPDATA", filepath.Join(dir, "AppData", "Roaming"))
27+
return filepath.Join(dir, "AppData", "Roaming", "PlumeImpactor", "pairing_files")
28+
}
29+
t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, ".config"))
30+
return filepath.Join(dir, ".config", "PlumeImpactor", "pairing_files")
31+
}
32+
1633
func writeJSON(t *testing.T, dir, name, content string) {
1734
t.Helper()
1835
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil {
@@ -62,9 +79,7 @@ func TestManualDeviceDedupedByDiscovered(t *testing.T) {
6279
}
6380

6481
func TestDeleteManualDeviceRemovesBothFiles(t *testing.T) {
65-
dir := t.TempDir()
66-
t.Setenv("HOME", dir)
67-
pdir := filepath.Join(dir, ".config", "PlumeImpactor", "pairing_files")
82+
pdir := setSideloadDataDir(t)
6883
if err := os.MkdirAll(pdir, 0o755); err != nil {
6984
t.Fatal(err)
7085
}
@@ -338,9 +353,7 @@ func TestIsManualDeviceAddr(t *testing.T) {
338353
}
339354

340355
func TestWarmManualDevicePortResolves(t *testing.T) {
341-
dir := t.TempDir()
342-
t.Setenv("HOME", dir)
343-
pdir := filepath.Join(dir, ".config", "PlumeImpactor", "pairing_files")
356+
pdir := setSideloadDataDir(t)
344357
if err := os.MkdirAll(pdir, 0o755); err != nil {
345358
t.Fatal(err)
346359
}

0 commit comments

Comments
 (0)
⌂ 换网址