257 lines
4.9 KiB
C++
257 lines
4.9 KiB
C++
#include "Notify.h"
|
|
#include <set>
|
|
|
|
#include <sys/inotify.h>
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
using namespace std;
|
|
|
|
Notify::Notify(std::ostream& log_stream = std::cout)
|
|
: m_log_stream(log_stream), m_notify_fd(-1)
|
|
{
|
|
m_notify_fd = inotify_init();
|
|
if (m_notify_fd < 0)
|
|
{
|
|
m_log_stream << __FILE__ << ":" << __LINE__ << endl;
|
|
m_log_stream << "Could not create notify FD - ";
|
|
m_log_stream << errno << " - " << strerror(errno) << endl;
|
|
}
|
|
}
|
|
|
|
Notify::~Notify()
|
|
{
|
|
if (m_notify_fd >= 0)
|
|
{
|
|
close(m_notify_fd);
|
|
m_notify_fd = -1;
|
|
}
|
|
}
|
|
|
|
vector<pair<int, string>> Notify::get_watch_events() const
|
|
{
|
|
vector<pair<int, string>> result;
|
|
int error;
|
|
unsigned int buffer_size;
|
|
void* buffer = nullptr;
|
|
int bytes;
|
|
inotify_event* event;
|
|
|
|
if (m_notify_fd < 0)
|
|
{
|
|
m_log_stream << __FILE__ << ":" << __LINE__ << endl;
|
|
m_log_stream << "Negative notify FD - " << m_notify_fd << endl;
|
|
return result;
|
|
}
|
|
|
|
error = ioctl(m_notify_fd, FIONREAD, &buffer_size);
|
|
if (0 != error)
|
|
{
|
|
m_log_stream << __FILE__ << ":" << __LINE__ << endl;
|
|
m_log_stream << "IOCTL for buffer size failed - ";
|
|
m_log_stream << errno << " - " << strerror(errno) << endl;
|
|
return result;
|
|
}
|
|
|
|
buffer = calloc(buffer_size, 1);
|
|
if (nullptr == buffer)
|
|
{
|
|
m_log_stream << __FILE__ << ":" << __LINE__ << endl;
|
|
m_log_stream << "Buffer allocation failed";
|
|
return result;
|
|
}
|
|
|
|
bytes = 0;
|
|
while (bytes < buffer_size)
|
|
{
|
|
error = read(m_notify_fd, buffer + bytes, buffer_size - bytes);
|
|
if (error < 1)
|
|
{
|
|
m_log_stream << __FILE__ << ":" << __LINE__ << endl;
|
|
m_log_stream << "Reading notify fd failed - ";
|
|
m_log_stream << errno << " - " << strerror(errno) << endl;
|
|
free(buffer);
|
|
return result;
|
|
}
|
|
bytes += error;
|
|
}
|
|
|
|
bytes = 0;
|
|
while (bytes < buffer_size)
|
|
{
|
|
event = static_cast<inotify_event*>(buffer + bytes);
|
|
result.push_back(make_pair(event->wd, string(event->name)));
|
|
bytes += sizeof(inotify_event) + event->len;
|
|
}
|
|
free(buffer);
|
|
buffer = nullptr;
|
|
|
|
return result;
|
|
}
|
|
|
|
void Notify::add_file(const string& filename, uint32_t mask)
|
|
{
|
|
int error;
|
|
if (m_watch_fds.count(filename) != 0)
|
|
{
|
|
// Append mask
|
|
mask |= IN_MASK_ADD;
|
|
}
|
|
|
|
// Add watch
|
|
error = inotify_add_watch(m_notify_fd, filename.c_str(), mask);
|
|
if (error < 0)
|
|
{
|
|
m_log_stream << __FILE__ << ":" << __LINE__ << endl;
|
|
m_log_stream << "Could not add watch FD - ";
|
|
m_log_stream << errno << " - " << strerror(errno) << endl;
|
|
}
|
|
else
|
|
{
|
|
m_watch_fds[filename] = error;
|
|
}
|
|
}
|
|
|
|
void Notify::add_list(const vector<string>& files, uint32_t mask)
|
|
{
|
|
for (auto file : files)
|
|
{
|
|
add_file(file, mask);
|
|
}
|
|
}
|
|
|
|
void Notify::add_list(const vector<pair<string, uint32_t>>& files)
|
|
{
|
|
for (auto file : files)
|
|
{
|
|
add_file(file.first, file.second);
|
|
}
|
|
}
|
|
|
|
void Notify::remove_file(const string& filename)
|
|
{
|
|
int error;
|
|
|
|
if (m_watch_fds.count(filename) != 0)
|
|
{
|
|
// Remove watch
|
|
error = inotify_rm_watch(m_notify_fd, m_watch_fds[filename]);
|
|
if (0 != error)
|
|
{
|
|
m_log_stream << __FILE__ << ":" << __LINE__ << endl;
|
|
m_log_stream << "Could not remove watch - ";
|
|
m_log_stream << errno << " - " << strerror(errno) << endl;
|
|
}
|
|
}
|
|
|
|
m_watch_fds.erase(filename);
|
|
}
|
|
|
|
void Notify::remove_list(const vector<string>& files)
|
|
{
|
|
for (auto file : files)
|
|
{
|
|
remove_file(file);
|
|
}
|
|
}
|
|
|
|
void Notify::clear()
|
|
{
|
|
int error;
|
|
|
|
for (auto fd : m_watch_fds)
|
|
{
|
|
// Remove watch
|
|
error = inotify_rm_watch(m_notify_fd, fd.second);
|
|
if (0 != error)
|
|
{
|
|
m_log_stream << __FILE__ << ":" << __LINE__ << endl;
|
|
m_log_stream << "Could not remove watch - ";
|
|
m_log_stream << errno << " - " << strerror(errno) << endl;
|
|
}
|
|
fd.second = -1;
|
|
}
|
|
|
|
m_watch_fds.clear();
|
|
}
|
|
|
|
size_t Notify::size() const
|
|
{
|
|
return m_watch_fds.size();
|
|
}
|
|
|
|
set<string> Notify::get_list() const
|
|
{
|
|
set<string> result;
|
|
|
|
for (auto fd : m_watch_fds)
|
|
{
|
|
result.insert(fd.first);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
void Notify::set_list(vector<pair<string, uint32_t>>& files)
|
|
{
|
|
map<string, int> existing;
|
|
|
|
for (auto fd : m_watch_fds)
|
|
{
|
|
existing[fd.first] = 0;
|
|
}
|
|
|
|
for (auto file : files)
|
|
{
|
|
add_file(file.first, file.second);
|
|
existing[file.first] = 1;
|
|
}
|
|
|
|
for (auto item : existing)
|
|
{
|
|
if (0 == item.second)
|
|
{
|
|
remove_file(item.first);
|
|
}
|
|
}
|
|
|
|
// Or do a clear + add
|
|
}
|
|
|
|
vector<string> Notify::get_events() const
|
|
{
|
|
vector<string> result;
|
|
vector<pair<int, string>> events = get_watch_events();
|
|
map<int, string> r_watch_fds;
|
|
|
|
// Do a reverse map
|
|
for (auto fd : m_watch_fds)
|
|
{
|
|
if (0 != r_watch_fds.count(fd.second))
|
|
{
|
|
m_log_stream << __FILE__ << ":" << __LINE__ << endl;
|
|
m_log_stream << "Watch FD is duplicated - " << fd.second;
|
|
m_log_stream << " -> \"" << r_watch_fds[fd.second];
|
|
m_log_stream << "\" -> \"" << fd.first << "\"" << endl;
|
|
}
|
|
r_watch_fds[fd.second] = fd.first;
|
|
}
|
|
|
|
// Translate
|
|
for (auto event : events)
|
|
{
|
|
result.push_back(r_watch_fds[event.first]);
|
|
|
|
// Test
|
|
if (event.second.size() != 0)
|
|
{
|
|
result.push_back(event.second);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|