From 14807bcf20826c6fe16eca4ddeff77e116d4a4d7 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 14 Nov 2026 18:20:03 +0800 Subject: [PATCH] update: a Windows download kept outside Downloads asks to install too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @XBJNTMs2mDbsvPH on X: on Windows magpie ran from wherever the downloaded exe was and updated itself in place there, so deleting the download deleted magpie, and opening copies of different versions left several Magpie entries in the Start menu. Cause: update.Settle installed the exe into %LOCALAPPDATA%\Programs\magpie only when it ran from the known Downloads or Desktop folder or a temp folder. A browser set to save elsewhere (D:\下载), or a download moved into a folder of the user's own, never counted, so that copy stayed the app, updated in place, and each copy opened was an app of its own to Windows (its own path: taskbar, Start's lists, Open at login, Magpie.lnk repointed at whichever ran last). Now an exe still named as the site's download (magpie-windows-amd64.exe, "… (1).exe", arm64; downloadName) counts as a download wherever it is, but only through the existing install dialog, never unasked (no dialog, no install). Installed, it opens the installed copy as a download in Downloads does, and a newer one replaces it. Cancel on such a copy is kept in HKCU\Software\magpie NotInstalled (the exe's path): it runs where it is from then on, is not asked again and never forwards; another copy is still asked. Cancel in Downloads is not recorded, as before. forwards() (no magpie.com beside a forwarding copy) follows the same rule. Uninstall's ForgetInstallDir takes the new value out with InstallLocation. A renamed copy (D:\Tools\magpie.exe) is left alone as before. No new UI: the dialog is the one from 49c9491b7. The shortcut code itself makes only one Magpie.lnk and repoints it; it was not changed. Tests: TestSettleAsksAboutADownloadKeptElsewhere and TestSettleDeclinedElsewhereIsKept (fakes, any OS), TestDeclinedRecord (real HKCU, a key of its own, Windows). TestSettleLeavesAChosenPlace now uses a renamed copy and checks nothing is asked. Run: - win-run (xiaomi-gpu, Windows 11) go test -count=1 ./internal/update/ ./internal/uninstall/ ./internal/shortcut/ ./internal/gui/: update, uninstall, shortcut ok except TestPackagedByDpkg (#4603, untouched); gui red TestGatewayHeldByAContainer and TestLodyImportHandoff (#6068, untouched). - win-run go test -run 'TestSettle|TestDeclinedRecord|TestForward| TestNoCompanion|TestInstall' -v ./internal/update/: ok. - Broken by hand on Windows: the name check off -> both new tests red (settle = "" want the installed; declined ""); the decline record ignored -> TestSettleDeclinedElsewhereIsKept red ("asked again"). - win-run go vet ./internal/update/ ./internal/uninstall/ ./internal/gui/ and wsl-run go vet -tags nogui ./internal/update/ ./internal/uninstall/ (magpie-desktop, covers install_other.go): clean. gofmt -l clean. - win-run go test -count=1 . (TestAllLazyPatternsCompile and the other root guards, the new pattern included): ok. Not run: the full suite and a darwin vet (mote at load 126-142, mica's disk full). Not tried: the real app's dialog on an interactive Windows desktop from a non-Downloads folder (the box's HKCU\Software\magpie and Start menu are shared with other sessions' magpie there), and the reporter's duplicate Start entries were not reproduced; which list showed them is not known. --- docs/subsystems/gui-shell.md | 13 ++- internal/update/install.go | 53 +++++++++- internal/update/install_other.go | 2 + .../update/install_record_windows_test.go | 49 ++++++++++ internal/update/install_test.go | 97 ++++++++++++++++++- internal/update/install_windows.go | 37 ++++++- 6 files changed, 237 insertions(+), 14 deletions(-) create mode 100644 internal/update/install_record_windows_test.go diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index 9a09799d9..ac3e45f2f 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -1144,6 +1144,15 @@ and `update-wait.test.cjs`. There is no installer: the Windows exe opened from Downloads, the Desktop or a temp folder installs itself (`update.Settle`, called from `gui.Run`). +So does one still named as the site's download (`magpie-windows-amd64.exe`, +`… (1).exe`; `downloadName`) anywhere else, as a browser saving to +`D:\下载` or a download moved into a folder and updated in place there +leaves it, but only through the dialog below, never unasked: run there, +deleting the download deleted magpie, and every copy opened was an app of +its own to Windows (@XBJNTMs2mDbsvPH on X). Cancel on such a copy is kept +(`HKCU\Software\magpie` `NotInstalled`, the exe's path): it runs where it +is from then on, is not asked about again and never opens the installed +one in its place. A copy renamed (`D:\Tools\magpie.exe`) is left alone. The first time, with nothing installed, it asks where in Windows' task dialog (`installDialog` in [`installdir_windows.go`](../../internal/gui/installdir_windows.go)): the @@ -1161,7 +1170,9 @@ stop its updates. Cancel runs the exe where it is. The shortcut, App Paths, autostart, the `magpie://` scheme, PATH and self-update all take the running exe's folder, so they follow the choice. Uninstall forgets the record (`update.ForgetInstallDir`) and leaves the program where it -is, as before. See `TestSettleInstallsWhereTheUserChose`, +is, as before. See `TestSettleAsksAboutADownloadKeptElsewhere`, +`TestSettleDeclinedElsewhereIsKept`, `TestDeclinedRecord`, +`TestSettleInstallsWhereTheUserChose`, `TestSettleOffersTheChosenFolderAgain`, `TestInstallableRefuses` in [`install_test.go`](../../internal/update/install_test.go) and `TestInstallWordsEveryLanguage`. diff --git a/internal/update/install.go b/internal/update/install.go index e2b269c3e..974117dcb 100644 --- a/internal/update/install.go +++ b/internal/update/install.go @@ -12,6 +12,7 @@ import ( "strings" "github.com/yetone/magpie/internal/appdir" + "github.com/yetone/magpie/internal/lazyre" ) // The Windows app is one exe with no installer, and the site's download @@ -38,10 +39,22 @@ import ( // administrator, and its login item and PATH are the user's own, so a // copy in Program Files would need an administrator for every update. // +// A copy still named as the site's download (magpie-windows-amd64.exe, +// magpie-windows-amd64 (1).exe) is a download wherever it is kept: a +// browser set to save into D:\下载, or a download moved into a folder of +// the user's own and updated in place there. Run like that, a deleted +// download was a deleted magpie, and each copy opened was an app of its +// own to Windows, with its own Start menu and taskbar entries +// (@XBJNTMs2mDbsvPH on X). So it asks the same question the first time. +// Cancel there is kept (HKCU\Software\magpie, NotInstalled): that copy +// runs where it is from then on and is never asked about again. Outside +// a download folder it is only ever installed through the dialog, which +// the user sees. +// // It is left alone, and runs where it is as before, when it runs from -// anywhere else (a folder the user chose: D:\Tools, a USB drive), when it -// is portable (a data folder or .portable file beside it, appdir), or -// with MAGPIE_NO_INSTALL=1. +// anywhere else under a name of its own (D:\Tools\magpie.exe, a USB +// drive), when it is portable (a data folder or .portable file beside +// it, appdir), or with MAGPIE_NO_INSTALL=1. // NoInstallEnv, set to 1, keeps a magpie run from Downloads there. const NoInstallEnv = "MAGPIE_NO_INSTALL" @@ -75,8 +88,32 @@ var ( // chosen); recordDir keeps it recordedDir = readInstallDir recordDir = writeInstallDir + // declinedExe is the download kept outside a download folder that + // the user would rather not install (Cancel); declineExe keeps it + declinedExe = readDeclined + declineExe = writeDeclined ) +// downloadName is the name the site's Windows download has, with the +// suffix a browser adds to a second one: magpie-windows-amd64.exe, +// magpie-windows-arm64 (1).exe. +var downloadName = lazyre.New(`(?i)^magpie-windows-(amd64|arm64)\b[^\\/]*\.exe$`) + +// isDownload says whether exe is a download Settle installs: one in a +// folder downloads land in, or one kept elsewhere under the download's +// name that the user hasn't declined to install. ask is whether the +// latter can be asked about; it is never installed unasked. +func isDownload(exe string, ask bool) bool { + if downloadFolder(filepath.Dir(exe)) { + return true + } + if !ask || !downloadName.MatchString(filepath.Base(exe)) { + return false + } + d := declinedExe() + return d == "" || !samePath(exe, d) +} + // AskFolder, when set, asks the user where to install the download, // offering suggest, a folder: it answers the folder chosen, which check // has accepted, or "" when the user would rather not install (the @@ -218,13 +255,19 @@ func Settle() (from, to string) { // exe where it is. func settle(exe string) (string, error) { to, ok := installed() - if to == "" || samePath(exe, to) || !downloadFolder(filepath.Dir(exe)) || portableData() != "" { + if to == "" || samePath(exe, to) || !isDownload(exe, ok || AskFolder != nil) || portableData() != "" { return "", nil } if !ok { dir := filepath.Dir(to) if AskFolder != nil { if dir = AskFolder(dir, Installable); dir == "" { + if !downloadFolder(filepath.Dir(exe)) { + // kept where the user put it: not asked again + if err := declineExe(exe); err != nil { + log.Println("install: keeping the answer:", err) + } + } return "", nil } } @@ -403,7 +446,7 @@ func forwards(exe string) bool { return false } to, ok := installed() - return ok && !samePath(exe, to) && downloadFolder(filepath.Dir(exe)) && portableData() == "" + return ok && !samePath(exe, to) && isDownload(exe, true) && portableData() == "" } // samePath says whether two paths name the same file, Windows' way: in diff --git a/internal/update/install_other.go b/internal/update/install_other.go index dfd7374d2..9cb30117b 100644 --- a/internal/update/install_other.go +++ b/internal/update/install_other.go @@ -9,6 +9,8 @@ func exeVersion(string) uint64 { return 0 } func webViewDefault(string) string { return "" } func readInstallDir() string { return "" } func writeInstallDir(string) error { return nil } +func readDeclined() string { return "" } +func writeDeclined(string) error { return nil } // ForgetInstallDir does nothing: only Windows records where it installed. func ForgetInstallDir() error { return nil } diff --git a/internal/update/install_record_windows_test.go b/internal/update/install_record_windows_test.go new file mode 100644 index 000000000..5e7ba03d5 --- /dev/null +++ b/internal/update/install_record_windows_test.go @@ -0,0 +1,49 @@ +package update + +import ( + "fmt" + "os" + "testing" + + "golang.org/x/sys/windows/registry" +) + +// The copy kept uninstalled is read back as written, and uninstalling +// takes it out with the folder chosen, and the key with them. +func TestDeclinedRecord(t *testing.T) { + installKey = fmt.Sprintf(`Software\magpie-test-%d`, os.Getpid()) + t.Cleanup(func() { + registry.DeleteKey(registry.CURRENT_USER, installKey) + installKey = `Software\magpie` + }) + if got := readDeclined(); got != "" { + t.Fatalf("declined %q with no key", got) + } + const kept = `D:\下载\magpie-windows-amd64.exe` + if err := writeDeclined(kept); err != nil { + t.Fatal(err) + } + if err := writeInstallDir(`D:\Apps\magpie`); err != nil { + t.Fatal(err) + } + if got := readDeclined(); got != kept { + t.Fatalf("declined %q, want %q", got, kept) + } + if err := ForgetInstallDir(); err != nil { + t.Fatal(err) + } + if k, err := registry.OpenKey(registry.CURRENT_USER, installKey, registry.QUERY_VALUE); err == nil { + k.Close() + t.Fatal("the key is left after uninstalling") + } + // only the copy kept: still all taken out + if err := writeDeclined(kept); err != nil { + t.Fatal(err) + } + if err := ForgetInstallDir(); err != nil { + t.Fatal(err) + } + if readDeclined() != "" { + t.Fatal("the copy kept is left after uninstalling") + } +} diff --git a/internal/update/install_test.go b/internal/update/install_test.go index 51b5c10d2..535298c8b 100644 --- a/internal/update/install_test.go +++ b/internal/update/install_test.go @@ -18,6 +18,7 @@ type installFixture struct { placed []string // folders installCLI was asked to put on PATH portable string record string // the folder chosen, as HKCU\Software\magpie keeps it + declined string // the copy kept uninstalled, as HKCU\Software\magpie keeps it asked []string // what AskFolder was offered } @@ -39,8 +40,10 @@ func newInstallFixture(t *testing.T) *installFixture { } saved := []any{installTarget, downloadFolder, fileVersion, portableData, webViewHome, installCLI} wasRead, wasWrite, wasAsk := recordedDir, recordDir, AskFolder + wasDeclined, wasDecline := declinedExe, declineExe t.Cleanup(func() { recordedDir, recordDir, AskFolder = wasRead, wasWrite, wasAsk + declinedExe, declineExe = wasDeclined, wasDecline installTarget = saved[0].(func() string) downloadFolder = saved[1].(func(string) bool) fileVersion = saved[2].(func(string) uint64) @@ -66,6 +69,8 @@ func newInstallFixture(t *testing.T) *installFixture { } recordedDir = func() string { return f.record } recordDir = func(dir string) error { f.record = dir; return nil } + declinedExe = func() string { return f.declined } + declineExe = func(exe string) error { f.declined = exe; return nil } AskFolder = nil return f } @@ -134,11 +139,13 @@ func TestSettleInstallsADownload(t *testing.T) { } } -// A copy kept anywhere the user chose, or a portable one, runs where it -// is and installs nothing; so does one run with MAGPIE_NO_INSTALL=1. +// A copy kept anywhere the user chose under a name of its own, or a +// portable one, runs where it is and installs nothing; so does one run +// with MAGPIE_NO_INSTALL=1. func TestSettleLeavesAChosenPlace(t *testing.T) { f := newInstallFixture(t) - tools := filepath.Join(filepath.Dir(f.downloads), "Tools", "magpie-windows-amd64.exe") + f.ask(t, filepath.Dir(f.to)) + tools := filepath.Join(filepath.Dir(f.downloads), "Tools", "magpie.exe") if err := os.MkdirAll(filepath.Dir(tools), 0o755); err != nil { t.Fatal(err) } @@ -156,8 +163,8 @@ func TestSettleLeavesAChosenPlace(t *testing.T) { if to, err := settle(f.to); to != "" || err != nil { t.Fatalf("the installed copy opens %q (%v)", to, err) } - if exists(f.to) { - t.Fatal("installed though it shouldn't have been") + if exists(f.to) || len(f.asked) != 0 { + t.Fatalf("installed or asked (%q) though it shouldn't have been", f.asked) } t.Setenv(NoInstallEnv, "1") if from, to := Settle(); from != "" || to != "" { @@ -572,3 +579,83 @@ func TestInstallableRefuses(t *testing.T) { t.Fatalf("a folder that can't be made: %v", err) } } + +// A copy still named as the download, kept in a folder downloads don't +// land in as far as magpie knows (a browser saving to D:\下载, a download +// moved into D:\Tools and updated in place there), is a download too +// (@XBJNTMs2mDbsvPH on X): it asks, and installed, it opens the installed +// copy, as a download in Downloads does. Without a dialog to ask in it +// is never installed. +func TestSettleAsksAboutADownloadKeptElsewhere(t *testing.T) { + for _, name := range []string{"magpie-windows-amd64.exe", "magpie-windows-arm64 (1).exe", "MAGPIE-WINDOWS-AMD64(2).EXE"} { + t.Run(name, func(t *testing.T) { + f := newInstallFixture(t) + kept := filepath.Join(filepath.Dir(f.downloads), "下载", name) + if err := os.MkdirAll(filepath.Dir(kept), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(kept, []byte("MZ kept"), 0o755); err != nil { + t.Fatal(err) + } + if to, err := settle(kept); to != "" || err != nil || exists(f.to) { + t.Fatalf("installed with no dialog to ask in: %q, %v", to, err) + } + f.ask(t, filepath.Dir(f.to)) + to, err := settle(kept) + if err != nil || to != f.to { + t.Fatalf("settle = %q, %v; want the installed %q", to, err, f.to) + } + if len(f.asked) != 1 || readString(t, f.to) != "MZ kept" || readString(t, kept) != "MZ kept" { + t.Fatalf("asked %q; installed %v; the copy kept: %q", f.asked, exists(f.to), readString(t, kept)) + } + // opened again, it opens the installed one with no question, + // and leaves the command to it + if to, err := settle(kept); err != nil || to != f.to || len(f.asked) != 1 { + t.Fatalf("second start: %q, %v, asked %q", to, err, f.asked) + } + if !forwards(kept) { + t.Fatal("the copy kept elsewhere doesn't count as forwarding") + } + }) + } +} + +// Cancel on a copy kept outside a download folder is kept: it runs where +// it is from then on, is never asked about again, and never opens an +// installed magpie in its place. Another copy is asked about still, and +// a download in Downloads as before. +func TestSettleDeclinedElsewhereIsKept(t *testing.T) { + f := newInstallFixture(t) + kept := filepath.Join(filepath.Dir(f.downloads), "Tools", "magpie-windows-amd64.exe") + if err := os.MkdirAll(filepath.Dir(kept), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(kept, []byte("MZ kept"), 0o755); err != nil { + t.Fatal(err) + } + f.ask(t, "") + if to, err := settle(kept); to != "" || err != nil { + t.Fatalf("settle = %q, %v after Cancel", to, err) + } + if !samePath(f.declined, kept) || len(f.asked) != 1 { + t.Fatalf("declined %q, asked %q", f.declined, f.asked) + } + if to, err := settle(kept); to != "" || err != nil || len(f.asked) != 1 { + t.Fatalf("asked again: %q, %v, %q", to, err, f.asked) + } + // a Cancel in Downloads isn't kept: that one is asked each time + if to, _ := settle(f.exe); to != "" || len(f.asked) != 2 || !samePath(f.declined, kept) { + t.Fatalf("Downloads: %q, asked %q, declined %q", to, f.asked, f.declined) + } + f.installed(t, "MZ installed", 0) + if to, err := settle(kept); to != "" || err != nil || forwards(kept) { + t.Fatalf("the declined copy opens %q (%v)", to, err) + } + other := filepath.Join(filepath.Dir(kept), "magpie-windows-amd64 (1).exe") + if err := os.WriteFile(other, []byte("MZ other"), 0o755); err != nil { + t.Fatal(err) + } + if to, err := settle(other); to != f.to || err != nil { + t.Fatalf("another copy opens %q (%v), want the installed one", to, err) + } +} diff --git a/internal/update/install_windows.go b/internal/update/install_windows.go index c5e1c34f4..e0bf23ce4 100644 --- a/internal/update/install_windows.go +++ b/internal/update/install_windows.go @@ -94,8 +94,9 @@ func hide(p string) { } // installKey holds InstallLocation, the folder the user chose to install -// magpie in (Settle). Uninstall removes it. -const installKey = `Software\magpie` +// magpie in (Settle). Uninstall removes it. A var so the test writes a +// key of its own. +var installKey = `Software\magpie` func readInstallDir() string { k, err := registry.OpenKey(registry.CURRENT_USER, installKey, registry.QUERY_VALUE) @@ -119,14 +120,44 @@ func writeInstallDir(dir string) error { return k.SetStringValue("InstallLocation", dir) } +// declinedValue holds the download kept outside a download folder that +// the user chose not to install (Settle). +const declinedValue = "NotInstalled" + +func readDeclined() string { + k, err := registry.OpenKey(registry.CURRENT_USER, installKey, registry.QUERY_VALUE) + if err != nil { + return "" + } + defer k.Close() + v, _, err := k.GetStringValue(declinedValue) + if err != nil { + return "" + } + return v +} + +func writeDeclined(exe string) error { + k, _, err := registry.CreateKey(registry.CURRENT_USER, installKey, registry.SET_VALUE) + if err != nil { + return err + } + defer k.Close() + return k.SetStringValue(declinedValue, exe) +} + // ForgetInstallDir takes out the record of the folder magpie was -// installed in, and its key once nothing else is in it. +// installed in, and of a copy kept uninstalled, and its key once nothing +// else is in it. func ForgetInstallDir() error { k, err := registry.OpenKey(registry.CURRENT_USER, installKey, registry.SET_VALUE|registry.QUERY_VALUE) if err != nil { return nil } err = k.DeleteValue("InstallLocation") + if derr := k.DeleteValue(declinedValue); derr != nil && derr != registry.ErrNotExist && (err == nil || err == registry.ErrNotExist) { + err = derr + } n, _ := k.Stat() k.Close() if err != nil && err != registry.ErrNotExist {