Notify now reports the events and files correctly
This commit is contained in:
parent
ccfdab6037
commit
f65e74af96
47
Notify.cpp
47
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<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;
|
||||
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<pair<int, string>> Notify::get_watch_events() const
|
||||
while (bytes < buffer_size)
|
||||
{
|
||||
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;
|
||||
}
|
||||
free(buffer);
|
||||
@ -183,13 +192,13 @@ size_t Notify::size() const
|
||||
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)
|
||||
{
|
||||
result.insert(fd.first);
|
||||
result.push_back(fd.first);
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -221,10 +230,11 @@ void Notify::set_list(vector<pair<string, uint32_t>>& files)
|
||||
// Or do a clear + add
|
||||
}
|
||||
|
||||
vector<string> Notify::get_events() const
|
||||
vector<notify_event> Notify::get_events() const
|
||||
{
|
||||
vector<string> result;
|
||||
vector<pair<int, string>> events = get_watch_events();
|
||||
vector<notify_event> result;
|
||||
vector<pair<int, notify_event>> events = get_watch_events();
|
||||
string filepath;
|
||||
map<int, string> r_watch_fds;
|
||||
|
||||
// Do a reverse map
|
||||
@ -243,13 +253,20 @@ vector<string> 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;
|
||||
|
||||
12
Notify.h
12
Notify.h
@ -7,6 +7,12 @@
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
|
||||
struct notify_event
|
||||
{
|
||||
std::string file;
|
||||
uint32_t mask;
|
||||
};
|
||||
|
||||
class Notify
|
||||
{
|
||||
private:
|
||||
@ -19,7 +25,7 @@ private:
|
||||
// Map of 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:
|
||||
Notify(std::ostream& log_stream = std::cout);
|
||||
@ -33,10 +39,10 @@ public:
|
||||
void clear();
|
||||
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);
|
||||
|
||||
std::vector<std::string> get_events() const;
|
||||
std::vector<notify_event> get_events() const;
|
||||
};
|
||||
|
||||
#endif // NOTIFY_H_
|
||||
|
||||
41
common.cpp
Normal file
41
common.cpp
Normal 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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user