Files
magpie/internal/update/launchservices.go
T
yetone eed140c4d9 update: a Mac update no longer leaves a second magpie in Spotlight (#3965)
Stage unpacked the new app to /Applications/.magpie-update/app/magpie.app
and Install moved the old one to .magpie-update/old.app. LaunchServices
registers a folder ending in .app in /Applications within seconds, and
Install only deleted the folder, so Spotlight kept listing the gone path
as another magpie.

- Stage ditto's the zip (one top folder, magpie.app, checked against the
  real v0.1.2119 zip: no FinderInfo bundle bit, no __MACOSX) into
  .magpie-update/unzip and renames magpie.app to .magpie-update/payload
  at once, then has lsregister -u forget the unzip path in case it was
  seen in that moment. codesign verifies a bundle named payload the same
  (checked on the real zip: verify ok, a tampered copy fails).
- Install and InstallAsAdmin move the old app to <stage>/old, and remove
  filepath.Dir(staged), which is still .magpie-update (or the cache's
  update folder on the admin path); renames stay on one volume.
- ForgetOldStaging runs once from the GUI's updater start, in a
  goroutine: lsregister -u on .magpie-update/app/magpie.app and
  .magpie-update/old.app beside the app and in ~/Library/Caches/magpie/
  update. lsregister is /System/Library/Frameworks/CoreServices.framework/
  Frameworks/LaunchServices.framework/Support/lsregister (present on
  macOS 27.2); failures are ignored, a 10s cap, marker in magpie's cache.
  /Volumes/magpie (an old DMG) is left alone.

Run at this head (base ffa07395e):
- mote (macOS 27.2): gofmt -l clean; go build, GOOS=windows go build,
  GOOS=linux go build -tags nogui; go vet, GOOS=windows go vet and
  GOOS=linux go vet -tags nogui on internal/update, internal/gui,
  internal/cli; go test internal/update internal/gui internal/proc
  internal/appdir: build and vet ok;
  update, proc, appdir ok; gui FAIL only in TestMainWindowCorners/
  macOS_26_SDK (the titlebar toolbar on macOS 27), already filed as
  #3949 and not in code this touches
- win-run (xiaomi-gpu, Windows 11): go test ./internal/update ok.
- wsl-run (magpie-desktop): go test ./internal/update ok.
- Real LaunchServices on mote, a fake bundle id (com.example...) in
  /Applications/lstest-3965, not magpie.app: the old flow by hand
  registered .magpie-update/app/magpie.app within 30s and kept it after
  the folder was deleted. A throwaway test running this Stage + Install
  with the real lsregister: after 45s staged and 15s installed,
  lsregister -dump shows only lstest-3965/magpie.app; with stagedName
  set back to magpie.app the same run shows .magpie-update/magpie.app
  registered (red). ForgetOldStaging with the real lsregister removed
  both registered .magpie-update paths after their folders were deleted.
  Everything registered was unregistered and the folder removed.

Broken by hand, each red: asideName "old.app" (TestInstallAsAdminAside-
IsNoApp), stagedName "magpie.app" (TestInstallStagesNoApp, TestStage-
UnpacksNoApp), RemoveAll two folders up as before (TestInstallStagesNoApp:
the Applications folder went with it), no unregister of the unzip path
(TestStageUnpacksNoApp), no marker (TestForgetOldStaging).

Not tried: a real magpie update of the installed /Applications/magpie.app
with Spotlight's UI (the fake bundle stood in), the administrator-password
path for real, the full go test ./... suite.
2026-10-30 09:23:35 +08:00

58 lines
1.9 KiB
Go

package update
import (
"context"
"os"
"path/filepath"
"runtime"
"time"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/proc"
)
// lsregister is the tool that edits LaunchServices' list of apps; -u on a
// path takes that path off the list, whether or not it still exists.
const lsregister = "/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"
// unregister has LaunchServices forget an app at path, as best it can: a
// missing tool or a failure leaves things as they were, and never holds
// an update up for long. Tests stand in for it.
var unregister = func(path string) {
if runtime.GOOS != "darwin" {
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
proc.CommandContext(ctx, lsregister, "-u", path).Run()
}
// forgotMarker, in magpie's cache, records that ForgetOldStaging has run.
const forgotMarker = "launchservices-forgot-staging"
// ForgetOldStaging has LaunchServices forget the apps updates before
// #3965 left on its list: the new version unpacked as
// .magpie-update/app/magpie.app and the old one moved aside to
// .magpie-update/old.app, beside bundle or in magpie's cache. Their
// folders are gone, but Spotlight listed each as another magpie. It runs
// once, and nothing waits on it.
func ForgetOldStaging(bundle string) {
if runtime.GOOS != "darwin" || bundle == "" {
return
}
marker := filepath.Join(appdir.Cache(), forgotMarker)
if _, err := os.Stat(marker); err == nil {
return
}
dirs := []string{filepath.Join(filepath.Dir(bundle), ".magpie-update")}
if cache, err := appdir.SystemCache(); err == nil {
dirs = append(dirs, filepath.Join(cache, "update"))
}
for _, d := range dirs {
unregister(filepath.Join(d, "app", "magpie.app"))
unregister(filepath.Join(d, "old.app"))
}
os.MkdirAll(filepath.Dir(marker), 0o755)
os.WriteFile(marker, nil, 0o644)
}