Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <experimental/filesystem> |
| 4 | #include <functional> |
| 5 | #include <systemd/sd-event.h> |
| 6 | |
| 7 | namespace phosphor |
| 8 | { |
| 9 | namespace software |
| 10 | { |
| 11 | namespace manager |
| 12 | { |
| 13 | |
| 14 | namespace 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 | */ |
| 23 | class 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 Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 32 | SyncWatch(sd_event& loop, std::function<int(int, fs::path&)> syncCallback); |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 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 */ |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 61 | std::function<int(int, fs::path&)> syncCallback; |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | } // namespace manager |
| 65 | } // namespace software |
| 66 | } // namespace phosphor |