Initial commit. Added Notify class, makefile and common header

This commit is contained in:
nedko 2022-10-05 16:48:30 +03:00
commit ccfdab6037
7 changed files with 325 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.vscode/*
server
client

15
Makefile Normal file
View File

@ -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

256
Notify.cpp Normal file
View File

@ -0,0 +1,256 @@
#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;
}

42
Notify.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef NOTIFY_H_
#define NOTIFY_H_
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <utility>
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<std::string, int> m_watch_fds;
std::vector<std::pair<int, std::string>> 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<std::string>& files, uint32_t mask);
void add_list(const std::vector<std::pair<std::string, uint32_t>>& files);
void remove_file(const std::string& filename);
void remove_list(const std::vector<std::string>& files);
void clear();
size_t size() const;
std::set<std::string> get_list() const;
void set_list(std::vector<std::pair<std::string, uint32_t>>& files);
std::vector<std::string> get_events() const;
};
#endif // NOTIFY_H_

0
client.cpp Normal file
View File

9
common.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef COMMON_H_
#define COMMON_H_
#include <string>
#include <vector>
std::vector<std::string> split_line(const std::string& line, char delim);
#endif // COMMON_H_

0
server.cpp Normal file
View File