From 18755d89715a23bbe00a0d5152e919d8dfb4065c Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Mon, 14 Oct 2024 15:16:55 +0100 Subject: [PATCH] Add access to new fields --- crates/librqbit/src/api.rs | 17 ++--- crates/librqbit/src/file_ops.rs | 18 ++++-- crates/librqbit/src/http_api.rs | 6 +- crates/librqbit/src/session.rs | 15 ++--- crates/librqbit/src/upnp_server_adapter.rs | 16 ++--- crates/librqbit_core/src/torrent_metainfo.rs | 66 ++++++++++---------- crates/rqbit/src/main.rs | 10 ++- 7 files changed, 78 insertions(+), 70 deletions(-) diff --git a/crates/librqbit/src/api.rs b/crates/librqbit/src/api.rs index c380828..aea4aba 100644 --- a/crates/librqbit/src/api.rs +++ b/crates/librqbit/src/api.rs @@ -533,23 +533,23 @@ fn make_torrent_details( output_folder: String, ) -> Result { let files = info - .iter_filenames_and_lengths() + .iter_file_details() .context("error iterating filenames and lengths")? .enumerate() - .map(|(idx, (filename_it, length))| { - let name = match filename_it.to_string() { + .map(|(idx, d)| { + let name = match d.filename.to_string() { Ok(s) => s, Err(err) => { warn!("error reading filename: {:?}", err); "".to_string() } }; - let components = filename_it.to_vec().unwrap_or_default(); + let components = d.filename.to_vec().unwrap_or_default(); let included = only_files.map(|o| o.contains(&idx)).unwrap_or(true); TorrentDetailsResponseFile { name, components, - length, + length: d.len, included, } }) @@ -568,10 +568,11 @@ fn torrent_file_mime_type( info: &TorrentMetaV1Info, file_idx: usize, ) -> Result<&'static str> { - info.iter_filenames_and_lengths()? + info.iter_file_details()? .nth(file_idx) - .and_then(|(f, _)| { - f.iter_components() + .and_then(|d| { + d.filename + .iter_components() .last() .and_then(|r| r.ok()) .and_then(|s| mime_guess::from_path(s).first_raw()) diff --git a/crates/librqbit/src/file_ops.rs b/crates/librqbit/src/file_ops.rs index 0442991..8bf16c4 100644 --- a/crates/librqbit/src/file_ops.rs +++ b/crates/librqbit/src/file_ops.rs @@ -181,7 +181,8 @@ impl<'a> FileOps<'a> { let mut piece_remaining_bytes = piece_length as usize; - for (file_idx, (name, file_len)) in self.torrent.iter_filenames_and_lengths()?.enumerate() { + for (file_idx, file_details) in self.torrent.iter_file_details()?.enumerate() { + let file_len = file_details.len; if absolute_offset > file_len { absolute_offset -= file_len; continue; @@ -205,7 +206,10 @@ impl<'a> FileOps<'a> { to_read_in_file, ) .with_context(|| { - format!("error reading {to_read_in_file} bytes, file_id: {file_idx} (\"{name:?}\")") + format!( + "error reading {to_read_in_file} bytes, file_id: {file_idx} (\"{:?}\")", + file_details.filename + ) })?; piece_remaining_bytes -= to_read_in_file; @@ -292,7 +296,8 @@ impl<'a> FileOps<'a> { let mut buf = data.block.as_ref(); let mut absolute_offset = self.lengths.chunk_absolute_offset(chunk_info); - for (file_idx, (name, file_len)) in self.torrent.iter_filenames_and_lengths()?.enumerate() { + for (file_idx, file_details) in self.torrent.iter_file_details()?.enumerate() { + let file_len = file_details.len; if absolute_offset > file_len { absolute_offset -= file_len; continue; @@ -313,7 +318,12 @@ impl<'a> FileOps<'a> { ); self.files .pwrite_all(file_idx, absolute_offset, &buf[..to_write]) - .with_context(|| format!("error writing to file {file_idx} (\"{name:?}\")"))?; + .with_context(|| { + format!( + "error writing to file {file_idx} (\"{:?}\")", + file_details.filename + ) + })?; buf = &buf[to_write..]; if buf.is_empty() { break; diff --git a/crates/librqbit/src/http_api.rs b/crates/librqbit/src/http_api.rs index e70de8b..69744ad 100644 --- a/crates/librqbit/src/http_api.rs +++ b/crates/librqbit/src/http_api.rs @@ -153,10 +153,10 @@ impl HttpApi { let mut playlist_items = handle .shared() .info - .iter_filenames_and_lengths()? + .iter_file_details()? .enumerate() - .filter_map(|(file_idx, (filename, _))| { - let filename = filename.to_vec().ok()?.join("/"); + .filter_map(|(file_idx, file_details)| { + let filename = file_details.filename.to_vec().ok()?.join("/"); let is_playable = mime_guess::from_path(&filename) .first() .map(|mime| { diff --git a/crates/librqbit/src/session.rs b/crates/librqbit/src/session.rs index 5418982..2aff62b 100644 --- a/crates/librqbit/src/session.rs +++ b/crates/librqbit/src/session.rs @@ -156,8 +156,9 @@ fn compute_only_files_regex>( ) -> anyhow::Result> { let filename_re = regex::Regex::new(filename_re).context("filename regex is incorrect")?; let mut only_files = Vec::new(); - for (idx, (filename, _)) in torrent.iter_filenames_and_lengths()?.enumerate() { - let full_path = filename + for (idx, fd) in torrent.iter_file_details()?.enumerate() { + let full_path = fd + .filename .to_pathbuf() .with_context(|| format!("filename of file {idx} is not valid utf8"))?; if filename_re.is_match(full_path.to_str().unwrap()) { @@ -191,12 +192,12 @@ fn compute_only_files( } (None, Some(filename_re)) => { let only_files = compute_only_files_regex(info, &filename_re)?; - for (idx, (filename, _)) in info.iter_filenames_and_lengths()?.enumerate() { + for (idx, fd) in info.iter_file_details()?.enumerate() { if !only_files.contains(&idx) { continue; } if !list_only { - info!(?filename, "will download"); + info!(filename=?fd.filename, "will download"); } } Ok(Some(only_files)) @@ -1043,8 +1044,8 @@ impl Session { info: &TorrentMetaV1Info, ) -> anyhow::Result> { let files = info - .iter_filenames_and_lengths()? - .map(|(f, l)| Ok((f.to_pathbuf()?, l))) + .iter_file_details()? + .map(|fd| Ok((fd.filename.to_pathbuf()?, fd.len))) .collect::>>()?; if files.len() < 2 { return Ok(None); @@ -1145,7 +1146,7 @@ impl Session { .map(|fd| { Ok::<_, anyhow::Error>(FileInfo { relative_filename: fd.details.filename.to_pathbuf()?, - offset_in_torrent: fd.details.offset, + offset_in_torrent: fd.offset, piece_range: fd.pieces, len: fd.details.len, }) diff --git a/crates/librqbit/src/upnp_server_adapter.rs b/crates/librqbit/src/upnp_server_adapter.rs index d91b0e7..ab60f37 100644 --- a/crates/librqbit/src/upnp_server_adapter.rs +++ b/crates/librqbit/src/upnp_server_adapter.rs @@ -68,10 +68,10 @@ impl TorrentFileTreeNode { let last_url_bit = torrent .shared() .info - .iter_filenames_and_lengths() + .iter_file_details() .ok() .and_then(|mut it| it.nth(fid)) - .and_then(|(fi, _)| fi.to_vec().ok()) + .and_then(|fd| fd.filename.to_vec().ok()) .map(|components| { components .into_iter() @@ -111,10 +111,10 @@ struct TorrentFileTree { } fn is_single_file_at_root(info: &TorrentMetaV1Info) -> bool { - info.iter_filenames_and_lengths() + info.iter_file_details() .into_iter() .flatten() - .flat_map(|(f, _)| f.iter_components()) + .flat_map(|fd| fd.filename.iter_components()) .nth(1) .is_none() } @@ -123,10 +123,10 @@ impl TorrentFileTree { fn build(torent_id: TorrentId, info: &TorrentMetaV1Info) -> anyhow::Result { if is_single_file_at_root(info) { let filename = info - .iter_filenames_and_lengths()? + .iter_file_details()? .next() .context("bug")? - .0 + .filename .iter_components() .last() .context("bug")??; @@ -159,8 +159,8 @@ impl TorrentFileTree { let mut name_cache = HashMap::new(); - for (fid, (fi, _)) in info.iter_filenames_and_lengths()?.enumerate() { - let components = match fi.to_vec() { + for (fid, fd) in info.iter_file_details()?.enumerate() { + let components = match fd.filename.to_vec() { Ok(v) => v, Err(_) => continue, }; diff --git a/crates/librqbit_core/src/torrent_metainfo.rs b/crates/librqbit_core/src/torrent_metainfo.rs index 8a5bc0d..0d3cef7 100644 --- a/crates/librqbit_core/src/torrent_metainfo.rs +++ b/crates/librqbit_core/src/torrent_metainfo.rs @@ -182,18 +182,19 @@ where pub struct FileDetails<'a, BufType> { pub filename: FileIteratorName<'a, BufType>, - // absolute offset in torrent if it was a flat blob of bytes - pub offset: u64, pub len: u64, // bep-47 - pub attr: Option, - pub sha1: Option, - pub symlink_path: Option>, + pub attr: Option<&'a BufType>, + pub sha1: Option<&'a BufType>, + pub symlink_path: Option<&'a [BufType]>, } pub struct FileDetailsExt<'a, BufType> { pub details: FileDetails<'a, BufType>, + // absolute offset in torrent if it was a flat blob of bytes + pub offset: u64, + // the pieces that contain this file pub pieces: std::ops::Range, } @@ -220,33 +221,38 @@ impl> TorrentMetaV1Info { } #[inline(never)] - pub fn iter_filenames_and_lengths( + pub fn iter_file_details( &self, - ) -> anyhow::Result, u64)>> { + ) -> anyhow::Result>> { match (self.length, self.files.as_ref()) { // Single-file - (Some(length), None) => Ok(Either::Left(once(( - FileIteratorName::Single(self.name.as_ref()), - length, - )))), + (Some(length), None) => Ok(Either::Left(once(FileDetails { + filename: FileIteratorName::Single(self.name.as_ref()), + len: length, + attr: self.attr.as_ref(), + sha1: self.sha1.as_ref(), + symlink_path: self.symlink_path.as_deref(), + }))), // Multi-file (None, Some(files)) => { if files.is_empty() { anyhow::bail!("expected multi-file torrent to have at least one file") } - Ok(Either::Right( - files - .iter() - .map(|f| (FileIteratorName::Tree(&f.path), f.length)), - )) + Ok(Either::Right(files.iter().map(|f| FileDetails { + filename: FileIteratorName::Tree(&f.path), + len: f.length, + attr: f.attr.as_ref(), + sha1: f.sha1.as_ref(), + symlink_path: f.symlink_path.as_deref(), + }))) } _ => anyhow::bail!("torrent can't be both in single and multi-file mode"), } } pub fn iter_file_lengths(&self) -> anyhow::Result + '_> { - Ok(self.iter_filenames_and_lengths()?.map(|(_, l)| l)) + Ok(self.iter_file_details()?.map(|d| d.len)) } // NOTE: lenghts MUST be construced with Lenghts::from_torrent, otherwise @@ -255,23 +261,15 @@ impl> TorrentMetaV1Info { &'a self, lengths: &'a Lengths, ) -> anyhow::Result> + 'a> { - Ok(self - .iter_filenames_and_lengths()? - .scan(0u64, |acc_offset, (filename, len)| { - let offset = *acc_offset; - *acc_offset += len; - Some(FileDetailsExt { - details: FileDetails { - filename, - offset, - len, - attr: None, - sha1: None, - symlink_path: None, - }, - pieces: lengths.iter_pieces_within_offset(offset, len), - }) - })) + Ok(self.iter_file_details()?.scan(0u64, |acc_offset, details| { + let offset = *acc_offset; + *acc_offset += details.len; + Some(FileDetailsExt { + pieces: lengths.iter_pieces_within_offset(offset, details.len), + details, + offset, + }) + })) } } diff --git a/crates/rqbit/src/main.rs b/crates/rqbit/src/main.rs index 6806125..a03c87c 100644 --- a/crates/rqbit/src/main.rs +++ b/crates/rqbit/src/main.rs @@ -762,17 +762,15 @@ async fn async_main(opts: Opts, cancel: CancellationToken) -> anyhow::Result<()> only_files, .. }) => { - for (idx, (filename, len)) in - info.iter_filenames_and_lengths()?.enumerate() - { + for (idx, fd) in info.iter_file_details()?.enumerate() { let included = match &only_files { Some(files) => files.contains(&idx), None => true, }; info!( - "File {}, size {}{}", - filename.to_string()?, - SF::new(len), + "File {:?}, size {}{}", + fd.filename, + SF::new(fd.len), if included { "" } else { ", will skip" } ) }