diff --git a/crates/clawhdf5/src/edit/mod.rs b/crates/clawhdf5/src/edit/mod.rs index 66c59ad..455c77a 100644 --- a/crates/clawhdf5/src/edit/mod.rs +++ b/crates/clawhdf5/src/edit/mod.rs @@ -832,10 +832,23 @@ impl FileEditor { Ok(f) } - /// A reader over the held file itself for planning an edit (it shares - /// the file's lock, and is dropped before the edit writes). + /// A reader over the held file for planning an edit (dropped before the + /// edit writes). On Linux a new open file description through + /// `/proc/self/fd`: a mapping of a clone of the held descriptor would + /// share its `flock`, and a process forked meanwhile (any + /// `std::process::Command` on another thread) would briefly keep the + /// lock alive after the editor is dropped. Elsewhere a clone of the + /// held descriptor, which follows the file wherever its path goes. fn plan_reader(&self) -> Result { let dir = self.path.parent().map(Path::to_path_buf); + #[cfg(target_os = "linux")] + { + use std::os::fd::AsRawFd; + let proc = format!("/proc/self/fd/{}", self.file.as_raw_fd()); + if let Ok(f) = std::fs::File::open(proc) { + return File::from_std_file(f, dir); + } + } File::from_std_file(self.file.try_clone()?, dir) }