From de45add25717afe36ec2cf2716058e6ca7ddb63f Mon Sep 17 00:00:00 2001 From: Rubens Brandao Date: Sun, 17 Dec 2023 07:37:38 -0300 Subject: [PATCH] remove the writer reference requirement --- backhand-cli/src/bin/add.rs | 4 ++-- backhand/Cargo.toml | 2 +- backhand/src/data.rs | 2 +- backhand/src/filesystem/writer.rs | 17 +++++++---------- backhand/src/metadata.rs | 4 ++-- 5 files changed, 13 insertions(+), 16 deletions(-) diff --git a/backhand-cli/src/bin/add.rs b/backhand-cli/src/bin/add.rs index 495ec315..ae25e1c4 100644 --- a/backhand-cli/src/bin/add.rs +++ b/backhand-cli/src/bin/add.rs @@ -104,8 +104,8 @@ fn main() -> ExitCode { } // write new file - let mut output = File::create(&args.out).unwrap(); - if let Err(e) = filesystem.write(&mut output) { + let output = File::create(&args.out).unwrap(); + if let Err(e) = filesystem.write(output) { println!("[!] {e}"); } println!("[-] added file and wrote to {}", args.out.display()); diff --git a/backhand/Cargo.toml b/backhand/Cargo.toml index 7ff57ec1..4a1fbb35 100644 --- a/backhand/Cargo.toml +++ b/backhand/Cargo.toml @@ -53,4 +53,4 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [lib] -bench = false \ No newline at end of file +bench = false diff --git a/backhand/src/data.rs b/backhand/src/data.rs index 21191115..b5a51ca0 100644 --- a/backhand/src/data.rs +++ b/backhand/src/data.rs @@ -230,7 +230,7 @@ impl<'a> DataWriter<'a> { /// Compress the fragments that were under length, write to data, add to fragment table, clear /// current fragment_bytes - pub fn finalize(&mut self, writer: &mut W) -> Result<(), BackhandError> { + pub fn finalize(&mut self, mut writer: W) -> Result<(), BackhandError> { let start = writer.stream_position()?; let cb = self.kind.compress(&self.fragment_bytes, self.fs_compressor, self.block_size)?; diff --git a/backhand/src/filesystem/writer.rs b/backhand/src/filesystem/writer.rs index 80eab7b1..919c1d4c 100644 --- a/backhand/src/filesystem/writer.rs +++ b/backhand/src/filesystem/writer.rs @@ -568,10 +568,7 @@ impl<'a, 'b, 'c> FilesystemWriter<'a, 'b, 'c> { /// /// # Returns /// (written populated [`SuperBlock`], total amount of bytes written including padding) - pub fn write( - &mut self, - mut w: &mut W, - ) -> Result<(SuperBlock, u64), BackhandError> { + pub fn write(&mut self, mut w: W) -> Result<(SuperBlock, u64), BackhandError> { let mut superblock = SuperBlock::new(self.fs_compressor.id, Kind { inner: self.kind.inner.clone() }); @@ -609,7 +606,7 @@ impl<'a, 'b, 'c> FilesystemWriter<'a, 'b, 'c> { Kind { inner: self.kind.inner.clone() }, ); metadata.write_all(&compression_opt_buf_out)?; - metadata.finalize(w)?; + metadata.finalize(&mut w)?; } let mut data_writer = @@ -631,7 +628,7 @@ impl<'a, 'b, 'c> FilesystemWriter<'a, 'b, 'c> { self.write_data(self.fs_compressor, self.block_size, &mut w, &mut data_writer)?; info!("Writing Data Fragments"); // Compress fragments and write - data_writer.finalize(w)?; + data_writer.finalize(&mut w)?; info!("Writing Other stuff"); let root = self.write_inode_dir( @@ -652,15 +649,15 @@ impl<'a, 'b, 'c> FilesystemWriter<'a, 'b, 'c> { info!("Writing Inodes"); superblock.inode_table = w.stream_position()?; - inode_writer.finalize(w)?; + inode_writer.finalize(&mut w)?; info!("Writing Dirs"); superblock.dir_table = w.stream_position()?; - dir_writer.finalize(w)?; + dir_writer.finalize(&mut w)?; info!("Writing Frag Lookup Table"); let (table_position, count) = - self.write_lookup_table(w, &data_writer.fragment_table, fragment::SIZE)?; + self.write_lookup_table(&mut w, &data_writer.fragment_table, fragment::SIZE)?; superblock.frag_table = table_position; superblock.frag_count = count; @@ -760,7 +757,7 @@ impl<'a, 'b, 'c> FilesystemWriter<'a, 'b, 'c> { /// ``` fn write_lookup_table, W: Write + Seek>( &self, - mut w: &mut W, + mut w: W, table: &Vec, element_size: usize, ) -> Result<(u64, u32), BackhandError> { diff --git a/backhand/src/metadata.rs b/backhand/src/metadata.rs index 6300dda4..107e5c27 100644 --- a/backhand/src/metadata.rs +++ b/backhand/src/metadata.rs @@ -75,7 +75,7 @@ impl MetadataWriter { Ok(()) } - pub fn finalize(&mut self, out: &mut W) -> Result<(), BackhandError> { + pub fn finalize(&mut self, mut out: W) -> Result<(), BackhandError> { //add any remaining data while !self.uncompressed_bytes.is_empty() { self.add_block()?; @@ -87,7 +87,7 @@ impl MetadataWriter { // if uncompressed, set the highest bit of len let len = compressed_bytes.len() as u16 | if *compressed { 0 } else { 1 << (u16::BITS - 1) }; - let mut writer = Writer::new(out); + let mut writer = Writer::new(&mut out); len.to_writer(&mut writer, self.kind.inner.data_endian)?; out.write_all(compressed_bytes)?; }