zff/footer/version2/
file_footer.rs

1// - STD
2use std::io::Cursor;
3
4// - internal
5use crate::{
6	Result,
7	HeaderCoding,
8	ValueDecoder,
9	ValueEncoder,
10	FOOTER_IDENTIFIER_FILE_FOOTER,
11};
12use crate::header::{
13	HashHeader,
14};
15
16/// The file footer is written at the end of each acquired file.
17/// The file footer contains several metadata about the acquisition process itself: e.g. the acquisition start/end time of the appropriate file,
18/// hash values, or size information.
19/// The general structure of the file footer is the same for all file types.
20#[derive(Debug,Clone,Eq,PartialEq)]
21pub struct FileFooter {
22	/// the version of the [FileFooter].
23	version: u8,
24	/// the acquisition start time for this file.
25	acquisition_start: u64,
26	/// the acquisition end/finish time for this file.
27	acquisition_end: u64,
28	/// The appropriate hash header for this file.
29	hash_header: HashHeader,
30	/// the first chunk number which was used for this file.
31	first_chunk_number: u64,
32	/// The full number of chunks for this file.
33	number_of_chunks: u64,
34	/// the original (uncompressed & unencrypted) length of the file.
35	length_of_data: u64,
36}
37
38impl FileFooter {
39	/// creates a new FileFooter by given values/hashes.
40	pub fn new(version: u8, acquisition_start: u64, acquisition_end: u64, hash_header: HashHeader, first_chunk_number: u64, number_of_chunks: u64, length_of_data: u64) -> FileFooter {
41		Self {
42			version,
43			acquisition_start,
44			acquisition_end,
45			hash_header,
46			first_chunk_number,
47			number_of_chunks,
48			length_of_data,
49		}
50	}
51
52	/// returns the acquisition start time.
53	pub fn acquisition_start(&self) -> u64 {
54		self.acquisition_start
55	}
56
57	/// returns the acquisition end time.
58	pub fn acquisition_end(&self) -> u64 {
59		self.acquisition_end
60	}
61
62	/// returns the hash header.
63	pub fn hash_header(&self) -> &HashHeader {
64		&self.hash_header
65	}
66
67	/// returns the first chunk number, used for the underlying file.
68	pub fn first_chunk_number(&self) -> u64 {
69		self.first_chunk_number
70	}
71
72	/// returns the total number of chunks, used for the underlying file.
73	pub fn number_of_chunks(&self) -> u64 {
74		self.number_of_chunks
75	}
76
77	/// 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).
78	/// if the file is a hardlink, this method returns the size of the inner value (just the size of the appropriate filenumber: 8).
79	/// if the file is a directory, this method returns the size of the underlying vector of children.
80	/// if the file is a symlink, this method returns the length of the linked path.
81	pub fn length_of_data(&self) -> u64 {
82		self.length_of_data
83	}
84}
85
86impl HeaderCoding for FileFooter {
87	type Item = FileFooter;
88	fn version(&self) -> u8 { 
89		self.version
90	}
91	fn identifier() -> u32 {
92		FOOTER_IDENTIFIER_FILE_FOOTER
93	}
94	fn encode_header(&self) -> Vec<u8> {
95		let mut vec = vec![self.version];
96		vec.append(&mut self.acquisition_start.encode_directly());
97		vec.append(&mut self.acquisition_end.encode_directly());
98		vec.append(&mut self.hash_header.encode_directly());
99		vec.append(&mut self.first_chunk_number.encode_directly());
100		vec.append(&mut self.number_of_chunks.encode_directly());
101		vec.append(&mut self.length_of_data.encode_directly());
102		vec
103	}
104	fn decode_content(data: Vec<u8>) -> Result<FileFooter> {
105		let mut cursor = Cursor::new(data);
106		let footer_version = u8::decode_directly(&mut cursor)?;
107		let acquisition_start = u64::decode_directly(&mut cursor)?;
108		let acquisition_end = u64::decode_directly(&mut cursor)?;
109		let hash_header = HashHeader::decode_directly(&mut cursor)?;
110		let first_chunk_number = u64::decode_directly(&mut cursor)?;
111		let number_of_chunks = u64::decode_directly(&mut cursor)?;
112		let length_of_data = u64::decode_directly(&mut cursor)?;
113		Ok(FileFooter::new(footer_version, acquisition_start, acquisition_end, hash_header, first_chunk_number, number_of_chunks, length_of_data))
114	}
115}