libfswatch  1.9.3
poll_monitor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015 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  */
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 
33 namespace fsw
34 {
41  class poll_monitor : public monitor
42  {
43  REGISTER_MONITOR(poll_monitor, poll_monitor_type);
44 
45  public:
49  poll_monitor(std::vector<std::string> paths,
51  void *context = nullptr);
52 
56  virtual ~poll_monitor();
57 
58  protected:
59  void run();
60 
61  private:
62  static const unsigned int MIN_POLL_LATENCY = 1;
63 
64  poll_monitor(const poll_monitor& orig) = delete;
65  poll_monitor& operator=(const poll_monitor& that) = delete;
66 
67  typedef bool (poll_monitor::*poll_monitor_scan_callback)(
68  const std::string& path,
69  const struct stat& stat);
70 
71  typedef struct watched_file_info
72  {
73  time_t mtime;
74  time_t ctime;
75  } watched_file_info;
76 
77  struct poll_monitor_data;
78 
79  void scan(const std::string& path, poll_monitor_scan_callback fn);
80  void collect_initial_data();
81  void collect_data();
82  bool add_path(const std::string& path,
83  const struct stat& fd_stat,
84  poll_monitor_scan_callback poll_callback);
85  bool initial_scan_callback(const std::string& path, const struct stat& stat);
86  bool intermediate_scan_callback(const std::string& path,
87  const struct stat& stat);
88  void find_removed_files();
89  void swap_data_containers();
90 
91  poll_monitor_data *previous_data;
92  poll_monitor_data *new_data;
93 
94  std::vector<event> events;
95  time_t curr_time;
96  };
97 }
98 
99 #endif /* FSW_POLL_MONITOR_H */
void * context
Pointer to context data that will be passed to the monitor::callback.
Definition: monitor.hpp:553
Base class of all monitors.
Definition: monitor.hpp:146
virtual ~poll_monitor()
Destroys an instance of this class.
Definition: poll_monitor.cpp:60
Main namespace of libfswatch.
Definition: event.cpp:23
Definition: poll_monitor.cpp:42
void run()
Execute monitor loop.
Definition: poll_monitor.cpp:195
poll_monitor(std::vector< std::string > paths, FSW_EVENT_CALLBACK *callback, void *context=nullptr)
Constructs an instance of this class.
Definition: cmonitor.h:50
Header of the fsw::monitor class.
std::vector< std::string > paths
List of paths to watch.
Definition: monitor.hpp:533
FSW_EVENT_CALLBACK * callback
Callback to which change events should be notified.
Definition: monitor.hpp:548
void FSW_EVENT_CALLBACK(const std::vector< event > &, void *)
Function definition of an event callback.
Definition: monitor.hpp:60
stat()-based monitor.
Definition: poll_monitor.hpp:41