Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to deku 0.18.0; Support ExtendedFile #608

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions backhand-cli/src/bin/unsquashfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ fn extract_all<'a, S: ParallelIterator<Item = &'a Node<SquashfsFileReader>>>(

// write to file
let fd = File::create(&filepath).unwrap();
let mut writer = BufWriter::with_capacity(file.basic.file_size as usize, &fd);
let file = filesystem.file(&file.basic);
let mut writer = BufWriter::with_capacity(file.file_len(), &fd);
let file = filesystem.file(file);
let mut reader = file.reader();

match io::copy(&mut reader, &mut writer) {
Expand Down
13 changes: 5 additions & 8 deletions backhand-test/tests/non_standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ fn full_test(
{
let file = BufReader::new(File::open(og_path).unwrap());
info!("calling from_reader");
let og_filesystem = FilesystemReader::from_reader_with_offset_and_kind(
file,
offset,
Kind::from_kind(&kind),
)
.unwrap();
let og_filesystem =
FilesystemReader::from_reader_with_offset_and_kind(file, offset, Kind::from_kind(kind))
.unwrap();
let mut new_filesystem = FilesystemWriter::from_fs_reader(&og_filesystem).unwrap();
if let Some(pad) = pad {
new_filesystem.set_kib_padding(pad);
Expand All @@ -57,7 +54,7 @@ fn full_test(
let _new_filesystem = FilesystemReader::from_reader_with_offset_and_kind(
created_file,
offset,
Kind::from_kind(&kind),
Kind::from_kind(kind),
)
.unwrap();
}
Expand Down Expand Up @@ -140,7 +137,7 @@ fn test_custom_compressor() {
if let Compressor::Gzip = compressor {
out.resize(out.capacity(), 0);
let mut decompressor = libdeflater::Decompressor::new();
let amt = decompressor.zlib_decompress(&bytes, out).unwrap();
let amt = decompressor.zlib_decompress(bytes, out).unwrap();
out.truncate(amt);
} else {
unimplemented!();
Expand Down
2 changes: 1 addition & 1 deletion backhand/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
deku = "0.17.0"
deku = { version = "0.18.1", default-features = false, features = ["std"] }
tracing = { version = "0.1.40" }
thiserror = "1.0.63"
flate2 = { version = "1.0.33", optional = true, features = ["zlib-ng"] }
Expand Down
4 changes: 2 additions & 2 deletions backhand/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl CompressionAction for DefaultCompressor {
if let Some(options) = &fs_compressor.options {
trace!("writing compression options");
superblock.flags |= Flags::CompressorOptionsArePresent as u16;
let mut compression_opt_buf_out = vec![];
let mut compression_opt_buf_out = Cursor::new(vec![]);
let mut writer = Writer::new(&mut compression_opt_buf_out);
match options {
CompressionOptions::Gzip(gzip) => {
Expand All @@ -398,7 +398,7 @@ impl CompressionAction for DefaultCompressor {
superblock.block_size,
Kind { inner: kind.inner.clone() },
);
metadata.write_all(&compression_opt_buf_out)?;
metadata.write_all(&compression_opt_buf_out.into_inner())?;
metadata.finalize(&mut w)?;
}

Expand Down
4 changes: 2 additions & 2 deletions backhand/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl<'a> DataWriter<'a> {
mut writer: W,
) -> Result<(usize, Added), BackhandError> {
//just clone it, because block sizes where never modified, just copy it
let mut block_sizes = reader.file.basic.block_sizes.clone();
let mut block_sizes = reader.file.file.block_sizes().to_vec();
let mut read_buf = vec![];
let mut decompress_buf = vec![];

Expand Down Expand Up @@ -190,7 +190,7 @@ impl<'a> DataWriter<'a> {
writer.write_all(&read_buf)?;
}
}
let file_size = reader.file.basic.file_size as usize;
let file_size = reader.file.file.file_len();
Ok((file_size, Added::Data { blocks_start, block_sizes }))
}

Expand Down
46 changes: 42 additions & 4 deletions backhand/src/filesystem/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::sync::{Arc, Mutex};

use super::normalize_squashfs_path;
use crate::data::Added;
use crate::inode::{BasicFile, InodeHeader};
use crate::{BackhandError, FilesystemReaderFile, Id};
use crate::inode::{BasicFile, ExtendedFile, InodeHeader};
use crate::{BackhandError, DataSize, FilesystemReaderFile, Id};

/// File information for Node
#[derive(Debug, PartialEq, Eq, Default, Clone, Copy)]
Expand Down Expand Up @@ -91,8 +91,46 @@ pub enum InnerNode<T> {

/// Unread file for filesystem
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SquashfsFileReader {
pub basic: BasicFile,
pub enum SquashfsFileReader {
Basic(BasicFile),
Extended(ExtendedFile),
}

impl SquashfsFileReader {
pub fn file_len(&self) -> usize {
match self {
SquashfsFileReader::Basic(basic) => basic.file_size as usize,
SquashfsFileReader::Extended(extended) => extended.file_size as usize,
}
}

pub fn frag_index(&self) -> usize {
match self {
SquashfsFileReader::Basic(basic) => basic.frag_index as usize,
SquashfsFileReader::Extended(extended) => extended.frag_index as usize,
}
}

pub fn block_sizes(&self) -> &[DataSize] {
match self {
SquashfsFileReader::Basic(basic) => &basic.block_sizes,
SquashfsFileReader::Extended(extended) => &extended.block_sizes,
}
}

pub fn blocks_start(&self) -> u64 {
match self {
SquashfsFileReader::Basic(basic) => basic.blocks_start as u64,
SquashfsFileReader::Extended(extended) => extended.blocks_start,
}
}

pub fn block_offset(&self) -> u32 {
match self {
SquashfsFileReader::Basic(basic) => basic.block_offset,
SquashfsFileReader::Extended(extended) => extended.block_offset,
}
}
}

/// Read file from other SquashfsFile or an user file
Expand Down
34 changes: 15 additions & 19 deletions backhand/src/filesystem/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::data::DataSize;
use crate::error::BackhandError;
use crate::fragment::Fragment;
use crate::id::Id;
use crate::inode::BasicFile;
use crate::kinds::Kind;
use crate::reader::BufReadSeek;
use crate::squashfs::Cache;
Expand Down Expand Up @@ -130,8 +129,8 @@ impl<'b> FilesystemReader<'b> {
}

/// Return a file handler for this file
pub fn file<'a>(&'a self, basic_file: &'a BasicFile) -> FilesystemReaderFile<'a, 'b> {
FilesystemReaderFile::new(self, basic_file)
pub fn file<'a>(&'a self, file: &'a SquashfsFileReader) -> FilesystemReaderFile<'a, 'b> {
FilesystemReaderFile::new(self, file)
}

/// Iterator of all files, including the root
Expand All @@ -154,7 +153,7 @@ impl<'b> FilesystemReader<'b> {
/// match &node.inner {
/// InnerNode::File(file) => {
/// let mut reader = filesystem
/// .file(&file.basic)
/// .file(&file)
/// .reader();
/// // Then, do something with the reader
/// },
Expand All @@ -171,12 +170,12 @@ impl<'b> FilesystemReader<'b> {
#[derive(Copy, Clone)]
pub struct FilesystemReaderFile<'a, 'b> {
pub(crate) system: &'a FilesystemReader<'b>,
pub(crate) basic: &'a BasicFile,
pub(crate) file: &'a SquashfsFileReader,
}

impl<'a, 'b> FilesystemReaderFile<'a, 'b> {
pub fn new(system: &'a FilesystemReader<'b>, basic: &'a BasicFile) -> Self {
Self { system, basic }
pub fn new(system: &'a FilesystemReader<'b>, file: &'a SquashfsFileReader) -> Self {
Self { system, file }
}

/// Create [`SquashfsReadFile`] that impls [`std::io::Read`] from [`FilesystemReaderFile`].
Expand All @@ -190,18 +189,15 @@ impl<'a, 'b> FilesystemReaderFile<'a, 'b> {
}

pub fn fragment(&self) -> Option<&'a Fragment> {
if self.basic.frag_index == 0xffffffff {
if self.file.frag_index() == 0xffffffff {
None
} else {
self.system
.fragments
.as_ref()
.map(|fragments| &fragments[self.basic.frag_index as usize])
self.system.fragments.as_ref().map(|fragments| &fragments[self.file.frag_index()])
}
}

pub(crate) fn raw_data_reader(&self) -> SquashfsRawData<'a, 'b> {
SquashfsRawData::new(Self { system: self.system, basic: self.basic })
SquashfsRawData::new(Self { system: self.system, file: self.file })
}
}

Expand All @@ -210,7 +206,7 @@ impl<'a, 'b> IntoIterator for FilesystemReaderFile<'a, 'b> {
type Item = <BlockIterator<'a> as Iterator>::Item;

fn into_iter(self) -> Self::IntoIter {
BlockIterator { blocks: &self.basic.block_sizes, fragment: self.fragment() }
BlockIterator { blocks: self.file.block_sizes(), fragment: self.fragment() }
}
}

Expand Down Expand Up @@ -252,7 +248,7 @@ pub(crate) struct SquashfsRawData<'a, 'b> {

impl<'a, 'b> SquashfsRawData<'a, 'b> {
pub fn new(file: FilesystemReaderFile<'a, 'b>) -> Self {
let pos = file.basic.blocks_start.into();
let pos = file.file.blocks_start();
let current_block = file.into_iter();
Self { file, current_block, pos }
}
Expand Down Expand Up @@ -332,10 +328,10 @@ impl<'a, 'b> SquashfsRawData<'a, 'b> {
#[inline]
fn fragment_range(&self) -> std::ops::Range<usize> {
let block_len = self.file.system.block_size as usize;
let block_num = self.file.basic.block_sizes.len();
let file_size = self.file.basic.file_size as usize;
let block_num = self.file.file.block_sizes().len();
let file_size = self.file.file.file_len();
let frag_len = file_size - (block_num * block_len);
let frag_start = self.file.basic.block_offset as usize;
let frag_start = self.file.file.block_offset() as usize;
let frag_end = frag_start + frag_len;
frag_start..frag_end
}
Expand Down Expand Up @@ -381,7 +377,7 @@ impl<'a, 'b> SquashfsRawData<'a, 'b> {
#[inline]
pub fn into_reader(self) -> SquashfsReadFile<'a, 'b> {
let block_size = self.file.system.block_size as usize;
let bytes_available = self.file.basic.file_size as usize;
let bytes_available = self.file.file.file_len();
SquashfsReadFile::new(block_size, self, 0, bytes_available)
}
}
Expand Down
Loading
Loading