From 2fc89b6ba75856888e917b393865366f6bdbced2 Mon Sep 17 00:00:00 2001 From: SW Date: Thu, 15 Oct 2020 12:53:29 -0600 Subject: [PATCH] Changes required to migrate from uTorrent. --- ImportHelper.cpp | 2 +- Store/TransmissionStateStore.cpp | 6 +++++- Store/uTorrentStateStore.cpp | 11 ++++++++--- Torrent/Box.h | 2 ++ Torrent/TorrentInfo.cpp | 20 ++++++++++++++++++++ Torrent/TorrentInfo.h | 2 ++ 6 files changed, 38 insertions(+), 5 deletions(-) diff --git a/ImportHelper.cpp b/ImportHelper.cpp index 2ef8865..3988feb 100644 --- a/ImportHelper.cpp +++ b/ImportHelper.cpp @@ -87,7 +87,7 @@ void ImportHelper::ImportImpl(fs::path const& targetDataDir, ITorrentStateIterat try { - Logger(Logger::Info) << prefix << "Import started"; + Logger(Logger::Info) << prefix << "Import started, SavePath: " << box.SavePath; m_targetStore->Import(targetDataDir, box, m_fileStreamProvider); ++result.SuccessCount; Logger(Logger::Info) << prefix << "Import succeeded"; diff --git a/Store/TransmissionStateStore.cpp b/Store/TransmissionStateStore.cpp index ec59561..1c8dda3 100644 --- a/Store/TransmissionStateStore.cpp +++ b/Store/TransmissionStateStore.cpp @@ -56,6 +56,7 @@ std::string const Dnd = "dnd"; std::string const DoneDate = "done-date"; std::string const Downloaded = "downloaded"; std::string const Name = "name"; +std::string const Files = "files"; std::string const Paused = "paused"; std::string const Priority = "priority"; std::string const Progress = "progress"; @@ -379,6 +380,9 @@ void TransmissionStateStore::Import(fs::path const& dataDir, Box const& box, IFi resume[RField::DoneDate] = static_cast(box.CompletedAt); resume[RField::Downloaded] = box.DownloadedSize; //resume["downloading-time-seconds"] = 0; + try { + resume[RField::Files] = box.Torrent.GetFiles(box.SavePath.filename().string()); + } catch (...) {} //resume["idle-limit"] = ojson::object(); //resume["max-peers"] = 5; resume[RField::Name] = box.SavePath.filename().string(); @@ -398,7 +402,7 @@ void TransmissionStateStore::Import(fs::path const& dataDir, Box const& box, IFi torrent.SetTrackers(box.Trackers); std::string const baseName = Util::GetEnvironmentVariable("BT_MIGRATE_TRANSMISSION_2_9X", {}).empty() ? - torrent.GetInfoHash() : resume[RField::Name].as_string() + '.' + torrent.GetInfoHash().substr(0, 16); + torrent.GetInfoHash() : box.Caption + '.' + torrent.GetInfoHash().substr(0, 16); fs::path const torrentFilePath = Detail::GetTorrentFilePath(dataDir, baseName, m_stateType); fs::create_directories(torrentFilePath.parent_path()); diff --git a/Store/uTorrentStateStore.cpp b/Store/uTorrentStateStore.cpp index fd9d254..19a3d6f 100644 --- a/Store/uTorrentStateStore.cpp +++ b/Store/uTorrentStateStore.cpp @@ -55,6 +55,7 @@ std::string const Path = "path"; std::string const Prio = "prio"; std::string const Started = "started"; std::string const Targets = "targets"; +std::string const Caption = "caption"; std::string const Trackers = "trackers"; std::string const Uploaded = "uploaded"; std::string const UpSpeed = "upspeed"; @@ -181,6 +182,7 @@ bool uTorrentTorrentStateIterator::GetNext(Box& nextBox) box.UploadedSize = resume[RField::Uploaded].as(); box.CorruptedSize = resume[RField::Corrupt].as(); box.SavePath = Util::GetPath(resume[RField::Path].as()); + box.Caption = resume[RField::Caption].as(); box.BlockSize = box.Torrent.GetPieceSize(); box.RatioLimit = FromStoreRatioLimit(resume[RField::OverrideSeedSettings], resume[RField::WantedRatio]); box.DownloadSpeedLimit = FromStoreSpeedLimit(resume[RField::DownSpeed]); @@ -195,7 +197,7 @@ bool uTorrentTorrentStateIterator::GetNext(Box& nextBox) fs::path const changedPath = GetChangedFilePath(targets, i); Box::FileInfo file; - file.DoNotDownload = filePriority == Detail::DoNotDownloadPriority; + file.DoNotDownload = (filePriority == Detail::DoNotDownloadPriority || filePriority < 0); file.Priority = file.DoNotDownload ? Box::NormalPriority : BoxHelper::Priority::FromStore(filePriority, Detail::MinPriority, Detail::MaxPriority); file.Path = changedPath; @@ -231,6 +233,7 @@ bool uTorrentTorrentStateIterator::GetNext(fs::path& torrentFilePath, ojson& res for (; m_torrentIt != m_torrentEnd; ++m_torrentIt) { + try{ torrentFilePath = m_dataDir / std::string(m_torrentIt->key()); if (torrentFilePath.extension().string() != Detail::TorrentFileExtension) { @@ -239,10 +242,12 @@ bool uTorrentTorrentStateIterator::GetNext(fs::path& torrentFilePath, ojson& res if (!fs::is_regular_file(torrentFilePath)) { - Logger(Logger::Warning) << "File " << torrentFilePath << " is not a regular file, skipping"; + //Logger(Logger::Warning) << "File " << torrentFilePath << " is not a regular file, skipping"; continue; } - + } catch (boost::filesystem::filesystem_error) { + continue; + } resume = m_torrentIt->value(); ++m_torrentIt; diff --git a/Torrent/Box.h b/Torrent/Box.h index dcb3379..44573ea 100644 --- a/Torrent/Box.h +++ b/Torrent/Box.h @@ -54,6 +54,7 @@ struct Box bool DoNotDownload; int Priority; boost::filesystem::path Path; + std::string Caption; FileInfo(); }; @@ -75,4 +76,5 @@ struct Box std::vector Files; std::vector ValidBlocks; std::vector> Trackers; + std::string Caption; }; diff --git a/Torrent/TorrentInfo.cpp b/Torrent/TorrentInfo.cpp index f506efa..acbd658 100644 --- a/Torrent/TorrentInfo.cpp +++ b/Torrent/TorrentInfo.cpp @@ -99,6 +99,26 @@ std::string TorrentInfo::GetName() const return info["name"].as(); } +ojson TorrentInfo::GetFiles(const std::string &base) const +{ + ojson result = ojson::array(); + + ojson const& info = m_torrent["info"]; + if (!info.contains("files")) { + throw 20; + } + ojson const& files = info["files"]; + for (std::size_t i = 0; i < files.size(); i++) { + std::string file_path = base; + for (ojson const& pathPart : files[i]["path"].array_range()) + { + file_path += "/" + pathPart.as(); + } + result.add(file_path); + } + return result; +} + fs::path TorrentInfo::GetFilePath(std::size_t fileIndex) const { fs::path result; diff --git a/Torrent/TorrentInfo.h b/Torrent/TorrentInfo.h index 2d2615d..ccdeb15 100644 --- a/Torrent/TorrentInfo.h +++ b/Torrent/TorrentInfo.h @@ -42,6 +42,8 @@ class TorrentInfo std::string GetName() const; boost::filesystem::path GetFilePath(std::size_t fileIndex) const; + ojson GetFiles(const std::string&) const; + void SetTrackers(std::vector> const& trackers); static TorrentInfo Decode(std::istream& stream, IStructuredDataCodec const& codec);