This commit is contained in:
Igor Katson 2023-11-24 12:47:17 +00:00
parent d8538af25d
commit 0aa1af7933
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 18 additions and 22 deletions

View file

@ -42,30 +42,29 @@ fn ensure_file_length(file: &File, length: u64) -> anyhow::Result<()> {
} }
pub struct TorrentStateInitializing { pub struct TorrentStateInitializing {
info: Arc<ManagedTorrentInfo>, meta: Arc<ManagedTorrentInfo>,
only_files: Option<Vec<usize>>, only_files: Option<Vec<usize>>,
} }
impl TorrentStateInitializing { impl TorrentStateInitializing {
pub fn new(info: Arc<ManagedTorrentInfo>, only_files: Option<Vec<usize>>) -> Self { pub fn new(meta: Arc<ManagedTorrentInfo>, only_files: Option<Vec<usize>>) -> Self {
Self { info, only_files } Self { meta, only_files }
} }
pub async fn check(&self) -> anyhow::Result<TorrentStatePaused> { pub async fn check(&self) -> anyhow::Result<TorrentStatePaused> {
let (files, filenames) = { let (files, filenames) = {
let mut files = Vec::<Arc<Mutex<File>>>::with_capacity( let mut files =
(&self.info).info.iter_file_lengths()?.count(), Vec::<Arc<Mutex<File>>>::with_capacity(self.meta.info.iter_file_lengths()?.count());
);
let mut filenames = Vec::new(); let mut filenames = Vec::new();
for (path_bits, _) in (&self.info).info.iter_filenames_and_lengths()? { for (path_bits, _) in self.meta.info.iter_filenames_and_lengths()? {
let mut full_path = (&self.info).out_dir.clone(); let mut full_path = self.meta.out_dir.clone();
let relative_path = path_bits let relative_path = path_bits
.to_pathbuf() .to_pathbuf()
.context("error converting file to path")?; .context("error converting file to path")?;
full_path.push(relative_path); full_path.push(relative_path);
std::fs::create_dir_all(full_path.parent().unwrap())?; std::fs::create_dir_all(full_path.parent().unwrap())?;
let file = if (&self.info).options.overwrite { let file = if (&self.meta).options.overwrite {
OpenOptions::new() OpenOptions::new()
.create(true) .create(true)
.read(true) .read(true)
@ -87,12 +86,12 @@ impl TorrentStateInitializing {
}; };
let lengths = let lengths =
make_lengths(&(&self.info).info).context("unable to compute Lengths from torrent")?; make_lengths(&(&self.meta).info).context("unable to compute Lengths from torrent")?;
debug!("computed lengths: {:?}", &lengths); debug!("computed lengths: {:?}", &lengths);
info!("Doing initial checksum validation, this might take a while..."); info!("Doing initial checksum validation, this might take a while...");
let initial_check_results = (&self.info).spawner.spawn_block_in_place(|| { let initial_check_results = (&self.meta).spawner.spawn_block_in_place(|| {
FileOps::<Sha1>::new(&(&self.info).info, &files, &lengths) FileOps::<Sha1>::new(&(&self.meta).info, &files, &lengths)
.initial_check(self.only_files.as_deref()) .initial_check(self.only_files.as_deref())
})?; })?;
@ -102,10 +101,10 @@ impl TorrentStateInitializing {
SF::new(initial_check_results.needed_bytes) SF::new(initial_check_results.needed_bytes)
); );
(&self.info).spawner.spawn_block_in_place(|| { self.meta.spawner.spawn_block_in_place(|| {
for (idx, (file, (name, length))) in files for (idx, (file, (name, length))) in files
.iter() .iter()
.zip((&self.info).info.iter_filenames_and_lengths().unwrap()) .zip(self.meta.info.iter_filenames_and_lengths().unwrap())
.enumerate() .enumerate()
{ {
if self if self
@ -139,18 +138,13 @@ impl TorrentStateInitializing {
lengths, lengths,
); );
#[allow(clippy::needless_update)]
let state_options = TorrentStateOptions {
peer_connect_timeout: (&self.info).options.peer_connect_timeout,
peer_read_write_timeout: (&self.info).options.peer_read_write_timeout,
..Default::default()
};
let paused = TorrentStatePaused { let paused = TorrentStatePaused {
info: self.info.clone(), info: self.meta.clone(),
files, files,
filenames, filenames,
chunk_tracker, chunk_tracker,
have_bytes: initial_check_results.have_bytes,
needed_bytes: initial_check_results.needed_bytes,
}; };
Ok(paused) Ok(paused)
} }

View file

@ -11,4 +11,6 @@ pub struct TorrentStatePaused {
pub(crate) files: Vec<Arc<Mutex<File>>>, pub(crate) files: Vec<Arc<Mutex<File>>>,
pub(crate) filenames: Vec<PathBuf>, pub(crate) filenames: Vec<PathBuf>,
pub(crate) chunk_tracker: ChunkTracker, pub(crate) chunk_tracker: ChunkTracker,
pub(crate) have_bytes: u64,
pub(crate) needed_bytes: u64,
} }