Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: publish recovered prefixes without requiring hard links

Richie McIlroyadbb91fe7906
diff --git a/crates/recording/Cargo.toml b/crates/recording/Cargo.toml
index 20f858ac0..6ddb0dd60 100644
--- a/crates/recording/Cargo.toml
+++ b/crates/recording/Cargo.toml
@@ -90,6 +90,7 @@ windows = { workspace = true, features = [
"Win32_UI_WindowsAndMessaging",
"Win32_System_Performance",
"Win32_Storage_Xps",
+ "Win32_Storage_FileSystem",
] }
cap-d3d-adapter = { path = "../d3d-adapter" }
diff --git a/crates/recording/src/recovery.rs b/crates/recording/src/recovery.rs
index 281513fc4..85971fade 100644
--- a/crates/recording/src/recovery.rs
+++ b/crates/recording/src/recovery.rs
@@ -23,6 +23,7 @@ use tracing::{debug, warn};
use crate::output_pipeline::{HealthSender, PipelineHealthEvent, emit_health};
use crate::studio_recording::CompletedRecording;
+mod prefix;
mod preparing_observer;
mod preparing_projection;
#[cfg(unix)]
@@ -912,7 +913,7 @@ impl RecoveryManager {
}
output.sync_all()?;
drop(output);
- std::fs::hard_link(&temporary, destination)
+ prefix::publish(&temporary, destination)
})();
let _ = std::fs::remove_file(&temporary);
result
diff --git a/crates/recording/src/recovery/prefix.rs b/crates/recording/src/recovery/prefix.rs
new file mode 100644
index 000000000..2f523b557
--- /dev/null
+++ b/crates/recording/src/recovery/prefix.rs
@@ -0,0 +1,69 @@
+use std::{io, path::Path};
+
+#[cfg(any(target_os = "macos", target_os = "linux"))]
+pub(super) fn publish(source: &Path, destination: &Path) -> io::Result<()> {
+ use std::{ffi::CString, os::unix::ffi::OsStrExt};
+
+ let source = CString::new(source.as_os_str().as_bytes())?;
+ let destination = CString::new(destination.as_os_str().as_bytes())?;
+ #[cfg(target_os = "macos")]
+ let result =
+ unsafe { libc::renamex_np(source.as_ptr(), destination.as_ptr(), libc::RENAME_EXCL) };
+ #[cfg(target_os = "linux")]
+ let result = unsafe {
+ libc::renameat2(
+ libc::AT_FDCWD,
+ source.as_ptr(),
+ libc::AT_FDCWD,
+ destination.as_ptr(),
+ libc::RENAME_NOREPLACE,
+ )
+ };
+ if result == 0 {
+ Ok(())
+ } else {
+ Err(io::Error::last_os_error())
+ }
+}
+
+#[cfg(windows)]
+pub(super) fn publish(source: &Path, destination: &Path) -> io::Result<()> {
+ use std::os::windows::ffi::OsStrExt;
+ use windows::{Win32::Storage::FileSystem::MoveFileW, core::PCWSTR};
+
+ let source: Vec<_> = source.as_os_str().encode_wide().chain(Some(0)).collect();
+ let destination: Vec<_> = destination
+ .as_os_str()
+ .encode_wide()
+ .chain(Some(0))
+ .collect();
+ if source[..source.len() - 1].contains(&0) || destination[..destination.len() - 1].contains(&0)
+ {
+ return Err(io::Error::new(
+ io::ErrorKind::InvalidInput,
+ "Path contains a null character",
+ ));
+ }
+ unsafe { MoveFileW(PCWSTR(source.as_ptr()), PCWSTR(destination.as_ptr())) }
+ .map_err(|_| io::Error::last_os_error())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn publication_moves_the_complete_file_without_replacing_existing_data() {
+ let directory = tempfile::tempdir().unwrap();
+ let source = directory.path().join("temporary");
+ let destination = directory.path().join("segment_001.m4s");
+ std::fs::write(&source, b"complete prefix").unwrap();
+ publish(&source, &destination).unwrap();
+ assert!(!source.exists());
+ assert_eq!(std::fs::read(&destination).unwrap(), b"complete prefix");
+ std::fs::write(&source, b"replacement").unwrap();
+ assert!(publish(&source, &destination).is_err());
+ assert_eq!(std::fs::read(&source).unwrap(), b"replacement");
+ assert_eq!(std::fs::read(&destination).unwrap(), b"complete prefix");
+ }
+}