Skip to content

Commit

Permalink
Code refactoring / cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ph0llux committed Dec 22, 2023
1 parent f7c57f3 commit 43aac3b
Show file tree
Hide file tree
Showing 26 changed files with 295 additions and 550 deletions.
1 change: 0 additions & 1 deletion src/lib/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ pub(crate) const ERROR_MISSING_SEGMENT_MAIN_FOOTER: &str = "A segment with a val
pub(crate) const ERROR_MISSING_OBJECT_HEADER_IN_SEGMENT: &str = "Missing object header in segment with following object number: ";
pub(crate) const ERROR_MISSING_OBJECT_FOOTER_IN_SEGMENT: &str = "Missing object footer in segment with following object number: ";
pub(crate) const ERROR_MISSING_FILE_NUMBER: &str = "Missing filenumber: ";
pub(crate) const ERROR_MISMATCH_ZFF_VERSION: &str = "mismatch zff version";
pub(crate) const ERROR_INVALID_OBJECT_TYPE_FLAG_VALUE: &str = "Invalid object type flag value:";
pub(crate) const ERROR_INVALID_OPTION_ZFFCREATE: &str = "Create new container";
pub(crate) const ERROR_LAST_GREATER_FIRST: &str = "First chunk number is greater than last chunk number. This is invalid.";
Expand Down
12 changes: 4 additions & 8 deletions src/lib/file/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ use crate::{
Encryption,
ZffError,
ZffErrorKind,
DEFAULT_HEADER_VERSION_HASH_VALUE_HEADER,
DEFAULT_HEADER_VERSION_HASH_HEADER,
DEFAULT_FOOTER_VERSION_FILE_FOOTER,
};

