Repo-Init
 
Loading...
Searching...
No Matches
FileHelpers.cpp
Go to the documentation of this file.
2
4
5#include <fcntl.h>
6#include <iostream>
7#include <sys/file.h>
8#include <sys/ioctl.h>
9#include <unistd.h>
10
11#include <spdlog/spdlog.h>
12
14constexpr int SLEEP_INTERVAL_MS = 50;
15
16void FileMonitor::threadFunc() const noexcept
17{
18 while (!_shouldStop._M_i)
19 {
20 // Buffer for reading events
21 unsigned int nBytes = 0;
22 if (ioctl(_fDescriptor, FIONREAD, &nBytes) < 0)
23 {
24 spdlog::error("Failed to get available events for file monitoring: {}", getErrnoString(errno));
25 }
26
27 auto buffer = std::vector<char>(nBytes + 1, '\0');
28 auto nRead = read(_fDescriptor, buffer.data(), nBytes);
29 if (nRead < 0)
30 {
31 spdlog::error("Failed to read events for file monitoring: {}", getErrnoString(errno));
32 }
33 else if (nRead == 0)
34 {
35 spdlog::debug("No events read for file monitoring");
36 }
37
38 ssize_t idx = 0;
39 while (_notifyCallback && idx < nRead)
40 {
41 const auto *event = reinterpret_cast<inotify_event *>(&buffer[static_cast<size_t>(idx)]);
42
43 // Check if file notify type matches
44 if ((event->mask & _notifyEvents) != 0)
45 {
47 break;
48 }
49
50 idx += static_cast<ssize_t>(sizeof(inotify_event) + event->len);
51 }
52
53 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_INTERVAL_MS));
54 }
55}
56
57FileMonitor::FileMonitor(std::filesystem::path filePath, uint32_t notifyEvents)
58 : _fDescriptor(inotify_init()), _filePath(std::move(filePath)), _notifyEvents(notifyEvents)
59{
60 if (_fDescriptor < 0)
61 {
62 throw std::ios_base::failure("Failed to initialize inotify");
63 }
64
65 _wDescriptor = inotify_add_watch(_fDescriptor, _filePath.c_str(), notifyEvents);
66 if (_wDescriptor < 0)
67 {
68 close(_fDescriptor);
69 throw std::ios_base::failure("Failed to add watch descriptor");
70 }
71
72 if (fcntl(_fDescriptor, F_SETFL, fcntl(_fDescriptor, F_GETFL) | O_NONBLOCK) < 0)
73 {
74 close(_fDescriptor);
75 throw std::ios_base::failure("Failed to set file descriptor to non-blocking mode");
76 }
77
78 _thread = std::make_unique<std::thread>(&FileMonitor::threadFunc, this);
79}
80
82{
83 _shouldStop.test_and_set();
84 if (_thread && _thread->joinable())
85 {
86 _thread->join();
87 _thread.reset();
88 }
89
90 // Remove watch descriptor first
91 if (_wDescriptor >= 0)
92 {
93 if (inotify_rm_watch(_fDescriptor, _wDescriptor) < 0)
94 {
95 try
96 {
97 spdlog::error("Failed to remove watch descriptor: {}", getErrnoString(errno));
98 }
99 catch (const std::exception &e)
100 {
101 std::cerr << "Failed to remove watch descriptor and also logger thrown an exception: "
102 << getErrnoString(errno) << " " << e.what() << '\n';
103 }
104 }
105 _wDescriptor = -1;
106 }
107
108 // Then close the file descriptor
109 if (_fDescriptor >= 0)
110 {
111 if (close(_fDescriptor) < 0)
112 {
113 try
114 {
115 spdlog::error("Failed to close file descriptor: {}", getErrnoString(errno));
116 }
117 catch (const std::exception &e)
118 {
119 std::cerr << "Failed to close file descriptor and also logger thrown an exception: "
120 << getErrnoString(errno) << " " << e.what() << '\n';
121 }
122 }
123 _fDescriptor = -1;
124 }
125}
std::string getErrnoString(int errVal)
constexpr int SLEEP_INTERVAL_MS
Sleep interval for the file monitor.
constexpr int SLEEP_INTERVAL_MS
const void * _userPtr
User pointer.
FNotifyCallback _notifyCallback
Callback function.
int _fDescriptor
File descriptor.
void threadFunc() const noexcept
std::atomic_flag _shouldStop
Flag to stop monitoring.
std::unique_ptr< std::thread > _thread
Thread.
FileMonitor(std::filesystem::path filePath, uint32_t notifyEvents=IN_MODIFY)
int _wDescriptor
Watch descriptor.
uint32_t _notifyEvents
Notify types.
std::filesystem::path _filePath
File path.