lucky_eagle_38 on Discord: Windows magpie is one exe that lands in
Downloads, the user moves it around, and the `magpie` command came from
a magpie.com ("ms-dos" file) magpie wrote beside it there. They asked
for a fixed install folder, with the command coming from PATH.
Before: wherever the exe ran, the Start menu Magpie.lnk, App Paths,
Open at login (HKCU Run "magpie") and updates named it there. Settings >
Command line put that folder on the user PATH and wrote magpie.com (or
magpie.cmd) beside it, and EnsureCompanion kept writing magpie.com there.
Now (update.Settle, first thing in gui.Run): the desktop app run from
Downloads, the Desktop or a temp folder (an exe opened inside a zip)
copies itself to %LOCALAPPDATA%\Programs\magpie\magpie.exe, per-user and
with no admin. It puts that folder first on the HKCU PATH with its
magpie.com (cliPlace, with WM_SETTINGCHANGE as before) and opens the
copy (Forward waits for its MAGPIE_STARTED, then passes that on, so an
update restart waiting on the download isn't rolled back). The copy's
shortcut.Ensure re-points the Start menu lnk and App Paths. Its updates
replace the installed exe in place. The download quits.
- Run again: the download opens the installed copy. If its RT_VERSION
is newer than the installed one, it first goes in place of that copy
(InstallBinaryAside, as an update does). An older or unversioned
download never replaces it.
- Open at login: the Run value is re-pointed only when it named the
download (autostart.Repoint). Task Manager's StartupApproved off flag
is kept.
- Leftovers beside the download (update.Settled, in the installed copy):
- Removed: what updates moved aside (.old). Also magpie.com and
magpie.cmd, but only when magpie wrote them for that exe (comRuns
and shimRuns check the marker and target).
- Never removed: the download itself and any other file.
- EnsureCompanion writes no magpie.com beside a download that forwards.
- WebView2's profile is keyed by exe name. The old profile
(%APPDATA%\magpie-windows-amd64.exe) is kept through a webview-profile
note beside the install, which appdir.WebView reads. magpie uninstall
removes that noted profile too (only a folder directly in %APPDATA%
with an EBWebView in it).
Not moved:
- an exe in a folder the user chose (D:\Tools, a USB drive, the home
folder, Documents, a subfolder of Downloads);
- a portable copy (a data folder or .portable beside it);
- any copy run with MAGPIE_NO_INSTALL=1;
- CLI runs (only the desktop app installs).
Left for old users: the Downloads entry an earlier Command line put on
the HKCU PATH stays. The user may have added it themselves, and it now
comes after the install folder. Taskbar pins still name the download,
which opens the installed copy.
Run:
- Real run on xiaomi-gpu (Windows 11) as a throwaway local user,
through a scheduled task (the user is deleted now; its profile hive
was still held at cleanup and is removed on the next unload):
- Seeded with the real v0.1.2124 in Downloads, its folder on PATH, its
magpie.com, a Run value with the off flag, the lnk and App Paths on
it, a .old, a user's own magpie.cmd and notes.txt.
- Then ran release-built (make release-windows) v0.1.2000 from
Downloads with `tray`:
- Programs\magpie got magpie.exe 0.1.2000, magpie.com and the note.
- PATH is that folder first.
- Run, lnk and App Paths now name it. The off flag is kept.
- Downloads lost magpie.com and the .old and kept the exe, the
user's magpie.cmd and notes.txt.
- The download exited 0, and the installed copy ran.
- Again: forwarded, nothing changed.
- v0.1.2001 from Downloads: it replaced the installed 0.1.2000.
- The registry PATH's `magpie` is Programs\magpie\magpie.com, and
`magpie version` gives v0.1.2001.
- Ran a copy at ~\Tools\magpie.exe: it ran there and the install was
not touched.
- The installed copy's `magpie update` went to the real v0.1.2126
in place (magpie.exe.old aside).
- win-run (Windows): go test ./internal/update ./internal/autostart
./internal/appdir ./internal/shortcut ok. ./internal/gui has 5 reds:
TestGatewayHeldByAContainer, TestLodyImportHandoff,
TestImportStatusUnreadableIsUnknown, TestAionUiImportRow and
TestUpdateThatWontStart. They fail the same alone on origin/main
4a4a90c23 and are filed in #4115.
- mote: go vet ./..., GOOS=windows go vet ./..., GOOS=linux go vet
-tags nogui ./... and GOOS=windows go build all OK. go test -tags
nogui -timeout 45m ./... gives 80 ok, no FAIL, at 9e8c22dad + this.
After rebasing onto fce294c5a (gui assets, pii, sessions, WorkBuddy
min), the vets and the gui, uninstall, update and sessions packages
ran again on mote: all ok. On Windows, uninstall, update, autostart
and appdir ran ok at that head. TestCoreScriptBudget was red at
6db21d869, the upstream head then, and fce294c5a fixed it.
- Broken by hand on Windows, each one red:
- no forwards check in EnsureCompanion:
TestNoCompanionBesideAForwardingDownload;
- always replace: TestSettleKeepsTheInstalledVersion;
- the shim always removed: TestSettledClearsOnlyMagpiesFiles;
- no download-folder check: TestSettleLeavesAChosenPlace;
- the original marker passed on: TestForwardWaitsForTheInstalledOne;
- no keepWebView: TestSettleKeepsTheWebViewProfile;
- a case-sensitive Run path: TestRepointed;
- uninstall without the noted profile: TestProfilesTakeTheNotedOne.
Not tried:
- the GUI on an interactive desktop: the real run was in a batch logon,
where WebView2 did start;
- a OneDrive-moved Downloads (it is read from the known folder);
- an exe opened from inside a zip;
- taskbar pins.
Not changed: magpie uninstall still leaves the exe, magpie.com and the
PATH entry, now in Programs\magpie. #2663 is open for the uninstall
flow that cleans them.
167 lines
5.8 KiB
Go
167 lines
5.8 KiB
Go
package update
|
||
|
||
import (
|
||
"bytes"
|
||
"fmt"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
)
|
||
|
||
// The Windows app is linked as a GUI program, so a double click opens no
|
||
// console window; but a shell doesn't wait for a GUI program either. In
|
||
// PowerShell or cmd, `magpie plugin login` gave the prompt back at once,
|
||
// printed after it, and the shell and magpie both read the keys typed:
|
||
// a hidden password never reached magpie (TomXPRIME, #3737). A shell looks
|
||
// for `magpie` by PATHEXT, which lists .COM before .EXE, so magpie writes
|
||
// magpie.com beside the app: a small console program (./launcher) that
|
||
// the shell waits for, which runs the app with its console and exits with
|
||
// the app's code. Double clicks and the Start menu still open magpie.exe.
|
||
//
|
||
// It is written wherever a terminal runs magpie from: when the app's
|
||
// folder is on PATH, where Settings › Command line puts it, or a magpie.com
|
||
// or magpie.cmd is there already. The app writes it again as it starts
|
||
// when its bytes differ, so an update brings it along. They differ only
|
||
// when the launcher does: it is built without the release's commit in it
|
||
// (the Makefile's -buildvcs=false), or every update wrote a new magpie.com
|
||
// (lucky_eagle_38410 on Discord, TestLauncherSameAtEveryCommit).
|
||
|
||
// ComEnv is set by magpie.com for the app it runs: the standard handles it
|
||
// hands over are the terminal's console or a redirection, and its console
|
||
// is the one to borrow (cli's console_windows.go).
|
||
const ComEnv = "MAGPIE_COM"
|
||
|
||
// comName is the companion's file name; it runs as `magpie`.
|
||
const comName = "magpie.com"
|
||
|
||
// comMagic ends magpie.com; the app it runs is named just before it,
|
||
// after a NUL (launcher's targetName).
|
||
const comMagic = "\x00magpie-com-target"
|
||
|
||
// comLauncher is the launcher program, built for this system and put in
|
||
// the Windows app by `make release-windows` (com_embed_windows_*.go); nil
|
||
// in any other build, which writes no magpie.com.
|
||
var comLauncher []byte
|
||
|
||
// comText is the magpie.com that runs the program exe in its folder.
|
||
func comText(launcher []byte, exe string) []byte {
|
||
name := filepath.Base(strings.ReplaceAll(exe, `\`, "/"))
|
||
out := make([]byte, 0, len(launcher)+1+len(name)+len(comMagic))
|
||
out = append(out, launcher...)
|
||
out = append(out, 0)
|
||
out = append(out, name...)
|
||
return append(out, comMagic...)
|
||
}
|
||
|
||
// comTarget is the program name in a magpie.com's last bytes, "" when it
|
||
// has none.
|
||
func comTarget(b []byte) string {
|
||
if !bytes.HasSuffix(b, []byte(comMagic)) {
|
||
return ""
|
||
}
|
||
b = b[:len(b)-len(comMagic)]
|
||
return string(b[bytes.LastIndexByte(b, 0)+1:])
|
||
}
|
||
|
||
// comRuns says whether cmd is a magpie.com beside exe that runs it.
|
||
func comRuns(cmd, exe string) bool {
|
||
if !strings.EqualFold(filepath.Base(cmd), comName) || !strings.EqualFold(filepath.Dir(cmd), filepath.Dir(exe)) {
|
||
return false
|
||
}
|
||
f, err := os.Open(cmd)
|
||
if err != nil {
|
||
return false
|
||
}
|
||
defer f.Close()
|
||
st, err := f.Stat()
|
||
if err != nil || st.Size() < int64(len(comMagic)) {
|
||
return false
|
||
}
|
||
n := min(st.Size(), 1024)
|
||
b := make([]byte, n)
|
||
if _, err := f.ReadAt(b, st.Size()-n); err != nil {
|
||
return false
|
||
}
|
||
return strings.EqualFold(comTarget(b), filepath.Base(exe))
|
||
}
|
||
|
||
// comOwner says whether exe is the program a magpie.com in its folder
|
||
// should run: a magpie.exe there is what `magpie` ran before, so another
|
||
// copy beside it (magpie-windows-amd64 (1).exe) leaves it be.
|
||
func comOwner(exe string) bool {
|
||
if strings.EqualFold(filepath.Base(exe), "magpie.exe") {
|
||
return true
|
||
}
|
||
st, err := os.Stat(filepath.Join(filepath.Dir(exe), "magpie.exe"))
|
||
return err != nil || st.IsDir()
|
||
}
|
||
|
||
// EnsureCompanion writes the magpie.com that runs exe beside it when a
|
||
// terminal runs magpie from that folder (on PATH, or a magpie.com or
|
||
// magpie.cmd there), or brings one there up to this version. Nothing in
|
||
// a build without the launcher, or when it is as it should be already.
|
||
func EnsureCompanion(exe string) error {
|
||
// a download that opens the installed magpie (Settle) leaves the
|
||
// command to that one, in its folder on PATH
|
||
if comLauncher == nil || !comOwner(exe) || forwards(exe) {
|
||
return nil
|
||
}
|
||
dir := filepath.Dir(exe)
|
||
if !onPath(dir, os.Getenv("PATH")) && !exists(filepath.Join(dir, comName)) && !exists(filepath.Join(dir, shimName)) {
|
||
return nil
|
||
}
|
||
_, err := writeCom(comLauncher, exe)
|
||
return err
|
||
}
|
||
|
||
// writeCom writes the magpie.com that runs exe beside it, unless it is
|
||
// there already, and is its path. A magpie.com running now (a `magpie
|
||
// serve` in a terminal) can't be written over, but can be moved aside,
|
||
// as an update moves the running app; the next run clears it.
|
||
func writeCom(launcher []byte, exe string) (string, error) {
|
||
p := filepath.Join(filepath.Dir(exe), comName)
|
||
want := comText(launcher, exe)
|
||
// the one moved aside last time, running then, is let go of now
|
||
RemoveOld(p)
|
||
if b, err := os.ReadFile(p); err == nil && bytes.Equal(b, want) {
|
||
return p, nil
|
||
}
|
||
f, err := os.CreateTemp(filepath.Dir(p), ".magpie-com-*")
|
||
if err != nil {
|
||
return "", fmt.Errorf("writing %s, which runs %s as magpie: %w", p, filepath.Base(exe), err)
|
||
}
|
||
tmp := f.Name()
|
||
_, err = f.Write(want)
|
||
if cerr := f.Close(); err == nil {
|
||
err = cerr
|
||
}
|
||
if err == nil {
|
||
if err = os.Rename(tmp, p); err != nil && exists(p) {
|
||
if aerr := os.Rename(p, oldName(p)); aerr == nil {
|
||
err = os.Rename(tmp, p)
|
||
}
|
||
}
|
||
}
|
||
if err != nil {
|
||
os.Remove(tmp)
|
||
return "", fmt.Errorf("writing %s, which runs %s as magpie: %w", p, filepath.Base(exe), err)
|
||
}
|
||
return p, nil
|
||
}
|
||
|
||
// onPath is whether dir is one of path's (Windows', ;-separated) folders.
|
||
func onPath(dir, path string) bool {
|
||
dir = strings.TrimRight(dir, `\/`)
|
||
for _, d := range strings.Split(path, string(os.PathListSeparator)) {
|
||
if d = strings.TrimRight(strings.TrimSpace(d), `\/`); d != "" && strings.EqualFold(d, dir) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
func exists(p string) bool {
|
||
_, err := os.Stat(p)
|
||
return err == nil
|
||
}
|