Restored cancellable scan

This commit is contained in:
emeric
2020-02-20 14:09:52 +01:00
parent 284231ff92
commit 0ad4973a5a
3 changed files with 23 additions and 12 deletions
+11 -7
View File
@@ -354,16 +354,18 @@ MediaScanner::countAllFiles(ScanStats& stats)
exploreFilesRecursive(_mediaDirectory, [&](std::error_code ec, const std::filesystem::path& path) exploreFilesRecursive(_mediaDirectory, [&](std::error_code ec, const std::filesystem::path& path)
{ {
if (ec) if (!_running)
return; return false;
if (isFileSupported(path, _fileExtensions)) if (!ec && isFileSupported(path, _fileExtensions))
{ {
stats.filesToScan++; stats.filesToScan++;
if (stats.filesToScan % 250 == 0) if (stats.filesToScan % 250 == 0)
notifyInProgressIfNeeded(stats); notifyInProgressIfNeeded(stats);
} }
return true;
}); });
} }
@@ -736,20 +738,22 @@ MediaScanner::scanMediaDirectory(const std::filesystem::path& mediaDirectory, bo
{ {
exploreFilesRecursive(mediaDirectory, [&](std::error_code ec, const std::filesystem::path& path) exploreFilesRecursive(mediaDirectory, [&](std::error_code ec, const std::filesystem::path& path)
{ {
if (!_running)
return false;
if (ec) if (ec)
{ {
LMS_LOG(DBUPDATER, ERROR) << "Cannot process entry '" << path.string() << "': " << ec.message(); LMS_LOG(DBUPDATER, ERROR) << "Cannot process entry '" << path.string() << "': " << ec.message();
stats.errors.emplace_back(ScanError {path, ScanErrorType::CannotReadFile, ec.message()}); stats.errors.emplace_back(ScanError {path, ScanErrorType::CannotReadFile, ec.message()});
return;
} }
else if (isFileSupported(path, _fileExtensions))
if (isFileSupported(path, _fileExtensions))
{ {
scanAudioFile(path, forceScan, stats ); scanAudioFile(path, forceScan, stats );
notifyInProgressIfNeeded(stats); notifyInProgressIfNeeded(stats);
} }
return true;
}); });
notifyInProgress(stats); notifyInProgress(stats);
+11 -4
View File
@@ -88,7 +88,7 @@ getLastWriteTime(const std::filesystem::path& file)
} }
void void
exploreFilesRecursive(const std::filesystem::path& directory, std::function<void(std::error_code, const std::filesystem::path&)> cb) exploreFilesRecursive(const std::filesystem::path& directory, std::function<bool(std::error_code, const std::filesystem::path&)> cb)
{ {
std::error_code ec; std::error_code ec;
std::filesystem::directory_iterator itPath {directory, std::filesystem::directory_options::follow_directory_symlink, ec}; std::filesystem::directory_iterator itPath {directory, std::filesystem::directory_options::follow_directory_symlink, ec};
@@ -102,23 +102,30 @@ exploreFilesRecursive(const std::filesystem::path& directory, std::function<void
std::filesystem::directory_iterator itEnd; std::filesystem::directory_iterator itEnd;
while (itPath != itEnd) while (itPath != itEnd)
{ {
bool continueExploring {true};
if (ec) if (ec)
{ {
cb(ec, *itPath); continueExploring = cb(ec, *itPath);
} }
else else
{ {
if (std::filesystem::is_regular_file(*itPath, ec)) if (std::filesystem::is_regular_file(*itPath, ec))
cb(ec, *itPath); {
continueExploring = cb(ec, *itPath);
}
else if (std::filesystem::is_directory(*itPath, ec)) else if (std::filesystem::is_directory(*itPath, ec))
{ {
if (!ec) if (!ec)
exploreFilesRecursive(*itPath, cb); exploreFilesRecursive(*itPath, cb);
else else
cb(ec, *itPath); continueExploring = cb(ec, *itPath);
} }
} }
if (!continueExploring)
break;
itPath.increment(ec); itPath.increment(ec);
} }
} }
+1 -1
View File
@@ -35,7 +35,7 @@ bool ensureDirectory(const std::filesystem::path& dir);
// Get the last write time since Epoch // Get the last write time since Epoch
Wt::WDateTime getLastWriteTime(const std::filesystem::path& dir); Wt::WDateTime getLastWriteTime(const std::filesystem::path& dir);
void exploreFilesRecursive(const std::filesystem::path& directory, std::function<void(std::error_code, const std::filesystem::path&)> cb); void exploreFilesRecursive(const std::filesystem::path& directory, std::function<bool(std::error_code, const std::filesystem::path&)> cb);
namespace std namespace std
{ {