/*
* Copyright (C) 2023 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see .
*/
#include "ScanStepCheckForRemovedFiles.hpp"
#include
#include
#include
#include
#include "core/IJob.hpp"
#include "core/ILogger.hpp"
#include "core/Path.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/ArtistInfo.hpp"
#include "database/objects/Image.hpp"
#include "database/objects/PlayListFile.hpp"
#include "database/objects/Track.hpp"
#include "database/objects/TrackLyrics.hpp"
#include "FileScanners.hpp"
#include "JobQueue.hpp"
#include "ScanContext.hpp"
#include "ScannerSettings.hpp"
#include "services/scanner/ScannerStats.hpp"
namespace lms::scanner
{
namespace
{
template
struct FileToCheck
{
IdType objectId;
std::filesystem::path file;
};
template
class CheckForRemovedFilesJob : public core::IJob
{
public:
CheckForRemovedFilesJob(const ScannerSettings& settings, const FileScanners& scanners, std::span> filesToCheck)
: _settings{ settings }
, _scanners{ scanners }
, _filesToCheck{ std::cbegin(filesToCheck), std::cend(filesToCheck) }
{
}
CheckForRemovedFilesJob(const CheckForRemovedFilesJob&) = delete;
CheckForRemovedFilesJob& operator=(const CheckForRemovedFilesJob&) = delete;
std::size_t getProcessedCount() const { return _processedCount; }
std::span getObjectsToRemove() const { return _objectsToRemove; }
private:
core::LiteralString getName() const override { return "Check For Removed Files"; }
void run() override
{
for (const FileToCheck& fileToCheck : _filesToCheck)
{
if (!checkFile(fileToCheck.file))
_objectsToRemove.push_back(fileToCheck.objectId);
}
_processedCount += _filesToCheck.size();
}
bool checkFile(const std::filesystem::path& p)
{
std::error_code ec;
const std::filesystem::directory_entry fileEntry{ p, ec };
if (ec)
{
// TODO store error?
LMS_LOG(DBUPDATER, ERROR, "Error while checking file " << p << ": " << ec.message());
return false;
}
// For file, make sure the the file still exists, is a regular file, is in a media directory and is of a supported format
if (!fileEntry.exists() || !fileEntry.is_regular_file())
{
LMS_LOG(DBUPDATER, DEBUG, "Removing " << p << ": missing");
return false;
}
if (std::none_of(std::cbegin(_settings.mediaLibraries), std::cend(_settings.mediaLibraries),
[&](const MediaLibraryInfo& libraryInfo) {
return core::pathUtils::isPathInRootPath(p, libraryInfo.rootDirectory, &excludeDirFileName);
}))
{
LMS_LOG(DBUPDATER, DEBUG, "Removing " << p << ": out of media directory");
return false;
}
if (!_scanners.select(p))
{
LMS_LOG(DBUPDATER, DEBUG, "Removing " << p << ": file format no longer handled");
return false;
}
return true;
}
const ScannerSettings& _settings;
const FileScanners& _scanners;
std::vector> _filesToCheck;
std::vector _objectsToRemove;
std::size_t _processedCount{};
};
template
std::size_t removeObjects(db::Session& session, std::deque& objectIdsToRemove, bool forceFullBatch)
{
std::size_t removedObjectCount{};
constexpr std::size_t writeBatchSize{ 50 };
std::vector ids;
while ((forceFullBatch && objectIdsToRemove.size() >= writeBatchSize) || !objectIdsToRemove.empty())
{
for (std::size_t i{}; !objectIdsToRemove.empty() && i < writeBatchSize; ++i)
{
ids.push_back(objectIdsToRemove.front());
objectIdsToRemove.pop_front();
}
{
auto transaction{ session.createWriteTransaction() };
session.destroy