2020-12-06 11:09:55 +00:00
|
|
|
use std::io::Read;
|
|
|
|
|
2021-09-13 09:54:24 +00:00
|
|
|
use crate::{
|
2020-12-06 11:09:55 +00:00
|
|
|
TapeRead,
|
2021-03-29 08:09:49 +00:00
|
|
|
BlockRead,
|
2021-04-12 09:25:40 +00:00
|
|
|
BlockReadError,
|
2021-09-13 09:54:24 +00:00
|
|
|
PROXMOX_TAPE_BLOCK_HEADER_MAGIC_1_0,
|
|
|
|
BlockHeader,
|
|
|
|
BlockHeaderFlags,
|
2020-12-06 11:09:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Read a block stream generated by 'BlockWriter'.
|
|
|
|
///
|
|
|
|
/// This class implements 'TapeRead'. It always read whole blocks from
|
|
|
|
/// the underlying reader, and does additional error checks:
|
|
|
|
///
|
|
|
|
/// - check magic number (detect streams not written by 'BlockWriter')
|
|
|
|
/// - check block size
|
|
|
|
/// - check block sequence numbers
|
|
|
|
///
|
|
|
|
/// The reader consumes the EOF mark after the data stream (if read to
|
|
|
|
/// the end of the stream).
|
|
|
|
pub struct BlockedReader<R> {
|
|
|
|
reader: R,
|
|
|
|
buffer: Box<BlockHeader>,
|
|
|
|
seq_nr: u32,
|
|
|
|
found_end_marker: bool,
|
|
|
|
incomplete: bool,
|
|
|
|
got_eod: bool,
|
|
|
|
read_error: bool,
|
|
|
|
read_pos: usize,
|
|
|
|
}
|
|
|
|
|
2021-03-29 08:09:49 +00:00
|
|
|
impl <R: BlockRead> BlockedReader<R> {
|
2020-12-06 11:09:55 +00:00
|
|
|
|
|
|
|
/// Create a new BlockedReader instance.
|
|
|
|
///
|
2021-04-12 09:25:40 +00:00
|
|
|
/// This tries to read the first block. Please inspect the error
|
|
|
|
/// to detect EOF and EOT.
|
|
|
|
pub fn open(mut reader: R) -> Result<Self, BlockReadError> {
|
2020-12-06 11:09:55 +00:00
|
|
|
|
|
|
|
let mut buffer = BlockHeader::new();
|
|
|
|
|
2021-04-12 09:25:40 +00:00
|
|
|
Self::read_block_frame(&mut buffer, &mut reader)?;
|
2020-12-06 11:09:55 +00:00
|
|
|
|
|
|
|
let (_size, found_end_marker) = Self::check_buffer(&buffer, 0)?;
|
|
|
|
|
|
|
|
let mut incomplete = false;
|
2021-02-03 12:15:18 +00:00
|
|
|
let mut got_eod = false;
|
|
|
|
|
2020-12-06 11:09:55 +00:00
|
|
|
if found_end_marker {
|
|
|
|
incomplete = buffer.flags.contains(BlockHeaderFlags::INCOMPLETE);
|
2021-02-03 12:15:18 +00:00
|
|
|
Self::consume_eof_marker(&mut reader)?;
|
|
|
|
got_eod = true;
|
2020-12-06 11:09:55 +00:00
|
|
|
}
|
2021-02-03 12:15:18 +00:00
|
|
|
|
2021-04-12 09:25:40 +00:00
|
|
|
Ok(Self {
|
2020-12-06 11:09:55 +00:00
|
|
|
reader,
|
|
|
|
buffer,
|
|
|
|
found_end_marker,
|
|
|
|
incomplete,
|
2021-02-03 12:15:18 +00:00
|
|
|
got_eod,
|
2020-12-06 11:09:55 +00:00
|
|
|
seq_nr: 1,
|
|
|
|
read_error: false,
|
|
|
|
read_pos: 0,
|
2021-04-12 09:25:40 +00:00
|
|
|
})
|
2020-12-06 11:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_buffer(buffer: &BlockHeader, seq_nr: u32) -> Result<(usize, bool), std::io::Error> {
|
|
|
|
|
|
|
|
if buffer.magic != PROXMOX_TAPE_BLOCK_HEADER_MAGIC_1_0 {
|
2022-02-21 10:39:18 +00:00
|
|
|
proxmox_lang::io_bail!("detected tape block with wrong magic number - not written by proxmox tape");
|
2020-12-06 11:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if seq_nr != buffer.seq_nr() {
|
2022-02-21 10:39:18 +00:00
|
|
|
proxmox_lang::io_bail!(
|
2021-03-10 15:37:09 +00:00
|
|
|
"detected tape block with wrong sequence number ({} != {})",
|
2020-12-06 11:09:55 +00:00
|
|
|
seq_nr, buffer.seq_nr())
|
|
|
|
}
|
|
|
|
|
|
|
|
let size = buffer.size();
|
|
|
|
let found_end_marker = buffer.flags.contains(BlockHeaderFlags::END_OF_STREAM);
|
|
|
|
|
|
|
|
if size > buffer.payload.len() {
|
2022-02-21 10:39:18 +00:00
|
|
|
proxmox_lang::io_bail!("detected tape block with wrong payload size ({} > {}", size, buffer.payload.len());
|
2021-01-19 10:06:26 +00:00
|
|
|
} else if size == 0 && !found_end_marker {
|
2022-02-21 10:39:18 +00:00
|
|
|
proxmox_lang::io_bail!("detected tape block with zero payload size");
|
2020-12-06 11:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok((size, found_end_marker))
|
|
|
|
}
|
|
|
|
|
2021-04-12 09:25:40 +00:00
|
|
|
fn read_block_frame(buffer: &mut BlockHeader, reader: &mut R) -> Result<(), BlockReadError> {
|
2020-12-06 11:09:55 +00:00
|
|
|
|
|
|
|
let data = unsafe {
|
|
|
|
std::slice::from_raw_parts_mut(
|
|
|
|
(buffer as *mut BlockHeader) as *mut u8,
|
|
|
|
BlockHeader::SIZE,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2021-04-12 09:25:40 +00:00
|
|
|
let bytes = reader.read_block(data)?;
|
|
|
|
|
|
|
|
if bytes != BlockHeader::SIZE {
|
2022-02-21 10:39:18 +00:00
|
|
|
return Err(proxmox_lang::io_format_err!("got wrong block size").into());
|
2021-03-29 08:09:49 +00:00
|
|
|
}
|
2021-04-12 09:25:40 +00:00
|
|
|
|
|
|
|
Ok(())
|
2020-12-06 11:09:55 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 12:15:18 +00:00
|
|
|
fn consume_eof_marker(reader: &mut R) -> Result<(), std::io::Error> {
|
|
|
|
let mut tmp_buf = [0u8; 512]; // use a small buffer for testing EOF
|
2021-03-29 08:09:49 +00:00
|
|
|
match reader.read_block(&mut tmp_buf) {
|
2021-04-12 09:25:40 +00:00
|
|
|
Ok(_) => {
|
2022-02-21 10:39:18 +00:00
|
|
|
proxmox_lang::io_bail!("detected tape block after block-stream end marker");
|
2021-03-29 08:09:49 +00:00
|
|
|
}
|
2021-04-12 09:25:40 +00:00
|
|
|
Err(BlockReadError::EndOfFile) => {
|
2022-02-08 13:57:16 +00:00
|
|
|
Ok(())
|
2021-03-29 08:09:49 +00:00
|
|
|
}
|
2021-04-12 09:25:40 +00:00
|
|
|
Err(BlockReadError::EndOfStream) => {
|
2022-02-21 10:39:18 +00:00
|
|
|
proxmox_lang::io_bail!("got unexpected end of tape");
|
2021-03-29 08:09:49 +00:00
|
|
|
}
|
2021-04-12 09:25:40 +00:00
|
|
|
Err(BlockReadError::Error(err)) => {
|
2022-02-08 13:57:16 +00:00
|
|
|
Err(err)
|
2021-03-29 08:09:49 +00:00
|
|
|
}
|
2021-02-03 12:15:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:11:44 +00:00
|
|
|
fn read_block(&mut self, check_end_marker: bool) -> Result<usize, std::io::Error> {
|
2020-12-06 11:09:55 +00:00
|
|
|
|
2021-04-12 09:25:40 +00:00
|
|
|
match Self::read_block_frame(&mut self.buffer, &mut self.reader) {
|
|
|
|
Ok(()) => { /* ok */ }
|
|
|
|
Err(BlockReadError::EndOfFile) => {
|
|
|
|
self.got_eod = true;
|
|
|
|
self.read_pos = self.buffer.payload.len();
|
2021-04-13 11:11:44 +00:00
|
|
|
if !self.found_end_marker && check_end_marker {
|
2022-02-21 10:39:18 +00:00
|
|
|
proxmox_lang::io_bail!("detected tape stream without end marker");
|
2021-04-12 09:25:40 +00:00
|
|
|
}
|
|
|
|
return Ok(0); // EOD
|
|
|
|
}
|
|
|
|
Err(BlockReadError::EndOfStream) => {
|
2022-02-21 10:39:18 +00:00
|
|
|
proxmox_lang::io_bail!("got unexpected end of tape");
|
2021-04-12 09:25:40 +00:00
|
|
|
}
|
|
|
|
Err(BlockReadError::Error(err)) => {
|
|
|
|
return Err(err);
|
2020-12-06 11:09:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let (size, found_end_marker) = Self::check_buffer(&self.buffer, self.seq_nr)?;
|
|
|
|
self.seq_nr += 1;
|
|
|
|
|
|
|
|
if found_end_marker { // consume EOF mark
|
|
|
|
self.found_end_marker = true;
|
|
|
|
self.incomplete = self.buffer.flags.contains(BlockHeaderFlags::INCOMPLETE);
|
2021-02-03 12:15:18 +00:00
|
|
|
Self::consume_eof_marker(&mut self.reader)?;
|
|
|
|
self.got_eod = true;
|
2020-12-06 11:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.read_pos = 0;
|
|
|
|
|
|
|
|
Ok(size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 08:09:49 +00:00
|
|
|
impl <R: BlockRead> TapeRead for BlockedReader<R> {
|
2020-12-06 11:09:55 +00:00
|
|
|
|
|
|
|
fn is_incomplete(&self) -> Result<bool, std::io::Error> {
|
|
|
|
if !self.got_eod {
|
2022-02-21 10:39:18 +00:00
|
|
|
proxmox_lang::io_bail!("is_incomplete failed: EOD not reached");
|
2020-12-06 11:09:55 +00:00
|
|
|
}
|
|
|
|
if !self.found_end_marker {
|
2022-02-21 10:39:18 +00:00
|
|
|
proxmox_lang::io_bail!("is_incomplete failed: no end marker found");
|
2020-12-06 11:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(self.incomplete)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_end_marker(&self) -> Result<bool, std::io::Error> {
|
|
|
|
if !self.got_eod {
|
2022-02-21 10:39:18 +00:00
|
|
|
proxmox_lang::io_bail!("has_end_marker failed: EOD not reached");
|
2020-12-06 11:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(self.found_end_marker)
|
|
|
|
}
|
2021-04-13 11:11:44 +00:00
|
|
|
|
|
|
|
// like ReadExt::skip_to_end(), but does not raise an error if the
|
|
|
|
// stream has no end marker.
|
|
|
|
fn skip_data(&mut self) -> Result<usize, std::io::Error> {
|
|
|
|
let mut bytes = 0;
|
|
|
|
let buffer_size = self.buffer.size();
|
|
|
|
let rest = (buffer_size as isize) - (self.read_pos as isize);
|
|
|
|
if rest > 0 {
|
|
|
|
bytes = rest as usize;
|
|
|
|
}
|
|
|
|
loop {
|
|
|
|
if self.got_eod {
|
|
|
|
return Ok(bytes);
|
|
|
|
}
|
|
|
|
bytes += self.read_block(false)?;
|
|
|
|
}
|
|
|
|
}
|
2020-12-06 11:09:55 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 08:09:49 +00:00
|
|
|
impl <R: BlockRead> Read for BlockedReader<R> {
|
2020-12-06 11:09:55 +00:00
|
|
|
|
|
|
|
fn read(&mut self, buffer: &mut [u8]) -> Result<usize, std::io::Error> {
|
|
|
|
|
|
|
|
if self.read_error {
|
2022-02-21 10:39:18 +00:00
|
|
|
proxmox_lang::io_bail!("detected read after error - internal error");
|
2020-12-06 11:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut buffer_size = self.buffer.size();
|
|
|
|
let mut rest = (buffer_size as isize) - (self.read_pos as isize);
|
|
|
|
|
|
|
|
if rest <= 0 && !self.got_eod { // try to refill buffer
|
2021-04-13 11:11:44 +00:00
|
|
|
buffer_size = match self.read_block(true) {
|
2020-12-06 11:09:55 +00:00
|
|
|
Ok(len) => len,
|
|
|
|
err => {
|
|
|
|
self.read_error = true;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
rest = buffer_size as isize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if rest <= 0 {
|
2021-01-19 09:50:42 +00:00
|
|
|
Ok(0)
|
2020-12-06 11:09:55 +00:00
|
|
|
} else {
|
|
|
|
let copy_len = if (buffer.len() as isize) < rest {
|
|
|
|
buffer.len()
|
|
|
|
} else {
|
|
|
|
rest as usize
|
|
|
|
};
|
|
|
|
buffer[..copy_len].copy_from_slice(
|
|
|
|
&self.buffer.payload[self.read_pos..(self.read_pos + copy_len)]);
|
|
|
|
self.read_pos += copy_len;
|
2021-01-19 09:50:42 +00:00
|
|
|
Ok(copy_len)
|
2020-12-06 11:09:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use std::io::Read;
|
2021-04-12 12:08:05 +00:00
|
|
|
use anyhow::{bail, Error};
|
2021-09-13 09:54:24 +00:00
|
|
|
use crate::{
|
2020-12-06 11:09:55 +00:00
|
|
|
TapeWrite,
|
2021-04-12 12:08:05 +00:00
|
|
|
BlockReadError,
|
2021-09-13 09:54:24 +00:00
|
|
|
EmulateTapeReader,
|
|
|
|
EmulateTapeWriter,
|
|
|
|
PROXMOX_TAPE_BLOCK_SIZE,
|
|
|
|
BlockedReader,
|
|
|
|
BlockedWriter,
|
2020-12-06 11:09:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
fn write_and_verify(data: &[u8]) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut tape_data = Vec::new();
|
|
|
|
|
2021-03-29 08:09:49 +00:00
|
|
|
{
|
|
|
|
let writer = EmulateTapeWriter::new(&mut tape_data, 1024*1024*10);
|
|
|
|
let mut writer = BlockedWriter::new(writer);
|
2020-12-06 11:09:55 +00:00
|
|
|
|
2021-03-29 08:09:49 +00:00
|
|
|
writer.write_all(data)?;
|
2020-12-06 11:09:55 +00:00
|
|
|
|
2021-03-29 08:09:49 +00:00
|
|
|
writer.finish(false)?;
|
|
|
|
}
|
2020-12-06 11:09:55 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
tape_data.len(),
|
|
|
|
((data.len() + PROXMOX_TAPE_BLOCK_SIZE)/PROXMOX_TAPE_BLOCK_SIZE)
|
|
|
|
*PROXMOX_TAPE_BLOCK_SIZE
|
|
|
|
);
|
|
|
|
|
|
|
|
let reader = &mut &tape_data[..];
|
2021-03-29 08:09:49 +00:00
|
|
|
let reader = EmulateTapeReader::new(reader);
|
2021-04-12 12:08:05 +00:00
|
|
|
let mut reader = BlockedReader::open(reader)?;
|
2020-12-06 11:09:55 +00:00
|
|
|
|
|
|
|
let mut read_data = Vec::with_capacity(PROXMOX_TAPE_BLOCK_SIZE);
|
|
|
|
reader.read_to_end(&mut read_data)?;
|
|
|
|
|
|
|
|
assert_eq!(data.len(), read_data.len());
|
|
|
|
|
|
|
|
assert_eq!(data, &read_data[..]);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_stream() -> Result<(), Error> {
|
|
|
|
write_and_verify(b"")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn small_data() -> Result<(), Error> {
|
|
|
|
write_and_verify(b"ABC")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn large_data() -> Result<(), Error> {
|
2021-11-23 16:57:00 +00:00
|
|
|
let data = proxmox_sys::linux::random_data(1024*1024*5)?;
|
2020-12-06 11:09:55 +00:00
|
|
|
write_and_verify(&data)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_data() -> Result<(), Error> {
|
|
|
|
let tape_data = Vec::new();
|
|
|
|
let reader = &mut &tape_data[..];
|
2021-03-29 08:09:49 +00:00
|
|
|
let reader = EmulateTapeReader::new(reader);
|
2021-04-12 12:08:05 +00:00
|
|
|
match BlockedReader::open(reader) {
|
|
|
|
Err(BlockReadError::EndOfFile) => { /* OK */ },
|
|
|
|
_ => bail!("expected EOF"),
|
|
|
|
}
|
2020-12-06 11:09:55 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_end_marker() -> Result<(), Error> {
|
|
|
|
let mut tape_data = Vec::new();
|
|
|
|
{
|
2021-03-29 08:09:49 +00:00
|
|
|
let writer = EmulateTapeWriter::new(&mut tape_data, 1024*1024);
|
|
|
|
let mut writer = BlockedWriter::new(writer);
|
2020-12-06 11:09:55 +00:00
|
|
|
// write at least one block
|
2021-11-23 16:57:00 +00:00
|
|
|
let data = proxmox_sys::linux::random_data(PROXMOX_TAPE_BLOCK_SIZE)?;
|
2020-12-06 11:09:55 +00:00
|
|
|
writer.write_all(&data)?;
|
|
|
|
// but do not call finish here
|
|
|
|
}
|
2021-03-29 08:09:49 +00:00
|
|
|
|
2020-12-06 11:09:55 +00:00
|
|
|
let reader = &mut &tape_data[..];
|
2021-03-29 08:09:49 +00:00
|
|
|
let reader = EmulateTapeReader::new(reader);
|
2021-04-12 12:08:05 +00:00
|
|
|
let mut reader = BlockedReader::open(reader)?;
|
2020-12-06 11:09:55 +00:00
|
|
|
|
|
|
|
let mut data = Vec::with_capacity(PROXMOX_TAPE_BLOCK_SIZE);
|
|
|
|
assert!(reader.read_to_end(&mut data).is_err());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn small_read_buffer() -> Result<(), Error> {
|
|
|
|
let mut tape_data = Vec::new();
|
|
|
|
|
2021-03-29 08:09:49 +00:00
|
|
|
{
|
|
|
|
let writer = EmulateTapeWriter::new(&mut tape_data, 1024*1024);
|
|
|
|
let mut writer = BlockedWriter::new(writer);
|
2020-12-06 11:09:55 +00:00
|
|
|
|
2021-03-29 08:09:49 +00:00
|
|
|
writer.write_all(b"ABC")?;
|
2020-12-06 11:09:55 +00:00
|
|
|
|
2021-03-29 08:09:49 +00:00
|
|
|
writer.finish(false)?;
|
|
|
|
}
|
2020-12-06 11:09:55 +00:00
|
|
|
|
|
|
|
let reader = &mut &tape_data[..];
|
2021-03-29 08:09:49 +00:00
|
|
|
let reader = EmulateTapeReader::new(reader);
|
2021-04-12 12:08:05 +00:00
|
|
|
let mut reader = BlockedReader::open(reader)?;
|
2020-12-06 11:09:55 +00:00
|
|
|
|
|
|
|
let mut buf = [0u8; 1];
|
|
|
|
assert_eq!(reader.read(&mut buf)?, 1, "wrong byte count");
|
|
|
|
assert_eq!(&buf, b"A");
|
|
|
|
assert_eq!(reader.read(&mut buf)?, 1, "wrong byte count");
|
|
|
|
assert_eq!(&buf, b"B");
|
|
|
|
assert_eq!(reader.read(&mut buf)?, 1, "wrong byte count");
|
|
|
|
assert_eq!(&buf, b"C");
|
|
|
|
assert_eq!(reader.read(&mut buf)?, 0, "wrong byte count");
|
|
|
|
assert_eq!(reader.read(&mut buf)?, 0, "wrong byte count");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|