ACTUAL proper handling of ranges, comments/documentatoin

This commit is contained in:
Janis 2023-04-11 02:55:44 +02:00
parent 15089abba2
commit 6dd58e3b65
2 changed files with 51 additions and 24 deletions

View file

@ -8,8 +8,8 @@ use scroll::Pread;
use crate::crc32c::calculate_crc32c; use crate::crc32c::calculate_crc32c;
use crate::path::Path; use crate::path::Path;
use crate::structs::{ use crate::structs::{
Chunk, DirItemEntry, DirItemType, ExtentData, INodeItem, INodeRefEntry, Item, Key, KeyPtr, Chunk, CompressionType, DirItemEntry, DirItemType, ExtentData, INodeItem, INodeRefEntry, Item,
KnownObjectId, ObjectType, RootItem, Stripe, Superblock, TreeItem, Key, KeyPtr, KnownObjectId, ObjectType, RootItem, Stripe, Superblock, TreeItem,
}; };
use crate::{Error, Result}; use crate::{Error, Result};
@ -569,27 +569,50 @@ impl<R: super::Read> Fs<R> {
log::info!("extents: {}", extents.len()); log::info!("extents: {}", extents.len());
log::info!("{:?}", extents); log::info!("{:?}", extents);
for (offset, extent) in extents.into_iter().filter(|(offset, extent)| { for (offset, extent) in extents.into_iter().filter(|(offset, extent)| {
// bounds of the current extent
let extent_start = *offset; let extent_start = *offset;
let extent_end = extent_start + extent.len(); let extent_end = extent_start + extent.len();
let extent_len = extent.len();
// length of the entire range to be read // entire range we want to read from the file
let range_len = end.map(|end| end - start); let range_len = end.map(|end| end - start);
// bounds and length of the intersection of the current extent and // start of the UNION (from lowest bound to highest bound) of the
// the entire range to be read // current extent and the entire range
let start2 = start.min(extent_start); let start = start.min(extent_start);
let end = end.map(|end| end.max(extent_end)).unwrap_or(extent.len()); // end of the UNION of the current extent and the entire range
let len = end - start2; let end = end.map(|end| end.max(extent_end));
// width of the union o fthe current extent and the entire range
let len = end.map(|end| (end - start));
// check if current extent intersects the range we want to read if let (Some(len), Some(range_len)) = (len, range_len) {
if let Some(range_len) = range_len { // proceed if the widths of the 2 ranges (the range we want to
range_len + range_len < len // read, and the current extent) are greater than the width of
// the union range:
//
// In the first example, the 2 ranges overlap, and the width of
// the union is smaller than the sum of the widths of the ranges:
//
// |------range-1------|
// |---range-2----|
// |-----width-of-union-----|
// |-------sum----|-of---widths-------|
// |------------width-of-union------------|
// |------range-1------|
// |---range-2----|
//
// In this second example, the ranges do not overlap, and the
// width of the unions is equal or greater than the sum of the
// widths.
len < extent_len + range_len
} else { } else {
start2 < extent_end start < extent_end
} }
}) { }) {
//
let start = start.saturating_sub(offset); let start = start.saturating_sub(offset);
let end = end.map(|end| end - offset).unwrap_or(extent.len());
let end = end.map(|end| end - offset).unwrap_or(start + extent.len());
let len = end - start; let len = end - start;
log::info!("reading {}..{:?} from extent.", start, end); log::info!("reading {}..{:?} from extent.", start, end);
@ -607,9 +630,10 @@ impl<R: super::Read> Fs<R> {
let range = address let range = address
..(address ..(address
+ match extent.extent_data1().compression() { + match extent.extent_data1().compression() {
crate::structs::CompressionType::Zlib // compressed size
| crate::structs::CompressionType::Lzo CompressionType::Zlib
| crate::structs::CompressionType::ZStd => extent.size(), // compressed size | CompressionType::Lzo
| CompressionType::ZStd => extent.size(),
_ => len, _ => len,
}); });
@ -622,8 +646,8 @@ impl<R: super::Read> Fs<R> {
log::info!("compression: {:?}", extent.header().compression()); log::info!("compression: {:?}", extent.header().compression());
let data = match extent.header().compression() { let data = match extent.header().compression() {
crate::structs::CompressionType::None => data, CompressionType::None => data,
crate::structs::CompressionType::Zlib => { CompressionType::Zlib => {
let mut state = miniz_oxide::inflate::stream::InflateState::new( let mut state = miniz_oxide::inflate::stream::InflateState::new(
miniz_oxide::DataFormat::Zlib, miniz_oxide::DataFormat::Zlib,
); );
@ -638,8 +662,6 @@ impl<R: super::Read> Fs<R> {
miniz_oxide::MZFlush::None, miniz_oxide::MZFlush::None,
); );
log::info!("inflated: {:?}", result);
match result.status.map_err(|_| Error::DecompressionError)? { match result.status.map_err(|_| Error::DecompressionError)? {
miniz_oxide::MZStatus::Ok => {} miniz_oxide::MZStatus::Ok => {}
miniz_oxide::MZStatus::StreamEnd => break, miniz_oxide::MZStatus::StreamEnd => break,
@ -663,10 +685,10 @@ impl<R: super::Read> Fs<R> {
output_data.into() output_data.into()
} }
crate::structs::CompressionType::Lzo => { CompressionType::Lzo => {
todo!() todo!()
} }
crate::structs::CompressionType::ZStd => { CompressionType::ZStd => {
todo!() todo!()
} }
c => { c => {
@ -675,6 +697,7 @@ impl<R: super::Read> Fs<R> {
} }
}; };
// truncate inflated data if needed
contents.extend_from_slice(&data[..len as usize]); contents.extend_from_slice(&data[..len as usize]);
} }

View file

@ -164,7 +164,9 @@ fn find_file_zlib() -> Result<()> {
log::info!("chidlren: {:?}", children); log::info!("chidlren: {:?}", children);
let cmake_list = fs.get_inode_by_path(b"/quibble/LICENCE")?; let cmake_list = fs.get_inode_by_path(b"/quibble/LICENCE")?;
let file_contents = fs.read_inode_raw(&cmake_list, ..).expect("file contents"); let file_contents = fs
.read_inode_raw(&cmake_list, ..100)
.expect("file contents");
//assert_eq!(&file_contents[..11], b"hello world"); //assert_eq!(&file_contents[..11], b"hello world");
log::info!("license file:"); log::info!("license file:");
log::info!("{}", String::from_utf8_lossy(&file_contents)); log::info!("{}", String::from_utf8_lossy(&file_contents));
@ -207,7 +209,9 @@ fn find_file_zstd() -> Result<()> {
log::info!("chidlren: {:?}", children); log::info!("chidlren: {:?}", children);
let cmake_list = fs.get_inode_by_path(b"/quibble/LICENCE")?; let cmake_list = fs.get_inode_by_path(b"/quibble/LICENCE")?;
let file_contents = fs.read_inode_raw(&cmake_list, ..).expect("file contents"); let file_contents = fs
.read_inode_raw(&cmake_list, ..100)
.expect("file contents");
assert_eq!( assert_eq!(
&file_contents[..52], &file_contents[..52],
b" GNU LESSER GENERAL PUBLIC LICENSE" b" GNU LESSER GENERAL PUBLIC LICENSE"