]> git.proxmox.com Git - proxmox-backup.git/blob - pbs-tape/src/emulate_tape_reader.rs
update to proxmox-sys 0.2 crate
[proxmox-backup.git] / pbs-tape / src / emulate_tape_reader.rs
1 use std::io::Read;
2
3 use proxmox_io::ReadExt;
4
5 use crate::{BlockRead, BlockReadError, PROXMOX_TAPE_BLOCK_SIZE};
6
7 /// Emulate tape read behavior on a normal Reader
8 ///
9 /// Tapes reads are always return one whole block PROXMOX_TAPE_BLOCK_SIZE.
10 pub struct EmulateTapeReader<R: Read> {
11 reader: R,
12 got_eof: bool,
13 }
14
15 impl <R: Read> EmulateTapeReader<R> {
16
17 pub fn new(reader: R) -> Self {
18 Self { reader, got_eof: false }
19 }
20 }
21
22 impl <R: Read> BlockRead for EmulateTapeReader<R> {
23 fn read_block(&mut self, buffer: &mut [u8]) -> Result<usize, BlockReadError> {
24 if self.got_eof {
25 return Err(BlockReadError::Error(proxmox_sys::io_format_err!("detected read after EOF!")));
26 }
27 match self.reader.read_exact_or_eof(buffer)? {
28 false => {
29 self.got_eof = true;
30 Err(BlockReadError::EndOfFile)
31 }
32 true => {
33 // test buffer len after EOF test (to allow EOF test with small buffers in BufferedReader)
34 if buffer.len() != PROXMOX_TAPE_BLOCK_SIZE {
35 return Err(BlockReadError::Error(
36 proxmox_sys::io_format_err!(
37 "EmulateTapeReader: read_block with wrong block size ({} != {})",
38 buffer.len(),
39 PROXMOX_TAPE_BLOCK_SIZE,
40 )
41 ));
42 }
43 Ok(buffer.len())
44 }
45 }
46 }
47 }