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 chunk_tracker;
pub mod clone_to_owned; pub mod clone_to_owned;
pub mod constants; pub mod constants;
pub mod files_ops; pub mod file_ops;
pub mod lengths; pub mod lengths;
pub mod peer_binary_protocol; pub mod peer_binary_protocol;
pub mod peer_connection; pub mod peer_connection;

View file

@ -249,7 +249,7 @@ impl PeerConnection {
"read_chunk_blocking(peer={}, chunk_info={:?}", "read_chunk_blocking(peer={}, chunk_info={:?}",
peer_handle, &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??; .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 // 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... // should we really do? If we unmark it, it will get requested forever...
this.state this.state
.write_chunk_blocking(handle, &piece, &chunk_info)?; .file_ops()
.write_chunk(handle, &piece, &chunk_info)?;
if !should_checksum { if !should_checksum {
return Ok(()); return Ok(());
@ -537,7 +538,8 @@ impl PeerConnection {
match this match this
.state .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))? .with_context(|| format!("error checking piece={}", index))?
{ {
true => { true => {

View file

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

View file

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