Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(recording): retire the original media backup on Windows too

Richie McIlroy18a509870ddd
diff --git a/crates/recording/src/recovery.rs b/crates/recording/src/recovery.rs
index 96c7618b5..228473b70 100644
--- a/crates/recording/src/recovery.rs
+++ b/crates/recording/src/recovery.rs
@@ -27,13 +27,9 @@ use crate::studio_recording::CompletedRecording;
mod prefix;
mod preparing_observer;
mod preparing_projection;
-#[cfg(unix)]
mod retirement;
-#[cfg(unix)]
type PreparingRecoveryLock = retirement::RecoveryLease;
-#[cfg(not(unix))]
-type PreparingRecoveryLock = RecoveryLock;
pub use preparing_observer::{
LivePreparingStudioSources, PreparingAudioLease, PreparingAudioTrack, PreparingSidecarKind,
@@ -306,7 +302,6 @@ impl RecoveryManager {
RecoveryLock::acquire,
preparing,
)?;
- #[cfg(unix)]
if unread && let Some(workspace) = published_workspace.into_inner() {
let retired = RecoveryLock::acquire(project_path)
.and_then(|_lock| retirement::retire_unread_originals(project_path, &workspace));
@@ -314,8 +309,6 @@ impl RecoveryManager {
warn!(path = %workspace.display(), %error, "Original media backup retained");
}
}
- #[cfg(not(unix))]
- let _ = (unread, published_workspace);
Ok(true)
}
@@ -1404,7 +1397,6 @@ impl RecoveryManager {
elapsed_ms = started.elapsed().as_secs_f64() * 1000.0,
"Studio finalization complete"
);
- #[cfg(unix)]
if matches!(purpose, RecoveryPurpose::Finalize)
&& preparing_source.is_some()
&& let HeldRecoveryLock::Shared(lease) = &lock
@@ -3090,7 +3082,6 @@ enum HeldRecoveryLock {
impl HeldRecoveryLock {
fn new(lock: RecoveryLock, preparing: bool) -> Self {
if preparing {
- #[cfg(unix)]
let lock = retirement::RecoveryLease::new(lock);
Self::Shared(Arc::new(lock))
} else {
@@ -3211,6 +3202,21 @@ impl RecoveryLock {
}
}
+impl RecoveryLock {
+ /// Whether this process took the lock: on unix a forked child inherits the
+ /// descriptor but not the lock's ownership. Windows has no fork.
+ fn owned_by_this_process(&self) -> bool {
+ #[cfg(unix)]
+ {
+ self.owner_pid == unsafe { libc::getpid() }
+ }
+ #[cfg(not(unix))]
+ {
+ true
+ }
+ }
+}
+
#[cfg(unix)]
impl Drop for RecoveryLock {
fn drop(&mut self) {
@@ -4850,7 +4856,6 @@ mod clean_studio_snapshot_tests {
assert_preparing_entry_point_matches_ordinary(PreparingEntryCase::Supported);
}
- #[cfg(unix)]
#[test]
fn preparing_retirement_preserves_every_encoded_video_packet() {
fn packets(path: &Path) -> Vec<Vec<u8>> {
@@ -5099,7 +5104,6 @@ mod clean_studio_snapshot_tests {
assert!(RecoveryLock::acquire(project).is_ok());
}
- #[cfg(unix)]
#[test]
fn a_stop_without_a_preparing_editor_retires_the_original_media_backup() {
let directory = playable_studio_project(StudioRecordingStatus::NeedsRemux);
@@ -5241,8 +5245,8 @@ mod clean_studio_snapshot_tests {
})
.unwrap();
// A stop without a preparing editor retires the original backup once
- // the published tracks above are in place (unix only, for now).
- assert_eq!(backup.join("original-segments").exists(), !cfg!(unix));
+ // the published tracks above are in place.
+ assert!(!backup.join("original-segments").exists());
}
#[test]
diff --git a/crates/recording/src/recovery/retirement.rs b/crates/recording/src/recovery/retirement.rs
index 065cc4826..46af50778 100644
--- a/crates/recording/src/recovery/retirement.rs
+++ b/crates/recording/src/recovery/retirement.rs
@@ -1,5 +1,7 @@
+#[cfg(unix)]
+use std::fs::File;
use std::{
- fs::{self, File},
+ fs,
path::{Path, PathBuf},
sync::Mutex,
};
@@ -35,14 +37,7 @@ impl RecoveryLease {
receipt: read_recovery_publication(&workspace.join(RECOVERY_PUBLICATION_RECEIPT))?,
};
retirement.validate()?;
- sync_directories(&project.join("content/segments"))?;
- for directory in [
- project.join("content"),
- project.to_path_buf(),
- workspace.to_path_buf(),
- ] {
- File::open(directory)?.sync_all()?;
- }
+ sync_publication(project, workspace)?;
*self.retirement.lock().map_err(|_| {
RecoveryError::Validation("Original media retirement lease poisoned".into())
})? = Some(retirement);
@@ -55,7 +50,7 @@ impl Drop for RecoveryLease {
if self
.lock
.as_ref()
- .is_none_or(|lock| lock.owner_pid != unsafe { libc::getpid() })
+ .is_none_or(|lock| !lock.owned_by_this_process())
{
return;
}
@@ -90,15 +85,22 @@ pub(super) fn retire_unread_originals(
receipt: read_recovery_publication(&workspace.join(RECOVERY_PUBLICATION_RECEIPT))?,
};
retirement.validate()?;
+ sync_publication(project, workspace)?;
+ retirement.remove_originals()
+}
+
+/// The durability barrier before a backup goes: the published tree and the
+/// directories its renames touched.
+fn sync_publication(project: &Path, workspace: &Path) -> Result<(), RecoveryError> {
sync_directories(&project.join("content/segments"))?;
for directory in [
project.join("content"),
project.to_path_buf(),
workspace.to_path_buf(),
] {
- File::open(directory)?.sync_all()?;
+ sync_directory(&directory)?;
}
- retirement.remove_originals()
+ Ok(())
}
struct OriginalMediaRetirement {
@@ -149,8 +151,8 @@ impl OriginalMediaRetirement {
fn remove_originals(&self) -> Result<(), RecoveryError> {
self.validate()?;
- fs::remove_dir_all(self.workspace.join("original-segments"))?;
- File::open(&self.workspace)?.sync_all()?;
+ remove_backup(&self.workspace.join("original-segments"))?;
+ sync_directory(&self.workspace)?;
tracing::info!(path = %self.workspace.display(), "Retired original media after final preparing reader closed");
Ok(())
}
@@ -166,10 +168,50 @@ fn sync_directories(path: &Path) -> Result<(), RecoveryError> {
sync_directories(&entry.path())?;
}
}
- File::open(path)?.sync_all()?;
+ sync_directory(path)?;
Ok(())
}
+fn sync_directory(path: &Path) -> std::io::Result<()> {
+ #[cfg(unix)]
+ {
+ File::open(path)?.sync_all()
+ }
+ // A directory can't be opened as a plain file on Windows. NTFS journals the
+ // renames that published the recording, and the published files themselves
+ // were flushed before publication.
+ #[cfg(not(unix))]
+ {
+ let _ = path;
+ Ok(())
+ }
+}
+
+fn remove_backup(path: &Path) -> std::io::Result<()> {
+ #[cfg(windows)]
+ {
+ // Antivirus and the search indexer briefly open new files without
+ // delete sharing; give them a moment rather than keeping the backup.
+ let mut attempt = 0;
+ loop {
+ match fs::remove_dir_all(path) {
+ Ok(()) => return Ok(()),
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
+ Err(error) if attempt < 4 => {
+ attempt += 1;
+ tracing::debug!(%error, attempt, "Original media backup busy; retrying removal");
+ std::thread::sleep(std::time::Duration::from_millis(100));
+ }
+ Err(error) => return Err(error),
+ }
+ }
+ }
+ #[cfg(not(windows))]
+ {
+ fs::remove_dir_all(path)
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -307,6 +349,7 @@ mod tests {
);
}
+ #[cfg(unix)]
#[test]
fn replacing_backup_with_a_link_preserves_the_link_target() {
let (_temporary, project, workspace) = fixture();