Skip to content

Commit

Permalink
remove the writer reference requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran authored and wcampbell0x2a committed Dec 18, 2023
1 parent 622c435 commit cedc675
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 16 deletions.
4 changes: 2 additions & 2 deletions backhand-cli/src/bin/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion backhand/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[lib]
bench = false
bench = false
2 changes: 1 addition & 1 deletion backhand/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W: Write + Seek>(&mut self, writer: &mut W) -> Result<(), BackhandError> {
pub fn finalize<W: Write + Seek>(&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)?;

Expand Down
17 changes: 7 additions & 10 deletions backhand/src/filesystem/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W: Write + Seek>(
&mut self,
mut w: &mut W,
) -> Result<(SuperBlock, u64), BackhandError> {
pub fn write<W: Write + Seek>(&mut self, mut w: W) -> Result<(SuperBlock, u64), BackhandError> {
let mut superblock =
SuperBlock::new(self.fs_compressor.id, Kind { inner: self.kind.inner.clone() });

Expand Down Expand Up @@ -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 =
Expand All @@ -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(
Expand All @@ -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;

Expand Down Expand Up @@ -760,7 +757,7 @@ impl<'a, 'b, 'c> FilesystemWriter<'a, 'b, 'c> {
/// ```
fn write_lookup_table<D: DekuWriter<deku::ctx::Endian>, W: Write + Seek>(
&self,
mut w: &mut W,
mut w: W,
table: &Vec<D>,
element_size: usize,
) -> Result<(u64, u32), BackhandError> {
Expand Down
4 changes: 2 additions & 2 deletions backhand/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl MetadataWriter {
Ok(())
}

pub fn finalize<W: Write + Seek>(&mut self, out: &mut W) -> Result<(), BackhandError> {
pub fn finalize<W: Write + Seek>(&mut self, mut out: W) -> Result<(), BackhandError> {
//add any remaining data
while !self.uncompressed_bytes.is_empty() {
self.add_block()?;
Expand All @@ -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)?;
}
Expand Down

0 comments on commit cedc675

Please sign in to comment.