diff --git a/src/libs/core/impl/Path.cpp b/src/libs/core/impl/Path.cpp index fc7c188a..ff7436a4 100644 --- a/src/libs/core/impl/Path.cpp +++ b/src/libs/core/impl/Path.cpp @@ -35,63 +35,6 @@ namespace lms::core::pathUtils return std::filesystem::create_directory(dir); } - bool exploreFilesRecursive(const std::filesystem::path& directory, ExploreFileCallback cb, const std::filesystem::path* excludeDirFileName) - { - std::error_code ec; - std::filesystem::directory_iterator itPath{ directory, std::filesystem::directory_options::follow_directory_symlink, ec }; - - if (ec) - { - cb(ec, directory, nullptr); - return true; // try to continue exploring anyway - } - - if (excludeDirFileName && !excludeDirFileName->empty()) - { - const std::filesystem::path excludePath{ directory / *excludeDirFileName }; - - if (std::filesystem::exists(excludePath, ec)) - { - // TODO: handle exclude another way + remove this log - LMS_LOG(DBUPDATER, DEBUG, "Found " << excludePath << ": skipping directory"); - return true; - } - } - - std::filesystem::directory_iterator itEnd; - while (itPath != itEnd) - { - bool continueExploring{ true }; - - if (ec) - { - continueExploring = cb(ec, *itPath, nullptr); - } - else - { - const std::filesystem::directory_entry& entry{ *itPath }; - - if (entry.is_regular_file()) - { - FileInfo fileInfo; - fileInfo.fileSize = entry.file_size(); - fileInfo.lastWriteTime = Wt::WDateTime{ std::chrono::file_clock::to_sys(entry.last_write_time()) }; - - continueExploring = cb(ec, *itPath, &fileInfo); - } - else if (entry.is_directory()) - continueExploring = exploreFilesRecursive(*itPath, cb, excludeDirFileName); - } - - if (!continueExploring) - return false; - - itPath.increment(ec); - } - - return true; - } - bool hasFileAnyExtension(const std::filesystem::path& file, std::span supportedExtensions) { const std::filesystem::path extension{ stringUtils::stringToLower(file.extension().c_str()) }; diff --git a/src/libs/core/include/core/Path.hpp b/src/libs/core/include/core/Path.hpp index ccc4c64b..2d8fece0 100644 --- a/src/libs/core/include/core/Path.hpp +++ b/src/libs/core/include/core/Path.hpp @@ -20,9 +20,7 @@ #pragma once #include -#include #include -#include #include @@ -32,15 +30,6 @@ namespace lms::core::pathUtils // Create it if needed bool ensureDirectory(const std::filesystem::path& dir); - struct FileInfo - { - Wt::WDateTime lastWriteTime; // Last write time of the file since Epoch - std::uint64_t fileSize{}; // Size of the file in bytes - }; - using ExploreFileCallback = std::function; - // returns false if aborted by user - bool exploreFilesRecursive(const std::filesystem::path& directory, ExploreFileCallback cb, const std::filesystem::path* excludeDirFileName = {}); - // Check if file's extension is one of provided extensions bool hasFileAnyExtension(const std::filesystem::path& file, std::span extensions); diff --git a/src/libs/metadata/impl/taglib/Utils.cpp b/src/libs/metadata/impl/taglib/Utils.cpp index 17e0f93a..4142c99c 100644 --- a/src/libs/metadata/impl/taglib/Utils.cpp +++ b/src/libs/metadata/impl/taglib/Utils.cpp @@ -154,10 +154,7 @@ namespace lms::metadata::taglib::utils #endif if (file && !file->isValid()) - { - LMS_LOG(METADATA, DEBUG, "File " << file << ": failed to parse by extension"); file.reset(); - } return file; } @@ -208,10 +205,7 @@ namespace lms::metadata::taglib::utils #endif if (file && !file->isValid()) - { - LMS_LOG(METADATA, DEBUG, "File " << file << ": failed to parse by content"); file.reset(); - } return file; } @@ -223,7 +217,12 @@ namespace lms::metadata::taglib::utils TagLib::FileStream fileStream{ createFileStream(p) }; std::unique_ptr file{ parseFileByExtension(&fileStream, p.extension(), readStyle, readAudioProperties.value()) }; if (!file) + { + LMS_LOG(METADATA, DEBUG, "File " << p << ": failed to parse by extension"); file = parseFileByContent(&fileStream, readStyle, readAudioProperties.value()); + if (!file) + LMS_LOG(METADATA, DEBUG, "File " << p << ": failed to parse by content"); + } return file; } diff --git a/src/libs/services/scanner/impl/steps/ScanStepScanFiles.cpp b/src/libs/services/scanner/impl/steps/ScanStepScanFiles.cpp index 29d9c576..bfafaef1 100644 --- a/src/libs/services/scanner/impl/steps/ScanStepScanFiles.cpp +++ b/src/libs/services/scanner/impl/steps/ScanStepScanFiles.cpp @@ -37,10 +37,59 @@ namespace lms::scanner { - using namespace db; - namespace { + using ExploreFileCallback = std::function; + bool exploreFilesRecursive(const std::filesystem::path& directory, ExploreFileCallback cb, const std::filesystem::path* excludeDirFileName) + { + std::error_code ec; + std::filesystem::directory_iterator itPath{ directory, std::filesystem::directory_options::follow_directory_symlink, ec }; + + if (ec) + { + cb(ec, directory, nullptr); + return true; // try to continue exploring anyway + } + + if (excludeDirFileName && !excludeDirFileName->empty()) + { + const std::filesystem::path excludePath{ directory / *excludeDirFileName }; + if (std::filesystem::exists(excludePath, ec)) + { + LMS_LOG(DBUPDATER, DEBUG, "Found " << excludePath << ": skipping directory"); + return true; + } + } + + std::filesystem::directory_iterator itEnd; + while (itPath != itEnd) + { + bool continueExploring{ true }; + + const std::filesystem::directory_entry& entry{ *itPath }; + const std::filesystem::path& path{ entry.path() }; + + if (ec) + { + continueExploring = cb(ec, path, nullptr); + } + else + { + if (entry.is_regular_file()) + continueExploring = cb(ec, path, &entry); + else if (entry.is_directory()) + continueExploring = exploreFilesRecursive(path, cb, excludeDirFileName); + } + + if (!continueExploring) + return false; + + itPath.increment(ec); + } + + return true; + } + class FileScanJob : public core::IJob { public: @@ -97,11 +146,11 @@ namespace lms::scanner std::vector> jobsDone; std::vector> scanOperations; - core::pathUtils::exploreFilesRecursive( - mediaLibrary.rootDirectory, [&](std::error_code ec, const std::filesystem::path& path, const core::pathUtils::FileInfo* fileInfo) { + exploreFilesRecursive( + mediaLibrary.rootDirectory, [&](std::error_code ec, const std::filesystem::path& path, const std::filesystem::directory_entry* fileEntry) { LMS_SCOPED_TRACE_DETAILED("Scanner", "OnExploreFile"); - assert((ec && !fileInfo) || (!ec && fileInfo)); + assert((ec && !fileEntry) || (!ec && fileEntry)); if (_abortScan) return false; // stop iterating @@ -117,8 +166,8 @@ namespace lms::scanner fileToScan.filePath = path; fileToScan.mediaLibrary = mediaLibrary; - fileToScan.lastWriteTime.setTime_t(fileInfo->lastWriteTime.toTime_t()); // sec resolution, as stored in the database - fileToScan.fileSize = fileInfo->fileSize; + fileToScan.lastWriteTime.setTime_t(Wt::WDateTime{ std::chrono::file_clock::to_sys(fileEntry->last_write_time()) }.toTime_t()); // sec resolution, as stored in the database + fileToScan.fileSize = fileEntry->file_size(); if (context.scanOptions.fullScan || scanner->needsScan(fileToScan)) {