From f65e74af96a1a9fa0ed8c21ad0b86acc48c80269 Mon Sep 17 00:00:00 2001 From: nedko Date: Mon, 10 Oct 2022 17:48:24 +0300 Subject: [PATCH] Notify now reports the events and files correctly --- Notify.cpp | 47 ++++++++++++++++++++++++++++++++--------------- Notify.h | 12 +++++++++--- common.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ common.h | 1 + 4 files changed, 83 insertions(+), 18 deletions(-) create mode 100644 common.cpp diff --git a/Notify.cpp b/Notify.cpp index e35de9a..cf935c5 100644 --- a/Notify.cpp +++ b/Notify.cpp @@ -10,7 +10,13 @@ using namespace std; -Notify::Notify(std::ostream& log_stream = std::cout) +#ifdef _WIN32 +constexpr const char FOLDER_DELIM = '\\'; +#else +constexpr const char FOLDER_DELIM = '/'; +#endif + +Notify::Notify(std::ostream& log_stream) : m_log_stream(log_stream), m_notify_fd(-1) { m_notify_fd = inotify_init(); @@ -31,14 +37,15 @@ Notify::~Notify() } } -vector> Notify::get_watch_events() const +vector> Notify::get_watch_events() const { - vector> result; + vector> result; int error; unsigned int buffer_size; void* buffer = nullptr; int bytes; inotify_event* event; + notify_event tmp; if (m_notify_fd < 0) { @@ -83,7 +90,9 @@ vector> Notify::get_watch_events() const while (bytes < buffer_size) { event = static_cast(buffer + bytes); - result.push_back(make_pair(event->wd, string(event->name))); + tmp.file = event->name; + tmp.mask = event->mask; + result.push_back(make_pair(event->wd, tmp)); bytes += sizeof(inotify_event) + event->len; } free(buffer); @@ -183,13 +192,13 @@ size_t Notify::size() const return m_watch_fds.size(); } -set Notify::get_list() const +vector Notify::get_list() const { - set result; + vector result; for (auto fd : m_watch_fds) { - result.insert(fd.first); + result.push_back(fd.first); } return result; @@ -221,10 +230,11 @@ void Notify::set_list(vector>& files) // Or do a clear + add } -vector Notify::get_events() const +vector Notify::get_events() const { - vector result; - vector> events = get_watch_events(); + vector result; + vector> events = get_watch_events(); + string filepath; map r_watch_fds; // Do a reverse map @@ -243,13 +253,20 @@ vector Notify::get_events() const // Translate for (auto event : events) { - result.push_back(r_watch_fds[event.first]); - - // Test - if (event.second.size() != 0) + if (event.second.file.size() != 0) { - result.push_back(event.second); + // Concat watched folder and filename + filepath = r_watch_fds[event.first]; + filepath += FOLDER_DELIM; + filepath += event.second.file; + event.second.file = filepath; } + else + { + // Watched file/folder is the target + event.second.file = r_watch_fds[event.first]; + } + result.push_back(event.second); } return result; diff --git a/Notify.h b/Notify.h index 96939b4..2c5384d 100644 --- a/Notify.h +++ b/Notify.h @@ -7,6 +7,12 @@ #include #include +struct notify_event +{ + std::string file; + uint32_t mask; +}; + class Notify { private: @@ -19,7 +25,7 @@ private: // Map of watch FDs std::map m_watch_fds; - std::vector> get_watch_events() const; + std::vector> get_watch_events() const; public: Notify(std::ostream& log_stream = std::cout); @@ -33,10 +39,10 @@ public: void clear(); size_t size() const; - std::set get_list() const; + std::vector get_list() const; void set_list(std::vector>& files); - std::vector get_events() const; + std::vector get_events() const; }; #endif // NOTIFY_H_ diff --git a/common.cpp b/common.cpp new file mode 100644 index 0000000..86c41a0 --- /dev/null +++ b/common.cpp @@ -0,0 +1,41 @@ +#include "common.h" + +using namespace std; + +vector split_line(const string& line, char delim) +{ + vector result; + + size_t pos1; + size_t pos2 = 0; + + while (pos2 != string::npos) + { + pos1 = pos2; + pos2 = line.find(delim, pos1); + + if (pos2 != string::npos) + { + result.push_back(line.substr(pos1, pos2 - pos1)); + ++pos2; + } + else + { + result.push_back(line.substr(pos1, pos2)); + } + } +} + +string& clean_whitespace(string& str) +{ + constexpr char whitespace[] = " \t\r\n\f\v"; + size_t pos; + + pos = str.find_first_not_of(whitespace); + str.erase(0, pos); + + pos = str.find_last_not_of(whitespace); + str.erase(pos); + + return str; +} diff --git a/common.h b/common.h index 73098bf..c23d876 100644 --- a/common.h +++ b/common.h @@ -5,5 +5,6 @@ #include std::vector split_line(const std::string& line, char delim); +std::string& clean_whitespace(std::string& str); #endif // COMMON_H_