blob: a7f855767c858caabee89b142ac2b8d7f32c5cae [file] [log] [blame]
Adriana Kobylakb072d1b2018-04-24 11:37:21 -05001#pragma once
2
3#include <experimental/filesystem>
4#include <functional>
5#include <systemd/sd-event.h>
6
7namespace phosphor
8{
9namespace software
10{
11namespace manager
12{
13
14namespace fs = std::experimental::filesystem;
15
16/** @class SyncWatch
17 *
18 * @brief Adds inotify watch on persistent files to be synced
19 *
20 * The inotify watch is hooked up with sd-event, so that on call back,
21 * appropriate actions related to syncing files can be taken.
22 */
23class SyncWatch
24{
25 public:
26 /** @brief ctor - hook inotify watch with sd-event
27 *
28 * @param[in] loop - sd-event object
29 * @param[in] syncCallback - The callback function for processing
30 * files
31 */
32 SyncWatch(sd_event& loop, std::function<int(fs::path&)> syncCallback);
33
34 SyncWatch(const SyncWatch&) = delete;
35 SyncWatch& operator=(const SyncWatch&) = delete;
36 SyncWatch(SyncWatch&&) = default;
37 SyncWatch& operator=(SyncWatch&&) = default;
38
39 /** @brief dtor - remove inotify watch and close fd's
40 */
41 ~SyncWatch();
42
43 private:
44 /** @brief sd-event callback
45 *
46 * @param[in] s - event source, floating (unused) in our case
47 * @param[in] fd - inotify fd
48 * @param[in] revents - events that matched for fd
49 * @param[in] userdata - pointer to SyncWatch object
50 * @returns 0 on success, -1 on fail
51 */
52 static int callback(sd_event_source* s, int fd, uint32_t revents,
53 void* userdata);
54
55 /** @brief Map of file descriptors, watch descriptors, and file paths */
56 using fd = int;
57 using wd = int;
58 std::map<fd, std::map<wd, fs::path>> fileMap;
59
60 /** @brief The callback function for processing the inotify event */
61 std::function<int(fs::path&)> syncCallback;
62};
63
64} // namespace manager
65} // namespace software
66} // namespace phosphor