Repo-Init
 
Loading...
Searching...
No Matches
FileMonitor Class Reference

#include <FileHelpers.hpp>

Public Member Functions

 FileMonitor (std::filesystem::path filePath, uint32_t notifyEvents=IN_MODIFY)
 
 FileMonitor (const FileMonitor &)=delete
 Copy constructor.
 
 FileMonitor (FileMonitor &&)=delete
 Move constructor.
 
FileMonitoroperator= (FileMonitor)=delete
 Copy assignment operator.
 
FileMonitoroperator= (FileMonitor &&)=delete
 Move assignment operator.
 
FNotifyCallback notifyCallback () const
 
void notifyCallback (FNotifyCallback func)
 
void userPtr (const void *ptr)
 
 ~FileMonitor ()
 

Private Member Functions

void threadFunc (const std::stop_token &stopToken) const noexcept
 

Private Attributes

int _fDescriptor {-1}
 File descriptor.
 
int _wDescriptor {-1}
 Watch descriptor.
 
std::filesystem::path _filePath
 File path.
 
FNotifyCallback _notifyCallback
 Callback function.
 
uint32_t _notifyEvents
 Notify types.
 
const void * _userPtr = nullptr
 User pointer.
 
std::unique_ptr< std::jthread > _thread
 Thread.
 

Detailed Description

Invokes functions for a file for given notify events

Definition at line 65 of file FileHelpers.hpp.

Constructor & Destructor Documentation

◆ FileMonitor() [1/3]

FileMonitor::FileMonitor ( std::filesystem::path filePath,
uint32_t notifyEvents = IN_MODIFY )
explicit

Constructor

Parameters
[in]filePathPath to the file
[in]notifyEventsEvents to notify

Definition at line 59 of file FileHelpers.cpp.

60 : _fDescriptor(inotify_init()), _filePath(std::move(filePath)), _notifyEvents(notifyEvents)
61{
62 if (_fDescriptor < 0)
63 {
64 throw std::ios_base::failure("Failed to initialize inotify");
65 }
66
67 _wDescriptor = inotify_add_watch(_fDescriptor, _filePath.c_str(), notifyEvents);
68 if (_wDescriptor < 0)
69 {
70 close(_fDescriptor);
71 throw std::ios_base::failure("Failed to add watch descriptor");
72 }
73
74 if (fcntl(_fDescriptor, F_SETFL, fcntl(_fDescriptor, F_GETFL) | O_NONBLOCK) < 0)
75 {
76 close(_fDescriptor);
77 throw std::ios_base::failure("Failed to set file descriptor to non-blocking mode");
78 }
79
80 _thread = std::make_unique<std::jthread>([this](const std::stop_token &sToken) { threadFunc(sToken); });
81}
void threadFunc(const std::stop_token &stopToken) const noexcept
int _fDescriptor
File descriptor.
int _wDescriptor
Watch descriptor.
uint32_t _notifyEvents
Notify types.
std::filesystem::path _filePath
File path.
std::unique_ptr< std::jthread > _thread
Thread.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FileMonitor() [2/3]

FileMonitor::FileMonitor ( const FileMonitor & )
delete

Copy constructor.

Here is the call graph for this function:

◆ FileMonitor() [3/3]

FileMonitor::FileMonitor ( FileMonitor && )
delete

Move constructor.

Here is the call graph for this function:

◆ ~FileMonitor()

FileMonitor::~FileMonitor ( )

Destructor

Definition at line 83 of file FileHelpers.cpp.

84{
85 if (_thread)
86 {
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)
Here is the call graph for this function:

Member Function Documentation

◆ notifyCallback() [1/2]

FNotifyCallback FileMonitor::notifyCallback ( ) const
inlinenodiscard

Definition at line 105 of file FileHelpers.hpp.

105{ return _notifyCallback; }
FNotifyCallback _notifyCallback
Callback function.

◆ notifyCallback() [2/2]

void FileMonitor::notifyCallback ( FNotifyCallback func)
inline

Definition at line 106 of file FileHelpers.hpp.

106{ _notifyCallback = std::move(func); }

◆ operator=() [1/2]

FileMonitor & FileMonitor::operator= ( FileMonitor && )
delete

Move assignment operator.

Here is the call graph for this function:

◆ operator=() [2/2]

FileMonitor & FileMonitor::operator= ( FileMonitor )
delete

Copy assignment operator.

Here is the call graph for this function:

◆ threadFunc()

void FileMonitor::threadFunc ( const std::stop_token & stopToken) const
privatenoexcept

Definition at line 18 of file FileHelpers.cpp.

19{
20 while (!stopToken.stop_requested())
21 {
22 // Buffer for reading events
23 unsigned int nBytes = 0;
24 if (ioctl(_fDescriptor, FIONREAD, &nBytes) < 0)
25 {
26 spdlog::error("Failed to get available events for file monitoring: {}", getErrnoString(errno));
27 }
28
29 auto buffer = std::vector<char>(nBytes + 1, '\0');
30 auto nRead = read(_fDescriptor, buffer.data(), nBytes);
31 if (nRead < 0 && errno != EAGAIN)
32 {
33 spdlog::error("Failed to read events for file monitoring: {}", getErrnoString(errno));
34 }
35 else if (nRead == 0)
36 {
37 spdlog::debug("No events read for file monitoring");
38 }
39
40 ssize_t idx = 0;
41 while (_notifyCallback && idx < nRead)
42 {
43 const auto *event = std::bit_cast<inotify_event *>(&buffer[static_cast<size_t>(idx)]);
44
45 // Check if file notify type matches
46 if ((event->mask & _notifyEvents) != 0)
47 {
49 break;
50 }
51
52 idx += static_cast<ssize_t>(sizeof(inotify_event) + event->len);
53 }
54
55 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_INTERVAL_MS));
56 }
57}
constexpr int SLEEP_INTERVAL_MS
const void * _userPtr
User pointer.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ userPtr()

void FileMonitor::userPtr ( const void * ptr)
inline

Sets user pointer

Parameters
[in]ptrUser pointer

Definition at line 112 of file FileHelpers.hpp.

112{ _userPtr = ptr; }

Member Data Documentation

◆ _fDescriptor

int FileMonitor::_fDescriptor {-1}
private

File descriptor.

Definition at line 68 of file FileHelpers.hpp.

68{-1};

◆ _filePath

std::filesystem::path FileMonitor::_filePath
private

File path.

Definition at line 72 of file FileHelpers.hpp.

◆ _notifyCallback

FNotifyCallback FileMonitor::_notifyCallback
private

Callback function.

Definition at line 74 of file FileHelpers.hpp.

◆ _notifyEvents

uint32_t FileMonitor::_notifyEvents
private

Notify types.

Definition at line 76 of file FileHelpers.hpp.

◆ _thread

std::unique_ptr<std::jthread> FileMonitor::_thread
private

Thread.

Definition at line 81 of file FileHelpers.hpp.

◆ _userPtr

const void* FileMonitor::_userPtr = nullptr
private

User pointer.

Definition at line 78 of file FileHelpers.hpp.

◆ _wDescriptor

int FileMonitor::_wDescriptor {-1}
private

Watch descriptor.

Definition at line 70 of file FileHelpers.hpp.

70{-1};

The documentation for this class was generated from the following files: