blob: bbfa476d37ea80bc4f01726a68991baa54193222 [file] [log] [blame]
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -05001#pragma once
2
3#include <experimental/filesystem>
4#include <systemd/sd-event.h>
5#include <sys/inotify.h>
6#include <map>
7
8#include "dump_utils.hpp"
9
10namespace phosphor
11{
12namespace dump
13{
14namespace inotify
15{
16
17namespace fs = std::experimental::filesystem;
18
19//User specfic call back function input map(path:event) type.
20using UserMap = std::map<fs::path, uint32_t>;
21
22//User specific callback function wrapper type.
23using UserType = std::function<void(const UserMap&)>;
24
25/** @class Watch
26 *
27 * @brief Adds inotify watch on directory.
28 *
29 * The inotify watch is hooked up with sd-event, so that on call back,
30 * appropriate actions are taken to collect files from the directory
31 * initialized by the object.
32 */
33class Watch
34{
35 public:
36 /** @brief ctor - hook inotify watch with sd-event
37 *
38 * @param[in] eventObj - Event loop object
39 * @param[in] flags - inotify flags
40 * @param[in] mask - Mask of events
41 * @param[in] events - Events to be watched
42 * @param[in] path - File path to be watched
43 * @param[in] userFunc - User specific callback fnction wrapper.
44 *
45 */
46 Watch(const EventPtr& eventObj,
47 int flags,
48 uint32_t mask,
49 uint32_t events,
50 const fs::path& path,
51 UserType userFunc);
52
53 Watch(const Watch&) = delete;
54 Watch& operator=(const Watch&) = delete;
55 Watch(Watch&&) = default;
56 Watch& operator=(Watch&&) = default;
57
58 /* @brief dtor - remove inotify watch and close fd's */
59 ~Watch();
60
61 private:
62 /** @brief sd-event callback.
63 * @details Collects the files and event info and call the
64 * appropriate user function for further action.
65 *
66 * @param[in] s - event source, floating (unused) in our case
67 * @param[in] fd - inotify fd
68 * @param[in] revents - events that matched for fd
69 * @param[in] userdata - pointer to Watch object
70 *
71 * @returns 0 on success, -1 on fail
72 */
73 static int callback(sd_event_source* s,
74 int fd,
75 uint32_t revents,
76 void* userdata);
77
78 /** initialize an inotify instance and returns file descriptor */
79 int inotifyInit();
80
81 /** @brief inotify flags */
82 int flags;
83
84 /** @brief Mask of events */
85 uint32_t mask;
86
87 /** @brief Events to be watched */
88 uint32_t events;
89
90 /** @brief File path to be watched */
91 fs::path path;
92
93 /** @brief dump file directory watch descriptor */
94 int wd = -1;
95
96 /** @brief file descriptor manager */
97 CustomFd fd;
98
99 /** @brief The user level callback function wrapper */
100 UserType userFunc;
101};
102
103} // namespace inotify
104} // namespace dump
105} // namespace phosphor