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