diff --git a/internal/autostart/autostart_android.go b/internal/autostart/autostart_android.go
index 0a410910f..1ea69cfe0 100644
--- a/internal/autostart/autostart_android.go
+++ b/internal/autostart/autostart_android.go
@@ -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 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)
diff --git a/internal/autostart/autostart_darwin.go b/internal/autostart/autostart_darwin.go
index 37dd931b7..422b0d774 100644
--- a/internal/autostart/autostart_darwin.go
+++ b/internal/autostart/autostart_darwin.go
@@ -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(`Label\s*` + regexp.QuoteMeta(label) + ``)
+
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("\tAssociatedBundleIdentifiers\n\t%s\n", html.EscapeString(html.UnescapeString(string(m[1]))))
}
-var program = regexp.MustCompile(`ProgramArguments\s*([^<]*)`)
+// with a path in the first string: an empty one names nothing to run, and
+// launchd refuses the job
+var program = regexp.MustCompile(`ProgramArguments\s*([^<]+)`)
// 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 {
diff --git a/internal/autostart/autostart_darwin_test.go b/internal/autostart/autostart_darwin_test.go
index 637b86a05..c1d9c4eda 100644
--- a/internal/autostart/autostart_darwin_test.go
+++ b/internal/autostart/autostart_darwin_test.go
@@ -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), "ProgramArguments")]
+ 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), "", "", 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)
+ }
+}
diff --git a/internal/autostart/autostart_other.go b/internal/autostart/autostart_other.go
index 64c219866..e05d6d5b9 100644
--- a/internal/autostart/autostart_other.go
+++ b/internal/autostart/autostart_other.go
@@ -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 {
diff --git a/internal/autostart/autostart_other_test.go b/internal/autostart/autostart_other_test.go
new file mode 100644
index 000000000..ca6daa6b0
--- /dev/null
+++ b/internal/autostart/autostart_other_test.go
@@ -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")
+ }
+}