Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: support filesystems without exclusive rename operations

Richie McIlroy0e7deeaf88d7
diff --git a/crates/recording/src/recovery/prefix.rs b/crates/recording/src/recovery/prefix.rs
index 2f523b557..8a09e9ad6 100644
--- a/crates/recording/src/recovery/prefix.rs
+++ b/crates/recording/src/recovery/prefix.rs
@@ -4,25 +4,38 @@ use std::{io, path::Path};
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())?;
+ let source_name = CString::new(source.as_os_str().as_bytes())?;
+ let destination_name = 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) };
+ let result = unsafe {
+ libc::renamex_np(
+ source_name.as_ptr(),
+ destination_name.as_ptr(),
+ libc::RENAME_EXCL,
+ )
+ };
#[cfg(target_os = "linux")]
let result = unsafe {
libc::renameat2(
libc::AT_FDCWD,
- source.as_ptr(),
+ source_name.as_ptr(),
libc::AT_FDCWD,
- destination.as_ptr(),
+ destination_name.as_ptr(),
libc::RENAME_NOREPLACE,
)
};
if result == 0 {
Ok(())
} else {
- Err(io::Error::last_os_error())
+ let error = io::Error::last_os_error();
+ if error.kind() == io::ErrorKind::Unsupported
+ || matches!(error.raw_os_error(), Some(libc::ENOSYS | libc::EINVAL))
+ {
+ // Production recovery publishes the enclosing workspace only after all prefixes validate.
+ copy_without_links(source, destination)
+ } else {
+ Err(error)
+ }
}
}
@@ -48,22 +61,78 @@ pub(super) fn publish(source: &Path, destination: &Path) -> io::Result<()> {
.map_err(|_| io::Error::last_os_error())
}
+#[cfg(any(unix, test))]
+fn copy_without_links(source: &Path, destination: &Path) -> io::Result<()> {
+ let mut input = std::fs::File::open(source)?;
+ let length = input.metadata()?.len();
+ copy_to_new_file(&mut input, destination, length)
+}
+
+#[cfg(any(unix, test))]
+fn copy_to_new_file(input: &mut impl io::Read, destination: &Path, length: u64) -> io::Result<()> {
+ let mut output = std::fs::OpenOptions::new()
+ .write(true)
+ .create_new(true)
+ .open(destination)?;
+ let result = (|| {
+ if io::copy(input, &mut output)? != length {
+ return Err(io::Error::new(
+ io::ErrorKind::UnexpectedEof,
+ "Incomplete recovered prefix copy",
+ ));
+ }
+ output.sync_all()
+ })();
+ drop(output);
+ if result.is_err() {
+ let _ = std::fs::remove_file(destination);
+ }
+ result
+}
+
#[cfg(test)]
mod tests {
use super::*;
#[test]
- fn publication_moves_the_complete_file_without_replacing_existing_data() {
+ fn publication_keeps_complete_bytes_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());
+ if source.exists() {
+ assert_eq!(std::fs::read(&source).unwrap(), b"complete prefix");
+ }
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");
}
+
+ #[test]
+ fn fallback_copy_preserves_source_and_refuses_existing_files() {
+ 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();
+ copy_without_links(&source, &destination).unwrap();
+ assert_eq!(std::fs::read(&source).unwrap(), b"complete prefix");
+ assert_eq!(std::fs::read(&destination).unwrap(), b"complete prefix");
+ std::fs::write(&source, b"replacement").unwrap();
+ assert!(copy_without_links(&source, &destination).is_err());
+ assert_eq!(std::fs::read(&destination).unwrap(), b"complete prefix");
+ }
+
+ #[test]
+ fn failed_fallback_copy_removes_only_its_partial_file() {
+ let directory = tempfile::tempdir().unwrap();
+ let destination = directory.path().join("segment_001.m4s");
+ assert!(copy_to_new_file(&mut &b"short"[..], &destination, 100).is_err());
+ assert!(!destination.exists());
+ std::fs::write(&destination, b"existing").unwrap();
+ assert!(copy_to_new_file(&mut &b"short"[..], &destination, 100).is_err());
+ assert_eq!(std::fs::read(destination).unwrap(), b"existing");
+ }
}