#[cfg(feature = "log")]
Expand Down Expand Up @@ -226,7 +223,7 @@ impl FileEncoder {
Some(key) => key,
None => return Err(ZffError::new(ZffErrorKind::MissingEncryptionKey, self.current_chunk_number.to_string()))
};
chunk_header.encrypt_and_consume(key, enc_header.algorithm())?.encode_directly()
chunk_header.encrypt_and_consume(key, &enc_header.algorithm)?.encode_directly()
} else {
chunk_header.encode_directly()
};
Expand Down Expand Up @@ -286,7 +283,7 @@ impl FileEncoder {
Some(key) => key,
None => return Err(ZffError::new(ZffErrorKind::MissingEncryptionKey, self.current_chunk_number.to_string()))
};
chunk_header.encrypt_and_consume(key, enc_header.algorithm())?.encode_directly()
chunk_header.encrypt_and_consume(key, &enc_header.algorithm)?.encode_directly()
} else {
chunk_header.encode_directly()
};
Expand All @@ -304,17 +301,16 @@ impl FileEncoder {
let mut hash_values = Vec::new();
for (hash_type, hasher) in self.hasher_map.clone() {
let hash = hasher.finalize();
let mut hash_value = HashValue::new_empty(DEFAULT_HEADER_VERSION_HASH_VALUE_HEADER, hash_type);
let mut hash_value = HashValue::new_empty(hash_type);
hash_value.set_hash(hash.to_vec());
hash_values.push(hash_value);
}

#[cfg(feature = "log")]
hashes_to_log(self.object_header.object_number, Some(self.file_header.file_number), &hash_values);

let hash_header = HashHeader::new(DEFAULT_HEADER_VERSION_HASH_HEADER, hash_values);
let hash_header = HashHeader::new(hash_values);
let footer = FileFooter::new(
DEFAULT_FOOTER_VERSION_FILE_FOOTER,
self.file_header.file_number,
self.acquisition_start,
self.acquisition_end,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use encoder::*;
// - internal
use crate::{
header::{FileHeader, FileType},
footer::{FileFooter},
footer::FileFooter,
};

/// The [File] contains the appropriate [FileHeader] and [FileFooter] of a dumped [File].
Expand Down Expand Up @@ -61,6 +61,6 @@ impl File {

/// returns the length of the data, read from the underlying [FileFooter].
pub fn length_of_data(&self) -> u64 {
self.footer.length_of_data()
self.footer.length_of_data
}
}
69 changes: 14 additions & 55 deletions src/lib/footer/file_footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ use crate::header::{

// - external
#[cfg(feature = "serde")]
use serde::{
Serialize,
};
use serde::Serialize;


/// The file footer is written at the end of each acquired file.
Expand All @@ -37,8 +35,6 @@ use serde::{
#[derive(Debug,Clone,Eq,PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct FileFooter {
/// the version of the [FileFooter].
pub version: u8,
/// the appropriate file number.
pub file_number: u64,
/// the acquisition start time for this file.
Expand All @@ -51,15 +47,18 @@ pub struct FileFooter {
pub first_chunk_number: u64,
/// The full number of chunks for this file.
pub number_of_chunks: u64,
/// the original (uncompressed & unencrypted) length of the file.
/// the original (uncompressed & unencrypted) length of the file.
/// - If the file is a regular file, this method returns the original (uncompressed, unencrypted) size
/// of the file (without "filesystem-"metadata - just the size of the file content).
/// - If the file is a hardlink, this method returns the size of the inner value (just the size of the appropriate filenumber: 8).
/// - If the file is a directory, this method returns the size of the underlying vector of children.
/// - If the file is a symlink, this method returns the length of the linked path.
pub length_of_data: u64,
}

impl FileFooter {
/// creates a new FileFooter by given values/hashes.
#[allow(clippy::too_many_arguments)]
pub fn new(
version: u8,
file_number: u64,
acquisition_start: u64,
acquisition_end: u64,
Expand All @@ -68,7 +67,6 @@ impl FileFooter {
number_of_chunks: u64,
length_of_data: u64) -> FileFooter {
Self {
version,
file_number,
acquisition_start,
acquisition_end,
Expand All @@ -79,39 +77,6 @@ impl FileFooter {
}
}

/// returns the acquisition start time.
pub fn acquisition_start(&self) -> u64 {
self.acquisition_start
}

/// returns the acquisition end time.
pub fn acquisition_end(&self) -> u64 {
self.acquisition_end
}

/// returns the hash header.
pub fn hash_header(&self) -> &HashHeader {
&self.hash_header
}

/// returns the first chunk number, used for the underlying file.
pub fn first_chunk_number(&self) -> u64 {
self.first_chunk_number
}

/// returns the total number of chunks, used for the underlying file.
pub fn number_of_chunks(&self) -> u64 {
self.number_of_chunks
}

/// if the file is a regular file, this method returns the original (uncompressed, unencrypted) size of the file (without "filesystem-"metadata - just the size of the file content).
/// if the file is a hardlink, this method returns the size of the inner value (just the size of the appropriate filenumber: 8).
/// if the file is a directory, this method returns the size of the underlying vector of children.
/// if the file is a symlink, this method returns the length of the linked path.
pub fn length_of_data(&self) -> u64 {
self.length_of_data
}

fn encode_content(&self) -> Vec<u8> {
let mut vec = Vec::new();
vec.append(&mut self.acquisition_start.encode_directly());
Expand Down Expand Up @@ -157,10 +122,7 @@ impl FileFooter {
let mut header_content = vec![0u8; header_length-DEFAULT_LENGTH_HEADER_IDENTIFIER-DEFAULT_LENGTH_VALUE_HEADER_LENGTH];
data.read_exact(&mut header_content)?;
let mut cursor = Cursor::new(header_content);
let version = u8::decode_directly(&mut cursor)?;
if version != DEFAULT_FOOTER_VERSION_FILE_FOOTER {
return Err(ZffError::new(ZffErrorKind::UnsupportedVersion, version.to_string()))
};
Self::check_version(&mut cursor)?;
let file_number = u64::decode_directly(&mut cursor)?;

let encrypted_data = Vec::<u8>::decode_directly(&mut cursor)?;
Expand All @@ -172,7 +134,7 @@ impl FileFooter {
algorithm)?;
let mut cursor = Cursor::new(decrypted_data);
let (acquisition_start, acquisition_end, hash_header, first_chunk_number, number_of_chunks, length_of_data) = Self::decode_inner_content(&mut cursor)?;
Ok(FileFooter::new(version, file_number, acquisition_start, acquisition_end, hash_header, first_chunk_number, number_of_chunks, length_of_data))
Ok(FileFooter::new(file_number, acquisition_start, acquisition_end, hash_header, first_chunk_number, number_of_chunks, length_of_data))
}

#[allow(clippy::type_complexity)]
Expand Down Expand Up @@ -204,27 +166,24 @@ impl FileFooter {

impl HeaderCoding for FileFooter {
type Item = FileFooter;
fn version(&self) -> u8 {
self.version
fn version() -> u8 {
DEFAULT_FOOTER_VERSION_FILE_FOOTER
}
fn identifier() -> u32 {
FOOTER_IDENTIFIER_FILE_FOOTER
}
fn encode_header(&self) -> Vec<u8> {
let mut vec = vec![self.version];
let mut vec = vec![Self::version()];
vec.append(&mut self.file_number.encode_directly());
vec.append(&mut self.encode_content());
vec
}
fn decode_content(data: Vec<u8>) -> Result<FileFooter> {
let mut cursor = Cursor::new(data);
let version = u8::decode_directly(&mut cursor)?;
if version != DEFAULT_FOOTER_VERSION_FILE_FOOTER {
return Err(ZffError::new(ZffErrorKind::UnsupportedVersion, version.to_string()))
};
Self::check_version(&mut cursor)?;
let file_number = u64::decode_directly(&mut cursor)?;
let (acquisition_start, acquisition_end, hash_header, first_chunk_number, number_of_chunks, length_of_data) = Self::decode_inner_content(&mut cursor)?;
Ok(FileFooter::new(version, file_number, acquisition_start, acquisition_end, hash_header, first_chunk_number, number_of_chunks, length_of_data))
Ok(FileFooter::new(file_number, acquisition_start, acquisition_end, hash_header, first_chunk_number, number_of_chunks, length_of_data))
}
}

Expand Down
18 changes: 6 additions & 12 deletions src/lib/footer/main_footer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// - STD
use std::io::Cursor;
use std::collections::{BTreeMap};
use std::collections::BTreeMap;
use std::fmt;

// - internal
Expand All @@ -11,7 +11,7 @@ use crate::{
ValueEncoder,
ZffErrorKind,
FOOTER_IDENTIFIER_MAIN_FOOTER,
ENCODING_KEY_DESCRIPTION_NOTES,
ENCODING_KEY_DESCRIPTION_NOTES, constants::DEFAULT_FOOTER_VERSION_MAIN_FOOTER,
};

// - external
Expand All @@ -28,8 +28,6 @@ use serde::{
#[cfg_attr(feature = "serde", derive(Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct MainFooter {
/// version of the footer.
pub version: u8,
/// the total number of segments for this container
pub number_of_segments: u64,
/// the segment numbers where the appropriate object header can be found.
Expand All @@ -47,15 +45,13 @@ pub struct MainFooter {
impl MainFooter {
/// creates a new MainFooter with a given values.
pub fn new(
version: u8,
number_of_segments: u64,
object_header: BTreeMap<u64, u64>,
object_footer: BTreeMap<u64, u64>,
chunk_maps: BTreeMap<u64, u64>,
description_notes: Option<String>,
footer_offset: u64) -> MainFooter {
Self {
version,
number_of_segments,
object_header,
object_footer,
Expand Down Expand Up @@ -123,13 +119,13 @@ impl HeaderCoding for MainFooter {
FOOTER_IDENTIFIER_MAIN_FOOTER
}

fn version(&self) -> u8 {
self.version
fn version() -> u8 {
DEFAULT_FOOTER_VERSION_MAIN_FOOTER
}

fn encode_header(&self) -> Vec<u8> {
let mut vec = Vec::new();
vec.append(&mut self.version.encode_directly());
vec.append(&mut Self::version().encode_directly());
vec.append(&mut self.number_of_segments.encode_directly());
vec.append(&mut self.object_header.encode_directly());
vec.append(&mut self.object_footer.encode_directly());
Expand All @@ -143,8 +139,7 @@ impl HeaderCoding for MainFooter {

fn decode_content(data: Vec<u8>) -> Result<MainFooter> {
let mut cursor = Cursor::new(data);

let footer_version = u8::decode_directly(&mut cursor)?;
Self::check_version(&mut cursor)?;
let number_of_segments = u64::decode_directly(&mut cursor)?;
let object_header = BTreeMap::<u64, u64>::decode_directly(&mut cursor)?;
let object_footer = BTreeMap::<u64, u64>::decode_directly(&mut cursor)?;
Expand All @@ -162,7 +157,6 @@ impl HeaderCoding for MainFooter {
};
let footer_offset = u64::decode_directly(&mut cursor)?;
Ok(MainFooter::new(
footer_version,
number_of_segments,
object_header,
object_footer,
Expand Down
24 changes: 8 additions & 16 deletions src/lib/footer/object_footer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ impl ObjectFooter {
/// returns the version of the object footer.
pub fn version(&self) -> u8 {
match self {
ObjectFooter::Physical(phy) => phy.version(),
ObjectFooter::Logical(log) => log.version(),
ObjectFooter::Virtual(virt) => virt.version(),
ObjectFooter::Physical(_) => ObjectFooterPhysical::version(),
ObjectFooter::Logical(_) => ObjectFooterLogical::version(),
ObjectFooter::Virtual(_) => ObjectFooterVirtual::version(),
}
}

/// checks if the identifier matches to an physical or logical object footer. Returns 1 for a physical object footer, 2 for a logical object footer and 0 if neither applies.
/// checks if the identifier matches to an physical or logical object footer.
/// Returns 1 for a physical object footer, 2 for a logical object footer and 0 if neither applies.
fn check_identifier<R: Read>(data: &mut R) -> u8 {
let identifier = match data.read_u32::<BigEndian>() {
Ok(val) => val,
Expand Down Expand Up @@ -182,8 +183,8 @@ impl EncryptedObjectFooter {
/// returns the version of the object footer.
pub fn version(&self) -> u8 {
match self {
EncryptedObjectFooter::Physical(phy) => phy.version(),
EncryptedObjectFooter::Logical(log) => log.version(),
EncryptedObjectFooter::Physical(_) => ObjectFooterPhysical::version(),
EncryptedObjectFooter::Logical(_) => ObjectFooterLogical::version(),
}
}

Expand Down Expand Up @@ -254,15 +255,6 @@ impl EncryptedObjectFooter {
A: Borrow<EncryptionAlgorithm>,
K: AsRef<[u8]>,
{
match self {
EncryptedObjectFooter::Physical(encrypted_inner_footer) => {
let decrypted_footer = encrypted_inner_footer.decrypt_and_consume(key, algorithm)?;
Ok(ObjectFooter::from(decrypted_footer))
},
EncryptedObjectFooter::Logical(encrypted_inner_footer) => {
let decrypted_footer = encrypted_inner_footer.decrypt_and_consume(key, algorithm)?;
Ok(ObjectFooter::from(decrypted_footer))
}
}
self.decrypt(key, algorithm)
}
}
Loading

0 comments on commit 43aac3b

Please sign in to comment.