Skip to content

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

Merge PR #7242: A second launch that sets only environment overrides keeps its own process

aionggc50708ea0870
diff --cc apps/photocraft/src/single_instance.rs
index 4b57fa694,ad893bd58..953da2074
--- a/apps/photocraft/src/single_instance.rs
+++ b/apps/photocraft/src/single_instance.rs
@@@ -841,78 -707,45 +902,120 @@@ mod tests
assert!(reason.is_some_and(|r| r.contains("--safe-gpu")));
}
+ /// #1540: inside a Flatpak sandbox the hand-off runs over a Unix socket in the app's runtime
+ /// directory, which every sandbox of the app shares, with the same lock, token and reply.
+ #[cfg(unix)]
+ #[test]
+ fn a_sandboxed_second_launch_hands_its_paths_over_a_unix_socket() {
+ use std::os::unix::fs::PermissionsExt;
+ let dir = temp_dir("socket");
+ // Short: a Unix socket path is limited to about 100 bytes (macOS: 104).
+ let socket = short_socket_path("a");
+ let transport = Transport::Socket(socket.clone());
+ let primary = match claim_in(&dir, &[], FAST, &transport) {
+ Launch::Primary(p) => p,
+ _ => panic!("first launch should be primary"),
+ };
+ assert_eq!(std::fs::metadata(&socket).unwrap().permissions().mode() & 0o777, 0o600);
+ let info: Value = serde_json::from_slice(&std::fs::read(dir.join(INFO_FILE)).unwrap()).unwrap();
+ assert_eq!(info.get("socket").and_then(Value::as_str), Some(socket.to_str().unwrap()));
+ assert!(info.get("port").is_none());
+ let mut ui = primary.connect(&egui::Context::default());
+ let paths = ["/home/a/one.psd".to_string()];
+ let (d, p, t) = (dir.clone(), paths.clone(), transport.clone());
+ let second = std::thread::spawn(move || claim_in(&d, &p, TIMING, &t));
+ assert_eq!(drain(&mut ui, 1), vec![OsEvent::Open(paths.to_vec())]);
+ assert!(matches!(second.join().unwrap(), Launch::Forwarded));
+ // The token still gates the socket.
+ let (endpoint, _) = read_info(&dir).unwrap();
+ assert!(send(&endpoint, "not the token", &["x.psd".into()], Instant::now() + FAST.handoff).is_err());
+ primary.shutdown();
+ assert!(!socket.exists(), "shutdown removes the socket file");
+ // The next primary binds the same path; the old one's drop (a second shutdown) must leave it.
+ let next = match claim_in(&dir, &[], FAST, &transport) {
+ Launch::Primary(p) => p,
+ _ => panic!("the lock is free again"),
+ };
+ drop(primary);
+ assert!(socket.exists(), "a second shutdown doesn't remove the next primary's socket");
+ drop(next);
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ /// A socket file left by a crashed primary (and a stale `instance.json` naming it) is replaced.
+ #[cfg(unix)]
+ #[test]
+ fn a_crashed_primary_s_socket_file_does_not_block_the_next_one() {
+ use std::os::unix::fs::FileTypeExt;
+ let dir = temp_dir("stale-socket");
+ let socket = short_socket_path("b");
+ std::fs::create_dir_all(&dir).unwrap();
+ std::fs::create_dir_all(socket.parent().unwrap()).unwrap();
+ std::fs::write(&socket, b"not a socket").unwrap();
+ write_info(&dir, &Endpoint::Socket(socket.clone()), &generate_token().unwrap()).unwrap();
+ let primary = match claim_in(&dir, &["a.psd".into()], FAST, &Transport::Socket(socket.clone())) {
+ Launch::Primary(p) => p,
+ Launch::Forwarded => panic!("nothing was listening"),
+ Launch::Standalone(e) => panic!("{e}"),
+ };
+ assert!(std::fs::metadata(&socket).unwrap().file_type().is_socket());
+ drop(primary);
+ let _ = std::fs::remove_dir_all(&dir);
+ let _ = std::fs::remove_dir_all(socket.parent().unwrap());
+ }
+
+ /// The socket transport is chosen only inside a Flatpak with a runtime directory.
+ #[cfg(unix)]
+ #[test]
+ fn the_socket_transport_needs_a_flatpak_id_and_a_runtime_directory() {
+ use std::ffi::OsStr;
+ let (id, run) = (OsStr::new("ai.storyteller.photocraft"), OsStr::new("/run/user/1000"));
+ assert_eq!(sandbox_socket(Some(id), Some(run)), Some(PathBuf::from("/run/user/1000/app/ai.storyteller.photocraft/instance.sock")));
+ assert_eq!(sandbox_socket(None, Some(run)), None);
+ assert_eq!(sandbox_socket(Some(id), None), None);
+ assert_eq!(sandbox_socket(Some(OsStr::new("")), Some(run)), None);
+ assert_eq!(sandbox_socket(Some(id), Some(OsStr::new(""))), None);
+ }
++
+ /// A variable documented as the equivalent of a per-launch flag counts as that flag: a launch
+ /// that sets only variables has per-process options, so it keeps its own process rather than
+ /// handing its files to the running instance, which never sees the override.
+ #[test]
+ fn an_environment_override_counts_as_a_process_option() {
+ let with = |values: &[(&str, &str)]| env_launch_options_from(|n| values.iter().find(|(k, _)| *k == n).map(|(_, v)| (*v).into()));
+ assert_eq!(with(&[("PHOTOCRAFT_PRESENT_MODE", "novsync")]), vec!["--present-mode"]);
+ assert_eq!(
+ with(&[("PHOTOCRAFT_STROKE_TRACE", "strokes.csv"), ("PHOTOCRAFT_AUTOMATION_WRITE_ROOT", "/tmp/out")]),
+ vec!["--stroke-trace", "--automation-write-root"]
+ );
+ // Nothing set, or a value that means off, is an ordinary launch. Each variable is off the way
+ // it is read where it takes effect, not by one rule shared across all six.
+ assert!(with(&[]).is_empty());
+ assert!(with(&[("PHOTOCRAFT_IN_WINDOW_MENUS", "0"), ("PHOTOCRAFT_FRAME_LATENCY", "")]).is_empty());
+ // The trace is off at `off` and `false` as well, and a value the presentation parser rejects
+ // is ignored there, so neither keeps a process of its own here.
+ assert!(with(&[("PHOTOCRAFT_STROKE_TRACE", "off"), ("PHOTOCRAFT_PRESENT_MODE", "maybe"), ("PHOTOCRAFT_FRAME_LATENCY", "4")]).is_empty());
+ assert!(with(&[("PHOTOCRAFT_AUTOMATION_READ_ROOT", "")]).is_empty());
+ // Spaces are part of the value only where the reader keeps them: the trace and the two
+ // presentation values are trimmed where they take effect, the menu flag and a path are not.
+ assert_eq!(with(&[("PHOTOCRAFT_PRESENT_MODE", " novsync "), ("PHOTOCRAFT_STROKE_TRACE", " 1 ")]), vec!["--present-mode", "--stroke-trace"]);
+ assert_eq!(
+ with(&[("PHOTOCRAFT_IN_WINDOW_MENUS", " 0 "), ("PHOTOCRAFT_AUTOMATION_WRITE_ROOT", " /tmp/out ")]),
+ vec!["--in-window-menus", "--automation-write-root"]
+ );
+ // A whitespace-only menu value is not empty, so it enables the menus there and here too.
+ assert_eq!(with(&[("PHOTOCRAFT_IN_WINDOW_MENUS", " ")]), vec!["--in-window-menus"]);
+ // `PHOTOCRAFT_IN_WINDOW_MENUS=off` means menus in the window there, so it means it here too.
+ assert_eq!(with(&[("PHOTOCRAFT_IN_WINDOW_MENUS", "off")]), vec!["--in-window-menus"]);
+ let base = LaunchContext {
+ per_user_settings: true,
+ new_instance_flag: false,
+ new_instance_env: None,
+ control: false,
+ unreadable_paths: false,
+ launch_options: &[],
+ };
+ let reason = skip_reason(&LaunchContext { launch_options: &with(&[("PHOTOCRAFT_PRESENT_MODE", "novsync")]), ..base });
+ assert!(reason.as_deref().is_some_and(|r| r.contains("--present-mode")), "{reason:?}");
+ }
}