rightkit-tauri-shell 0.3.0

Shared Tauri desktop shell for Right Suite apps: webview hardening, window state, native menu, tray, single instance, deep links, autostart, rich clipboard, watchdog and engine wiring.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//! Launch at login. Base: CodeRight `os_shell/autostart` (Linux XDG `.desktop`, Windows HKCU Run
//! through `reg.exe` with no console), plus the macOS LaunchAgent the donor lacked (HeardRight's
//! `login_launch` covered it only for its own bundle). The login launch receives
//! [`AutostartOptions::args`] (default `--background`), so it never starts a second engine.
//!
//! Migration. Earlier builds used `tauri-plugin-autostart`, which `auto-launch` writes under the
//! package name: `~/Library/LaunchAgents/<name>.plist` on macOS, the HKCU Run value `<name>` on
//! Windows, and `~/.config/autostart/<name>.desktop` elsewhere. [`migrate_plugin_entry`] enables the
//! shell's entry and removes that one, so an upgraded user never has two login items.
//!
//! The exact legacy names are [`LegacyAutostartNames`]. Read from `tauri-plugin-autostart` 2.5.1
//! (`src/lib.rs`), `auto-launch` 0.5.0 (`src/{windows,macos,linux}.rs`) and `tauri-codegen` 2.6.3
//! (`src/context.rs`), the versions HeardRight's lockfile pins for the first two. The entry name is
//! the plugin's `app_name` if the app set one, otherwise `PackageInfo::name`, which Tauri fills with
//! `tauri.conf.json` `productName` when present and the Cargo package name otherwise. For HeardRight
//! (`productName` `HeardRight`) that is `HeardRight`: the Windows Run value `HeardRight`, the macOS
//! LaunchAgent label `HeardRight` at `~/Library/LaunchAgents/HeardRight.plist`.

use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
#[cfg(not(windows))]
use std::fs;

pub const BACKGROUND_ARG: &str = "--background";

/// How the login entry launches the app.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AutostartOptions {
    /// Arguments the login launch receives. Default: `["--background"]`.
    pub args: Vec<String>,
    /// macOS LaunchAgent `LowPriorityIO`: yields the disk during the login storm (Spotlight,
    /// Gatekeeper). Ignored elsewhere. Default: off.
    pub low_priority_io: bool,
    /// Remove the entry `tauri-plugin-autostart` wrote, whenever this shell enables or disables login
    /// launch, so an upgraded user never has two login items. Default: off.
    pub migrate_from_plugin_entry: bool,
    /// Linux: written as `StartupWMClass` in the desktop entry so the shell matches the app's window
    /// class. `None` (the default) writes no such key. A value with control characters is ignored.
    pub startup_wm_class: Option<String>,
    /// Where the per-user config lives, instead of the environment (`RIGHTKIT_SHELL_HOME`,
    /// `XDG_CONFIG_HOME`, `HOME`). Linux: the XDG config directory, so the entry is
    /// `<config_home>/autostart/<id>.desktop`. macOS: the home directory, so the entry is
    /// `<config_home>/Library/LaunchAgents/<id>.autostart.plist`. For tests and sandboxes; `None`
    /// (the default) keeps the environment lookup. Ignored on Windows.
    pub config_home: Option<PathBuf>,
    /// macOS: report login launch as unsupported and create no LaunchAgent, for an app that handles
    /// login items itself. Ignored elsewhere. Default: off.
    pub macos_unsupported: bool,
}

/// True when `options` make login launch unsupported on the platform (`target_macos`).
pub fn reports_unsupported(options: &AutostartOptions, target_macos: bool) -> bool {
    target_macos && options.macos_unsupported
}

