fswatch 1.20.1
Loading...
Searching...
No Matches
poll_monitor.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014-2024 Enrico M. Crisostomo
3 *
4 * This program is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation; either version 3, or (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 * details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
15 */
25
26#ifndef FSW_POLL_MONITOR_H
27# define FSW_POLL_MONITOR_H
28
29# include "monitor.hpp"
30# include <sys/stat.h>
31# include <ctime>
32# include <memory>
33# include <filesystem>
34# include <functional>
35
36namespace fsw
37{
44 class poll_monitor : public monitor
45 {
46 public:
50 poll_monitor(std::vector<std::string> paths,
52 void *context = nullptr);
53
57 ~poll_monitor() override;
58
59 protected:
60 void run() override;
61
62 private:
63 static const unsigned int MIN_POLL_LATENCY = 1;
64
65 poll_monitor(const poll_monitor& orig) = delete;
66 poll_monitor& operator=(const poll_monitor& that) = delete;
67
68 using path_visitor = std::function<bool(const std::string&, const struct stat&)>;
69
70 struct watched_file_info
71 {
72 time_t mtime;
73 time_t ctime;
74 };
75
76 using watched_file_info = struct watched_file_info;
77
78 struct poll_monitor_data;
79
80 void scan(const std::filesystem::path& path, const path_visitor& fn);
81 void collect_initial_data();
82 void collect_data();
83 bool add_path(const std::string& path,
84 const struct stat& fd_stat,
85 const path_visitor& poll_callback);
86 bool initial_scan_callback(const std::string& path, const struct stat& stat);
87 bool intermediate_scan_callback(const std::string& path,
88 const struct stat& stat);
89 void find_removed_files();
90 void swap_data_containers();
91
92 std::unique_ptr<poll_monitor_data> previous_data;
93 std::unique_ptr<poll_monitor_data> new_data;
94
95 std::vector<event> events;
96 time_t curr_time;
97 };
98}
99
100#endif /* FSW_POLL_MONITOR_H */
std::vector< std::string > paths
List of paths to watch.
Definition monitor.hpp:527
void * context
Pointer to context data that will be passed to the monitor::callback.
Definition monitor.hpp:547
FSW_EVENT_CALLBACK * callback
Callback to which change events should be notified.
Definition monitor.hpp:542
monitor(std::vector< std::string > paths, FSW_EVENT_CALLBACK *callback, void *context=nullptr)
Constructs a monitor watching the specified paths.
Definition monitor.cpp:50
poll_monitor(std::vector< std::string > paths, FSW_EVENT_CALLBACK *callback, void *context=nullptr)
Constructs an instance of this class.
~poll_monitor() override
Destroys an instance of this class.
void run() override
Execute monitor loop.
Definition poll_monitor.cpp:218
Header of the fsw::monitor class.
Main namespace of libfswatch.
Definition event.cpp:24
void FSW_EVENT_CALLBACK(const std::vector< event > &, void *)
Function definition of an event callback.
Definition monitor.hpp:57