From 4f0990dc297c40ba3047a539d3bce10803d67c78 Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Tue, 11 Oct 2022 10:05:21 +0300 Subject: [PATCH] Use 'fail if exist' strategy on automatic move Prevent existing files overwriting when torrent is moved automatically (e.g. when moved from incomplete to final save path). PR #17855. --- src/base/bittorrent/sessionimpl.cpp | 19 ++++++++++++++++--- src/base/bittorrent/torrentimpl.cpp | 2 +- src/base/bittorrent/torrentimpl.h | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index ceca6ed64..6a1fd4a49 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -298,6 +298,21 @@ namespace return {}; } #endif + + constexpr lt::move_flags_t toNative(const MoveStorageMode mode) + { + switch (mode) + { + default: + Q_ASSERT(false); + case MoveStorageMode::FailIfExist: + return lt::move_flags_t::fail_if_exist; + case MoveStorageMode::KeepExistingFiles: + return lt::move_flags_t::dont_replace; + case MoveStorageMode::Overwrite: + return lt::move_flags_t::always_replace_files; + } + } } struct BitTorrent::SessionImpl::ResumeSessionContext final : public QObject @@ -4690,9 +4705,7 @@ void SessionImpl::moveTorrentStorage(const MoveStorageJob &job) const const QString torrentName = (torrent ? torrent->name() : id.toString()); LogMsg(tr("Start moving torrent. Torrent: \"%1\". Destination: \"%2\"").arg(torrentName, job.path.toString())); - job.torrentHandle.move_storage(job.path.toString().toStdString() - , ((job.mode == MoveStorageMode::Overwrite) - ? lt::move_flags_t::always_replace_files : lt::move_flags_t::dont_replace)); + job.torrentHandle.move_storage(job.path.toString().toStdString(), toNative(job.mode)); } void SessionImpl::handleMoveTorrentStorageJobFinished(const Path &newPath) diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index f6a87e90e..094309340 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -2166,7 +2166,7 @@ void TorrentImpl::adjustStorageLocation() const Path targetPath = ((isFinished || downloadPath.isEmpty()) ? savePath() : downloadPath); if ((targetPath != actualStorageLocation()) || isMoveInProgress()) - moveStorage(targetPath, MoveStorageMode::Overwrite); + moveStorage(targetPath, MoveStorageMode::FailIfExist); } lt::torrent_handle TorrentImpl::nativeHandle() const diff --git a/src/base/bittorrent/torrentimpl.h b/src/base/bittorrent/torrentimpl.h index d03b319e8..eebacfd8f 100644 --- a/src/base/bittorrent/torrentimpl.h +++ b/src/base/bittorrent/torrentimpl.h @@ -63,6 +63,7 @@ namespace BitTorrent enum class MoveStorageMode { + FailIfExist, KeepExistingFiles, Overwrite };