Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
inotify.hpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Contact: Jan Olszak <j.olszak@samsung.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
17 */
18 
25 #ifndef COMMON_UTILS_INOTIFY_HPP
26 #define COMMON_UTILS_INOTIFY_HPP
27 
29 
30 #include <functional>
31 #include <mutex>
32 #include <vector>
33 
34 #include <sys/inotify.h>
35 
36 
37 namespace utils {
38 
43 class Inotify {
44 public:
45  typedef std::function<void(const std::string&, const uint32_t)> Callback;
46 
48  virtual ~Inotify();
49 
50  Inotify(const Inotify&) = delete;
51  Inotify& operator=(const Inotify&) = delete;
52 
56  void setHandler(const std::string& path, const uint32_t eventMask, const Callback&& callback);
57 
61  void removeHandler(const std::string& path);
62 
66  int getFD() const;
67 
68 private:
69  struct Handler {
70  std::string path;
71  int watchID;
73  };
74 
75  typedef std::lock_guard<std::recursive_mutex> Lock;
76  std::recursive_mutex mMutex;
77 
78  int mFD;
80  std::vector<Handler> mHandlers;
81 
82  void handleInternal();
83  void removeHandlerInternal(const std::string& path);
84 };
85 
86 } // namespace utils
87 
88 #endif // COMMON_UTILS_INOTIFY_HPP
void setHandler(const std::string &path, const uint32_t eventMask, const Callback &&callback)
Add a callback for a specified path.
Definition: inotify.cpp:75
C++ epoll wrapper.
Definition: inotify.hpp:69
void handleInternal()
Definition: inotify.cpp:122
std::string path
Definition: inotify.hpp:70
std::lock_guard< std::recursive_mutex > Lock
Definition: inotify.hpp:75
void removeHandlerInternal(const std::string &path)
Definition: inotify.cpp:94
Callback call
Definition: inotify.hpp:72
Inotify & operator=(const Inotify &)=delete
void removeHandler(const std::string &path)
Stop watching the path.
Definition: inotify.cpp:115
std::recursive_mutex mMutex
Definition: inotify.hpp:76
std::function< void(const std::string &, const uint32_t)> Callback
Definition: inotify.hpp:45
Inotify monitors a directory and when a specified file or folder is created or deleted it calls a cor...
Definition: inotify.hpp:43
std::vector< Handler > mHandlers
Definition: inotify.hpp:80
virtual ~Inotify()
Definition: inotify.cpp:54
cargo::ipc::epoll::EventPoll & mEventPoll
Definition: inotify.hpp:79
int mFD
Definition: inotify.hpp:78
Inotify(cargo::ipc::epoll::EventPoll &eventPoll)
Definition: inotify.cpp:41
This class waits on registered file descriptor for events.
Definition: event-poll.hpp:47
int watchID
Definition: inotify.hpp:71
int getFD() const
Definition: inotify.cpp:70