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.
277 lines
9.0 KiB
Go
277 lines
9.0 KiB
Go
package update
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
)
|
|
|
|
// The Windows app is one exe with no installer, and the site's download
|
|
// lands it in Downloads as magpie-windows-amd64.exe. Left there, the Start
|
|
// menu shortcut, Open at login and the `magpie` command all name a file
|
|
// the user tidies away, and the command came from a magpie.com written
|
|
// beside it in Downloads (lucky_eagle_38410 on Discord). So the desktop
|
|
// app, opened from a folder downloads land in, installs itself the way a
|
|
// Mac app is moved into Applications: it copies itself to
|
|
// %LOCALAPPDATA%\Programs\magpie\magpie.exe — the user's own programs
|
|
// folder, no administrator — puts that folder on the user's PATH, and
|
|
// opens the copy there, which makes the shortcut, Open at login and its
|
|
// updates name it. The download is left where it is: it is the user's
|
|
// file. Opened again, it opens the installed copy, or puts itself there
|
|
// first when it is the newer version.
|
|
//
|
|
// 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.
|
|
|
|
// NoInstallEnv, set to 1, keeps a magpie run from Downloads there.
|
|
const NoInstallEnv = "MAGPIE_NO_INSTALL"
|
|
|
|
// installedFromEnv names, for the installed copy started in its place,
|
|
// the exe it was installed from, whose leftovers it clears (Settled).
|
|
const installedFromEnv = "MAGPIE_INSTALLED_FROM"
|
|
|
|
// What Settle asks of the system; tests set them.
|
|
var (
|
|
// installTarget is %LOCALAPPDATA%\Programs\magpie\magpie.exe, "" off
|
|
// Windows
|
|
installTarget = installPath
|
|
// downloadFolder says whether dir is one downloads land in:
|
|
// Downloads, the Desktop or a temporary folder (an exe opened from
|
|
// inside a zip); not a folder inside them, the home folder or
|
|
// Documents
|
|
downloadFolder = isDownloadFolder
|
|
// fileVersion is the version in an exe's resources, 0 when unknown
|
|
fileVersion = exeVersion
|
|
// portableData is a portable magpie's data folder, "" installed
|
|
portableData = appdir.Portable
|
|
// webViewHome is the folder WebView2 keeps an installed magpie's
|
|
// profile in by default, %APPDATA%\<exe name>; "" off Windows
|
|
webViewHome = webViewDefault
|
|
// installCLI puts the installed folder on the user's PATH with the
|
|
// command a terminal runs there
|
|
installCLI = cliPlace
|
|
)
|
|
|
|
// Settle installs the desktop app run from a download folder (see
|
|
// above), and says what to open instead of running on here: from is this
|
|
// exe and to the installed one, both "" when this one runs on. Anything
|
|
// that goes wrong leaves this one running, as before.
|
|
func Settle() (from, to string) {
|
|
if os.Getenv(NoInstallEnv) == "1" {
|
|
return "", ""
|
|
}
|
|
exe, err := Executable()
|
|
if err != nil {
|
|
return "", ""
|
|
}
|
|
to, err = settle(exe)
|
|
if err != nil {
|
|
log.Println("install:", err)
|
|
return "", ""
|
|
}
|
|
if to == "" {
|
|
return "", ""
|
|
}
|
|
return exe, to
|
|
}
|
|
|
|
// settle is Settle for exe: the installed exe to open instead, "" to run
|
|
// exe where it is.
|
|
func settle(exe string) (string, error) {
|
|
to := installTarget()
|
|
if to == "" || samePath(exe, to) || !downloadFolder(filepath.Dir(exe)) || portableData() != "" {
|
|
return "", nil
|
|
}
|
|
if !exists(to) {
|
|
if err := installFresh(exe, to); err != nil {
|
|
return "", err
|
|
}
|
|
return to, nil
|
|
}
|
|
if sameFile(exe, to) || sameBytes(exe, to) {
|
|
return to, nil
|
|
}
|
|
mine, theirs := fileVersion(exe), fileVersion(to)
|
|
if mine == 0 || theirs == 0 || mine <= theirs {
|
|
// the installed copy is as new or newer, or it can't be told:
|
|
// that one is kept, and updates itself
|
|
return to, nil
|
|
}
|
|
// a newer version, downloaded again: it goes in place of the
|
|
// installed one, as an update would
|
|
staged, err := copyBeside(exe, to)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if _, err := InstallBinaryAside(staged, to); err != nil {
|
|
os.Remove(staged)
|
|
return "", fmt.Errorf("putting %s in place of %s: %w", filepath.Base(exe), to, err)
|
|
}
|
|
return to, nil
|
|
}
|
|
|
|
// installFresh puts a copy of exe at to, puts its folder on the user's
|
|
// PATH with the `magpie` command a terminal runs there (cliPlace), and
|
|
// points the installed copy at the webview profile exe had, which is kept
|
|
// by its file name.
|
|
func installFresh(exe, to string) error {
|
|
dir := filepath.Dir(to)
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
return err
|
|
}
|
|
staged, err := copyBeside(exe, to)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := moveIn(staged, to); err != nil {
|
|
os.Remove(staged)
|
|
return fmt.Errorf("installing to %s: %w", to, err)
|
|
}
|
|
keepWebView(exe, to)
|
|
if err := installCLI(to, dir); err != nil {
|
|
// the app runs the same without the command; Settings' Command
|
|
// line says it isn't there and offers it again
|
|
log.Println("install: the magpie command:", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// keepWebView has the copy at to keep the webview profile exe had: the
|
|
// pages' storage there would be lost to a new profile under magpie.exe's
|
|
// name. Nothing when that one has a profile already.
|
|
func keepWebView(exe, to string) {
|
|
home := webViewHome
|
|
old, now := home(exe), home(to)
|
|
if old == "" || now == "" || samePath(old, now) || exists(now) || !exists(old) {
|
|
return
|
|
}
|
|
if err := os.WriteFile(filepath.Join(filepath.Dir(to), appdir.WebViewNote), []byte(old+"\n"), 0o644); err != nil {
|
|
log.Println("install: the webview profile:", err)
|
|
}
|
|
}
|
|
|
|
// copyBeside copies exe into a new file in to's folder, ready to be moved
|
|
// in at to; the file is its path.
|
|
func copyBeside(exe, to string) (string, error) {
|
|
src, err := os.Open(exe)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer src.Close()
|
|
f, err := os.CreateTemp(filepath.Dir(to), ".magpie-install-*")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
_, err = io.Copy(f, src)
|
|
if cerr := f.Close(); err == nil {
|
|
err = cerr
|
|
}
|
|
if err != nil {
|
|
os.Remove(f.Name())
|
|
return "", fmt.Errorf("copying %s to %s: %w", filepath.Base(exe), filepath.Dir(to), err)
|
|
}
|
|
return f.Name(), nil
|
|
}
|
|
|
|
// Forward opens the installed magpie at to with this one's arguments, and
|
|
// waits for it to say it is up; the caller then quits. A restart to
|
|
// update waits on this one for that word (MAGPIE_STARTED): it is passed on
|
|
// once the installed one has said it. When to can't be started, this one
|
|
// runs on.
|
|
func Forward(from, to string) error {
|
|
marker := filepath.Join(os.TempDir(), fmt.Sprintf("magpie-installed-%d", os.Getpid()))
|
|
os.Remove(marker)
|
|
defer os.Remove(marker)
|
|
var env []string
|
|
for _, e := range os.Environ() {
|
|
if !strings.HasPrefix(e, startedEnv+"=") && !strings.HasPrefix(e, installedFromEnv+"=") {
|
|
env = append(env, e)
|
|
}
|
|
}
|
|
env = append(env, startedEnv+"="+marker, installedFromEnv+"="+from)
|
|
exited, kill, err := launch(to, os.Args[1:], env)
|
|
if err == nil {
|
|
err = awaitStarted(marker, exited, kill)
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("opening %s: %w", to, err)
|
|
}
|
|
reportStarted()
|
|
return nil
|
|
}
|
|
|
|
// Settled clears, in the installed magpie Forward opened, what magpie left
|
|
// beside the exe it was installed from: what updates moved aside there,
|
|
// and the magpie.com or magpie.cmd magpie wrote there to run it, which
|
|
// the `magpie` on PATH is now. A file it didn't write, or wrote for
|
|
// another program, is left.
|
|
func Settled() {
|
|
from := os.Getenv(installedFromEnv)
|
|
os.Unsetenv(installedFromEnv)
|
|
if from == "" || !filepath.IsAbs(from) {
|
|
return
|
|
}
|
|
clearBeside(from)
|
|
}
|
|
|
|
func clearBeside(from string) {
|
|
RemoveOld(from)
|
|
dir := filepath.Dir(from)
|
|
if p := filepath.Join(dir, comName); comRuns(p, from) {
|
|
RemoveOld(p)
|
|
if err := os.Remove(p); err != nil && !os.IsNotExist(err) {
|
|
log.Println("install: removing", p+":", err)
|
|
}
|
|
}
|
|
if p := filepath.Join(dir, shimName); shimRuns(p, from) {
|
|
if err := os.Remove(p); err != nil && !os.IsNotExist(err) {
|
|
log.Println("install: removing", p+":", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// forwards says whether exe is a download Settle opens the installed
|
|
// magpie in place of: nothing is written beside it for a terminal, which
|
|
// runs the installed one.
|
|
func forwards(exe string) bool {
|
|
if runtime.GOOS != "windows" || os.Getenv(NoInstallEnv) == "1" {
|
|
return false
|
|
}
|
|
to := installTarget()
|
|
return to != "" && !samePath(exe, to) && downloadFolder(filepath.Dir(exe)) && portableData() == "" && exists(to)
|
|
}
|
|
|
|
// samePath says whether two paths name the same file, Windows' way: in
|
|
// any case.
|
|
func samePath(a, b string) bool {
|
|
return strings.EqualFold(filepath.Clean(a), filepath.Clean(b))
|
|
}
|
|
|
|
// sameBytes says whether the files at a and b hold the same bytes.
|
|
func sameBytes(a, b string) bool {
|
|
sa, err1 := os.Stat(a)
|
|
sb, err2 := os.Stat(b)
|
|
if err1 != nil || err2 != nil || sa.Size() != sb.Size() {
|
|
return false
|
|
}
|
|
ha, hb := fileHash(a), fileHash(b)
|
|
return ha != "" && ha == hb
|
|
}
|
|
|
|
// inFolder says whether path is dir or lies in it, in any case.
|
|
func inFolder(path, dir string) bool {
|
|
if dir == "" || !filepath.IsAbs(dir) {
|
|
return false
|
|
}
|
|
rel, err := filepath.Rel(strings.ToLower(filepath.Clean(dir)), strings.ToLower(filepath.Clean(path)))
|
|
return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator))
|
|
}
|