fix(autostart): a launch agent left half-written reads as Open at log… (#1079)
* fix(autostart): a launch agent left half-written reads as Open at login off, not on The Mac's launch agent, the Linux .desktop and the Android boot script were written with a plain write, which empties the file before it fills it, so a crash or a full disk part way through left a record that is empty or half there. launchd refuses a job that names neither its own label nor a program, and the desktop starts no entry with no Exec, so magpie simply never opened at login. Enabled() on the Mac and on Linux asked only whether the file was there, so Settings kept showing the switch on; and Refresh(), which the app runs at every start, rewrites only a record its ProgramArguments regexp can still find a program in, which a half-written one does not have. Nothing repaired it and nothing said so, so the user was left believing a switch that did nothing. The four writes of these records now go through edit.WriteAtomic, which writes a temp file beside the record and renames it over: a write lands whole or not at all, and one cut short leaves the previous record, still loadable, where it was. No import cycle: internal/edit brings in only internal/steady and internal/filememo. enabled() on the Mac and on Linux now asks whether the record is one that starts magpie — the launch agent names both its label and its program, the desktop entry has both its Type and its Exec — instead of whether the file is there. A record a write cut short now reads as off, so the switch stops saying magpie opens at login when it does not, and the user sees the off that Refresh() cannot repair. A record magpie did not write is still left alone by Refresh() (TestRefreshOlderLaunchAgent), which is unchanged: refresh() rewrites what it can parse, and enabled() reports off for what it cannot. Windows' Run key is not touched here. TestSetThenTruncatedRecordReadsAsOff is new, once for the Mac (autostart_darwin_test.go) and once for Linux (autostart_other_test.go), where the record is written whole and then left as a write cut short leaves it: the head, stopping before the program it names, and the same record emptied. Against the old code, whose check was os.Stat, the Mac one fails with "a launch agent with no ProgramArguments reads as on" and the Linux one with "a desktop entry with no Exec reads as on", each on both records. With the change both pass, and TestSet, TestRefreshOlderLaunchAgent and TestLaunchAgentLetsTheRelaunchLive are unchanged. go vet, the windows, darwin and linux builds and test compiles, and gofmt on the seven files (LF-normalized first, since the worktree is CRLF) pass. Not run on a Mac or with plutil: this is a Windows box, so the darwin and linux behaviour is checked by GOOS=darwin and GOOS=linux builds plus their test compiles, and the two new predicates were run here against the bytes those platforms write from a scratch harness, which showed the old os.Stat check reading all three records as on. That harness is not committed. * fix(autostart): a record that names nothing to run reads as off The Linux and Mac checks the last commit added ask for the Exec key and the ProgramArguments array, not for anything in them, so a record whose Exec is present and empty still read as on. A desktop starts no entry that names nothing to run and launchd refuses a job whose program is an empty string, so that is the same lie the truncated record told, in the one shape the truncation could leave that the check didn't cover. entryExec now wants a non-space after the key, and program wants something other than the closing tag in the first string. Android's check needs nothing here: it reads the file's exec bit, which a write cut short does not leave on it. Each of the two tests now writes the empty-valued record beside the truncated one and the empty one, so all three shapes are covered. * fix(autostart): the darwin check's new block sits inside the function The block added to TestSetThenTruncatedRecordReadsAsOff landed after the closing brace, so the darwin and linux jobs answered "autostart_darwin_test.go:134:2: expected declaration, found empty" at the gofmt step and nothing of the package was ever compiled there. Windows never compiled the file, so the branch was green on the one platform that could not see it. The block now sits before the closing brace with the rest of the check. Verified on Windows with go1.27.2: go vet ./internal/autostart passes for windows/amd64, darwin/arm64 and linux/amd64, and the package's tests pass. --------- Co-authored-by: TryWorld <TryWorld2026@users.noreply.github.com> Co-authored-by: yetone <yetoneful@gmail.com>
This commit is contained in:
co-authored by
TryWorld
yetone
parent
5e5b6a5ac3
commit
e1b70c13de
@@ -4,6 +4,8 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
)
|
||||
|
||||
// Termux:Boot runs executable scripts in ~/.termux/boot after Android boots.
|
||||
@@ -25,7 +27,7 @@ func enable(exe string) error {
|
||||
// Android's shell is available even before Termux's PATH is loaded.
|
||||
q := "'" + strings.ReplaceAll(exe, "'", "'\"'\"'") + "'"
|
||||
body := "#!/system/bin/sh\nexec " + q + " serve </dev/null >/dev/null 2>&1 &\n"
|
||||
if err := os.WriteFile(p, []byte(body), 0o700); err != nil {
|
||||
if err := edit.WriteAtomic(p, []byte(body)); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Chmod(p, 0o700)
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
)
|
||||
|
||||
const label = "com.yetone.magpie"
|
||||
@@ -16,9 +18,14 @@ func record() string {
|
||||
return filepath.Join(home, "Library", "LaunchAgents", label+".plist")
|
||||
}
|
||||
|
||||
// launchd loads no job that names neither its own label nor a program to
|
||||
// run: a record a write left short of either sits in LaunchAgents, is
|
||||
// refused, and magpie never opens at login however long the file is there.
|
||||
var ourLabel = regexp.MustCompile(`<key>Label</key>\s*<string>` + regexp.QuoteMeta(label) + `</string>`)
|
||||
|
||||
func enabled() bool {
|
||||
_, err := os.Stat(record())
|
||||
return err == nil
|
||||
b, err := os.ReadFile(record())
|
||||
return err == nil && ourLabel.Match(b) && program.Match(b)
|
||||
}
|
||||
|
||||
// a launch agent the system loads at the next login: the app itself, run
|
||||
@@ -28,7 +35,7 @@ func enable(exe string) error {
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(p, plist(exe), 0o644)
|
||||
return edit.WriteAtomic(p, plist(exe))
|
||||
}
|
||||
|
||||
// plist is the launch agent for exe. AssociatedBundleIdentifiers names the
|
||||
@@ -78,7 +85,9 @@ func associated(exe string) string {
|
||||
return fmt.Sprintf("\t<key>AssociatedBundleIdentifiers</key>\n\t<array><string>%s</string></array>\n", html.EscapeString(html.UnescapeString(string(m[1]))))
|
||||
}
|
||||
|
||||
var program = regexp.MustCompile(`<key>ProgramArguments</key>\s*<array><string>([^<]*)</string>`)
|
||||
// with a path in the first string: an empty one names nothing to run, and
|
||||
// launchd refuses the job
|
||||
var program = regexp.MustCompile(`<key>ProgramArguments</key>\s*<array><string>([^<]+)</string>`)
|
||||
|
||||
// refresh writes a launch agent an older magpie wrote over as this one
|
||||
// would, for the program it names: the path stays the one the user turned
|
||||
@@ -101,7 +110,7 @@ func refresh() error {
|
||||
if bytes.Equal(b, want) {
|
||||
return nil
|
||||
}
|
||||
return os.WriteFile(p, want, 0o644)
|
||||
return edit.WriteAtomic(p, want)
|
||||
}
|
||||
|
||||
func disable() error {
|
||||
|
||||
@@ -164,3 +164,48 @@ func TestLaunchAgentNamesItsApp(t *testing.T) {
|
||||
t.Fatalf("a bare program's launch agent names an app:\n%s", b)
|
||||
}
|
||||
}
|
||||
|
||||
// A record a write cut short is not one that opens magpie at login: launchd
|
||||
// refuses a job with no program to run, and one with nothing in it at all,
|
||||
// so Open at login reads off rather than saying on for a record that never
|
||||
// starts anything.
|
||||
func TestSetThenTruncatedRecordReadsAsOff(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv("USERPROFILE", home)
|
||||
if err := Set(true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !Enabled() {
|
||||
t.Fatal("not on after Set(true)")
|
||||
}
|
||||
p := record()
|
||||
b, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// the head of the launch agent, cut off before the program it names,
|
||||
// which is what a write that goes no further leaves behind
|
||||
head := b[:strings.Index(string(b), "<key>ProgramArguments</key>")]
|
||||
if err := os.WriteFile(p, head, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if Enabled() {
|
||||
t.Errorf("a launch agent with no ProgramArguments reads as on:\n%s", head)
|
||||
}
|
||||
if err := os.WriteFile(p, nil, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if Enabled() {
|
||||
t.Error("an empty launch agent reads as on")
|
||||
}
|
||||
// a program named as nothing is no program to run either, and launchd
|
||||
// refuses the job
|
||||
empty := strings.Replace(string(b), "<array><string>", "<array><string></string><string>", 1)
|
||||
if err := os.WriteFile(p, []byte(empty), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if Enabled() {
|
||||
t.Errorf("a launch agent with no program reads as on:\n%s", empty)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,11 @@ package autostart
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/yetone/magpie/internal/appdir"
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
)
|
||||
|
||||
func record() string {
|
||||
@@ -19,9 +21,19 @@ func record() string {
|
||||
return filepath.Join(dir, "autostart", "magpie.desktop")
|
||||
}
|
||||
|
||||
// the two keys a desktop entry needs before the desktop starts it at login:
|
||||
// no Type and it is not an application entry, no Exec and it names nothing
|
||||
// to run. A .desktop a write left short of either reads as off, not as on.
|
||||
var (
|
||||
entryType = regexp.MustCompile(`(?m)^Type=Application$`)
|
||||
// the Exec key with something to run after it: an empty one names
|
||||
// nothing, and the desktop starts no entry that names nothing
|
||||
entryExec = regexp.MustCompile(`(?m)^Exec=\s*\S`)
|
||||
)
|
||||
|
||||
func enabled() bool {
|
||||
_, err := os.Stat(record())
|
||||
return err == nil
|
||||
b, err := os.ReadFile(record())
|
||||
return err == nil && entryType.Match(b) && entryExec.Match(b)
|
||||
}
|
||||
|
||||
// an XDG autostart entry, which the desktop starts at login
|
||||
@@ -34,7 +46,7 @@ func enable(exe string) error {
|
||||
q := strings.NewReplacer(`\`, `\\`, `"`, `\"`, "`", "\\`", `$`, `\$`).Replace(exe)
|
||||
body := "[Desktop Entry]\nType=Application\nName=magpie\nComment=one place to pick every agent's model\n" +
|
||||
"Exec=\"" + q + "\" " + Arg + "\nTerminal=false\nX-GNOME-Autostart-enabled=true\n"
|
||||
return os.WriteFile(p, []byte(body), 0o644)
|
||||
return edit.WriteAtomic(p, []byte(body))
|
||||
}
|
||||
|
||||
func disable() error {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
//go:build !darwin && !windows && !android
|
||||
|
||||
package autostart
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A record a write cut short is not one that opens magpie at login: the
|
||||
// desktop starts no entry with no Exec, and none at all with nothing in it,
|
||||
// so Open at login reads off rather than saying on for a record that never
|
||||
// starts anything.
|
||||
func TestSetThenTruncatedRecordReadsAsOff(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv("USERPROFILE", home)
|
||||
t.Setenv("XDG_CONFIG_HOME", home+"/.config")
|
||||
t.Setenv("APPIMAGE", "/opt/my apps/magpie.AppImage")
|
||||
if err := Set(true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !Enabled() {
|
||||
t.Fatal("not on after Set(true)")
|
||||
}
|
||||
p := record()
|
||||
b, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// the head of the entry, cut off before the Exec that names the
|
||||
// program, which is what a write that goes no further leaves behind
|
||||
head := b[:strings.Index(string(b), "Exec=")]
|
||||
if err := os.WriteFile(p, head, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if Enabled() {
|
||||
t.Errorf("a desktop entry with no Exec reads as on:\n%s", head)
|
||||
}
|
||||
if err := os.WriteFile(p, nil, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if Enabled() {
|
||||
t.Error("an empty desktop entry reads as on")
|
||||
}
|
||||
// an Exec key with nothing after it names nothing to run either, and the
|
||||
// desktop starts no entry that names nothing
|
||||
if err := os.WriteFile(p, []byte("[Desktop Entry]\nType=Application\nName=magpie\nExec=\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if Enabled() {
|
||||
t.Error("a desktop entry with an empty Exec reads as on")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user