Browse Source

Merge pull request #7176 from glassez/temp-folder

Don't create subfolder inside temp folder. Closes #5503
adaptive-webui-19844
sledgehammer999 7 years ago committed by GitHub
parent
commit
6043584305
  1. 15
      src/base/bittorrent/session.cpp
  2. 2
      src/base/bittorrent/session.h
  3. 8
      src/base/bittorrent/torrenthandle.cpp
  4. 22
      src/base/bittorrent/torrentinfo.cpp
  5. 2
      src/base/bittorrent/torrentinfo.h

15
src/base/bittorrent/session.cpp

@ -613,11 +613,14 @@ QString Session::tempPath() const
return Utils::Fs::fromNativePath(m_tempPath); return Utils::Fs::fromNativePath(m_tempPath);
} }
QString Session::torrentTempPath(const InfoHash &hash) const QString Session::torrentTempPath(const TorrentInfo &torrentInfo) const
{ {
if ((torrentInfo.filesCount() > 1) && !torrentInfo.hasRootFolder())
return tempPath() return tempPath()
+ static_cast<QString>(hash).left(7) + QString::fromStdString(torrentInfo.nativeInfo()->orig_files().name())
+ "/"; + "/";
return tempPath();
} }
bool Session::isValidCategoryName(const QString &name) bool Session::isValidCategoryName(const QString &name)
@ -1648,7 +1651,7 @@ bool Session::deleteTorrent(const QString &hash, bool deleteLocalFiles)
// Remove it from session // Remove it from session
if (deleteLocalFiles) { if (deleteLocalFiles) {
if (torrent->savePath(true) == torrentTempPath(torrent->hash())) { if (torrent->savePath(true) == torrentTempPath(torrent->info())) {
m_savePathsToRemove[torrent->hash()] = torrent->savePath(true); m_savePathsToRemove[torrent->hash()] = torrent->savePath(true);
} }
else { else {
@ -1996,7 +1999,7 @@ bool Session::findIncompleteFiles(TorrentInfo &torrentInfo, QString &savePath) c
bool found = findInDir(savePath, torrentInfo); bool found = findInDir(savePath, torrentInfo);
if (!found && isTempPathEnabled()) { if (!found && isTempPathEnabled()) {
savePath = torrentTempPath(torrentInfo.hash()); savePath = torrentTempPath(torrentInfo);
found = findInDir(savePath, torrentInfo); found = findInDir(savePath, torrentInfo);
} }
@ -3698,9 +3701,7 @@ void Session::handleTorrentRemovedAlert(libt::torrent_removed_alert *p)
void Session::handleTorrentDeletedAlert(libt::torrent_deleted_alert *p) void Session::handleTorrentDeletedAlert(libt::torrent_deleted_alert *p)
{ {
const QString path = m_savePathsToRemove.take(p->info_hash); Utils::Fs::smartRemoveEmptyFolderTree(m_savePathsToRemove.take(p->info_hash));
if (path == torrentTempPath(p->info_hash))
Utils::Fs::smartRemoveEmptyFolderTree(path);
} }
void Session::handleTorrentDeleteFailedAlert(libt::torrent_delete_failed_alert *p) void Session::handleTorrentDeleteFailedAlert(libt::torrent_delete_failed_alert *p)

2
src/base/bittorrent/session.h

@ -210,7 +210,7 @@ namespace BitTorrent
void setTempPath(QString path); void setTempPath(QString path);
bool isTempPathEnabled() const; bool isTempPathEnabled() const;
void setTempPathEnabled(bool enabled); void setTempPathEnabled(bool enabled);
QString torrentTempPath(const InfoHash &hash) const; QString torrentTempPath(const TorrentInfo &torrentInfo) const;
static bool isValidCategoryName(const QString &name); static bool isValidCategoryName(const QString &name);
// returns category itself and all top level categories // returns category itself and all top level categories

8
src/base/bittorrent/torrenthandle.cpp

@ -1498,7 +1498,7 @@ void TorrentHandle::handleStorageMovedAlert(libtorrent::storage_moved_alert *p)
} }
qDebug("Torrent is successfully moved from %s to %s", qPrintable(m_oldPath), qPrintable(m_newPath)); qDebug("Torrent is successfully moved from %s to %s", qPrintable(m_oldPath), qPrintable(m_newPath));
if (QDir(m_oldPath) == QDir(m_session->torrentTempPath(hash()))) { if (QDir(m_oldPath) == QDir(m_session->torrentTempPath(info()))) {
qDebug() << "Removing torrent temp folder:" << m_oldPath; qDebug() << "Removing torrent temp folder:" << m_oldPath;
Utils::Fs::smartRemoveEmptyFolderTree(m_oldPath); Utils::Fs::smartRemoveEmptyFolderTree(m_oldPath);
} }
@ -1926,9 +1926,9 @@ void TorrentHandle::adjustActualSavePath_impl()
path = savePath(); path = savePath();
} }
else { else {
// Moving all downloading torrents to temporary save path // Moving all downloading torrents to temporary folder
path = m_session->torrentTempPath(hash()); path = m_session->torrentTempPath(info());
qDebug() << "Moving torrent to its temp save path:" << path; qDebug() << "Moving torrent to its temporary folder:" << path;
} }
moveStorage(Utils::Fs::toNativePath(path)); moveStorage(Utils::Fs::toNativePath(path));

22
src/base/bittorrent/torrentinfo.cpp

@ -308,9 +308,29 @@ int BitTorrent::TorrentInfo::fileIndex(const QString& fileName) const
return -1; return -1;
} }
bool TorrentInfo::hasRootFolder() const
{
QString testRootFolder;
for (int i = 0; i < filesCount(); ++i) {
const QString filePath = this->filePath(i);
if (QDir::isAbsolutePath(filePath)) continue;
const auto filePathElements = filePath.splitRef('/');
// if at least one file has no root folder, no common root folder exists
if (filePathElements.count() <= 1) return false;
if (testRootFolder.isEmpty())
testRootFolder = filePathElements.at(0).toString();
else if (testRootFolder != filePathElements.at(0))
return false;
}
return true;
}
void TorrentInfo::stripRootFolder() void TorrentInfo::stripRootFolder()
{ {
if (filesCount() <= 1) return; if (!hasRootFolder()) return;
libtorrent::file_storage files = m_nativeInfo->files(); libtorrent::file_storage files = m_nativeInfo->files();

2
src/base/bittorrent/torrentinfo.h

@ -100,6 +100,8 @@ namespace BitTorrent
PieceRange filePieces(int fileIndex) const; PieceRange filePieces(int fileIndex) const;
void renameFile(uint index, const QString &newPath); void renameFile(uint index, const QString &newPath);
bool hasRootFolder() const;
void stripRootFolder(); void stripRootFolder();
NativePtr nativeInfo() const; NativePtr nativeInfo() const;

Loading…
Cancel
Save