blob: 4303671b304a2cd4d6ad7a7b1c5182349fb11e4d [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 */
Adriana Kobylaka9074342018-05-08 11:52:44 -050032 SyncWatch(sd_event& loop, std::function<int(int, fs::path&)> syncCallback);
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050033
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 */
Adriana Kobylaka9074342018-05-08 11:52:44 -050061 std::function<int(int, fs::path&)> syncCallback;
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050062};
63
64} // namespace manager
65} // namespace software
66} // namespace phosphor