Leosac  0.8.0
Open Source Access Control
unixfilewatcher.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
26 #ifndef UNIXFILEWATCHER_HPP
27 #define UNIXFILEWATCHER_HPP
28 
29 #include <atomic>
30 #include <map>
31 #include <string>
32 #include <thread>
33 
34 namespace Leosac
35 {
36 namespace Tools
37 {
38 
40 {
41  using UnixFd = int;
42  struct WatchParams
43  {
44  std::string path;
45  int mask;
46  };
47  using Watches = std::map<UnixFd, WatchParams>;
48 
49  static const long DefaultTimeoutMs = 2000;
50 
51  public:
52  explicit UnixFileWatcher();
53 
55 
56  UnixFileWatcher(const UnixFileWatcher &other) = delete;
57 
58  UnixFileWatcher &operator=(const UnixFileWatcher &other) = delete;
59 
60  public:
61  void start();
62 
63  void stop();
64 
65  public:
66  void watchFile(const std::string &path);
67 
68  bool fileHasChanged(const std::string &path) const;
69 
70  void fileReset(const std::string &path);
71 
72  std::size_t size() const;
73 
74  private:
75  void run();
76 
77  private:
78  std::thread _thread;
79  std::atomic<bool> _isRunning;
82 };
83 }
84 }
85 
86 #endif // UNIXFILEWATCHER_HPP
Leosac::Tools::UnixFileWatcher::run
void run()
Definition: unixfilewatcher.cpp:121
Leosac::Tools::UnixFileWatcher::watchFile
void watchFile(const std::string &path)
Definition: unixfilewatcher.cpp:80
Leosac::Tools::UnixFileWatcher::UnixFileWatcher
UnixFileWatcher()
Definition: unixfilewatcher.cpp:39
Leosac::Tools::UnixFileWatcher::operator=
UnixFileWatcher & operator=(const UnixFileWatcher &other)=delete
Leosac::Tools::UnixFileWatcher::start
void start()
Definition: unixfilewatcher.cpp:59
Leosac::Tools::UnixFileWatcher::_inotifyFd
UnixFd _inotifyFd
Definition: unixfilewatcher.hpp:80
Leosac::Tools::UnixFileWatcher::_isRunning
std::atomic< bool > _isRunning
Definition: unixfilewatcher.hpp:79
Leosac::Tools::UnixFileWatcher::stop
void stop()
Definition: unixfilewatcher.cpp:66
Leosac::Tools::UnixFileWatcher::_thread
std::thread _thread
Definition: unixfilewatcher.hpp:78
Leosac::Tools::UnixFileWatcher::WatchParams::path
std::string path
Definition: unixfilewatcher.hpp:44
Leosac::Tools::UnixFileWatcher::Watches
std::map< UnixFd, WatchParams > Watches
Definition: unixfilewatcher.hpp:47
Leosac::Tools::UnixFileWatcher::size
std::size_t size() const
Definition: unixfilewatcher.cpp:116
Leosac::Tools::UnixFileWatcher::_watches
Watches _watches
Definition: unixfilewatcher.hpp:81
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Tools::UnixFileWatcher
Definition: unixfilewatcher.hpp:39
Leosac::Tools::UnixFileWatcher::fileReset
void fileReset(const std::string &path)
Definition: unixfilewatcher.cpp:103
Leosac::Tools::UnixFileWatcher::WatchParams
Definition: unixfilewatcher.hpp:42
Leosac::Tools::UnixFileWatcher::UnixFd
int UnixFd
Definition: unixfilewatcher.hpp:41
Leosac::Tools::UnixFileWatcher::~UnixFileWatcher
~UnixFileWatcher()
Definition: unixfilewatcher.cpp:46
Leosac::Tools::UnixFileWatcher::fileHasChanged
bool fileHasChanged(const std::string &path) const
Definition: unixfilewatcher.cpp:93
Leosac::Tools::UnixFileWatcher::WatchParams::mask
int mask
Definition: unixfilewatcher.hpp:45
Leosac::Tools::UnixFileWatcher::DefaultTimeoutMs
static const long DefaultTimeoutMs
Definition: unixfilewatcher.hpp:49