From e8f6149a6d69777d84726c11316b010f92119b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Bri=C3=A8re?= Date: Thu, 27 Apr 2017 15:31:30 -0400 Subject: [PATCH] findIncompleteFiles(): Replace dir listing with individual checks. Closes #6265. Looking for incomplete files in a new torrent by using a pre-generated directory listing presents several disadvantages: - It requires us to extract the top-level directory name (in case it was renamed). - It requires us to know whether the top-level directory was stripped. - In the latter case, it may result in recursively traversing the entire contents of all downloaded torrents. Calling QFile::exists() individually for each file solves all these issues. In so doing, the handling of single-file and multiple-file torrents are rendered pretty much identical, and can therefore be merged. --- src/base/bittorrent/session.cpp | 43 ++++++--------------------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 09e3bc22f..62a0bf31b 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -33,7 +33,6 @@ #include #include #include -#include #include #include #include @@ -156,16 +155,6 @@ namespace return expanded; } - QStringList findAllFiles(const QString &dirPath) - { - QStringList files; - QDirIterator it(dirPath, QDir::Files, QDirIterator::Subdirectories); - while (it.hasNext()) - files << it.next(); - - return files; - } - template struct LowerLimited { @@ -1729,35 +1718,19 @@ bool Session::findIncompleteFiles(TorrentInfo &torrentInfo, QString &savePath) c { auto findInDir = [](const QString &dirPath, TorrentInfo &torrentInfo) -> bool { + const QDir dir(dirPath); bool found = false; - if (torrentInfo.filesCount() == 1) { - const QString filePath = dirPath + torrentInfo.filePath(0); - if (QFile(filePath).exists()) { + for (int i = 0; i < torrentInfo.filesCount(); ++i) { + const QString filePath = torrentInfo.filePath(i); + if (dir.exists(filePath)) { found = true; } - else if (QFile(filePath + QB_EXT).exists()) { + else if (dir.exists(filePath + QB_EXT)) { found = true; - torrentInfo.renameFile(0, torrentInfo.filePath(0) + QB_EXT); - } - } - else { - QSet allFiles; - int dirPathSize = dirPath.size(); - foreach (const QString &file, findAllFiles(dirPath + torrentInfo.name())) - allFiles << file.mid(dirPathSize); - for (int i = 0; i < torrentInfo.filesCount(); ++i) { - QString filePath = torrentInfo.filePath(i); - if (allFiles.contains(filePath)) { - found = true; - } - else { - filePath += QB_EXT; - if (allFiles.contains(filePath)) { - found = true; - torrentInfo.renameFile(i, filePath); - } - } + torrentInfo.renameFile(i, filePath + QB_EXT); } + if ((i % 100) == 0) + qApp->processEvents(); } return found;