Skip to content

Commit

Permalink
return reader references for recursive readers
Browse files Browse the repository at this point in the history
  • Loading branch information
iliazeus committed Oct 13, 2024
1 parent 511a989 commit e1d6451
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
16 changes: 3 additions & 13 deletions examples/list_files.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::io::{BufReader, Read, Seek};

use std::fs::File;
use std::path::{Path, PathBuf};
use std::path::PathBuf;

use anyhow::{Context, Error};

Expand All @@ -22,11 +20,9 @@ fn main() -> Result<(), Error> {

println!("extracting ISO metadata");

let source_iso_file = open_file_for_buffered_reading(&args.source_iso)
.context("error opening source ISO file")?;
let source_iso_file = File::open(&args.source_iso).context("error opening source ISO file")?;

let source_iso = iso::IsoReader::read(BufReader::new(source_iso_file))
.context("error reading source ISO")?;
let source_iso = iso::IsoReader::read(source_iso_file).context("error reading source ISO")?;

println!("{:?}", source_iso.volume_descriptor);
println!("max used size: {}", source_iso.get_max_used_prefix_size());
Expand All @@ -36,12 +32,6 @@ fn main() -> Result<(), Error> {
Ok(())
}

fn open_file_for_buffered_reading(path: &Path) -> Result<impl Read + Seek, Error> {
let file = File::options().read(true).open(path)?;
let file = BufReader::with_capacity(8 * 1024 * 1024, file);
Ok(file)
}

fn print_dir(path: String, dir: &iso::DirectoryTable) {
for entry in dir.entries.iter() {
if let Some(subdir) = &entry.subdirectory {
Expand Down
6 changes: 3 additions & 3 deletions src/bin/iso2god.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::{BufReader, Seek, SeekFrom, Write};
use std::io::{Seek, SeekFrom, Write};

use std::fs;
use std::fs::File;
Expand Down Expand Up @@ -65,8 +65,8 @@ fn main() -> Result<(), Error> {
let source_iso_file_meta =
fs::metadata(&args.source_iso).context("error reading source ISO file metadata")?;

let mut source_iso = iso::IsoReader::read(BufReader::new(source_iso_file))
.context("error reading source ISO")?;
let mut source_iso =
iso::IsoReader::read(source_iso_file).context("error reading source ISO")?;

let title_info =
TitleInfo::from_image(&mut source_iso).context("error reading image executable")?;
Expand Down
12 changes: 6 additions & 6 deletions src/iso/directory_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ bitflags! {

impl DirectoryTable {
pub fn read_root<R: Read + Seek>(
reader: R,
mut reader: R,
volume: &VolumeDescriptor,
) -> Result<DirectoryTable, Error> {
Self::read(
reader,
&mut reader,
volume,
volume.root_directory_sector,
volume.root_directory_size,
)
}

fn read<R: Read + Seek>(
mut reader: R,
reader: &mut R,
volume: &VolumeDescriptor,
sector: u32,
size: u32,
Expand All @@ -64,7 +64,7 @@ impl DirectoryTable {
((sector + sector_index) as u64) * volume.sector_size + volume.root_offset;
reader.seek(SeekFrom::Start(sector_position))?;

while let Some(entry) = DirectoryEntry::read(&mut reader, volume)? {
while let Some(entry) = DirectoryEntry::read(reader, volume)? {
entries.push(entry);
}
}
Expand All @@ -85,7 +85,7 @@ impl DirectoryTable {

impl DirectoryEntry {
fn read<R: Read + Seek>(
mut reader: R,
reader: &mut R,
volume: &VolumeDescriptor,
) -> Result<Option<DirectoryEntry>, Error> {
let subtree_left = reader.read_u16::<LE>()?;
Expand All @@ -112,7 +112,7 @@ impl DirectoryEntry {
let is_directory = attributes.contains(DirectoryEntryAttributes::DIRECTORY);
let subdirectory = if is_directory {
let reader_position = reader.stream_position()?;
let subdir = DirectoryTable::read(&mut reader, &volume, sector, size)?;
let subdir = DirectoryTable::read(reader, &volume, sector, size)?;
reader.seek(SeekFrom::Start(reader_position))?;
Some(subdir)
} else {
Expand Down

0 comments on commit e1d6451

Please sign in to comment.