[pbs-devel] [PATCH v2 backup-qemu 5/5] read_image_at: iterate until buffer is filled
Stefan Reiter
s.reiter at proxmox.com
Wed Jul 22 15:56:25 CEST 2020
QEMU will always assume EOF when less bytes than requested are returned
by a block drivers 'read' interface, so we need to fill the buffer up to
'size' if possible.
Signed-off-by: Stefan Reiter <s.reiter at proxmox.com>
---
v2 note: this was previously a QEMU patch, but honestly that's stupid so let's
do it in Rust instead.
current-api.h | 4 ++--
src/lib.rs | 4 ++--
src/restore.rs | 20 ++++++++++++++++----
3 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/current-api.h b/current-api.h
index 15bb275..d77eff6 100644
--- a/current-api.h
+++ b/current-api.h
@@ -364,8 +364,8 @@ int proxmox_restore_read_image_at(ProxmoxRestoreHandle *handle,
* Note: The data pointer needs to be valid until the async
* opteration is finished.
*
- * Note: It is not an error for a successful call to transfer fewer
- * bytes than requested.
+ * Note: The call will only ever transfer less than 'size' bytes if
+ * the end of the file has been reached.
*/
void proxmox_restore_read_image_at_async(ProxmoxRestoreHandle *handle,
uint8_t aid,
diff --git a/src/lib.rs b/src/lib.rs
index d4b9370..3346be8 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -934,8 +934,8 @@ pub extern "C" fn proxmox_restore_read_image_at(
/// Note: The data pointer needs to be valid until the async
/// opteration is finished.
///
-/// Note: It is not an error for a successful call to transfer fewer
-/// bytes than requested.
+/// Note: The call will only ever transfer less than 'size' bytes if
+/// the end of the file has been reached.
#[no_mangle]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn proxmox_restore_read_image_at_async(
diff --git a/src/restore.rs b/src/restore.rs
index 2be0295..e43d040 100644
--- a/src/restore.rs
+++ b/src/restore.rs
@@ -262,10 +262,22 @@ impl RestoreTask {
}
let mut reader = reader.lock().await;
- reader.seek(SeekFrom::Start(offset)).await?;
- let buf: &mut [u8] = unsafe { std::slice::from_raw_parts_mut(data.0 as *mut u8, size as usize)};
- let bytes = reader.read(buf).await?;
- Ok(bytes.try_into()?)
+ let buf: &mut [u8] = unsafe { std::slice::from_raw_parts_mut(data.0 as *mut u8, size as usize)};
+ let mut read = 0;
+
+ while read < size {
+ reader.seek(SeekFrom::Start(offset + read)).await?;
+ let bytes = reader.read(&mut buf[read as usize..]).await?;
+
+ if bytes == 0 {
+ // EOF
+ break;
+ }
+
+ read += bytes as u64;
+ }
+
+ Ok(read.try_into()?)
}
}
--
2.20.1
More information about the pbs-devel
mailing list