ClickHouse/src/Storages/FileLog/FileLogDirectoryWatcher.cpp

89 lines
2.5 KiB
C++
Raw Normal View History

2021-07-03 17:14:56 +00:00
#include <Storages/FileLog/FileLogDirectoryWatcher.h>
#include <Poco/Delegate.h>
#include <Poco/DirectoryWatcher.h>
FileLogDirectoryWatcher::FileLogDirectoryWatcher(const std::string & path_)
: path(path_), dw(std::make_shared<Poco::DirectoryWatcher>(path))
{
dw->itemAdded += Poco::delegate(this, &FileLogDirectoryWatcher::onItemAdded);
dw->itemRemoved += Poco::delegate(this, &FileLogDirectoryWatcher::onItemRemoved);
dw->itemModified += Poco::delegate(this, &FileLogDirectoryWatcher::onItemModified);
dw->itemMovedFrom += Poco::delegate(this, &FileLogDirectoryWatcher::onItemMovedFrom);
dw->itemMovedTo += Poco::delegate(this, &FileLogDirectoryWatcher::onItemMovedTo);
}
FileLogDirectoryWatcher::Events FileLogDirectoryWatcher::getEvents()
{
std::lock_guard<std::mutex> lock(mutex);
Events res;
res.swap(events);
return res;
}
2021-09-05 11:41:13 +00:00
bool FileLogDirectoryWatcher::hasError() const
2021-07-03 17:14:56 +00:00
{
return error;
}
const std::string & FileLogDirectoryWatcher::getPath() const
{
return path;
}
void FileLogDirectoryWatcher::onItemAdded(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
std::lock_guard<std::mutex> lock(mutex);
DirEvent de;
2021-07-04 06:31:41 +00:00
de.callback = "onItemAdded";
de.path = ev.item.path();
de.type = ev.event;
events.emplace_back(de);
2021-07-03 17:14:56 +00:00
}
void FileLogDirectoryWatcher::onItemRemoved(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
std::lock_guard<std::mutex> lock(mutex);
2021-07-04 06:31:41 +00:00
DirEvent de;
de.callback = "onItemRemoved";
de.path = ev.item.path();
de.type = ev.event;
events.emplace_back(de);
2021-07-03 17:14:56 +00:00
}
void FileLogDirectoryWatcher::onItemModified(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
std::lock_guard<std::mutex> lock(mutex);
2021-07-04 06:31:41 +00:00
DirEvent de;
de.callback = "onItemModified";
de.path = ev.item.path();
de.type = ev.event;
events.emplace_back(de);
2021-07-03 17:14:56 +00:00
}
void FileLogDirectoryWatcher::onItemMovedFrom(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
std::lock_guard<std::mutex> lock(mutex);
DirEvent de;
2021-07-04 06:31:41 +00:00
de.callback = "onItemMovedFrom";
de.path = ev.item.path();
de.type = ev.event;
events.emplace_back(de);
2021-07-03 17:14:56 +00:00
}
void FileLogDirectoryWatcher::onItemMovedTo(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
std::lock_guard<std::mutex> lock(mutex);
2021-07-04 06:31:41 +00:00
DirEvent de;
de.callback = "onItemMovedTo";
de.path = ev.item.path();
de.type = ev.event;
events.emplace_back(de);
2021-07-03 17:14:56 +00:00
}
void FileLogDirectoryWatcher::onError(const Poco::Exception &)
{
2021-07-04 06:31:41 +00:00
error = true;
2021-07-03 17:14:56 +00:00
}