remove file if write to update file fails
@@ -122,7 +122,7 @@ where
&self,
uuid: Uuid,
meta: UpdateMeta,
- mut payload: mpsc::Receiver<PayloadData<D>>,
+ payload: mpsc::Receiver<PayloadData<D>>,
) -> Result<UpdateStatus> {
let file_path = match meta {
UpdateMeta::DocumentsAddition { .. } => {
@@ -137,23 +137,37 @@ where
.open(&path)
.await?;
+ async fn write_to_file<D>(file: &mut fs::File, mut payload: mpsc::Receiver<PayloadData<D>>) -> Result<usize>
+ where
+ D: AsRef<[u8]> + Sized + 'static,
+ {
let mut file_len = 0;
while let Some(bytes) = payload.recv().await {
let bytes = bytes?;
file_len += bytes.as_ref().len();
file.write_all(bytes.as_ref()).await?;
}
+ Ok(file_len)
+ }
+
+ let file_len = write_to_file(&mut file, payload).await;
- if file_len != 0 {
+ match file_len {
+ Ok(len) if len > 0 => {
file.flush().await?;
let file = file.into_std().await;
Some((file, update_file_id))
- } else {
- // empty update, delete the empty file.
+ }
+ Err(e) => {
+ fs::remove_file(&path).await?;
+ return Err(e)
+ }
+ _ => {
fs::remove_file(&path).await?;
None
}
}
+ }
_ => None,
};
