remove file if write to update file fails

This commit is contained in:
marin postma 2021-06-23 14:45:26 +02:00
parent f62779671b
commit b000ae7614
No known key found for this signature in database
GPG Key ID: 6088B7721C3E39F9

View File

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