From 3c871cd892e30034fc6778f6d74bd37852b701a0 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 25 Sep 2026 08:40:28 -0400 Subject: [PATCH] don't consider duplicated torrent as error. Return id of existing one instead --- libi2pd_client/TorrentsTunnel.cpp | 40 +++++++++++++++++++------------ libi2pd_client/TorrentsTunnel.h | 2 +- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/libi2pd_client/TorrentsTunnel.cpp b/libi2pd_client/TorrentsTunnel.cpp index 4fa0659b..d57aaf9a 100644 --- a/libi2pd_client/TorrentsTunnel.cpp +++ b/libi2pd_client/TorrentsTunnel.cpp @@ -137,16 +137,14 @@ namespace torrents if (torrent && !torrent->IsValid ()) { - // a torrent whose parsing stopped, an unsafe name among the rest, must - // not be used even in part: it would leave stray files behind LogPrint (eLogError, "TorrentsTunnel: Invalid torrent file ", torrentFilePath, ". Skipped"); torrent = nullptr; } if (torrent) { torrent->SetFullPath (m_TorrentsDir/std::filesystem::path (torrent->GetName ())); - InitTorrentFiles (torrent); InsertTorrent (torrent); + InitTorrentFiles (torrent); } } @@ -333,12 +331,13 @@ namespace torrents if (m_Torrents.find (torrent->GetInfoHash ()) == m_Torrents.end ()) { torrent->SetFullPath (m_TorrentsDir/std::filesystem::path (torrent->GetName ())); - boost::asio::post (GetDiskIOService (), [this, torrent]() - { - SaveTorrentFile (torrent); - InitTorrentFiles (torrent); - }); - auto id = InsertTorrent (torrent); + auto [id, inserted] = InsertTorrent (torrent); + if (inserted) + boost::asio::post (GetDiskIOService (), [this, torrent]() + { + SaveTorrentFile (torrent); + InitTorrentFiles (torrent); + }); if (!torrent->GetError ()) boost::asio::post (GetService (), [this, torrent] { RequestTorrentTrackers (torrent, eTrackerAnnounceEventNone); }); return { torrent, id }; @@ -396,17 +395,18 @@ namespace torrents auto torrent = std::make_shared (infoHash); if (!announce.empty ()) torrent->SetAnnounce (announce); if (!name.empty ()) torrent->SetName (name); - auto id = InsertTorrent (torrent); - boost::asio::post (GetService (), [this, torrent] { RequestTorrentTrackers (torrent, eTrackerAnnounceEventNone); }); + auto [id, inserted] = InsertTorrent (torrent); + if (inserted) + boost::asio::post (GetService (), [this, torrent] { RequestTorrentTrackers (torrent, eTrackerAnnounceEventNone); }); return { torrent, id }; } } return { nullptr, 0 }; } - int TorrentsTunnel::InsertTorrent (std::shared_ptr torrent) + std::pair TorrentsTunnel::InsertTorrent (std::shared_ptr torrent) { - if (!torrent) return 0; + if (!torrent) return { 0, false }; std::lock_guard l(m_TorrentsMutex); if (m_Torrents.emplace (torrent->GetInfoHash (), torrent).second) { @@ -416,9 +416,19 @@ namespace torrents m_TorrentsByID.emplace (id, torrent); if (!torrent->GetAnnounce ().empty ()) torrent->SetAnnounceTrackerID (AddTracker (torrent->GetAnnounce (), false)); // add announce to trackers - return id; + return { id, true }; } - return 0; + else // already exists, find id + { + auto it = std::find_if (m_TorrentsByID.begin (), m_TorrentsByID.end (), + [torrent](const auto& torrentByID) + { + return torrentByID.second.lock () == torrent; + }); + if (it != m_TorrentsByID.begin ()) + return { it->first, false }; + } + return { 0, false }; } bool TorrentsTunnel::RemoveTorrent (int id, bool deleteFiles) diff --git a/libi2pd_client/TorrentsTunnel.h b/libi2pd_client/TorrentsTunnel.h index 251f8ce2..c858c489 100644 --- a/libi2pd_client/TorrentsTunnel.h +++ b/libi2pd_client/TorrentsTunnel.h @@ -127,7 +127,7 @@ namespace torrents void ReadTorrentFile (const std::filesystem::path& torrentFilePath); void SaveTorrentFile (std::shared_ptr torrent); void InitTorrentFiles (std::shared_ptr torrent); - int InsertTorrent (std::shared_ptr torrent); // returns id > 0 if success and 0 if failed + std::pair InsertTorrent (std::shared_ptr torrent); // returns id > 0 if success and 0 if failed, true is innserted void RemoveTorrent (std::shared_ptr torrent, bool deleteFiles); bool CreateAndReserveFile (const std::filesystem::path& filePath, size_t reserve); void CompleteTorrent (std::shared_ptr torrent);