hlssink3: Write playlists atomically

We want to try to ensure that playlist files are written completely in a
single shot, to avoid the possibility of serving up a patially written
playlist.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/2098>
This commit is contained in:
eipachte 2025-02-18 21:19:05 +02:00 committed by GStreamer Marge Bot
parent 9453e021dc
commit 1b866222eb

View file

@ -510,19 +510,30 @@ impl HlsBaseSink {
where
P: AsRef<path::Path>,
{
let file = fs::File::create(location).map_err(move |err| {
let error_msg = gst::error_msg!(
gst::ResourceError::OpenWrite,
[
"Could not open file {} for writing: {}",
location.as_ref().to_str().unwrap(),
err.to_string(),
]
);
self.post_error_message(error_msg);
err.to_string()
})?;
Ok(gio::WriteOutputStream::new(file).upcast())
let file = gio::File::for_path(location);
// Open the file for writing, creating it if it doesn't exist
// Use replace() to write the content in atomic mode
// (writes to a temporary file and then atomically rename over the destination when the stream is closed)
let output_stream = file
.replace(
None,
false,
gio::FileCreateFlags::empty(),
None::<&gio::Cancellable>,
)
.map_err(move |err| {
let error_msg = gst::error_msg!(
gst::ResourceError::OpenWrite,
[
"Could not open file {} for writing: {}",
location.as_ref().to_str().unwrap(),
err.to_string(),
]
);
self.post_error_message(error_msg);
err.to_string()
})?;
Ok(output_stream.upcast())
}
fn delete_fragment<P>(&self, location: &P)