Browse Source

Don't create subfolder inside temp folder

adaptive-webui-19844
Vladimir Golovnev (Glassez) 7 years ago
parent
commit
5f47d3b021
  1. 17
      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

17
src/base/bittorrent/session.cpp

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

2
src/base/bittorrent/session.h

@ -210,7 +210,7 @@ namespace BitTorrent @@ -210,7 +210,7 @@ namespace BitTorrent
void setTempPath(QString path);
bool isTempPathEnabled() const;
void setTempPathEnabled(bool enabled);
QString torrentTempPath(const InfoHash &hash) const;
QString torrentTempPath(const TorrentInfo &torrentInfo) const;
static bool isValidCategoryName(const QString &name);
// 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) @@ -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));
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;
Utils::Fs::smartRemoveEmptyFolderTree(m_oldPath);
}
@ -1926,9 +1926,9 @@ void TorrentHandle::adjustActualSavePath_impl() @@ -1926,9 +1926,9 @@ void TorrentHandle::adjustActualSavePath_impl()
path = savePath();
}
else {
// Moving all downloading torrents to temporary save path
path = m_session->torrentTempPath(hash());
qDebug() << "Moving torrent to its temp save path:" << path;
// Moving all downloading torrents to temporary folder
path = m_session->torrentTempPath(info());
qDebug() << "Moving torrent to its temporary folder:" << path;
}
moveStorage(Utils::Fs::toNativePath(path));

22
src/base/bittorrent/torrentinfo.cpp

@ -308,9 +308,29 @@ int BitTorrent::TorrentInfo::fileIndex(const QString& fileName) const @@ -308,9 +308,29 @@ int BitTorrent::TorrentInfo::fileIndex(const QString& fileName) const
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()
{
if (filesCount() <= 1) return;
if (!hasRootFolder()) return;
libtorrent::file_storage files = m_nativeInfo->files();

2
src/base/bittorrent/torrentinfo.h

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

Loading…
Cancel
Save