Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Linux: Reveal never hands xdg-open a file (#7166) (#7168)

Michael Higgins4d9f163c6174
diff --git a/crates/engine/src/layer_menu_cmds.rs b/crates/engine/src/layer_menu_cmds.rs
index 8e033b244..ab2591229 100644
--- a/crates/engine/src/layer_menu_cmds.rs
+++ b/crates/engine/src/layer_menu_cmds.rs
@@ -387,10 +387,24 @@ pub(crate) fn reveal(path: &str, dry_run: bool) -> Result<Value> {
if dry_run {
return Ok(json!({"program": program, "args": args}));
}
+ ensure_folder(program, &args)?;
spawn(program, &args)?;
Ok(json!({"path": path}))
}
+/// `xdg-open` opens whatever it is given in its default application, so it only gets a folder: a
+/// linked path such as `tool.jar/a.png` would otherwise launch `tool.jar` instead of showing it.
+fn ensure_folder(program: &str, args: &[String]) -> Result<()> {
+ if program != "xdg-open" {
+ return Ok(());
+ }
+ match args.first() {
+ Some(dir) if std::path::Path::new(dir).is_dir() => Ok(()),
+ Some(dir) => Err(other(format!("{dir} is not a folder, so it can't be shown in the file manager"))),
+ None => Err(other("no folder to show in the file manager")),
+ }
+}
+
/// The file-manager invocation that selects `path` on this platform.
pub fn reveal_command(path: &str) -> Result<(&'static str, Vec<String>)> {
reveal_command_on(std::env::consts::OS, path)
diff --git a/crates/engine/src/layer_menu_cmds/tests.rs b/crates/engine/src/layer_menu_cmds/tests.rs
index 72ace605c..171034876 100644
--- a/crates/engine/src/layer_menu_cmds/tests.rs
+++ b/crates/engine/src/layer_menu_cmds/tests.rs
@@ -515,6 +515,26 @@ fn reveal_follows_linked_paths_like_the_link_and_passes_paths_as_paths() {
}
}
+/// `xdg-open` launches a file it is given, so Reveal hands it the link's folder only when that is a
+/// folder: a link stored as `tool.jar/a.png` names a file, not a folder, as its parent.
+#[test]
+fn reveal_on_linux_never_hands_xdg_open_a_file() {
+ let dir = std::env::temp_dir().join(format!("photocraft-reveal-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let file = dir.join("tool.jar");
+ std::fs::write(&file, b"not a folder").unwrap();
+ let (program, args) = reveal_command_on("linux", &file.join("a.png").to_string_lossy()).unwrap();
+ assert_eq!(args, vec![file.to_string_lossy().into_owned()]);
+ let err = ensure_folder(program, &args).unwrap_err();
+ assert!(err.to_string().contains("is not a folder"), "{err}");
+ let (program, args) = reveal_command_on("linux", &dir.join("a.png").to_string_lossy()).unwrap();
+ ensure_folder(program, &args).unwrap();
+ assert!(ensure_folder("xdg-open", &[]).is_err());
+ // Finder and Explorer select the file itself, whatever it is.
+ ensure_folder("open", &["-R".into(), "--".into(), file.to_string_lossy().into_owned()]).unwrap();
+ std::fs::remove_dir_all(&dir).unwrap();
+}
+
#[test]
fn mask_all_objects_masks_the_subject() {
let mut s = session(8, "rgb");