Move some fields into ManagedTorrentInfo

This commit is contained in:
Igor Katson 2024-08-21 16:06:16 +01:00
parent ad7b59ea3c
commit b4512e4809
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
3 changed files with 9 additions and 10 deletions

View file

@ -1117,6 +1117,7 @@ impl Session {
let span = error_span!(parent: self.rs(), "torrent", id); let span = error_span!(parent: self.rs(), "torrent", id);
let peer_opts = self.merge_peer_opts(opts.peer_opts); let peer_opts = self.merge_peer_opts(opts.peer_opts);
let minfo = Arc::new(ManagedTorrentInfo { let minfo = Arc::new(ManagedTorrentInfo {
id,
span, span,
file_infos, file_infos,
info, info,
@ -1127,6 +1128,7 @@ impl Session {
spawner: self.spawner, spawner: self.spawner,
peer_id: self.peer_id, peer_id: self.peer_id,
lengths, lengths,
storage_factory,
options: ManagedTorrentOptions { options: ManagedTorrentOptions {
force_tracker_interval: opts.force_tracker_interval, force_tracker_interval: opts.force_tracker_interval,
peer_connect_timeout: peer_opts.connect_timeout, peer_connect_timeout: peer_opts.connect_timeout,
@ -1141,16 +1143,14 @@ impl Session {
let initializing = Arc::new(TorrentStateInitializing::new( let initializing = Arc::new(TorrentStateInitializing::new(
minfo.clone(), minfo.clone(),
only_files.clone(), only_files.clone(),
storage_factory.create_and_init(&minfo)?, minfo.storage_factory.create_and_init(&minfo)?,
)); ));
let handle = Arc::new(ManagedTorrent { let handle = Arc::new(ManagedTorrent {
id,
locked: RwLock::new(ManagedTorrentLocked { locked: RwLock::new(ManagedTorrentLocked {
state: ManagedTorrentState::Initializing(initializing), state: ManagedTorrentState::Initializing(initializing),
only_files, only_files,
}), }),
state_change_notify: Notify::new(), state_change_notify: Notify::new(),
storage_factory,
info: minfo, info: minfo,
session: Arc::downgrade(self), session: Arc::downgrade(self),
}); });
@ -1257,7 +1257,7 @@ impl Session {
_ => None, _ => None,
}) })
.map(Ok) .map(Ok)
.unwrap_or_else(|| removed.storage_factory.create(removed.info())); .unwrap_or_else(|| removed.info.storage_factory.create(removed.info()));
match (storage, delete_files) { match (storage, delete_files) {
(Err(e), true) => return Err(e).context("torrent deleted, but could not delete files"), (Err(e), true) => return Err(e).context("torrent deleted, but could not delete files"),

View file

@ -128,6 +128,7 @@ impl JsonSessionPersistenceStore {
write_torrent_file: bool, write_torrent_file: bool,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
if !torrent if !torrent
.info
.storage_factory .storage_factory
.is_type_id(TypeId::of::<FilesystemStorageFactory>()) .is_type_id(TypeId::of::<FilesystemStorageFactory>())
{ {

View file

@ -100,6 +100,7 @@ pub(crate) struct ManagedTorrentOptions {
} }
pub struct ManagedTorrentInfo { pub struct ManagedTorrentInfo {
pub id: TorrentId,
pub info: TorrentMetaV1Info<ByteBufOwned>, pub info: TorrentMetaV1Info<ByteBufOwned>,
pub torrent_bytes: Bytes, pub torrent_bytes: Bytes,
pub info_bytes: Bytes, pub info_bytes: Bytes,
@ -112,14 +113,11 @@ pub struct ManagedTorrentInfo {
pub span: tracing::Span, pub span: tracing::Span,
pub(crate) options: ManagedTorrentOptions, pub(crate) options: ManagedTorrentOptions,
pub(crate) connector: Arc<StreamConnector>, pub(crate) connector: Arc<StreamConnector>,
pub(crate) storage_factory: BoxStorageFactory,
} }
pub struct ManagedTorrent { pub struct ManagedTorrent {
pub id: TorrentId,
// TODO: merge ManagedTorrent and ManagedTorrentInfo
pub info: Arc<ManagedTorrentInfo>, pub info: Arc<ManagedTorrentInfo>,
pub(crate) storage_factory: BoxStorageFactory,
pub(crate) session: Weak<Session>, pub(crate) session: Weak<Session>,
pub(crate) state_change_notify: Notify, pub(crate) state_change_notify: Notify,
pub(crate) locked: RwLock<ManagedTorrentLocked>, pub(crate) locked: RwLock<ManagedTorrentLocked>,
@ -127,7 +125,7 @@ pub struct ManagedTorrent {
impl ManagedTorrent { impl ManagedTorrent {
pub fn id(&self) -> TorrentId { pub fn id(&self) -> TorrentId {
self.id self.info.id
} }
pub fn info(&self) -> &ManagedTorrentInfo { pub fn info(&self) -> &ManagedTorrentInfo {
@ -369,7 +367,7 @@ impl ManagedTorrent {
let initializing = Arc::new(TorrentStateInitializing::new( let initializing = Arc::new(TorrentStateInitializing::new(
self.info.clone(), self.info.clone(),
g.only_files.clone(), g.only_files.clone(),
self.storage_factory.create_and_init(self.info())?, self.info.storage_factory.create_and_init(self.info())?,
)); ));
g.state = ManagedTorrentState::Initializing(initializing.clone()); g.state = ManagedTorrentState::Initializing(initializing.clone());
drop(g); drop(g);