From ccfdab60378db471a50e4ba7317813c6295de950 Mon Sep 17 00:00:00 2001 From: nedko Date: Wed, 5 Oct 2022 16:48:30 +0300 Subject: [PATCH] Initial commit. Added Notify class, makefile and common header --- .gitignore | 3 + Makefile | 15 ++++ Notify.cpp | 256 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Notify.h | 42 +++++++++ client.cpp | 0 common.h | 9 ++ server.cpp | 0 7 files changed, 325 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 Notify.cpp create mode 100644 Notify.h create mode 100644 client.cpp create mode 100644 common.h create mode 100644 server.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a33a6e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vscode/* +server +client diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a63b7da --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +all: + make server + make client + +clean: + rm -f server + rm -f client + +server: + g++ -Wall -Wextra -Wfatal-errors -o server server.cpp Notify.cpp + +client: + g++ -Wall -Wextra -Wfatal-errors -o client client.cpp Notify.cpp + +.PHONY: all server client clean diff --git a/Notify.cpp b/Notify.cpp new file mode 100644 index 0000000..e35de9a --- /dev/null +++ b/Notify.cpp @@ -0,0 +1,256 @@ +#include "Notify.h" +#include + +#include +#include + +#include +#include +#include + +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> Notify::get_watch_events() const +{ + vector> 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(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& files, uint32_t mask) +{ + for (auto file : files) + { + add_file(file, mask); + } +} + +void Notify::add_list(const vector>& 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& 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 Notify::get_list() const +{ + set result; + + for (auto fd : m_watch_fds) + { + result.insert(fd.first); + } + + return result; +} + +void Notify::set_list(vector>& files) +{ + map 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 Notify::get_events() const +{ + vector result; + vector> events = get_watch_events(); + map 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; +} diff --git a/Notify.h b/Notify.h new file mode 100644 index 0000000..96939b4 --- /dev/null +++ b/Notify.h @@ -0,0 +1,42 @@ +#ifndef NOTIFY_H_ +#define NOTIFY_H_ + +#include +#include +#include +#include +#include + +class Notify +{ +private: + // Stream where to log errors + std::ostream& m_log_stream; + + // inotify FD + int m_notify_fd; + + // Map of watch FDs + std::map m_watch_fds; + + std::vector> get_watch_events() const; + +public: + Notify(std::ostream& log_stream = std::cout); + ~Notify(); + + void add_file(const std::string& filename, uint32_t mask); + void add_list(const std::vector& files, uint32_t mask); + void add_list(const std::vector>& files); + void remove_file(const std::string& filename); + void remove_list(const std::vector& files); + void clear(); + size_t size() const; + + std::set get_list() const; + void set_list(std::vector>& files); + + std::vector get_events() const; +}; + +#endif // NOTIFY_H_ diff --git a/client.cpp b/client.cpp new file mode 100644 index 0000000..e69de29 diff --git a/common.h b/common.h new file mode 100644 index 0000000..73098bf --- /dev/null +++ b/common.h @@ -0,0 +1,9 @@ +#ifndef COMMON_H_ +#define COMMON_H_ + +#include +#include + +std::vector split_line(const std::string& line, char delim); + +#endif // COMMON_H_ diff --git a/server.cpp b/server.cpp new file mode 100644 index 0000000..e69de29