]> git.proxmox.com Git - proxmox-backup.git/blob - pbs-tape/src/tape_write.rs
update to proxmox-sys 0.2 crate
[proxmox-backup.git] / pbs-tape / src / tape_write.rs
1 use endian_trait::Endian;
2
3 use crate::MediaContentHeader;
4
5 /// Write trait for tape devices
6 ///
7 /// The 'write_all' function returns if the drive reached the Logical
8 /// End Of Media (early warning).
9 ///
10 /// It is mandatory to call 'finish' before closing the stream to mark it
11 /// as correctly written.
12 ///
13 /// Please note that there is no flush method. Tapes flush there internal
14 /// buffer when they write an EOF marker.
15 pub trait TapeWrite {
16 /// writes all data, returns true on LEOM
17 fn write_all(&mut self, data: &[u8]) -> Result<bool, std::io::Error>;
18
19 /// Returns how many bytes (raw data on tape) have been written
20 fn bytes_written(&self) -> usize;
21
22 /// flush last block, write file end mark
23 ///
24 /// The incomplete flag is used to mark multivolume stream.
25 fn finish(&mut self, incomplete: bool) -> Result<bool, std::io::Error>;
26
27 /// Returns true if the writer already detected the logical end of media
28 fn logical_end_of_media(&self) -> bool;
29
30 /// writes header and data, returns true on LEOM
31 fn write_header(
32 &mut self,
33 header: &MediaContentHeader,
34 data: &[u8],
35 ) -> Result<bool, std::io::Error> {
36 if header.size as usize != data.len() {
37 proxmox_sys::io_bail!("write_header with wrong size - internal error");
38 }
39 let header = header.to_le();
40
41 let res = self.write_all(unsafe { std::slice::from_raw_parts(
42 &header as *const MediaContentHeader as *const u8,
43 std::mem::size_of::<MediaContentHeader>(),
44 )})?;
45
46 if data.is_empty() { return Ok(res); }
47
48 self.write_all(data)
49 }
50 }
51
52 /// Write streams of blocks
53 pub trait BlockWrite {
54 /// Write a data block
55 ///
56 /// Returns true if the drive reached the Logical End Of Media
57 /// (early warning)
58 fn write_block(&mut self, buffer: &[u8]) -> Result<bool, std::io::Error>;
59
60 /// Write a filemark
61 fn write_filemark(&mut self) -> Result<(), std::io::Error>;
62 }