Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

linux: dev desktop entry only for source-tree runs; both Exec layers; exact Exec match

user.email4955515bcd42
diff --git a/apps/filmcraft/src/dev_icon.rs b/apps/filmcraft/src/dev_icon.rs
index a00986d23..7eb572adf 100644
--- a/apps/filmcraft/src/dev_icon.rs
+++ b/apps/filmcraft/src/dev_icon.rs
@@ -3,22 +3,40 @@
//! The Linux shell (Wayland *and* X11) resolves the window to a `.desktop` file by app id (set in
//! `main.rs` via `eframe`'s `with_app_id`) and takes the taskbar / window-list icon from its
//! `Icon=` entry in the hicolor theme. Packaging installs both
-//! (`packaging/linux/ai.storyteller.filmcraft.desktop` and the hicolor PNGs) — but a dev machine
-//! running `cargo run` has neither, so KDE/GNOME fall back to the generic Wayland icon. The fix is
-//! to ensure a user-level entry at startup: when no entry for the app id exists in any XDG data
-//! dir, install one (plus the hicolor icon PNGs, embedded so it works whatever the working
-//! directory is) into `$XDG_DATA_HOME`/`~/.local/share`. An existing packaged entry is never
-//! overridden; the user-owned dev copy is rewritten when it points at another binary (a dev build
-//! moves between checkouts). The shell picks the icon up at the *next* app start;
+//! (`packaging/linux/ai.storyteller.filmcraft.desktop` and the hicolor PNGs) — but a source-tree
+//! run has neither, so KDE/GNOME fall back to the generic Wayland icon. The fix is to ensure a
+//! user-level entry for those runs: when no entry for the app id exists in any XDG data dir,
+//! install one (plus the hicolor icon PNGs, embedded so it works whatever the working directory
+//! is) into `$XDG_DATA_HOME`/`~/.local/share`. An existing packaged entry is never overridden;
+//! the user-owned dev copy is rewritten when it points at another binary (a dev build moves
+//! between checkouts). The shell picks the icon up at the *next* app start;
//! `update-desktop-database`/`kbuildsycoca` is not needed for icon lookup.
//!
-//! Every failure is reported and skipped — a missing dev icon must never stop the app
+//! Only source-tree runs install anything: a debug build, or a release build with
+//! `FILMCRAFT_DEV_DESKTOP_ENTRY` set (the docs' `cargo run --release` flow). Packaged runs are
+//! excluded — AppImage (`APPIMAGE`) mounts at a fresh `/tmp/.mount_*` each launch, so its
+//! `Exec` path changes every time and it ships its own entry, and a Flatpak (`FLATPAK_ID`)
+//! installs its entry through the manifest. `packaging/linux/install.sh` writes the packaged
+//! `Exec=` the same two-layer way as here; keep the two in sync.
+//!
+//! Every failure is logged and skipped — a missing dev icon must never stop the app
//! ([`AGENTS.md`](/AGENTS.md) §0 "never crash").
-/// Install the user-level desktop entry and icons when the app id has no entry anywhere (Linux).
+/// Install the user-level desktop entry and icons for a source-tree run (Linux).
pub fn ensure_dev_desktop_entry() {
+ // Packaged and sandboxed runs never write into the user's profile: an AppImage mounts at a
+ // fresh /tmp/.mount_* every launch (the Exec path changes each time) and a Flatpak installs
+ // its own entry.
+ if std::env::var_os("APPIMAGE").is_some() || std::env::var_os("FLATPAK_ID").is_some() {
+ return;
+ }
+ // Dev builds only: a debug build, or a release run opted in — a packaged release must not
+ // touch the profile (the packages and install.sh install the real entry).
+ if !cfg!(debug_assertions) && std::env::var_os("FILMCRAFT_DEV_DESKTOP_ENTRY").is_none() {
+ return;
+ }
if let Err(e) = ensure() {
- eprintln!("filmcraft: desktop entry (taskbar icon) not installed: {e}");
+ log::warn!("desktop entry (taskbar icon) not installed: {e}");
}
}
@@ -27,7 +45,16 @@ fn ensure() -> Result<(), DevIconError> {
let file_name = format!("{}.desktop", crate::APP_ID);
let user_dir = user_data_dir()?;
let exe = std::env::current_exe().map_err(DevIconError::Io)?;
- let exe = exe.to_string_lossy();
+ // Not `to_string_lossy`: a substituted U+FFFD would install an `Exec` that can never start
+ // the app, and the same lossy string would match on every later run, so the broken entry
+ // would never be rewritten or warned about.
+ let exe = exe.to_str().ok_or(DevIconError::BadExecPath)?;
+ if exe.contains('=') || exe.contains('%') || exe.chars().any(|c| c.is_control()) {
+ // A desktop entry cannot carry these (install.sh rejects them for the same reason):
+ // '=' separates key and value, '%' introduces a field code, control characters are
+ // not valid in the string layer.
+ return Err(DevIconError::BadExecPath);
+ }
for dir in search_dirs(&user_dir) {
let candidate = dir.join("applications").join(&file_name);
if !candidate.is_file() {
@@ -39,7 +66,7 @@ fn ensure() -> Result<(), DevIconError> {
return Ok(());
}
let body = std::fs::read_to_string(&candidate).map_err(DevIconError::Io)?;
- if desktop_file_exec_is_ours(&body, &exe) {
+ if desktop_file_exec_is_ours(&body, exe) {
return Ok(());
}
break; // a dev entry from another checkout: fall through and rewrite it
@@ -55,7 +82,7 @@ fn ensure() -> Result<(), DevIconError> {
}
let applications = user_dir.join("applications");
std::fs::create_dir_all(&applications).map_err(DevIconError::Io)?;
- let body = desktop_file_content(crate::APP_ID, "FilmCraft", &exe);
+ let body = desktop_file_content(crate::APP_ID, "FilmCraft", exe);
std::fs::write(applications.join(&file_name), body).map_err(DevIconError::Io)?;
Ok(())
}
@@ -68,8 +95,9 @@ const ICONS: &[(u16, &[u8])] = &[
];
/// The user data dir we install into: `$XDG_DATA_HOME`, else `$HOME/.local/share` (XDG base
-/// directory spec). Untrusted environment: an empty value means the default, and a relative or
-/// missing one is an error — never a guess that would write into the working directory.
+/// directory spec). Untrusted environment: an empty or relative `XDG_DATA_HOME` means the
+/// default, and a missing or relative `HOME` is an error — never a guess that would write
+/// into the working directory.
fn user_data_dir() -> Result<std::path::PathBuf, DevIconError> {
if let Some(v) = std::env::var_os("XDG_DATA_HOME")
&& !v.is_empty()
@@ -77,7 +105,7 @@ fn user_data_dir() -> Result<std::path::PathBuf, DevIconError> {
{
return Ok(std::path::PathBuf::from(v));
}
- match std::env::var_os("HOME").filter(|h| !h.is_empty()) {
+ match std::env::var_os("HOME").filter(|h| !h.is_empty() && std::path::Path::new(h).is_absolute()) {
Some(home) => Ok(std::path::PathBuf::from(home).join(".local").join("share")),
None => Err(DevIconError::NoHome),
}
@@ -100,15 +128,71 @@ fn search_dirs(user: &std::path::Path) -> Vec<std::path::PathBuf> {
dirs
}
-/// Whether the `.desktop` body's `Exec` line points at our binary (a stale dev entry from another
-/// checkout does not, and is rewritten; a packaged `Exec=filmcraft` never matches a dev path).
+/// Whether the `.desktop` body's `Exec` points at our binary exactly (a stale dev entry from
+/// another checkout — `/a/filmcraft-old` is not `/a/filmcraft` — does not, and is rewritten; a
+/// packaged `Exec=filmcraft` never matches a source-tree path).
fn desktop_file_exec_is_ours(body: &str, exe: &str) -> bool {
- body.lines().any(|l| l.strip_prefix("Exec=").is_some_and(|rest| rest.contains(exe)))
+ exec_line(body).is_some_and(|arg| arg == exe)
+}
+
+/// The first `Exec=` argument (the executable), unescaped through both Desktop Entry layers:
+/// first the string layer of the file value (`\\` `\s` `\n` `\t` `\r`, per
+/// <https://specifications.freedesktop.org/desktop-entry/latest/>), then the shell layer of the
+/// argument (double quotes, and `\` before `` ` `"` `\` `$` inside them). `None` when there is no
+/// `Exec=` line or its first argument cannot be parsed.
+fn exec_line(body: &str) -> Option<String> {
+ let value = body.lines().find(|l| l.starts_with("Exec=")).and_then(|l| l.strip_prefix("Exec="))?;
+ let value = unescape_string_value(value);
+ if let Some(rest) = value.strip_prefix('"') {
+ let mut out = String::new();
+ let mut chars = rest.chars();
+ while let Some(c) = chars.next() {
+ match c {
+ '"' => return Some(out),
+ '\\' => match chars.next() {
+ Some(e @ ('\\' | '"' | '`' | '$')) => out.push(e),
+ Some(e) => {
+ out.push('\\');
+ out.push(e);
+ }
+ None => return None, // a dangling escape ends the argument
+ },
+ c => out.push(c),
+ }
+ }
+ None // no closing quote
+ } else {
+ Some(value.split(' ').next().unwrap_or_default().to_string())
+ }
+}
+
+/// Undo the desktop entry's string layer: `\\` `\s` `\n` `\t` `\r` are the defined sequences; a
+/// backslash before anything else is kept as the character itself (lenient, like desktop file
+/// parsers in the wild).
+fn unescape_string_value(value: &str) -> String {
+ let mut out = String::with_capacity(value.len());
+ let mut chars = value.chars();
+ while let Some(c) = chars.next() {
+ if c != '\\' {
+ out.push(c);
+ continue;
+ }
+ match chars.next() {
+ Some('\\') => out.push('\\'),
+ Some('s') => out.push(' '),
+ Some('n') => out.push('\n'),
+ Some('t') => out.push('\t'),
+ Some('r') => out.push('\r'),
+ Some(e) => out.push(e),
+ None => out.push('\\'),
+ }
+ }
+ out
}
/// The `.desktop` body: same fields as `packaging/linux/ai.storyteller.filmcraft.desktop`, with
/// `Exec` pointing at this exact binary (a dev build isn't on `$PATH`, so `TryExec` is left out)
-/// and the icon by theme name. `exe` is shell-quoted to survive spaces in the path.
+/// and the icon by theme name.
fn desktop_file_content(app_id: &str, name: &str, exe: &str) -> String {
let mut out = String::new();
out.push_str("[Desktop Entry]\n");
@@ -116,7 +200,7 @@ fn desktop_file_content(app_id: &str, name: &str, exe: &str) -> String {
out.push_str(&format!("Name={name}\n"));
out.push_str("GenericName=Video Editor\n");
out.push_str("Comment=Edit video, color and sound\n");
- out.push_str(&format!("Exec={} %F\n", quote(exe)));
+ out.push_str(&format!("Exec=\"{}\" %F\n", escape_exec_argument(exe)));
out.push_str(&format!("Icon={app_id}\n"));
out.push_str("Terminal=false\n");
out.push_str("StartupNotify=true\n");
@@ -126,30 +210,26 @@ fn desktop_file_content(app_id: &str, name: &str, exe: &str) -> String {
out
}
-/// Quote `s` for a shell `Exec=` line (Desktop Entry spec §"Exec string": an argument with
-/// reserved characters is wrapped in double quotes, escaping `\`, `"`, `` ` `` and `$`).
-fn quote(s: &str) -> String {
- let needs = s.is_empty() || s.bytes().any(|b| b" \"'\\`$<>&~|;()*?[]#".contains(&b));
- if !needs {
- return s.into();
- }
- let mut out = String::with_capacity(s.len() + 2);
- out.push('"');
+/// Escape `s` for the `Exec=` value's two layers, like `packaging/linux/install.sh`: first the
+/// shell layer (`` ` `` `"` `$` `\` get a backslash, and the argument is double-quoted), then
+/// the string layer of the file value (every backslash doubled).
+fn escape_exec_argument(s: &str) -> String {
+ let mut shell = String::with_capacity(s.len() + 2);
for c in s.chars() {
- if matches!(c, '\\' | '"' | '`' | '$') {
- out.push('\\');
+ if matches!(c, '`' | '"' | '$' | '\\') {
+ shell.push('\\');
}
- out.push(c);
+ shell.push(c);
}
- out.push('"');
- out
+ shell.replace('\\', "\\\\")
}
-/// Everything that can keep the dev icon out of the taskbar; printed, never fatal.
+/// Everything that can keep the dev icon out of the taskbar; logged, never fatal.
#[derive(Debug)]
enum DevIconError {
Io(std::io::Error),
NoHome,
+ BadExecPath,
}
impl std::fmt::Display for DevIconError {
@@ -157,57 +237,73 @@ impl std::fmt::Display for DevIconError {
match self {
DevIconError::Io(e) => write!(f, "{e}"),
DevIconError::NoHome => write!(f, "no home directory for the user data dir"),
+ DevIconError::BadExecPath => write!(f, "the binary path has a character a desktop entry cannot carry (=, % or a control character), or is not valid UTF-8"),
}
}
}
-impl From<std::io::Error> for DevIconError {
- fn from(e: std::io::Error) -> Self {
- DevIconError::Io(e)
- }
-}
-
#[cfg(test)]
mod tests {
use super::*;
- /// The entry mirrors the packaged one (minus `TryExec`, plus the dev `Exec` path).
+ /// The entry mirrors the packaged one, derived from the real file so the two cannot drift:
+ /// minus `TryExec` (a dev build isn't on `$PATH`), plus the quoted dev `Exec` path.
#[test]
fn desktop_file_matches_the_packaged_fields() {
+ let packaged = include_str!("../../../packaging/linux/ai.storyteller.filmcraft.desktop");
+ let expected = packaged
+ .lines()
+ .filter(|l| !l.starts_with("TryExec="))
+ .map(|l| if l.starts_with("Exec=") { "Exec=\"/target/debug/filmcraft\" %F" } else { l })
+ .collect::<Vec<_>>()
+ .join("\n")
+ + "\n";
let body = desktop_file_content("ai.storyteller.filmcraft", "FilmCraft", "/target/debug/filmcraft");
- assert_eq!(
- body,
- "[Desktop Entry]\n\
- Type=Application\n\
- Name=FilmCraft\n\
- GenericName=Video Editor\n\
- Comment=Edit video, color and sound\n\
- Exec=/target/debug/filmcraft %F\n\
- Icon=ai.storyteller.filmcraft\n\
- Terminal=false\n\
- StartupNotify=true\n\
- StartupWMClass=ai.storyteller.filmcraft\n\
- Categories=AudioVideo;Video;AudioVideoEditing;\n\
- Keywords=video;editor;film;timeline;color;grading;nle;\n"
- );
- }
-
- /// A path with shell-reserved characters stays one `Exec` argument.
+ assert_eq!(body, expected);
+ }
+
+ /// Both layers are escaped, like install.sh: shell first (`` ` `` `"` `$` `\` get a
+ /// backslash), then *every* backslash doubled for the file value — including the escape
+ /// backslashes just added.
#[test]
- fn exec_quotes_shell_reserved_paths() {
- assert_eq!(quote("/opt/film craft/filmcraft"), "\"/opt/film craft/filmcraft\"");
- assert_eq!(quote("/bin/filmcraft"), "/bin/filmcraft");
- assert_eq!(quote("/a$b\"c\\d"), "\"/a\\$b\\\"c\\\\d\"");
+ fn exec_escapes_both_desktop_entry_layers() {
+ assert_eq!(escape_exec_argument("/opt/film craft/filmcraft"), "/opt/film craft/filmcraft");
+ // a\b → shell a\\b → string a\\\\b
+ assert_eq!(escape_exec_argument("a\\b"), "a\\\\\\\\b");
+ // a$b → shell a\$b → string a\\$b
+ assert_eq!(escape_exec_argument("a$b"), "a\\\\$b");
}
- /// Only an `Exec` line that contains our binary marks the entry as ours — a packaged
- /// `Exec=filmcraft` or another checkout's path does not.
+ /// A written entry reads back as the exact binary it was written for, through both layers.
+ #[test]
+ fn exec_round_trips_through_both_layers() {
+ for exe in ["/home/dev/Projects/filmcraft/target/debug/filmcraft", "/opt/film craft/filmcraft", "/a$b\"c`d\\e"] {
+ let body = desktop_file_content("ai.storyteller.filmcraft", "FilmCraft", exe);
+ assert_eq!(exec_line(&body).as_deref(), Some(exe));
+ assert!(desktop_file_exec_is_ours(&body, exe));
+ }
+ }
+
+ /// Only the exact executable counts: another checkout's path, a path with our path as a
+ /// prefix, and a packaged `Exec=filmcraft` are all someone else's entry.
#[test]
fn desktop_file_exec_is_ours_matches_only_our_binary() {
let body = desktop_file_content("ai.storyteller.filmcraft", "FilmCraft", "/a/filmcraft");
- assert!(desktop_file_exec_is_ours(&body, "/a/filmcraft"));
+ assert!(!desktop_file_exec_is_ours(&body, "/a/filmcraft-old"));
assert!(!desktop_file_exec_is_ours(&body, "/b/filmcraft"));
assert!(!desktop_file_exec_is_ours("Exec=filmcraft %F\n", "/a/filmcraft"));
assert!(!desktop_file_exec_is_ours("Name=filmcraft\n", "/a/filmcraft"));
+ assert!(!desktop_file_exec_is_ours("Exec=\"dangling\n", "/a/filmcraft"));
+ }
+
+ /// The string layer is undone before the argument is split: `\s` becomes a space, which
+ /// ends a bare argument (the shell layer's quoting, not `\s`, is how a space travels), and
+ /// `\\` is one backslash. install.sh always quotes, so a space in our own `Exec` never
+ /// splits.
+ #[test]
+ fn exec_reads_the_string_layer() {
+ assert_eq!(exec_line("Exec=/a\\sb %F\n").as_deref(), Some("/a"));
+ assert_eq!(exec_line("Exec=\"/a\\sb\" %F\n").as_deref(), Some("/a b"));
+ assert_eq!(exec_line("Exec=/a\\\\b %F\n").as_deref(), Some("/a\\b"));
}
}
diff --git a/docs/contributing.md b/docs/contributing.md
index 570259e19..c52729bbf 100644
--- a/docs/contributing.md
+++ b/docs/contributing.md
@@ -40,6 +40,7 @@ cargo xtask web --serve 8765 # the web app on http://12
|---|---|
| `FILMCRAFT_CONTROL_PORT` | Same as `--control <port>` |
| `FILMCRAFT_CPU_COMPOSITE=1` | Disables the GPU compositor |
+| `FILMCRAFT_DEV_DESKTOP_ENTRY=1` | A release run from a source tree installs the user-level desktop entry for the taskbar icon (a debug build always does; packaged runs never) |
| `FILMCRAFT_DATA_DIR` | Same as `--data-dir <dir>`: auto-save, crash recovery, settings and logs |
| `RUST_LOG` | Log levels for standard error and the log file (see [Logs](#logs)) |