Skip to content

Commit

Permalink
Don't create subfolder inside temp folder
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Jul 29, 2017
1 parent d22b626 commit 5f47d3b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
17 changes: 9 additions & 8 deletions src/base/bittorrent/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/base/bittorrent/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/base/bittorrent/torrenthandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
Expand Down
22 changes: 21 additions & 1 deletion src/base/bittorrent/torrentinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 2 additions & 0 deletions src/base/bittorrent/torrentinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5f47d3b

Please sign in to comment.