2021-03-29 08:09:49 +00:00
|
|
|
use std::io::Read;
|
2020-12-06 08:41:16 +00:00
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use proxmox_io::ReadExt;
|
2021-03-29 08:09:49 +00:00
|
|
|
|
2021-09-13 09:54:24 +00:00
|
|
|
use crate::{BlockRead, BlockReadError, PROXMOX_TAPE_BLOCK_SIZE};
|
2020-12-06 08:41:16 +00:00
|
|
|
|
|
|
|
/// Emulate tape read behavior on a normal Reader
|
|
|
|
///
|
|
|
|
/// Tapes reads are always return one whole block PROXMOX_TAPE_BLOCK_SIZE.
|
2021-03-29 08:09:49 +00:00
|
|
|
pub struct EmulateTapeReader<R: Read> {
|
2020-12-06 08:41:16 +00:00
|
|
|
reader: R,
|
2021-03-29 08:09:49 +00:00
|
|
|
got_eof: bool,
|
2020-12-06 08:41:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 15:00:29 +00:00
|
|
|
impl<R: Read> EmulateTapeReader<R> {
|
2020-12-06 08:41:16 +00:00
|
|
|
pub fn new(reader: R) -> Self {
|
2022-04-06 15:00:29 +00:00
|
|
|
Self {
|
|
|
|
reader,
|
|
|
|
got_eof: false,
|
|
|
|
}
|
2020-12-06 08:41:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 15:00:29 +00:00
|
|
|
impl<R: Read> BlockRead for EmulateTapeReader<R> {
|
2021-04-12 09:25:40 +00:00
|
|
|
fn read_block(&mut self, buffer: &mut [u8]) -> Result<usize, BlockReadError> {
|
2021-03-29 08:09:49 +00:00
|
|
|
if self.got_eof {
|
2022-04-06 15:00:29 +00:00
|
|
|
return Err(BlockReadError::Error(proxmox_lang::io_format_err!(
|
|
|
|
"detected read after EOF!"
|
|
|
|
)));
|
2021-03-29 08:09:49 +00:00
|
|
|
}
|
|
|
|
match self.reader.read_exact_or_eof(buffer)? {
|
|
|
|
false => {
|
|
|
|
self.got_eof = true;
|
2021-04-12 09:25:40 +00:00
|
|
|
Err(BlockReadError::EndOfFile)
|
2021-03-29 08:09:49 +00:00
|
|
|
}
|
|
|
|
true => {
|
|
|
|
// test buffer len after EOF test (to allow EOF test with small buffers in BufferedReader)
|
|
|
|
if buffer.len() != PROXMOX_TAPE_BLOCK_SIZE {
|
2022-04-06 15:00:29 +00:00
|
|
|
return Err(BlockReadError::Error(proxmox_lang::io_format_err!(
|
|
|
|
"EmulateTapeReader: read_block with wrong block size ({} != {})",
|
|
|
|
buffer.len(),
|
|
|
|
PROXMOX_TAPE_BLOCK_SIZE,
|
|
|
|
)));
|
2020-12-06 08:41:16 +00:00
|
|
|
}
|
2021-04-12 09:25:40 +00:00
|
|
|
Ok(buffer.len())
|
2020-12-06 08:41:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|