impl Default for AutostartOptions {
    fn default() -> Self {
        Self {
            args: vec![BACKGROUND_ARG.to_owned()],
            low_priority_io: false,
            migrate_from_plugin_entry: false,
            startup_wm_class: None,
            config_home: None,
            macos_unsupported: false,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AutostartStatus {
    pub supported: bool,
    pub enabled: bool,
    /// Plist / desktop-entry path (None on Windows where the entry is a registry value).
    pub entry: Option<String>,
}

pub struct AutostartSpec {
    /// Reverse-DNS or short id; the LaunchAgent label and the registry value name.
    pub app_id: String,
    pub app_name: String,
    pub executable: PathBuf,
    pub options: AutostartOptions,
    /// The name `tauri-plugin-autostart` keyed its entry by. Set (by the shell, from the package name)
    /// only when [`AutostartOptions::migrate_from_plugin_entry`] is on.
    pub legacy_plugin_name: Option<String>,
}

/// What [`migrate_plugin_entry`] did.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum AutostartMigration {
    /// No `tauri-plugin-autostart` entry exists.
    NoLegacy,
    /// The legacy entry was replaced by the shell's entry and removed.
    Migrated,
}

/// The entries `tauri-plugin-autostart` writes for one entry name (see the module docs for where the
/// name comes from). Pure derivation, identical on every host.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LegacyAutostartNames {
    /// Windows: the value name under `HKCU\Software\Microsoft\Windows\CurrentVersion\Run`, verbatim.
    /// The plugin also writes a same-named value under
    /// `HKCU\...\Explorer\StartupApproved\Run` (Task Manager's enabled flag), which the migration
    /// leaves in place: it is inert once the Run value is gone.
    pub windows_run_value: String,
    /// macOS (LaunchAgent launcher, the default): the plist `Label`, verbatim.
    pub macos_launch_agent_label: String,
    /// macOS: the plist path relative to the user's home, `Library/LaunchAgents/<name>.plist`.
    pub macos_plist_relative: PathBuf,
    /// Linux: the desktop entry relative to the user's home, `.config/autostart/<name>.desktop`
    /// (the plugin does not honour `XDG_CONFIG_HOME`).
    pub linux_desktop_relative: PathBuf,
}

impl LegacyAutostartNames {
    /// Derive the entries for the plugin's entry name.
    pub fn for_entry_name(name: &str) -> Self {
        Self {
            windows_run_value: name.to_owned(),
            macos_launch_agent_label: name.to_owned(),
            macos_plist_relative: PathBuf::from("Library/LaunchAgents")
                .join(format!("{name}.plist")),
            linux_desktop_relative: PathBuf::from(".config/autostart")
                .join(format!("{name}.desktop")),
        }
    }
}

fn xml(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

/// Program arguments for a macOS login item with the default `--background` argument.
pub fn macos_program_arguments(exe: &Path) -> Vec<String> {
    macos_program_arguments_with(exe, &[BACKGROUND_ARG.to_owned()])
}

/// Program arguments for a macOS login item. Inside a `.app` bundle the bundle is launched through
/// LaunchServices (`open -g -a <bundle> --args ...`) so it behaves like a Dock launch.
pub fn macos_program_arguments_with(exe: &Path, args: &[String]) -> Vec<String> {
    let s = exe.to_string_lossy();
    let mut out: Vec<String>;
    let in_bundle = s.find(".app/Contents/MacOS/");
    if let Some(idx) = in_bundle {
        let bundle = &s[..idx + 4];
        out = vec![
            "/usr/bin/open".into(),
            "-g".into(),
            "-a".into(),
            bundle.into(),
        ];
        if !args.is_empty() {
            out.push("--args".into());
        }
    } else {
        out = vec![s.into_owned()];
    }
    out.extend(args.iter().cloned());
    out
}

pub fn macos_plist(label: &str, args: &[String]) -> String {
    macos_plist_with(label, args, false)
}

/// A LaunchAgent plist. `low_priority_io` adds `LowPriorityIO`.
pub fn macos_plist_with(label: &str, args: &[String], low_priority_io: bool) -> String {
    let items: String = args
        .iter()
        .map(|a| format!("    <string>{}</string>\n", xml(a)))
        .collect();
    let low = if low_priority_io {
        "  <key>LowPriorityIO</key>\n  <true/>\n"
    } else {
        ""
    };
    format!(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n  <key>Label</key>\n  <string>{}</string>\n  <key>ProgramArguments</key>\n  <array>\n{items}  </array>\n  <key>RunAtLoad</key>\n  <true/>\n  <key>ProcessType</key>\n  <string>Interactive</string>\n{low}</dict>\n</plist>\n",
        xml(label)
    )
}

fn desktop_exec_quote(v: &str) -> String {
    if !v
        .chars()
        .any(|c| matches!(c, ' ' | '\t' | '\n' | '"' | '$' | '`' | '\\'))
    {
        return v.to_owned();
    }
    let mut out = String::from("\"");
    for c in v.chars() {
        if matches!(c, '"' | '$' | '`' | '\\') {
            out.push('\\');
        }
        out.push(c);
    }
    out.push('"');
    out
}

pub fn linux_desktop_entry(name: &str, exe: &Path) -> String {
    linux_desktop_entry_with(name, exe, &[BACKGROUND_ARG.to_owned()])
}

/// A desktop entry whose `Exec` line carries `args`.
pub fn linux_desktop_entry_with(name: &str, exe: &Path, args: &[String]) -> String {
    linux_desktop_entry_for(name, exe, args, None)
}

/// [`linux_desktop_entry_with`] plus `StartupWMClass=<wm_class>` when it is a usable single-line
/// value (blank values and values with control characters write no key).
pub fn linux_desktop_entry_for(
    name: &str,
    exe: &Path,
    args: &[String],
    wm_class: Option<&str>,
) -> String {
    // Control characters are refused on the raw value: trimming first would silently drop a
    // trailing CR or LF and write a key the caller did not validate.
    let class = wm_class
        .filter(|c| !c.chars().any(char::is_control))
        .map(str::trim)
        .filter(|c| !c.is_empty())
        .map(|c| format!("StartupWMClass={c}\n"))
        .unwrap_or_default();
    let mut exec = desktop_exec_quote(&exe.to_string_lossy());
    for arg in args {
        exec.push(' ');
        exec.push_str(&desktop_exec_quote(arg));
    }
    format!("[Desktop Entry]\nType=Application\nName={name}\nExec={exec}\nTerminal=false\n{class}X-GNOME-Autostart-enabled=true\n")
}

/// The Windows Run command: the quoted executable, then each argument (quoted when it has spaces).
pub fn windows_run_command(exe: &Path, args: &[String]) -> String {
    let mut command = format!("\"{}\"", exe.display());
    for arg in args {
        command.push(' ');
        if arg.contains(' ') || arg.contains('"') {
            command.push('"');
            command.push_str(&arg.replace('"', "\\\""));
            command.push('"');
        } else {
            command.push_str(arg);
        }
    }
    command
}

/// Home directory for login entries. `RIGHTKIT_SHELL_HOME` redirects them (isolated QA runs and
/// sandboxes must never write the real user's LaunchAgents / autostart / config).
// Used by the macOS (LaunchAgents) and other-Unix (XDG autostart) paths; Windows uses the registry.
#[cfg_attr(windows, allow(dead_code))]
fn home() -> Option<PathBuf> {
    std::env::var_os("RIGHTKIT_SHELL_HOME")
        .filter(|v| !v.is_empty())
        .or_else(|| std::env::var_os("HOME"))
        .or_else(|| std::env::var_os("USERPROFILE"))
        .map(PathBuf::from)
}

/// The login-item file for `stem` on this platform (None on Windows, where entries are registry
/// values).
#[cfg(not(windows))]
fn login_item_path(stem: &str, config_home: Option<&Path>) -> Option<PathBuf> {
    #[cfg(target_os = "macos")]
    {
        config_home
            .map(Path::to_path_buf)
            .or_else(home)
            .map(|h| h.join("Library/LaunchAgents").join(format!("{stem}.plist")))
    }
    #[cfg(all(unix, not(target_os = "macos")))]
    {
        if let Some(cfg) = config_home {
            return Some(cfg.join("autostart").join(format!("{stem}.desktop")));
        }
        let cfg = std::env::var_os("XDG_CONFIG_HOME")
            .filter(|v| !v.is_empty())
            .map(PathBuf::from)
            .or_else(|| home().map(|h| h.join(".config")))?;
        Some(cfg.join("autostart").join(format!("{stem}.desktop")))
    }
}

/// Where `tauri-plugin-autostart` put its login item for `stem` (None on Windows).
#[cfg(not(windows))]
fn legacy_login_item_path(stem: &str, config_home: Option<&Path>) -> Option<PathBuf> {
    let names = LegacyAutostartNames::for_entry_name(stem);
    #[cfg(target_os = "macos")]
    {
        config_home
            .map(Path::to_path_buf)
            .or_else(home)
            .map(|h| h.join(names.macos_plist_relative))
    }
    #[cfg(not(target_os = "macos"))]
    {
        match config_home {
            // The injected directory is the config directory itself: `autostart/<name>.desktop`.
            Some(cfg) => Some(cfg.join("autostart").join(format!("{stem}.desktop"))),
            None => home().map(|h| h.join(names.linux_desktop_relative)),
        }
    }
}

/// The stem of the shell's own entry: `<app_id>.autostart` on macOS (the LaunchAgent label), the
/// app id elsewhere.
// Not used by the Windows backend.
#[cfg_attr(not(unix), allow(dead_code))]
fn our_stem(spec: &AutostartSpec) -> String {
    #[cfg(target_os = "macos")]
    {
        format!("{}.autostart", spec.app_id)
    }
    #[cfg(not(target_os = "macos"))]
    {
        spec.app_id.clone()
    }
}

/// Where the shell's login entry lives on this platform (None on Windows).
pub fn entry_path(spec: &AutostartSpec) -> Option<PathBuf> {
    #[cfg(windows)]
    {
        let _ = spec;
        None
    }
    #[cfg(not(windows))]
    {
        login_item_path(&our_stem(spec), spec.options.config_home.as_deref())
    }
}

#[cfg(windows)]
const RUN_KEY: &str = r"HKCU\Software\Microsoft\Windows\CurrentVersion\Run";
#[cfg(windows)]
const STARTUP_APPROVED_KEY: &str =
    r"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run";

/// The two registry keys Windows login items live under. Only [`migrate_plugin_entry_in`] takes
/// them, so tests can run the migration against a scratch key; everything else uses the real keys.
/// Ignored off Windows.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RegistryKeys {
    /// The `Run` key holding the command (`HKCU\...\CurrentVersion\Run`).
    pub run: String,
    /// The `StartupApproved\Run` key holding Task Manager's enabled or disabled flag per value name
    /// (a 12-byte `REG_BINARY`; `tauri-plugin-autostart` writes it as well as the Run value).
    pub startup_approved: String,
}

#[allow(clippy::derivable_impls)] // the Windows default is the real keys
impl Default for RegistryKeys {
    fn default() -> Self {
        #[cfg(windows)]
        {
            Self {
                run: RUN_KEY.to_owned(),
                startup_approved: STARTUP_APPROVED_KEY.to_owned(),
            }
        }
        #[cfg(not(windows))]
        {
            Self {
                run: String::new(),
                startup_approved: String::new(),
            }
        }
    }
}

/// True when a `StartupApproved` value says the user disabled the item in Task Manager. The rule is
/// `auto-launch` 0.5.0's own (`windows.rs`, `last_eight_bytes_all_zeros`): enabled when the last
/// eight bytes are all zero (`02 00 00 00 ...`), disabled when they hold the disable timestamp
/// (`03 00 00 00` plus a FILETIME). A value shorter than eight bytes is not a Task Manager record
/// and counts as enabled.
pub fn startup_approved_is_disabled(bytes: &[u8]) -> bool {
    bytes.len() >= 8 && bytes.iter().rev().take(8).any(|b| *b != 0)
}

#[cfg(windows)]
fn reg(args: &[&str]) -> Result<std::process::Output, String> {
    use std::os::windows::process::CommandExt;
    std::process::Command::new("reg.exe")
        .creation_flags(0x0800_0000)
        .args(args)
        .output()
        .map_err(|e| format!("run reg.exe: {e}"))
}

/// The bytes of a `REG_BINARY` value, or None when the key or value is absent or not binary.
#[cfg(windows)]
fn reg_read_binary(key: &str, name: &str) -> Option<Vec<u8>> {
    let out = reg(&["QUERY", key, "/v", name]).ok()?;
    if !out.status.success() {
        return None;
    }
    let text = String::from_utf8_lossy(&out.stdout);
    let line = text.lines().find(|l| l.contains("REG_BINARY"))?;
    let hex = line.split("REG_BINARY").nth(1)?.trim();
    if hex.len() % 2 != 0 {
        return None;
    }
    (0..hex.len())
        .step_by(2)
        .map(|i| u8::from_str_radix(hex.get(i..i + 2)?, 16).ok())
        .collect()
}

#[cfg(windows)]
fn reg_write_binary(key: &str, name: &str, bytes: &[u8]) -> Result<(), String> {
    let hex: String = bytes.iter().map(|b| format!("{b:02X}")).collect();
    let out = reg(&["ADD", key, "/v", name, "/t", "REG_BINARY", "/d", &hex, "/f"])?;
    if out.status.success() {
        Ok(())
    } else {
        Err(String::from_utf8_lossy(&out.stderr).trim().to_owned())
    }
}

pub fn status(spec: &AutostartSpec) -> AutostartStatus {
    if reports_unsupported(&spec.options, cfg!(target_os = "macos")) {
        return AutostartStatus {
            supported: false,
            enabled: false,
            entry: None,
        };
    }
    #[cfg(windows)]
    {
        let enabled = reg(&["QUERY", RUN_KEY, "/v", &spec.app_id])
            .map(|o| o.status.success())
            .unwrap_or(false);
        AutostartStatus {
            supported: true,
            enabled,
            entry: None,
        }
    }
    #[cfg(not(windows))]
    {
        match entry_path(spec) {
            Some(p) => AutostartStatus {
                supported: true,
                enabled: p.is_file(),
                entry: Some(p.to_string_lossy().into_owned()),
            },
            None => AutostartStatus {
                supported: false,
                enabled: false,
                entry: None,
            },
        }
    }
}

/// Enable or disable login launch. With [`AutostartOptions::migrate_from_plugin_entry`] and a
/// legacy name on the spec, the legacy entry is removed as well.
pub fn set_enabled(spec: &AutostartSpec, enabled: bool) -> Result<AutostartStatus, String> {
    if reports_unsupported(&spec.options, cfg!(target_os = "macos")) {
        // Nothing was ever written, so turning it off has nothing to undo.
        return if enabled {
            Err("login launch is not supported by this app on macOS".into())
        } else {
            Ok(status(spec))
        };
    }
    if enabled {
        write_entry(&RegistryKeys::default(), spec)?;
    } else {
        remove_entry(spec)?;
    }
    if spec.options.migrate_from_plugin_entry {
        if let Some(legacy) = spec.legacy_plugin_name.as_deref() {
            remove_legacy(&RegistryKeys::default(), spec, legacy)?;
        }
    }
    Ok(status(spec))
}

/// One-time migration from `tauri-plugin-autostart`: when the legacy entry `legacy_name` exists,
/// write the shell's entry with the configured options and remove the legacy one. The user's
/// login-at-start choice is kept: a legacy entry means login launch was on.
pub fn migrate_plugin_entry(
    spec: &AutostartSpec,
    legacy_name: &str,
) -> Result<AutostartMigration, String> {
    migrate_plugin_entry_in(&RegistryKeys::default(), spec, legacy_name)
}

/// [`migrate_plugin_entry`] against explicit registry keys.
///
/// On Windows the plugin wrote two values under `legacy_name`: the command under `Run`, and Task
/// Manager's flag under `StartupApproved\Run`. Migration carries both. A legacy flag that says
/// the user disabled the item is copied to the shell's value name, so a user who turned login launch
/// off in Task Manager stays off; an enabled or absent flag changes nothing. The legacy flag value
/// is then removed along with the legacy command (unless the shell's entry uses the same name, in
/// which case the flag already applies to it).
pub fn migrate_plugin_entry_in(
    keys: &RegistryKeys,
    spec: &AutostartSpec,
    legacy_name: &str,
) -> Result<AutostartMigration, String> {
    if reports_unsupported(&spec.options, cfg!(target_os = "macos")) {
        // This host declined macOS login items: write nothing, leave the legacy entry alone.
        return Ok(AutostartMigration::NoLegacy);
    }
    if !legacy_present(keys, legacy_name, spec.options.config_home.as_deref()) {
        return Ok(AutostartMigration::NoLegacy);
    }
    #[cfg(windows)]
    let carried = reg_read_binary(&keys.startup_approved, legacy_name)
        .filter(|bytes| startup_approved_is_disabled(bytes));
    write_entry(keys, spec)?;
    #[cfg(windows)]
    if let Some(bytes) = carried {
        reg_write_binary(&keys.startup_approved, &spec.app_id, &bytes)?;
    }
    remove_legacy(keys, spec, legacy_name)?;
    Ok(AutostartMigration::Migrated)
}

fn write_entry(keys: &RegistryKeys, spec: &AutostartSpec) -> Result<(), String> {
    #[cfg(not(windows))]
    let _ = keys;
    #[cfg(windows)]
    {
        let command = windows_run_command(&spec.executable, &spec.options.args);
        let out = reg(&[
            "ADD",
            &keys.run,
            "/v",
            &spec.app_id,
            "/t",
            "REG_SZ",
            "/d",
            &command,
            "/f",
        ])?;
        if !out.status.success() {
            return Err(String::from_utf8_lossy(&out.stderr).trim().to_owned());
        }
        Ok(())
    }
    #[cfg(not(windows))]
    {
        let Some(path) = entry_path(spec) else {
            return Ok(());
        };
        if let Some(dir) = path.parent() {
            fs::create_dir_all(dir).map_err(|e| format!("create {}: {e}", dir.display()))?;
        }
        #[cfg(target_os = "macos")]
        let body = macos_plist_with(
            &our_stem(spec),
            &macos_program_arguments_with(&spec.executable, &spec.options.args),
            spec.options.low_priority_io,
        );
        #[cfg(not(target_os = "macos"))]
        let body = linux_desktop_entry_for(
            &spec.app_name,
            &spec.executable,
            &spec.options.args,
            spec.options.startup_wm_class.as_deref(),
        );
        fs::write(&path, body).map_err(|e| format!("write {}: {e}", path.display()))
    }
}

fn remove_entry(spec: &AutostartSpec) -> Result<(), String> {
    #[cfg(windows)]
    {
        // A missing value is already the desired state.
        let _ = reg(&["DELETE", RUN_KEY, "/v", &spec.app_id, "/f"]);
        Ok(())
    }
    #[cfg(not(windows))]
    {
        let Some(path) = entry_path(spec) else {
            return Ok(());
        };
        match fs::remove_file(&path) {
            Ok(()) => Ok(()),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
            Err(e) => Err(format!("remove {}: {e}", path.display())),
        }
    }
}

fn legacy_present(keys: &RegistryKeys, legacy_name: &str, config_home: Option<&Path>) -> bool {
    #[cfg(not(windows))]
    let _ = keys;
    #[cfg(windows)]
    let _ = config_home;
    #[cfg(windows)]
    {
        reg(&["QUERY", &keys.run, "/v", legacy_name])
            .map(|o| o.status.success())
            .unwrap_or(false)
    }
    #[cfg(not(windows))]
    {
        legacy_login_item_path(legacy_name, config_home).is_some_and(|p| p.exists())
    }
}

/// Remove the legacy entry, unless it is the same entry as the shell's own (then it was overwritten).
fn remove_legacy(
    keys: &RegistryKeys,
    spec: &AutostartSpec,
    legacy_name: &str,
) -> Result<bool, String> {
    #[cfg(not(windows))]
    let _ = keys;
    #[cfg(windows)]
    {
        if legacy_name == spec.app_id
            || !legacy_present(keys, legacy_name, spec.options.config_home.as_deref())
        {
            return Ok(false);
        }
        let out = reg(&["DELETE", &keys.run, "/v", legacy_name, "/f"])?;
        if !out.status.success() {
            return Err(String::from_utf8_lossy(&out.stderr).trim().to_owned());
        }
        // The flag is inert without the command; a missing flag is already the desired state.
        let _ = reg(&["DELETE", &keys.startup_approved, "/v", legacy_name, "/f"]);
        Ok(true)
    }
    #[cfg(not(windows))]
    {
        let Some(legacy) = legacy_login_item_path(legacy_name, spec.options.config_home.as_deref())
        else {
            return Ok(false);
        };
        if entry_path(spec).as_ref() == Some(&legacy) || !legacy.exists() {
            return Ok(false);
        }
        fs::remove_file(&legacy).map_err(|e| format!("remove {}: {e}", legacy.display()))?;
        Ok(true)
    }
}