Files
magpie/internal/gui/instance_unix.go
T
yetone e8eb8005f3 gui: one magpie at a time on the Mac, so one started at login twice (its own open at login and a Login Item the user added, or the Mac reopening it after a shutdown it wasn't quit for) ends instead of putting a second bird in the menu bar (Crispin on Discord) — Wails has no single instance on the Mac, and magpie's LaunchAgent runs the binary itself, past the Mac's one-copy-of-an-app, so both starts ran: the second found the gateway served and only watched it, with its own tray icon. A magpie now takes a flock in its config dir, keyed by the config dir and its resolved executable (a sandboxed HOME, and a copy elsewhere such as the owner's magpie-dev, each run on their own); a second one that finds it held ends: a start at login quietly, one for the window or the panel opens the .app, which has the running magpie show its window (the Dock's reopen; at most once in 10 s, so it can't go on), and a magpie:// link is opened, which the Mac hands to the running one. The system lets a flock go when its holder ends however it ends, so a shutdown leaves nothing held behind. Restart to update relaunches only once the old process has gone, so it is unaffected. TestOneMagpieAtATime (a second process holds the lock, this one is refused, the holder is killed, the lock is free) fails on the old code. Two GUI launches on the real Mac weren't tried, since a GUI magpie registers its magpie:// handler with the system. go vet, builds (darwin gui and nogui, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-04 23:43:34 +08:00

52 lines
1.4 KiB
Go

//go:build !windows
package gui
import (
"crypto/sha256"
"encoding/hex"
"errors"
"os"
"path/filepath"
"syscall"
)
// heldInstance is the open lock file of the magpie that holds its
// instance; it stays open, so locked, until this process ends.
var heldInstance *os.File
// instanceLock is where the magpie running exe on the config in dir holds
// its lock. A copy elsewhere (the owner's magpie-dev) has its own.
func instanceLock(dir, exe string) string {
if r, err := filepath.EvalSymlinks(exe); err == nil {
exe = r
}
sum := sha256.Sum256([]byte(dir + "\x00" + exe))
return filepath.Join(dir, "app-"+hex.EncodeToString(sum[:6])+".lock")
}
// holdInstance takes the lock at path for this process, or reports that
// another magpie holds it. It is a flock, which the system lets go when
// its holder ends however it ends, so a Mac shut down with magpie running
// leaves nothing behind that looks held (Crispin on Discord).
func holdInstance(path string) (bool, error) {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return false, err
}
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return false, err
}
err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if errors.Is(err, syscall.EWOULDBLOCK) {
f.Close()
return false, nil
}
if err != nil {
f.Close()
return false, err
}
heldInstance = f
return true, nil
}