GCC Code Coverage Report


Directory: ./
File: FileMonitor.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 150 165 90.9%
Functions: 13 15 86.7%
Branches: 135 227 59.5%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2022 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the GPL v2 as described in the
10 * included LICENSE.GPL file.
11 */
12
13 #if __has_include(<sys/inotify.h>)
14 #define HAS_INOTIFY 1
15 #include <sys/inotify.h>
16 #endif
17
18 #include <cstring>
19 #include <unistd.h>
20 #include <vector>
21
22 #include "FileMonitor.h"
23 #include "Timers.h"
24 #include "common_mini.h"
25
26 class FileMonitorInfo {
27 public:
28 32073 FileMonitorInfo() = default;
29 31698 ~FileMonitorInfo() = default;
30
31 26768 bool checkForChanges(const std::string& fn) {
32 26768 uint64_t newTimestamp = FileTimestamp(fn);
33
2/2
✓ Branch 0 taken 6894 times.
✓ Branch 1 taken 19874 times.
26768 if (newTimestamp != timestamp) {
34 6894 timestamp = newTimestamp;
35 6894 return true; // File has changed
36 }
37 19874 return false; // No change
38 }
39
40 uint64_t timestamp = 0; // Last modified timestamp
41 int inotify_watch_fd = -1; // Inotify watch descriptor
42 bool isDirectory = false; // Is this a directory, just looking for create/delete events
43
44 std::map<std::string, std::function<void()>> callbacks; // Callbacks for this file
45 };
46
47 FileMonitor FileMonitor::INSTANCE;
48 3252 FileMonitor::FileMonitor() :
49 3252 inotify_fd_(-1) {
50 #ifdef HAS_INOTIFY
51 3252 inotify_fd_ = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
52 #endif
53 3252 }
54 600 FileMonitor::~FileMonitor() {
55 #ifdef HAS_INOTIFY
56
1/2
✓ Branch 0 taken 600 times.
✗ Branch 1 not taken.
600 if (inotify_fd_ >= 0) {
57 600 close(inotify_fd_);
58 600 inotify_fd_ = -1;
59 }
60 #endif
61 600 }
62
63 2649 void FileMonitor::Initialize(std::map<int, std::function<bool(int)>>& callbacks) {
64
1/2
✓ Branch 0 taken 2649 times.
✗ Branch 1 not taken.
2649 if (inotify_fd_ >= 0) {
65
1/2
✓ Branch 0 taken 2649 times.
✗ Branch 1 not taken.
5298 callbacks[inotify_fd_] = [this](int fd) {
66 3057 this->fileChangedEvent();
67 3056 return false;
68 2649 };
69 } else {
70 ✗ Timers::INSTANCE.addPeriodicTimer("FileMonitor", 2000, [this]() {
71 ✗ this->fileChangedEvent();
72 ✗ });
73 }
74 2649 }
75 2620 void FileMonitor::Cleanup() {
76 #ifdef HAS_INOTIFY
77
1/2
✓ Branch 0 taken 2620 times.
✗ Branch 1 not taken.
2620 std::lock_guard<std::mutex> lock(mutex_);
78
2/2
✓ Branch 0 taken 12077 times.
✓ Branch 1 taken 2620 times.
14697 for (const auto& entry : fileMapping_) {
79 12077 inotify_rm_watch(inotify_fd_, entry.first);
80 }
81 #endif
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2620 times.
2620 if (inotify_fd_ < 0) {
83 ✗ Timers::INSTANCE.stopPeriodicTimer("FileMonitor");
84 }
85 2620 fileMapping_.clear();
86 2620 files_.clear();
87 2620 }
88 29419 FileMonitor& FileMonitor::AddFile(const std::string& id, const std::string& file, const std::function<void()>& callback, bool modificationsOnly) {
89
1/2
✓ Branch 0 taken 29419 times.
✗ Branch 1 not taken.
29419 std::lock_guard<std::mutex> lock(mutex_);
90
91
1/2
✓ Branch 0 taken 29419 times.
✗ Branch 1 not taken.
29419 auto it = files_.find(file);
92
2/2
✓ Branch 0 taken 26768 times.
✓ Branch 1 taken 2651 times.
29419 if (it == files_.end()) {
93
2/4
✓ Branch 0 taken 26768 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26768 times.
✗ Branch 3 not taken.
26768 files_[file].checkForChanges(file);
94 }
95
1/2
✓ Branch 0 taken 29419 times.
✗ Branch 1 not taken.
29419 auto& fileInfo = files_[file];
96
2/4
✓ Branch 0 taken 29419 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29419 times.
✗ Branch 3 not taken.
29419 fileInfo.callbacks[id] = callback;
97 #ifdef HAS_INOTIFY
98
1/2
✓ Branch 0 taken 29419 times.
✗ Branch 1 not taken.
29419 if (!modificationsOnly) {
99
1/2
✓ Branch 0 taken 29419 times.
✗ Branch 1 not taken.
29419 std::string dir = file.substr(0, file.find_last_of("/") + 1);
100
1/2
✓ Branch 0 taken 29419 times.
✗ Branch 1 not taken.
29419 auto dit = files_.find(dir);
101
2/2
✓ Branch 0 taken 5305 times.
✓ Branch 1 taken 24114 times.
29419 if (dit == files_.end()) {
102 // IN_MOVED_TO catches files that appear via an atomic rename
103 // (write-temp-then-rename), which is how config files are written so
104 // a reader never sees a partially-written file. Without it, watchers
105 // on rename-replaced files would silently stop firing.
106 5305 int inotify_watch_fd = inotify_add_watch(inotify_fd_, dir.c_str(), IN_CREATE | IN_DELETE | IN_MOVED_TO);
107
2/4
✓ Branch 0 taken 5305 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5305 times.
✗ Branch 3 not taken.
5305 fileMapping_[inotify_watch_fd] = dir;
108
1/2
✓ Branch 0 taken 5305 times.
✗ Branch 1 not taken.
5305 files_[dir].inotify_watch_fd = inotify_watch_fd;
109
1/2
✓ Branch 0 taken 5305 times.
✗ Branch 1 not taken.
5305 files_[dir].isDirectory = true; // Mark as directory for create/delete events
110 }
111 29419 }
112
7/8
✓ Branch 0 taken 29419 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7801 times.
✓ Branch 3 taken 21618 times.
✓ Branch 4 taken 6894 times.
✓ Branch 5 taken 907 times.
✓ Branch 6 taken 6894 times.
✓ Branch 7 taken 22525 times.
29419 if (FileExists(file) && fileInfo.inotify_watch_fd < 0) {
113 // IN_CLOSE_WRITE, not IN_MODIFY: one save is an open with O_TRUNC and
114 // one or more writes, each its own IN_MODIFY, so every save ran the
115 // callbacks two or more times -- the first time on a truncated file.
116 // A channel output reload per event re-created every output twice.
117 6894 int inotify_watch_fd = inotify_add_watch(inotify_fd_, file.c_str(), IN_CLOSE_WRITE);
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6894 times.
6894 if (inotify_watch_fd < 0) {
119 ✗ return *this;
120 }
121
2/4
✓ Branch 0 taken 6894 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6894 times.
✗ Branch 3 not taken.
6894 fileMapping_[inotify_watch_fd] = file;
122 6894 fileInfo.inotify_watch_fd = inotify_watch_fd;
123 }
124 #endif
125 29419 return *this;
126 29419 }
127 10408 FileMonitor& FileMonitor::RemoveFile(const std::string& id, const std::string& file) {
128
1/2
✓ Branch 0 taken 10408 times.
✗ Branch 1 not taken.
10408 std::lock_guard<std::mutex> lock(mutex_);
129
1/2
✓ Branch 0 taken 10408 times.
✗ Branch 1 not taken.
10408 auto it = files_.find(file);
130
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10407 times.
10408 if (it != files_.end()) {
131
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 it->second.callbacks.erase(id);
132
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 if (it->second.callbacks.empty() && !it->second.isDirectory) {
133 // If no callbacks left, remove the file from monitoring
134 #ifdef HAS_INOTIFY
135
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (it->second.inotify_watch_fd >= 0) {
136 1 inotify_rm_watch(inotify_fd_, it->second.inotify_watch_fd);
137
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 fileMapping_.erase(it->second.inotify_watch_fd);
138 }
139 #endif
140
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 files_.erase(it);
141 }
142 }
143 10408 return *this;
144 10408 }
145
146 // Copy the matching callbacks out and invoke them with mutex_ released.
147 // Callbacks do arbitrary heavyweight work (reloading channel outputs, plugin
148 // teardown); if one crashes, the process exit path runs ~Plugin -> RemoveFile
149 // on this same thread, and holding mutex_ here turns that crash into a
150 // self-deadlocked zombie instead of a clean crash-exit.
151 // `id` empty means "every callback on this file".
152 23858 void FileMonitor::fireCallbacks(const std::string& id, const std::string& file) {
153 23858 std::vector<std::function<void()>> callbacks;
154 {
155
1/2
✓ Branch 0 taken 23858 times.
✗ Branch 1 not taken.
23858 std::lock_guard<std::mutex> lock(mutex_);
156
1/2
✓ Branch 0 taken 23858 times.
✗ Branch 1 not taken.
23858 auto it = files_.find(file);
157
1/2
✓ Branch 0 taken 23858 times.
✗ Branch 1 not taken.
23858 if (it != files_.end()) {
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23858 times.
23858 if (id.empty()) {
159 ✗ for (const auto& callback : it->second.callbacks) {
160 ✗ callbacks.push_back(callback.second);
161 }
162 } else {
163
1/2
✓ Branch 0 taken 23858 times.
✗ Branch 1 not taken.
23858 auto cit = it->second.callbacks.find(id);
164
1/2
✓ Branch 0 taken 23858 times.
✗ Branch 1 not taken.
23858 if (cit != it->second.callbacks.end()) {
165
1/2
✓ Branch 0 taken 23858 times.
✗ Branch 1 not taken.
23858 callbacks.push_back(cit->second);
166 }
167 }
168 }
169 23858 }
170
1/2
✓ Branch 0 taken 23858 times.
✗ Branch 1 not taken.
23858 if (!callbacks.empty()) {
171
1/2
✓ Branch 0 taken 23858 times.
✗ Branch 1 not taken.
23858 std::lock_guard<std::mutex> cbLock(callbackMutex_);
172
2/2
✓ Branch 0 taken 23858 times.
✓ Branch 1 taken 23858 times.
47716 for (auto& callback : callbacks) {
173
1/2
✓ Branch 0 taken 23858 times.
✗ Branch 1 not taken.
23858 callback();
174 }
175 23858 }
176 23858 }
177
178 ✗ FileMonitor& FileMonitor::TriggerFileChanged(const std::string& file) {
179 ✗ fireCallbacks("", file);
180 ✗ return *this;
181 }
182
183 23858 FileMonitor& FileMonitor::TriggerFileChanged(const std::string& id, const std::string& file) {
184 // An empty id would mean "all" to fireCallbacks(); AddFile() allows one, so
185 // keep the two overloads from silently converging on different behaviour.
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23858 times.
23858 if (id.empty()) {
187 ✗ return *this;
188 }
189 23858 fireCallbacks(id, file);
190 23858 return *this;
191 }
192
193 3057 void FileMonitor::fileChangedEvent() {
194 #ifdef HAS_INOTIFY
195 char buffer[16384];
196 size_t buffer_i;
197 struct inotify_event* pevent;
198 ssize_t r;
199 size_t event_size;
200
201 3057 memset(buffer, 0, sizeof(buffer));
202
1/2
✓ Branch 0 taken 3057 times.
✗ Branch 1 not taken.
3057 r = read(inotify_fd_, buffer, sizeof(buffer));
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3057 times.
3057 if (r <= 0) {
204 ✗ return;
205 }
206 3057 buffer_i = 0;
207
2/2
✓ Branch 0 taken 3360 times.
✓ Branch 1 taken 147 times.
3507 while (buffer_i < r) {
208 /* Parse events and queue them. */
209 3360 pevent = (struct inotify_event*)&buffer[buffer_i];
210 3360 event_size = offsetof(struct inotify_event, name) + pevent->len;
211 3360 buffer_i += event_size;
212
213 // Callbacks are collected under the lock but invoked after it is
214 // released -- see TriggerFileChanged for why holding mutex_ across a
215 // callback is not survivable.
216 3360 std::vector<std::function<void()>> pendingCallbacks;
217
1/2
✓ Branch 0 taken 3360 times.
✗ Branch 1 not taken.
3360 std::unique_lock<std::mutex> lock(mutex_);
218
2/4
✓ Branch 0 taken 3360 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3359 times.
✗ Branch 3 not taken.
3360 std::string path = fileMapping_[pevent->wd];
219
220 // printf("%d) %s: (%s) id: %d mask: %X\n", buffer_i, path.c_str(), pevent->name, pevent->wd, pevent->mask);
221
222
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 3209 times.
3359 if (pevent->mask & IN_IGNORED) {
223 // The kernel auto-removed this watch -- e.g. the file was deleted, or
224 // it was replaced via an atomic rename (write-temp-then-rename), which
225 // unlinks the old inode. Drop our stale wd mapping and clear the
226 // file's watch descriptor so a subsequent IN_CREATE/IN_MOVED_TO for
227 // the same name re-arms an inode watch on the new file.
228
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 auto mit = fileMapping_.find(pevent->wd);
229
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 if (mit != fileMapping_.end()) {
230
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 auto fit = files_.find(mit->second);
231
5/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 147 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 147 times.
150 if (fit != files_.end() && fit->second.inotify_watch_fd == pevent->wd) {
232 3 fit->second.inotify_watch_fd = -1;
233 }
234
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 fileMapping_.erase(mit);
235 }
236 150 continue;
237
2/2
✓ Branch 0 taken 2683 times.
✓ Branch 1 taken 526 times.
3359 } else if (pevent->mask & (IN_CREATE | IN_MOVED_TO)) {
238 // A watched file appeared: either created in place (IN_CREATE) or
239 // atomically renamed into place (IN_MOVED_TO). For a rename over an
240 // existing file the previous inode watch is gone (its IN_IGNORED is
241 // handled above or still queued), so re-arm an IN_CLOSE_WRITE watch on the
242 // current inode before notifying.
243
1/2
✓ Branch 0 taken 2683 times.
✗ Branch 1 not taken.
2683 path += pevent->name;
244
1/2
✓ Branch 0 taken 2683 times.
✗ Branch 1 not taken.
2683 auto it = files_.find(path);
245
2/2
✓ Branch 0 taken 159 times.
✓ Branch 1 taken 2524 times.
2683 if (it != files_.end()) {
246 159 bool notify = false;
247
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 11 times.
159 if (pevent->mask & IN_MOVED_TO) {
248 // New inode in place: drop any stale watch on the replaced
249 // inode (the kernel auto-removes it) and always notify.
250
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 2 times.
148 if (it->second.inotify_watch_fd >= 0) {
251
1/2
✓ Branch 0 taken 146 times.
✗ Branch 1 not taken.
146 fileMapping_.erase(it->second.inotify_watch_fd);
252 146 it->second.inotify_watch_fd = -1;
253 }
254 148 notify = true;
255 }
256
1/2
✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
159 if (it->second.inotify_watch_fd < 0) {
257 159 int wd = inotify_add_watch(inotify_fd_, path.c_str(), IN_CLOSE_WRITE);
258
1/2
✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
159 if (wd >= 0) {
259 159 it->second.inotify_watch_fd = wd;
260
2/4
✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 159 times.
✗ Branch 3 not taken.
159 fileMapping_[wd] = path;
261 }
262 159 notify = true;
263 }
264
1/2
✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
159 if (notify) {
265
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 159 times.
325 for (const auto& callback : it->second.callbacks) {
266
1/2
✓ Branch 0 taken 166 times.
✗ Branch 1 not taken.
166 pendingCallbacks.push_back(callback.second);
267 }
268 }
269 }
270
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 517 times.
526 } else if (pevent->mask & IN_DELETE) {
271
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 path += pevent->name;
272
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 auto it = files_.find(path);
273
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if (it != files_.end()) {
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (it->second.inotify_watch_fd >= 0) {
275 ✗ inotify_rm_watch(inotify_fd_, it->second.inotify_watch_fd);
276 ✗ fileMapping_.erase(it->second.inotify_watch_fd);
277 ✗ it->second.inotify_watch_fd = -1; // Reset watch descriptor
278 }
279 // Notify whether or not the file's own watch is still here:
280 // the unlink frees the inode first, so that watch's IN_IGNORED
281 // is usually queued ahead of this and has already cleared it.
282 // Gating on it meant a deleted file was never reported.
283
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for (const auto& callback : it->second.callbacks) {
284
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 pendingCallbacks.push_back(callback.second);
285 }
286 }
287
1/2
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
517 } else if (pevent->mask & IN_CLOSE_WRITE) {
288
1/2
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
517 auto it = files_.find(path);
289
1/2
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
517 if (it != files_.end()) {
290
2/2
✓ Branch 0 taken 585 times.
✓ Branch 1 taken 517 times.
1102 for (const auto& callback : it->second.callbacks) {
291
1/2
✓ Branch 0 taken 585 times.
✗ Branch 1 not taken.
585 pendingCallbacks.push_back(callback.second);
292 }
293 }
294 }
295
1/2
✓ Branch 0 taken 3209 times.
✗ Branch 1 not taken.
3209 lock.unlock();
296
2/2
✓ Branch 0 taken 679 times.
✓ Branch 1 taken 2530 times.
3209 if (!pendingCallbacks.empty()) {
297
1/2
✓ Branch 0 taken 679 times.
✗ Branch 1 not taken.
679 std::lock_guard<std::mutex> cbLock(callbackMutex_);
298
2/2
✓ Branch 0 taken 754 times.
✓ Branch 1 taken 679 times.
1433 for (auto& callback : pendingCallbacks) {
299
1/2
✓ Branch 0 taken 754 times.
✗ Branch 1 not taken.
754 callback();
300 }
301 679 }
302
2/2
✓ Branch 0 taken 3032 times.
✓ Branch 1 taken 177 times.
3209 if (buffer_i >= r) {
303
1/2
✓ Branch 0 taken 3032 times.
✗ Branch 1 not taken.
3032 r = read(inotify_fd_, buffer, sizeof(buffer));
304
2/2
✓ Branch 0 taken 2909 times.
✓ Branch 1 taken 123 times.
3032 if (r <= 0) {
305 2909 break; // No more events to process
306 }
307 123 buffer_i = 0;
308 }
309
9/9
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 2909 times.
✓ Branch 3 taken 300 times.
✓ Branch 4 taken 150 times.
✓ Branch 5 taken 2909 times.
✓ Branch 6 taken 300 times.
✓ Branch 7 taken 150 times.
✓ Branch 8 taken 2909 times.
9477 }
310 #else
311 std::vector<std::function<void()>> pendingCallbacks;
312 {
313 std::lock_guard<std::mutex> lock(mutex_);
314 for (auto& fileEntry : files_) {
315 const std::string& file = fileEntry.first;
316 FileMonitorInfo& fileInfo = fileEntry.second;
317 if (fileInfo.checkForChanges(file)) {
318 for (const auto& callback : fileInfo.callbacks) {
319 pendingCallbacks.push_back(callback.second);
320 }
321 }
322 }
323 }
324 if (!pendingCallbacks.empty()) {
325 std::lock_guard<std::mutex> cbLock(callbackMutex_);
326 for (auto& callback : pendingCallbacks) {
327 callback();
328 }
329 }
330 #endif
331 }
332