2022-06-03 10:06:03 +02:00
|
|
|
#include <QDateTime>
|
|
|
|
#include "filesaver.h"
|
|
|
|
#include "indexsyncer.h"
|
|
|
|
|
|
|
|
IndexSyncer::IndexSyncer(SqliteDbService &dbService)
|
|
|
|
{
|
|
|
|
this->dbService = &dbService;
|
|
|
|
}
|
|
|
|
|
2023-01-08 17:37:28 +01:00
|
|
|
void IndexSyncer::setFileSaverOptions(FileSaverOptions options)
|
2022-06-03 10:06:03 +02:00
|
|
|
{
|
2023-01-08 17:37:28 +01:00
|
|
|
fileSaverOptions = options;
|
2022-06-03 10:06:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-08 17:37:28 +01:00
|
|
|
void IndexSyncer::setDryRun(bool dryRun)
|
2022-06-03 10:06:03 +02:00
|
|
|
{
|
2023-01-08 17:37:28 +01:00
|
|
|
this->dryRun = dryRun;
|
2022-06-03 10:06:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IndexSyncer::setRemoveDeletedFromIndex(bool removeDeletedFromIndex)
|
|
|
|
{
|
|
|
|
this->removeDeletedFromIndex = removeDeletedFromIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IndexSyncer::setPattern(QString pattern)
|
|
|
|
{
|
|
|
|
this->pattern = pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IndexSyncer::sync()
|
|
|
|
{
|
2022-06-04 14:07:06 +02:00
|
|
|
this->stopToken.store(false, std::memory_order_relaxed);
|
2023-01-08 17:37:28 +01:00
|
|
|
|
2022-06-03 10:06:03 +02:00
|
|
|
QVector<FileData> files;
|
|
|
|
int offset = 0;
|
2022-06-04 14:07:06 +02:00
|
|
|
int limit = 10000;
|
2022-06-03 10:06:03 +02:00
|
|
|
unsigned int processedRows = dbService->getFiles(files, pattern, offset, limit);
|
|
|
|
|
|
|
|
unsigned int totalUpdatesFilesCount = 0;
|
|
|
|
unsigned int totalDeletedFilesCount = 0;
|
|
|
|
unsigned int totalErroredFilesCount = 0;
|
|
|
|
|
|
|
|
while(processedRows > 0)
|
|
|
|
{
|
|
|
|
QVector<QString> filePathsToUpdate;
|
|
|
|
for(FileData &fileData : files)
|
|
|
|
{
|
2022-06-04 14:07:06 +02:00
|
|
|
if(processedRows % 100 == 0 && this->stopToken.load(std::memory_order_relaxed))
|
|
|
|
{
|
|
|
|
emit finished(totalUpdatesFilesCount, totalDeletedFilesCount, totalErroredFilesCount);
|
|
|
|
return;
|
|
|
|
}
|
2022-06-05 20:55:06 +02:00
|
|
|
if(Common::isMountPath(fileData.absPath))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-03 10:06:03 +02:00
|
|
|
QFileInfo fileInfo(fileData.absPath);
|
|
|
|
if(fileInfo.exists())
|
|
|
|
{
|
|
|
|
if(fileInfo.isFile())
|
|
|
|
{
|
|
|
|
if(fileInfo.lastModified().toSecsSinceEpoch() != fileData.mtime)
|
|
|
|
{
|
|
|
|
if(!dryRun)
|
|
|
|
{
|
|
|
|
filePathsToUpdate.append(fileData.absPath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emit updatedDryRun(fileData.absPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(this->removeDeletedFromIndex)
|
|
|
|
{
|
|
|
|
if(!dryRun)
|
|
|
|
{
|
|
|
|
if(!this->dbService->deleteFile(fileData.absPath))
|
|
|
|
{
|
|
|
|
emit error("Error: Failed to delete " + fileData.absPath + " from the index");
|
2023-01-08 17:37:28 +01:00
|
|
|
if(!this->fileSaverOptions.keepGoing)
|
2022-06-03 10:06:03 +02:00
|
|
|
{
|
|
|
|
emit finished(totalUpdatesFilesCount, totalDeletedFilesCount, totalErroredFilesCount);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit removed(fileData.absPath);
|
|
|
|
++totalDeletedFilesCount;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emit removedDryRun(fileData.absPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 17:37:28 +01:00
|
|
|
FileSaver saver(*this->dbService);
|
|
|
|
saver.setFileSaverOptions(this->fileSaverOptions);
|
|
|
|
unsigned int updatedFilesCount = saver.updateFiles(filePathsToUpdate);
|
2022-06-03 10:06:03 +02:00
|
|
|
unsigned int shouldHaveUpdatedCount = static_cast<unsigned int>(filePathsToUpdate.size());
|
|
|
|
if(updatedFilesCount != shouldHaveUpdatedCount)
|
|
|
|
{
|
|
|
|
|
|
|
|
totalErroredFilesCount += (shouldHaveUpdatedCount - updatedFilesCount);
|
2023-01-08 17:37:28 +01:00
|
|
|
if(!this->fileSaverOptions.keepGoing)
|
2022-06-03 10:06:03 +02:00
|
|
|
{
|
|
|
|
QString errorMsg = QString("Failed to update all files selected for updating in this batch. Updated") +
|
|
|
|
updatedFilesCount + "out of" + shouldHaveUpdatedCount + "selected for updating";
|
|
|
|
emit error(errorMsg);
|
|
|
|
emit finished(totalUpdatesFilesCount, totalDeletedFilesCount, totalErroredFilesCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
offset += limit;
|
|
|
|
files.clear();
|
|
|
|
totalUpdatesFilesCount += updatedFilesCount;
|
2022-06-04 14:07:06 +02:00
|
|
|
|
|
|
|
processedRows = this->dbService->getFiles(files, pattern, offset, limit);
|
2022-06-03 10:06:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emit finished(totalUpdatesFilesCount, totalDeletedFilesCount, totalErroredFilesCount);
|
|
|
|
}
|
2022-06-04 14:07:06 +02:00
|
|
|
|
|
|
|
void IndexSyncer::cancel()
|
|
|
|
{
|
|
|
|
this->stopToken.store(true, std::memory_order_seq_cst);
|
|
|
|
}
|