Files
magpie/internal/update/install_windows.go
T
yetone 14807bcf20 update: a Windows download kept outside Downloads asks to install too
@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.
2026-11-14 18:20:53 +08:00

173 lines
4.9 KiB
Go

package update
import (
"os"
"path/filepath"
ptr "unsafe"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
"github.com/yetone/magpie/internal/appdir"
)
// installPath is %LOCALAPPDATA%\Programs\magpie\magpie.exe: the user's own
// programs folder (FOLDERID_UserProgramFiles), where per-user installers
// put their apps, which needs no administrator.
func installPath() string {
local := appdir.Getenv("LOCALAPPDATA")
if local == "" {
if p, err := windows.KnownFolderPath(windows.FOLDERID_UserProgramFiles, 0); err == nil && p != "" {
return filepath.Join(p, "magpie", "magpie.exe")
}
return ""
}
return filepath.Join(local, "Programs", "magpie", "magpie.exe")
}
// isDownloadFolder: Downloads and Desktop, where they are kept (the
// known folders, which OneDrive or the user may have moved) and under the
// home folder, and any temporary folder, where Explorer opens an exe from
// inside a zip. A folder of the user's own, even one inside these, isn't.
func isDownloadFolder(dir string) bool {
var dirs []string
if h, err := appdir.Home(); err == nil {
dirs = append(dirs, filepath.Join(h, "Downloads"), filepath.Join(h, "Desktop"))
}
for _, id := range []*windows.KNOWNFOLDERID{windows.FOLDERID_Downloads, windows.FOLDERID_Desktop} {
if p, err := windows.KnownFolderPath(id, 0); err == nil && p != "" {
dirs = append(dirs, p)
}
}
for _, d := range dirs {
if samePath(dir, d) {
return true
}
}
for _, t := range []string{os.TempDir(), appdir.Getenv("TEMP"), appdir.Getenv("TMP")} {
if inFolder(dir, t) {
return true
}
}
return false
}
// exeVersion is the file version in the exe's resources (make
// release-windows writes the release's there), as one number to compare:
// 0 when it has none, as a build of one's own.
func exeVersion(path string) uint64 {
n, err := windows.GetFileVersionInfoSize(path, nil)
if err != nil || n == 0 {
return 0
}
buf := make([]byte, n)
if err := windows.GetFileVersionInfo(path, 0, n, ptr.Pointer(&buf[0])); err != nil {
return 0
}
var fixed *windows.VS_FIXEDFILEINFO
var size uint32
if err := windows.VerQueryValue(ptr.Pointer(&buf[0]), `\`, ptr.Pointer(&fixed), &size); err != nil || fixed == nil || size == 0 {
return 0
}
return uint64(fixed.FileVersionMS)<<32 | uint64(fixed.FileVersionLS)
}
// webViewDefault is where WebView2 keeps the profile of the program at
// exe when magpie names none: %APPDATA%\<exe name> (Wails' default).
func webViewDefault(exe string) string {
a := os.Getenv("AppData")
if a == "" {
return ""
}
return filepath.Join(a, filepath.Base(exe))
}
// hide gives p the hidden attribute, which Explorer leaves out of view.
func hide(p string) {
u, err := windows.UTF16PtrFromString(p)
if err != nil {
return
}
if a, err := windows.GetFileAttributes(u); err == nil {
windows.SetFileAttributes(u, a|windows.FILE_ATTRIBUTE_HIDDEN)
}
}
// installKey holds InstallLocation, the folder the user chose to install
// 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)
if err != nil {
return ""
}
defer k.Close()
v, _, err := k.GetStringValue("InstallLocation")
if err != nil {
return ""
}
return v
}
func writeInstallDir(dir string) error {
k, _, err := registry.CreateKey(registry.CURRENT_USER, installKey, registry.SET_VALUE)
if err != nil {
return err
}
defer k.Close()
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 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 {
return err
}
if n != nil && n.ValueCount == 0 && n.SubKeyCount == 0 {
if err := registry.DeleteKey(registry.CURRENT_USER, installKey); err != nil && err != registry.ErrNotExist {
return err
}
}
return nil
}