2021-07-06 10:49:10 +00:00
|
|
|
use std::io::{Seek, SeekFrom, Write};
|
|
|
|
use std::sync::Arc;
|
2019-08-14 11:05:11 +00:00
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use anyhow::Error;
|
|
|
|
|
|
|
|
use proxmox_io::WriteExt;
|
|
|
|
|
2021-09-07 07:22:14 +00:00
|
|
|
use pbs_tools::crypt_config::CryptConfig;
|
|
|
|
|
2021-07-06 10:49:10 +00:00
|
|
|
use crate::checksum_writer::ChecksumWriter;
|
|
|
|
use crate::crypt_writer::CryptWriter;
|
|
|
|
use crate::file_formats::{self, DataBlobHeader, EncryptedDataBlobHeader};
|
2019-08-14 11:05:11 +00:00
|
|
|
|
2021-06-02 10:14:37 +00:00
|
|
|
enum BlobWriterState<'writer, W: Write> {
|
2021-07-06 10:49:10 +00:00
|
|
|
Uncompressed {
|
|
|
|
csum_writer: ChecksumWriter<W>,
|
|
|
|
},
|
|
|
|
Compressed {
|
|
|
|
compr: zstd::stream::write::Encoder<'writer, ChecksumWriter<W>>,
|
|
|
|
},
|
|
|
|
Encrypted {
|
|
|
|
crypt_writer: CryptWriter<ChecksumWriter<W>>,
|
|
|
|
},
|
|
|
|
EncryptedCompressed {
|
|
|
|
compr: zstd::stream::write::Encoder<'writer, CryptWriter<ChecksumWriter<W>>>,
|
|
|
|
},
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Data blob writer
|
2021-06-02 10:14:37 +00:00
|
|
|
pub struct DataBlobWriter<'writer, W: Write> {
|
|
|
|
state: BlobWriterState<'writer, W>,
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 10:49:10 +00:00
|
|
|
impl<W: Write + Seek> DataBlobWriter<'_, W> {
|
2019-08-14 11:05:11 +00:00
|
|
|
pub fn new_uncompressed(mut writer: W) -> Result<Self, Error> {
|
|
|
|
writer.seek(SeekFrom::Start(0))?;
|
2021-07-06 10:49:10 +00:00
|
|
|
let head = DataBlobHeader {
|
|
|
|
magic: file_formats::UNCOMPRESSED_BLOB_MAGIC_1_0,
|
|
|
|
crc: [0; 4],
|
|
|
|
};
|
2019-08-14 11:05:11 +00:00
|
|
|
unsafe {
|
|
|
|
writer.write_le_value(head)?;
|
|
|
|
}
|
|
|
|
let csum_writer = ChecksumWriter::new(writer, None);
|
2021-07-06 10:49:10 +00:00
|
|
|
Ok(Self {
|
|
|
|
state: BlobWriterState::Uncompressed { csum_writer },
|
|
|
|
})
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_compressed(mut writer: W) -> Result<Self, Error> {
|
2021-07-06 10:49:10 +00:00
|
|
|
writer.seek(SeekFrom::Start(0))?;
|
|
|
|
let head = DataBlobHeader {
|
|
|
|
magic: file_formats::COMPRESSED_BLOB_MAGIC_1_0,
|
|
|
|
crc: [0; 4],
|
|
|
|
};
|
2019-08-14 11:05:11 +00:00
|
|
|
unsafe {
|
|
|
|
writer.write_le_value(head)?;
|
|
|
|
}
|
|
|
|
let csum_writer = ChecksumWriter::new(writer, None);
|
|
|
|
let compr = zstd::stream::write::Encoder::new(csum_writer, 1)?;
|
2021-07-06 10:49:10 +00:00
|
|
|
Ok(Self {
|
|
|
|
state: BlobWriterState::Compressed { compr },
|
|
|
|
})
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 07:19:01 +00:00
|
|
|
pub fn new_encrypted(mut writer: W, config: Arc<CryptConfig>) -> Result<Self, Error> {
|
2019-08-14 11:05:11 +00:00
|
|
|
writer.seek(SeekFrom::Start(0))?;
|
|
|
|
let head = EncryptedDataBlobHeader {
|
2021-07-06 10:49:10 +00:00
|
|
|
head: DataBlobHeader {
|
|
|
|
magic: file_formats::ENCRYPTED_BLOB_MAGIC_1_0,
|
|
|
|
crc: [0; 4],
|
|
|
|
},
|
2019-08-14 11:05:11 +00:00
|
|
|
iv: [0u8; 16],
|
|
|
|
tag: [0u8; 16],
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
writer.write_le_value(head)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let csum_writer = ChecksumWriter::new(writer, None);
|
2021-07-06 10:49:10 +00:00
|
|
|
let crypt_writer = CryptWriter::new(csum_writer, config)?;
|
|
|
|
Ok(Self {
|
|
|
|
state: BlobWriterState::Encrypted { crypt_writer },
|
|
|
|
})
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 10:49:10 +00:00
|
|
|
pub fn new_encrypted_compressed(
|
|
|
|
mut writer: W,
|
|
|
|
config: Arc<CryptConfig>,
|
|
|
|
) -> Result<Self, Error> {
|
2019-08-14 11:05:11 +00:00
|
|
|
writer.seek(SeekFrom::Start(0))?;
|
|
|
|
let head = EncryptedDataBlobHeader {
|
2021-07-06 10:49:10 +00:00
|
|
|
head: DataBlobHeader {
|
|
|
|
magic: file_formats::ENCR_COMPR_BLOB_MAGIC_1_0,
|
|
|
|
crc: [0; 4],
|
|
|
|
},
|
2019-08-14 11:05:11 +00:00
|
|
|
iv: [0u8; 16],
|
|
|
|
tag: [0u8; 16],
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
writer.write_le_value(head)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let csum_writer = ChecksumWriter::new(writer, None);
|
2021-07-06 10:49:10 +00:00
|
|
|
let crypt_writer = CryptWriter::new(csum_writer, config)?;
|
2019-08-14 11:05:11 +00:00
|
|
|
let compr = zstd::stream::write::Encoder::new(crypt_writer, 1)?;
|
2021-07-06 10:49:10 +00:00
|
|
|
Ok(Self {
|
|
|
|
state: BlobWriterState::EncryptedCompressed { compr },
|
|
|
|
})
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finish(self) -> Result<W, Error> {
|
|
|
|
match self.state {
|
|
|
|
BlobWriterState::Uncompressed { csum_writer } => {
|
|
|
|
// write CRC
|
|
|
|
let (mut writer, crc, _) = csum_writer.finish()?;
|
2021-07-06 10:49:10 +00:00
|
|
|
let head = DataBlobHeader {
|
|
|
|
magic: file_formats::UNCOMPRESSED_BLOB_MAGIC_1_0,
|
|
|
|
crc: crc.to_le_bytes(),
|
|
|
|
};
|
2019-08-14 11:05:11 +00:00
|
|
|
|
|
|
|
writer.seek(SeekFrom::Start(0))?;
|
|
|
|
unsafe {
|
|
|
|
writer.write_le_value(head)?;
|
|
|
|
}
|
|
|
|
|
2019-10-26 09:36:01 +00:00
|
|
|
Ok(writer)
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
|
|
|
BlobWriterState::Compressed { compr } => {
|
|
|
|
let csum_writer = compr.finish()?;
|
|
|
|
let (mut writer, crc, _) = csum_writer.finish()?;
|
|
|
|
|
2021-07-06 10:49:10 +00:00
|
|
|
let head = DataBlobHeader {
|
|
|
|
magic: file_formats::COMPRESSED_BLOB_MAGIC_1_0,
|
|
|
|
crc: crc.to_le_bytes(),
|
|
|
|
};
|
2019-08-14 11:05:11 +00:00
|
|
|
|
|
|
|
writer.seek(SeekFrom::Start(0))?;
|
|
|
|
unsafe {
|
|
|
|
writer.write_le_value(head)?;
|
|
|
|
}
|
|
|
|
|
2019-10-26 09:36:01 +00:00
|
|
|
Ok(writer)
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
|
|
|
BlobWriterState::Encrypted { crypt_writer } => {
|
|
|
|
let (csum_writer, iv, tag) = crypt_writer.finish()?;
|
|
|
|
let (mut writer, crc, _) = csum_writer.finish()?;
|
|
|
|
|
|
|
|
let head = EncryptedDataBlobHeader {
|
2021-07-06 10:49:10 +00:00
|
|
|
head: DataBlobHeader {
|
|
|
|
magic: file_formats::ENCRYPTED_BLOB_MAGIC_1_0,
|
|
|
|
crc: crc.to_le_bytes(),
|
|
|
|
},
|
|
|
|
iv,
|
|
|
|
tag,
|
2019-08-14 11:05:11 +00:00
|
|
|
};
|
|
|
|
writer.seek(SeekFrom::Start(0))?;
|
|
|
|
unsafe {
|
|
|
|
writer.write_le_value(head)?;
|
|
|
|
}
|
2019-10-26 09:36:01 +00:00
|
|
|
Ok(writer)
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
|
|
|
BlobWriterState::EncryptedCompressed { compr } => {
|
|
|
|
let crypt_writer = compr.finish()?;
|
|
|
|
let (csum_writer, iv, tag) = crypt_writer.finish()?;
|
|
|
|
let (mut writer, crc, _) = csum_writer.finish()?;
|
|
|
|
|
|
|
|
let head = EncryptedDataBlobHeader {
|
2021-07-06 10:49:10 +00:00
|
|
|
head: DataBlobHeader {
|
|
|
|
magic: file_formats::ENCR_COMPR_BLOB_MAGIC_1_0,
|
|
|
|
crc: crc.to_le_bytes(),
|
|
|
|
},
|
|
|
|
iv,
|
|
|
|
tag,
|
2019-08-14 11:05:11 +00:00
|
|
|
};
|
|
|
|
writer.seek(SeekFrom::Start(0))?;
|
|
|
|
unsafe {
|
|
|
|
writer.write_le_value(head)?;
|
|
|
|
}
|
2019-10-26 09:36:01 +00:00
|
|
|
Ok(writer)
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 10:49:10 +00:00
|
|
|
impl<W: Write + Seek> Write for DataBlobWriter<'_, W> {
|
2019-08-14 11:05:11 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
|
|
|
|
match self.state {
|
2021-07-06 10:49:10 +00:00
|
|
|
BlobWriterState::Uncompressed {
|
|
|
|
ref mut csum_writer,
|
|
|
|
} => csum_writer.write(buf),
|
|
|
|
BlobWriterState::Compressed { ref mut compr } => compr.write(buf),
|
|
|
|
BlobWriterState::Encrypted {
|
|
|
|
ref mut crypt_writer,
|
|
|
|
} => crypt_writer.write(buf),
|
|
|
|
BlobWriterState::EncryptedCompressed { ref mut compr } => compr.write(buf),
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<(), std::io::Error> {
|
|
|
|
match self.state {
|
2021-07-06 10:49:10 +00:00
|
|
|
BlobWriterState::Uncompressed {
|
|
|
|
ref mut csum_writer,
|
|
|
|
} => csum_writer.flush(),
|
|
|
|
BlobWriterState::Compressed { ref mut compr } => compr.flush(),
|
|
|
|
BlobWriterState::Encrypted {
|
|
|
|
ref mut crypt_writer,
|
|
|
|
} => crypt_writer.flush(),
|
|
|
|
BlobWriterState::EncryptedCompressed { ref mut compr } => compr.flush(),
|
2019-08-14 11:05:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|