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 {