Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: open recording checkpoint files with Windows flush access

Richie McIlroyf3d65638916a
diff --git a/crates/enc-ffmpeg/src/file_sync.rs b/crates/enc-ffmpeg/src/file_sync.rs
new file mode 100644
index 000000000..457770ef7
--- /dev/null
+++ b/crates/enc-ffmpeg/src/file_sync.rs
@@ -0,0 +1,39 @@
+use std::{fs::OpenOptions, io, path::Path};
+
+pub fn sync_media_file(path: &Path) -> io::Result<()> {
+ // Windows FlushFileBuffers requires GENERIC_WRITE; Unix also permits read-only handles.
+ OpenOptions::new()
+ .read(true)
+ .write(cfg!(windows))
+ .open(path)?
+ .sync_all()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn existing_media_flush_preserves_bytes() {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("segment.m4s");
+ let bytes = b"completed fragment bytes must remain unchanged";
+ std::fs::write(&path, bytes).unwrap();
+
+ sync_media_file(&path).unwrap();
+
+ assert_eq!(std::fs::read(&path).unwrap(), bytes);
+ }
+
+ #[test]
+ fn missing_media_flush_does_not_create_a_file() {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("missing.m4s");
+
+ assert_eq!(
+ sync_media_file(&path).unwrap_err().kind(),
+ io::ErrorKind::NotFound
+ );
+ assert!(!path.exists());
+ }
+}
diff --git a/crates/enc-ffmpeg/src/lib.rs b/crates/enc-ffmpeg/src/lib.rs
index ff9246182..99bcf2bcd 100644
--- a/crates/enc-ffmpeg/src/lib.rs
+++ b/crates/enc-ffmpeg/src/lib.rs
@@ -1,5 +1,8 @@
mod base;
+mod file_sync;
+pub use file_sync::sync_media_file;
+
mod audio;
pub use audio::*;
diff --git a/crates/enc-ffmpeg/src/mux/dash_audio.rs b/crates/enc-ffmpeg/src/mux/dash_audio.rs
index 9ee89ce28..b3dfcc007 100644
--- a/crates/enc-ffmpeg/src/mux/dash_audio.rs
+++ b/crates/enc-ffmpeg/src/mux/dash_audio.rs
@@ -39,9 +39,7 @@ fn atomic_write_json<T: Serialize>(path: &Path, data: &T) -> std::io::Result<()>
}
fn sync_file(path: &Path) {
- if let Ok(file) = std::fs::File::open(path)
- && let Err(e) = file.sync_all()
- {
+ if let Err(e) = crate::sync_media_file(path) {
tracing::warn!("File fsync failed for {}: {e}", path.display());
}
}
diff --git a/crates/enc-ffmpeg/src/mux/fragment_manifest.rs b/crates/enc-ffmpeg/src/mux/fragment_manifest.rs
index e78951ffe..4f25236ae 100644
--- a/crates/enc-ffmpeg/src/mux/fragment_manifest.rs
+++ b/crates/enc-ffmpeg/src/mux/fragment_manifest.rs
@@ -38,9 +38,7 @@ fn atomic_write_json<T: Serialize>(path: &Path, data: &T) -> std::io::Result<()>
}
fn sync_file(path: &Path) {
- if let Ok(file) = std::fs::File::open(path)
- && let Err(e) = file.sync_all()
- {
+ if let Err(e) = crate::sync_media_file(path) {
tracing::warn!("File fsync failed for {}: {e}", path.display());
}
}
diff --git a/crates/enc-ffmpeg/src/mux/segmented_audio.rs b/crates/enc-ffmpeg/src/mux/segmented_audio.rs
index e24a8ff05..18d155d87 100644
--- a/crates/enc-ffmpeg/src/mux/segmented_audio.rs
+++ b/crates/enc-ffmpeg/src/mux/segmented_audio.rs
@@ -33,9 +33,7 @@ fn atomic_write_json<T: Serialize>(path: &Path, data: &T) -> std::io::Result<()>
}
fn sync_file(path: &Path) {
- if let Ok(file) = std::fs::File::open(path)
- && let Err(e) = file.sync_all()
- {
+ if let Err(e) = crate::sync_media_file(path) {
tracing::warn!("File fsync failed for {}: {e}", path.display());
}
}
diff --git a/crates/enc-ffmpeg/src/mux/segmented_stream.rs b/crates/enc-ffmpeg/src/mux/segmented_stream.rs
index 5de5af88e..3b6091ceb 100644
--- a/crates/enc-ffmpeg/src/mux/segmented_stream.rs
+++ b/crates/enc-ffmpeg/src/mux/segmented_stream.rs
@@ -51,9 +51,7 @@ fn atomic_write_json<T: Serialize>(path: &Path, data: &T) -> std::io::Result<()>
}
fn sync_file(path: &Path) {
- if let Ok(file) = std::fs::File::open(path)
- && let Err(e) = file.sync_all()
- {
+ if let Err(e) = crate::sync_media_file(path) {
tracing::warn!("File fsync failed for {}: {e}", path.display());
}
}
diff --git a/crates/recording/src/fragmentation/mod.rs b/crates/recording/src/fragmentation/mod.rs
index b2cf2e51a..b56c5459e 100644
--- a/crates/recording/src/fragmentation/mod.rs
+++ b/crates/recording/src/fragmentation/mod.rs
@@ -29,8 +29,8 @@ pub fn atomic_write_json<T: Serialize>(path: &Path, data: &T) -> std::io::Result
}
pub fn sync_file(path: &Path) {
- if let Ok(file) = std::fs::File::open(path) {
- let _ = file.sync_all();
+ if let Err(error) = cap_enc_ffmpeg::sync_media_file(path) {
+ tracing::warn!(%error, path = %path.display(), "File fsync failed");
}
}