Move all file operatiosn into a separate file

This commit is contained in:
Igor Katson 2021-06-28 15:17:11 +01:00
parent e0ffb3afe1
commit a799ff1219
4 changed files with 12 additions and 53 deletions

View file

@ -2,7 +2,7 @@ pub mod buffers;
pub mod chunk_tracker;
pub mod clone_to_owned;
pub mod constants;
pub mod files_ops;
pub mod file_ops;
pub mod lengths;
pub mod peer_binary_protocol;
pub mod peer_connection;

View file

@ -249,7 +249,7 @@ impl PeerConnection {
"read_chunk_blocking(peer={}, chunk_info={:?}",
peer_handle, &chunk_info
),
move || state.read_chunk_blocking(peer_handle, chunk_info),
move || state.file_ops().read_chunk(peer_handle, chunk_info),
)
.await??;
@ -529,7 +529,8 @@ impl PeerConnection {
// TODO: in theory we should unmark the piece as downloaded here. But if there was a disk error, what
// should we really do? If we unmark it, it will get requested forever...
this.state
.write_chunk_blocking(handle, &piece, &chunk_info)?;
.file_ops()
.write_chunk(handle, &piece, &chunk_info)?;
if !should_checksum {
return Ok(());
@ -537,7 +538,8 @@ impl PeerConnection {
match this
.state
.check_piece_blocking(handle, chunk_info.piece_index, &chunk_info)
.file_ops()
.check_piece(handle, chunk_info.piece_index, &chunk_info)
.with_context(|| format!("error checking piece={}", index))?
{
true => {

View file

@ -19,7 +19,7 @@ use size_format::SizeFormatterBinary as SF;
use crate::{
chunk_tracker::ChunkTracker,
files_ops::initial_check,
file_ops::FileOps,
lengths::Lengths,
peer_binary_protocol::MessageOwned,
peer_connection::PeerConnection,
@ -146,7 +146,7 @@ impl TorrentManager {
info!("Doing initial checksum validation, this might take a while...");
let initial_check_results =
initial_check(&torrent, &files, only_files.as_deref(), &lengths)?;
FileOps::new(&torrent, &files, &lengths).initial_check(only_files.as_deref())?;
info!(
"Initial check results: have {}, needed {}",

View file

@ -14,11 +14,10 @@ use parking_lot::{Mutex, RwLock};
use tokio::sync::mpsc::Sender;
use crate::{
buffers::ByteString,
chunk_tracker::ChunkTracker,
files_ops::{check_piece, read_chunk, write_chunk},
file_ops::FileOps,
lengths::{ChunkInfo, Lengths, ValidPieceIndex},
peer_binary_protocol::{Handshake, Message, MessageOwned, Piece},
peer_binary_protocol::{Handshake, Message, MessageOwned},
peer_state::{LivePeerState, PeerState},
torrent_metainfo::TorrentMetaV1Owned,
type_aliases::{PeerHandle, BF},
@ -160,50 +159,8 @@ pub struct TorrentState {
}
impl TorrentState {
pub fn check_piece_blocking(
&self,
who_sent: PeerHandle,
piece_index: ValidPieceIndex,
last_received_chunk: &ChunkInfo,
) -> anyhow::Result<bool> {
check_piece(
&self.torrent,
&self.files,
&self.lengths,
who_sent,
piece_index,
last_received_chunk,
)
}
pub fn read_chunk_blocking(
&self,
who_sent: PeerHandle,
chunk_info: ChunkInfo,
) -> anyhow::Result<Vec<u8>> {
read_chunk(
&self.torrent,
&self.files,
&self.lengths,
who_sent,
chunk_info,
)
}
pub fn write_chunk_blocking(
&self,
who_sent: PeerHandle,
data: &Piece<ByteString>,
chunk_info: &ChunkInfo,
) -> anyhow::Result<()> {
write_chunk(
&self.torrent,
&self.files,
&self.lengths,
who_sent,
data,
chunk_info,
)
pub fn file_ops(&self) -> FileOps<'_> {
FileOps::new(&self.torrent, &self.files, &self.lengths)
}
pub fn get_next_needed_piece(&self, peer_handle: PeerHandle) -> Option<ValidPieceIndex> {