Notify now reports the events and files correctly

This commit is contained in:
nedko 2022-10-10 17:48:24 +03:00
parent ccfdab6037
commit f65e74af96
4 changed files with 83 additions and 18 deletions

View File

@ -10,7 +10,13 @@
using namespace std; 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_log_stream(log_stream), m_notify_fd(-1)
{ {
m_notify_fd = inotify_init(); m_notify_fd = inotify_init();
@ -31,14 +37,15 @@ Notify::~Notify()
} }
} }
vector<pair<int, string>> Notify::get_watch_events() const vector<pair<int, notify_event>> Notify::get_watch_events() const
{ {
vector<pair<int, string>> result; vector<pair<int, notify_event>> result;
int error; int error;
unsigned int buffer_size; unsigned int buffer_size;
void* buffer = nullptr; void* buffer = nullptr;
int bytes; int bytes;
inotify_event* event; inotify_event* event;
notify_event tmp;
if (m_notify_fd < 0) if (m_notify_fd < 0)
{ {
@ -83,7 +90,9 @@ vector<pair<int, string>> Notify::get_watch_events() const
while (bytes < buffer_size) while (bytes < buffer_size)
{ {
event = static_cast<inotify_event*>(buffer + bytes); event = static_cast<inotify_event*>(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; bytes += sizeof(inotify_event) + event->len;
} }
free(buffer); free(buffer);
@ -183,13 +192,13 @@ size_t Notify::size() const
return m_watch_fds.size(); return m_watch_fds.size();
} }
set<string> Notify::get_list() const vector<string> Notify::get_list() const
{ {
set<string> result; vector<string> result;
for (auto fd : m_watch_fds) for (auto fd : m_watch_fds)
{ {
result.insert(fd.first); result.push_back(fd.first);
} }
return result; return result;
@ -221,10 +230,11 @@ void Notify::set_list(vector<pair<string, uint32_t>>& files)
// Or do a clear + add // Or do a clear + add
} }
vector<string> Notify::get_events() const vector<notify_event> Notify::get_events() const
{ {
vector<string> result; vector<notify_event> result;
vector<pair<int, string>> events = get_watch_events(); vector<pair<int, notify_event>> events = get_watch_events();
string filepath;
map<int, string> r_watch_fds; map<int, string> r_watch_fds;
// Do a reverse map // Do a reverse map
@ -243,13 +253,20 @@ vector<string> Notify::get_events() const
// Translate // Translate
for (auto event : events) for (auto event : events)
{ {
result.push_back(r_watch_fds[event.first]); if (event.second.file.size() != 0)
// Test
if (event.second.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; return result;

View File

@ -7,6 +7,12 @@
#include <vector> #include <vector>
#include <utility> #include <utility>
struct notify_event
{
std::string file;
uint32_t mask;
};
class Notify class Notify
{ {
private: private:
@ -19,7 +25,7 @@ private:
// Map of watch FDs // Map of watch FDs
std::map<std::string, int> m_watch_fds; std::map<std::string, int> m_watch_fds;
std::vector<std::pair<int, std::string>> get_watch_events() const; std::vector<std::pair<int, notify_event>> get_watch_events() const;
public: public:
Notify(std::ostream& log_stream = std::cout); Notify(std::ostream& log_stream = std::cout);
@ -33,10 +39,10 @@ public:
void clear(); void clear();
size_t size() const; size_t size() const;
std::set<std::string> get_list() const; std::vector<std::string> get_list() const;
void set_list(std::vector<std::pair<std::string, uint32_t>>& files); void set_list(std::vector<std::pair<std::string, uint32_t>>& files);
std::vector<std::string> get_events() const; std::vector<notify_event> get_events() const;
}; };
#endif // NOTIFY_H_ #endif // NOTIFY_H_

41
common.cpp Normal file
View File

@ -0,0 +1,41 @@
#include "common.h"
using namespace std;
vector<string> split_line(const string& line, char delim)
{
vector<string> 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;
}

View File

@ -5,5 +5,6 @@
#include <vector> #include <vector>
std::vector<std::string> split_line(const std::string& line, char delim); std::vector<std::string> split_line(const std::string& line, char delim);
std::string& clean_whitespace(std::string& str);
#endif // COMMON_H_ #endif // COMMON_H_