remove tmp file on error
This commit is contained in:
23
src/main.rs
23
src/main.rs
@@ -95,7 +95,7 @@ async fn transcode(src: &Path, dest: &Path) -> Result<()> {
|
||||
&gmake("flacparse")?,
|
||||
&gmake("flacdec")?,
|
||||
&resample,
|
||||
// `audioconvert` converts the bitdepth
|
||||
// `audioconvert` converts audio format, bitdepth, ...
|
||||
&gmake("audioconvert")?,
|
||||
encoder.upcast_ref(),
|
||||
&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()))?,
|
||||
)?;
|
||||
|
||||
rm_file_on_err(&tmp_dest, async {
|
||||
pipeline
|
||||
.set_state(gstreamer::State::Playing)
|
||||
.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)
|
||||
.context("Unable to set the pipeline to the `Null` state")?;
|
||||
|
||||
std::fs::rename(tmp_dest, dest)?;
|
||||
std::fs::rename(&tmp_dest, dest)?;
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user