Reuse file_infos in a couple places

This commit is contained in:
Igor Katson 2024-10-14 15:41:44 +01:00
parent 2e7c7216e3
commit 32d658e2d9
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 9 additions and 13 deletions

View file

@ -181,8 +181,8 @@ impl<'a> FileOps<'a> {
let mut piece_remaining_bytes = piece_length as usize; let mut piece_remaining_bytes = piece_length as usize;
for (file_idx, file_details) in self.torrent.iter_file_details()?.enumerate() { for (file_idx, fi) in self.file_infos.iter().enumerate() {
let file_len = file_details.len; let file_len = fi.len;
if absolute_offset > file_len { if absolute_offset > file_len {
absolute_offset -= file_len; absolute_offset -= file_len;
continue; continue;
@ -208,7 +208,7 @@ impl<'a> FileOps<'a> {
.with_context(|| { .with_context(|| {
format!( format!(
"error reading {to_read_in_file} bytes, file_id: {file_idx} (\"{:?}\")", "error reading {to_read_in_file} bytes, file_id: {file_idx} (\"{:?}\")",
file_details.filename fi.relative_filename
) )
})?; })?;
@ -250,7 +250,7 @@ impl<'a> FileOps<'a> {
let mut absolute_offset = self.lengths.chunk_absolute_offset(chunk_info); let mut absolute_offset = self.lengths.chunk_absolute_offset(chunk_info);
let mut buf = result_buf; let mut buf = result_buf;
for (file_idx, file_len) in self.torrent.iter_file_lengths()?.enumerate() { for (file_idx, file_len) in self.file_infos.iter().map(|d| d.len).enumerate() {
if absolute_offset > file_len { if absolute_offset > file_len {
absolute_offset -= file_len; absolute_offset -= file_len;
continue; continue;
@ -296,8 +296,8 @@ impl<'a> FileOps<'a> {
let mut buf = data.block.as_ref(); let mut buf = data.block.as_ref();
let mut absolute_offset = self.lengths.chunk_absolute_offset(chunk_info); let mut absolute_offset = self.lengths.chunk_absolute_offset(chunk_info);
for (file_idx, file_details) in self.torrent.iter_file_details()?.enumerate() { for (file_idx, file_info) in self.file_infos.iter().enumerate() {
let file_len = file_details.len; let file_len = file_info.len;
if absolute_offset > file_len { if absolute_offset > file_len {
absolute_offset -= file_len; absolute_offset -= file_len;
continue; continue;
@ -321,7 +321,7 @@ impl<'a> FileOps<'a> {
.with_context(|| { .with_context(|| {
format!( format!(
"error writing to file {file_idx} (\"{:?}\")", "error writing to file {file_idx} (\"{:?}\")",
file_details.filename file_info.relative_filename
) )
})?; })?;
buf = &buf[to_write..]; buf = &buf[to_write..];

View file

@ -151,13 +151,9 @@ impl TorrentStorage for FilesystemStorage {
fn init(&mut self, meta: &ManagedTorrentShared) -> anyhow::Result<()> { fn init(&mut self, meta: &ManagedTorrentShared) -> anyhow::Result<()> {
let mut files = Vec::<OpenedFile>::new(); let mut files = Vec::<OpenedFile>::new();
for file_details in meta.info.iter_file_details_ext(&meta.lengths)? { for file_details in meta.file_infos.iter() {
let mut full_path = self.output_folder.clone(); let mut full_path = self.output_folder.clone();
let relative_path = file_details let relative_path = &file_details.relative_filename;
.details
.filename
.to_pathbuf()
.context("error converting file to path")?;
full_path.push(relative_path); full_path.push(relative_path);
std::fs::create_dir_all(full_path.parent().context("bug: no parent")?)?; std::fs::create_dir_all(full_path.parent().context("bug: no parent")?)?;