2022-05-16 20:30:40 +01:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "BoxMonitor.h"
|
2022-05-19 18:53:51 +01:00
|
|
|
#include "../MiscHelpers/Common/Common.h"
|
2022-05-16 20:30:40 +01:00
|
|
|
|
|
|
|
CBoxMonitor::CBoxMonitor()
|
|
|
|
{
|
|
|
|
m_bTerminate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBoxMonitor::~CBoxMonitor()
|
|
|
|
{
|
2022-06-13 19:12:53 +01:00
|
|
|
Stop();
|
2022-05-16 20:30:40 +01:00
|
|
|
}
|
|
|
|
|
2022-09-29 17:28:48 +01:00
|
|
|
void CBoxMonitor::Notify(const std::wstring& strDirectory)
|
2022-05-16 20:30:40 +01:00
|
|
|
{
|
|
|
|
m_Mutex.lock();
|
|
|
|
m_Boxes[QString::fromStdWString(strDirectory)].Changed = true;
|
|
|
|
m_Mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 CBoxMonitor::CounDirSize(const QString& Directory, SBox* Box)
|
|
|
|
{
|
|
|
|
quint64 TotalSize = 0;
|
2022-06-13 19:12:53 +01:00
|
|
|
if (m_bTerminate || Box->pBox.isNull())
|
2022-05-16 20:30:40 +01:00
|
|
|
return TotalSize;
|
|
|
|
|
|
|
|
QDir Dir(Directory);
|
|
|
|
foreach(const QFileInfo & Info, Dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot))
|
|
|
|
{
|
|
|
|
if (Info.isDir())
|
|
|
|
TotalSize += CounDirSize(Info.filePath(), Box);
|
|
|
|
else
|
|
|
|
TotalSize += QFile(Info.filePath()).size();
|
|
|
|
}
|
|
|
|
|
|
|
|
return TotalSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBoxMonitor::run()
|
|
|
|
{
|
|
|
|
while (!m_bTerminate)
|
|
|
|
{
|
2022-06-19 07:55:41 +01:00
|
|
|
Sleep(100);
|
2022-06-13 19:12:53 +01:00
|
|
|
|
2022-05-16 20:30:40 +01:00
|
|
|
m_Mutex.lock();
|
|
|
|
QList<QString> Keys = m_Boxes.keys();
|
|
|
|
m_Mutex.unlock();
|
|
|
|
|
|
|
|
quint64 CurTick = GetCurTick();
|
|
|
|
|
|
|
|
foreach(const QString& Key, Keys) {
|
|
|
|
|
|
|
|
m_Mutex.lock();
|
|
|
|
SBox* Box = &m_Boxes[Key];
|
|
|
|
m_Mutex.unlock();
|
|
|
|
|
|
|
|
quint64 MinScanInterval = Box->ScanDuration * 100;
|
|
|
|
if (MinScanInterval < 30 * 1000)
|
|
|
|
MinScanInterval = 30 * 1000;
|
|
|
|
if (MinScanInterval > 30 * 60 * 1000)
|
|
|
|
MinScanInterval = 30 * 60 * 1000;
|
|
|
|
|
2022-06-13 19:12:53 +01:00
|
|
|
if ((Box->Changed && (!Box->IsWatched || Box->LastScan == 0 || (CurTick - Box->LastScan) > MinScanInterval)) || Box->ForceUpdate) {
|
2022-05-16 20:30:40 +01:00
|
|
|
|
|
|
|
qDebug() << "Rescanning:" << Key << "(" + QDateTime::currentDateTime().toString() + ")";
|
|
|
|
|
|
|
|
quint64 ScanStart = GetCurTick();
|
|
|
|
|
2022-06-13 19:12:53 +01:00
|
|
|
Box->ScanDuration = -1;
|
|
|
|
|
2022-05-16 20:30:40 +01:00
|
|
|
Box->TotalSize = CounDirSize(Key, Box);
|
|
|
|
|
|
|
|
Box->ScanDuration = GetCurTick() - ScanStart;
|
|
|
|
Box->LastScan = GetCurTick();
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "UpdateBox", Qt::QueuedConnection,
|
|
|
|
//Q_RETURN_ARG(int, retVal),
|
|
|
|
Q_ARG(QString, Key)
|
|
|
|
);
|
|
|
|
|
|
|
|
Box->Changed = false;
|
2022-06-13 19:12:53 +01:00
|
|
|
Box->ForceUpdate = false;
|
2022-05-16 20:30:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_Mutex.lock();
|
|
|
|
if (Box->pBox.isNull())
|
|
|
|
m_Boxes.remove(Key);
|
|
|
|
m_Mutex.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBoxMonitor::UpdateBox(const QString& Path)
|
|
|
|
{
|
2022-06-13 19:12:53 +01:00
|
|
|
// Note: this private functin runs in the main thread
|
2022-05-16 20:30:40 +01:00
|
|
|
|
|
|
|
m_Mutex.lock();
|
|
|
|
SBox Box = m_Boxes.value(Path);
|
|
|
|
m_Mutex.unlock();
|
|
|
|
|
|
|
|
if (Box.pBox)
|
|
|
|
Box.pBox->SetSize(Box.TotalSize);
|
|
|
|
}
|
|
|
|
|
2022-06-13 19:12:53 +01:00
|
|
|
void CBoxMonitor::WatchBox(CSandBoxPlus* pBox)
|
2022-05-16 20:30:40 +01:00
|
|
|
{
|
|
|
|
QMutexLocker Lock(&m_Mutex);
|
2022-06-13 19:12:53 +01:00
|
|
|
if (!isRunning()) start();
|
2022-05-16 20:30:40 +01:00
|
|
|
|
2022-06-13 19:12:53 +01:00
|
|
|
SBox& Box = m_Boxes[pBox->GetFileRoot()];
|
|
|
|
Box.pBox = pBox;
|
2022-05-16 20:30:40 +01:00
|
|
|
|
2022-06-13 19:12:53 +01:00
|
|
|
Box.IsWatched = true;
|
|
|
|
AddDirectory(pBox->GetFileRoot().toStdWString().c_str(), true, FILE_NOTIFY_CHANGE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBoxMonitor::ScanBox(CSandBoxPlus* pBox)
|
|
|
|
{
|
|
|
|
QMutexLocker Lock(&m_Mutex);
|
|
|
|
if (!isRunning()) start();
|
|
|
|
|
|
|
|
SBox& Box = m_Boxes[pBox->GetFileRoot()];
|
|
|
|
Box.pBox = pBox;
|
|
|
|
|
|
|
|
Box.ForceUpdate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBoxMonitor::CloseBox(CSandBoxPlus* pBox)
|
|
|
|
{
|
|
|
|
QMutexLocker Lock(&m_Mutex);
|
|
|
|
if (!isRunning()) return;
|
|
|
|
|
|
|
|
SBox& Box = m_Boxes[pBox->GetFileRoot()];
|
|
|
|
|
|
|
|
if(Box.IsWatched)
|
|
|
|
DetachDirectory(pBox->GetFileRoot().toStdWString().c_str());
|
|
|
|
Box.IsWatched = false;
|
|
|
|
|
|
|
|
//Box.Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBoxMonitor::RemoveBox(CSandBoxPlus* pBox)
|
|
|
|
{
|
|
|
|
QMutexLocker Lock(&m_Mutex);
|
|
|
|
if (!isRunning()) return;
|
|
|
|
|
|
|
|
auto I = m_Boxes.find(pBox->GetFileRoot());
|
|
|
|
if (I == m_Boxes.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(I->IsWatched)
|
|
|
|
DetachDirectory(pBox->GetFileRoot().toStdWString().c_str());
|
|
|
|
I->pBox.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBoxMonitor::IsScanPending(const CSandBoxPlus* pBox)
|
|
|
|
{
|
|
|
|
QMutexLocker Lock(&m_Mutex);
|
|
|
|
if (!isRunning()) false;
|
|
|
|
|
|
|
|
auto I = m_Boxes.find(pBox->GetFileRoot());
|
|
|
|
if (I == m_Boxes.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return (I->Changed && !I->IsWatched) || I->ForceUpdate || I->ScanDuration == -1;
|
2022-05-16 20:30:40 +01:00
|
|
|
}
|
|
|
|
|
2022-06-13 19:12:53 +01:00
|
|
|
void CBoxMonitor::Stop()
|
2022-05-16 20:30:40 +01:00
|
|
|
{
|
2022-06-13 19:12:53 +01:00
|
|
|
if (!isRunning()) return;
|
|
|
|
|
|
|
|
m_bTerminate = true;
|
2022-05-16 20:30:40 +01:00
|
|
|
|
2022-06-13 19:12:53 +01:00
|
|
|
if (!wait(10 * 1000)) {
|
|
|
|
terminate();
|
|
|
|
qDebug() << "Failed to stop monitor thread, terminating!!!";
|
|
|
|
}
|
2022-05-16 20:30:40 +01:00
|
|
|
|
2022-06-19 07:55:41 +01:00
|
|
|
QMutexLocker Lock(&m_Mutex);
|
|
|
|
|
2022-06-13 19:12:53 +01:00
|
|
|
while (!m_Boxes.isEmpty()) {
|
|
|
|
SBox Box = m_Boxes.take(m_Boxes.firstKey());
|
|
|
|
if(Box.IsWatched && Box.pBox)
|
|
|
|
DetachDirectory(Box.pBox->GetFileRoot().toStdWString().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bTerminate = false;
|
2022-05-16 20:30:40 +01:00
|
|
|
}
|