2021-02-04 07:36:35 +00:00
|
|
|
use std::io::{Read};
|
|
|
|
|
|
|
|
use anyhow::{bail, Error};
|
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use proxmox_io::ReadExt;
|
2021-02-04 07:36:35 +00:00
|
|
|
|
2021-09-13 09:54:24 +00:00
|
|
|
use pbs_tape::{TapeRead, MediaContentHeader};
|
2021-02-04 07:36:35 +00:00
|
|
|
|
|
|
|
/// Read multi volume data streams written by `MultiVolumeWriter`
|
|
|
|
///
|
|
|
|
/// Note: We do not use this feature currently.
|
|
|
|
pub struct MultiVolumeReader<'a> {
|
|
|
|
reader: Option<Box<dyn TapeRead + 'a>>,
|
|
|
|
next_reader_fn: Box<dyn 'a + FnMut() -> Result<Box<dyn TapeRead +'a>, Error>>,
|
|
|
|
complete: bool,
|
|
|
|
header: MediaContentHeader,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <'a> MultiVolumeReader<'a> {
|
|
|
|
|
|
|
|
/// Creates a new instance
|
|
|
|
pub fn new(
|
|
|
|
reader: Box<dyn TapeRead +'a>,
|
|
|
|
header: MediaContentHeader,
|
|
|
|
next_reader_fn: Box<dyn 'a + FnMut() -> Result<Box<dyn TapeRead +'a>, Error>>,
|
|
|
|
) -> Result<Self, Error> {
|
|
|
|
|
|
|
|
if header.part_number != 0 {
|
|
|
|
bail!("MultiVolumeReader::new - got wrong header part_number ({} != 0)",
|
|
|
|
header.part_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
reader: Some(reader),
|
|
|
|
next_reader_fn,
|
|
|
|
complete: false,
|
|
|
|
header,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <'a> Read for MultiVolumeReader<'a> {
|
|
|
|
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
|
|
|
|
if self.complete {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.reader.is_none() {
|
|
|
|
let mut reader = (self.next_reader_fn)()
|
2021-11-23 16:57:00 +00:00
|
|
|
.map_err(|err| proxmox_sys::io_format_err!("multi-volume next failed: {}", err))?;
|
2021-02-04 07:36:35 +00:00
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
proxmox_lang::try_block!({
|
2021-02-04 07:36:35 +00:00
|
|
|
let part_header: MediaContentHeader = unsafe { reader.read_le_value()? };
|
|
|
|
self.reader = Some(reader);
|
|
|
|
|
|
|
|
if part_header.uuid != self.header.uuid {
|
2021-11-23 16:57:00 +00:00
|
|
|
proxmox_sys::io_bail!("got wrong part uuid");
|
2021-02-04 07:36:35 +00:00
|
|
|
}
|
|
|
|
if part_header.content_magic!= self.header.content_magic {
|
2021-11-23 16:57:00 +00:00
|
|
|
proxmox_sys::io_bail!("got wrong part content magic");
|
2021-02-04 07:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let expect_part_number = self.header.part_number + 1;
|
|
|
|
|
|
|
|
if part_header.part_number != expect_part_number {
|
2021-11-23 16:57:00 +00:00
|
|
|
proxmox_sys::io_bail!("got wrong part number ({} != {})",
|
2021-02-04 07:36:35 +00:00
|
|
|
part_header.part_number, expect_part_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.header.part_number = expect_part_number;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}).map_err(|err| {
|
2021-11-23 16:57:00 +00:00
|
|
|
proxmox_sys::io_format_err!("multi-volume read content header failed: {}", err)
|
2021-02-04 07:36:35 +00:00
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.reader {
|
|
|
|
None => unreachable!(),
|
|
|
|
Some(ref mut reader) => {
|
|
|
|
match reader.read(buf) {
|
|
|
|
Ok(0) => {
|
|
|
|
if reader.is_incomplete()? {
|
|
|
|
self.reader = None;
|
|
|
|
self.read(buf)
|
|
|
|
} else {
|
|
|
|
self.reader = None;
|
|
|
|
self.complete = true;
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(n) => Ok(n),
|
|
|
|
Err(err) => Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|