blob: d85ac8cb9459b4102f8f82066f296f0796eabd3b [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 Kobylakc98d9122020-05-05 10:36:01 -05005#include <filesystem>
Adriana Kobylakb072d1b2018-04-24 11:37:21 -05006#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
Adriana Kobylakc98d9122020-05-05 10:36:01 -050016namespace fs = std::filesystem;
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050017
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
Adriana Kobylak3a19e622018-11-09 11:39:46 -060057 /** @brief Adds an inotify watch to the specified file or directory path
58 *
59 * @param[in] path - The path to the file or directory
60 */
61 void addInotifyWatch(const fs::path& path);
62
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050063 /** @brief Map of file descriptors, watch descriptors, and file paths */
64 using fd = int;
65 using wd = int;
Brad Bishop02516d32018-11-19 15:29:26 -050066 fd inotifyFd;
67 std::map<wd, fs::path> fileMap;
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050068
69 /** @brief The callback function for processing the inotify event */
Adriana Kobylaka9074342018-05-08 11:52:44 -050070 std::function<int(int, fs::path&)> syncCallback;
Adriana Kobylak3a19e622018-11-09 11:39:46 -060071
72 /** @brief Persistent sd_event loop */
73 sd_event& loop;
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050074};
75
76} // namespace manager
77} // namespace software
78} // namespace phosphor