remove tmp file on error

This commit is contained in:
2020-03-14 14:03:31 +01:00
parent d84145b91f
commit f7f6625585

View File

@@ -95,7 +95,7 @@ async fn transcode(src: &Path, dest: &Path) -> Result<()> {
&gmake("flacparse")?, &gmake("flacparse")?,
&gmake("flacdec")?, &gmake("flacdec")?,
&resample, &resample,
// `audioconvert` converts the bitdepth // `audioconvert` converts audio format, bitdepth, ...
&gmake("audioconvert")?, &gmake("audioconvert")?,
encoder.upcast_ref(), encoder.upcast_ref(),
&gmake("oggmux")?, &gmake("oggmux")?,
@@ -114,6 +114,7 @@ async fn transcode(src: &Path, dest: &Path) -> Result<()> {
.with_context(|| format!("could not get parent dir for {}", dest.display()))?, .with_context(|| format!("could not get parent dir for {}", dest.display()))?,
)?; )?;
rm_file_on_err(&tmp_dest, async {
pipeline pipeline
.set_state(gstreamer::State::Playing) .set_state(gstreamer::State::Playing)
.context("Unable to set the pipeline to the `Playing` state")?; .context("Unable to set the pipeline to the `Playing` state")?;
@@ -142,7 +143,25 @@ async fn transcode(src: &Path, dest: &Path) -> Result<()> {
.set_state(gstreamer::State::Null) .set_state(gstreamer::State::Null)
.context("Unable to set the pipeline to the `Null` state")?; .context("Unable to set the pipeline to the `Null` state")?;
std::fs::rename(tmp_dest, dest)?; std::fs::rename(&tmp_dest, dest)?;
Ok(()) Ok(())
})
.await
}
async fn rm_file_on_err<F, T>(path: &Path, f: F) -> F::Output
where
F: Future<Output = Result<T>>,
{
match f.await {
Err(err) => match std::fs::remove_file(path) {
Ok(..) => Err(err),
Err(rm_err) if rm_err.kind() == std::io::ErrorKind::NotFound => Err(err),
Err(rm_err) => Err(rm_err)
.context(format!("removing {}", path.display()))
.context(err),
},
res @ Ok(..) => res,
}
} }