Skip to content

Commit

Permalink
improve handling of #81 in FileMonitoringManager_V2
Browse files Browse the repository at this point in the history
  • Loading branch information
fxdeniz committed Jun 27, 2023
1 parent 6552fa0 commit 641bbd1
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 45 deletions.
121 changes: 77 additions & 44 deletions Backend/FileMonitorSubSystem/FileMonitoringManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,40 @@ void FileMonitoringManager::slotOnDeleteEventDetected(const QString &fileName, c
}
else if(database->isFileExist(currentPath)) // When file deleted
{
currentStatus = database->getStatusOfFile(currentPath);
QString originalFileName = database->getOldNameOfFile(currentPath);
QString originalPath = QDir::toNativeSeparators(dir + originalFileName);

if(currentStatus == FileSystemEventDb::ItemStatus::NewAdded) // Remove new added files since they're temporary
database->deleteFile(currentPath);
else
auto fsm = FileStorageManager::instance();

QJsonObject fileJson = fsm->getFileJsonByUserPath(currentPath);
QJsonObject originalFileJson = fsm->getFileJsonByUserPath(originalPath);

bool isFilePersists = fileJson[JsonKeys::IsExist].toBool();
bool isFileFrozen = fileJson[JsonKeys::File::IsFrozen].toBool();

bool isOriginalFilePersists = originalFileJson[JsonKeys::IsExist].toBool();
bool isOriginalFileFrozen = originalFileJson[JsonKeys::File::IsFrozen].toBool();

if(isOriginalFilePersists)
{
bool isRenamedFileAlreadyMonitored = database->isFileExist(originalPath);

if(isRenamedFileAlreadyMonitored)
database->deleteFile(currentPath);
else
{
database->setNameOfFile(currentPath, originalFileName);
database->setStatusOfFile(originalPath, FileSystemEventDb::ItemStatus::Deleted);
database->setOldNameOfFile(originalPath, "");
}
}
else if(isFilePersists)
database->setStatusOfFile(currentPath, FileSystemEventDb::ItemStatus::Deleted);
else
database->deleteFile(currentPath);

emit signalEventDbUpdated();
if((isFilePersists && !isFileFrozen) || (isOriginalFilePersists && !isOriginalFileFrozen))
emit signalEventDbUpdated();
}
}

Expand Down Expand Up @@ -353,45 +379,7 @@ void FileMonitoringManager::slotOnMoveEventDetected(const QString &fileName, con
auto fsm = FileStorageManager::instance();
QFileInfo info(currentNewPath);

if(info.isFile() && !info.isHidden()) // Only accept real files.
{
FileSystemEventDb::ItemStatus currentStatus = database->getStatusOfFile(currentOldPath);

bool isOldFileMonitored = database->isFileExist(currentOldPath);
bool isNewFileMonitored = database->isFileExist(currentNewPath);

if(isOldFileMonitored && !isNewFileMonitored)
{
// Do not count renames (moves) if files is new added.
if(currentStatus != FileSystemEventDb::ItemStatus::NewAdded)
{
if(currentStatus == FileSystemEventDb::ItemStatus::Updated ||
currentStatus == FileSystemEventDb::UpdatedAndRenamed)
{
database->setStatusOfFile(currentOldPath, FileSystemEventDb::ItemStatus::UpdatedAndRenamed);
}
else
database->setStatusOfFile(currentOldPath, FileSystemEventDb::ItemStatus::Renamed);
}

bool isFilePersists = fsm->getFileJsonByUserPath(currentOldPath)[JsonKeys::IsExist].toBool();
if(isFilePersists)
database->setOldNameOfFile(currentOldPath, oldFileName);

database->setNameOfFile(currentOldPath, fileName);

emit signalEventDbUpdated();
}
else if(isOldFileMonitored && isNewFileMonitored) // Delete temp file (Issue #81).
{
bool isFilePersists = fsm->getFileJsonByUserPath(currentNewPath)[JsonKeys::IsExist].toBool();
if(isFilePersists)
database->deleteFile(currentOldPath);

emit signalEventDbUpdated();
}
}
else if(info.isDir())
if(info.isDir())
{
bool isOldFolderMonitored = database->isFolderExist(currentOldPath);
bool isNewFolderMonitored = database->isFolderExist(currentNewPath);
Expand All @@ -413,6 +401,51 @@ void FileMonitoringManager::slotOnMoveEventDetected(const QString &fileName, con

emit signalEventDbUpdated();
}
}
else if(info.isFile() && !info.isHidden())
{
QString originalFileName = database->getOldNameOfFile(currentOldPath);

QJsonObject newFileJson = fsm->getFileJsonByUserPath(currentNewPath);

bool isNewFilePersists = newFileJson[JsonKeys::IsExist].toBool();
bool isNewFileFrozen = newFileJson[JsonKeys::File::IsFrozen].toBool();

FileSystemEventDb::ItemStatus oldFileStatus = database->getStatusOfFile(currentOldPath);

if(isNewFilePersists && !isNewFileFrozen)
{
if(isNewFilePersists && !isNewFileFrozen)
{
qDebug() << "1) executing for old file = " << currentOldPath;
qDebug() << "1) executing for new file = " << currentNewPath;

database->deleteFile(currentOldPath);
database->deleteFile(currentNewPath);
database->addFile(currentNewPath);

database->setOldNameOfFile(currentNewPath, originalFileName);
database->setStatusOfFile(currentNewPath, FileSystemEventDb::ItemStatus::Updated);

emit signalEventDbUpdated();
}
}
else
{
qDebug() << "2) executing for old file = " << currentOldPath;
qDebug() << "2) executing for new file = " << currentNewPath;

if(originalFileName.isEmpty())
database->setOldNameOfFile(currentOldPath, oldFileName);

if(oldFileStatus == FileSystemEventDb::Updated || oldFileStatus == FileSystemEventDb::ItemStatus::UpdatedAndRenamed)
database->setStatusOfFile(currentOldPath, FileSystemEventDb::ItemStatus::UpdatedAndRenamed);
else
database->setStatusOfFile(currentOldPath, FileSystemEventDb::ItemStatus::Renamed);

database->setNameOfFile(currentOldPath, fileName);

emit signalEventDbUpdated();
}
}
}
1 change: 0 additions & 1 deletion Backend/FileMonitorSubSystem/FileSystemEventDb.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define FILESYSTEMEVENTDB_H

#include "efsw/efsw.hpp"
#include "qdatetime.h"
#include <QSqlDatabase>

class FileSystemEventDb
Expand Down

0 comments on commit 641bbd1

Please sign in to comment.