Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 3 | #include <systemd/sd-event.h> |
| 4 | |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame^] | 5 | #include <filesystem> |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 6 | #include <functional> |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 7 | #include <map> |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace software |
| 12 | { |
| 13 | namespace manager |
| 14 | { |
| 15 | |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame^] | 16 | namespace fs = std::filesystem; |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 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 | */ |
| 25 | class 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 Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 34 | SyncWatch(sd_event& loop, std::function<int(int, fs::path&)> syncCallback); |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 35 | |
| 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 Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 57 | /** @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 Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 63 | /** @brief Map of file descriptors, watch descriptors, and file paths */ |
| 64 | using fd = int; |
| 65 | using wd = int; |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 66 | fd inotifyFd; |
| 67 | std::map<wd, fs::path> fileMap; |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 68 | |
| 69 | /** @brief The callback function for processing the inotify event */ |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 70 | std::function<int(int, fs::path&)> syncCallback; |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 71 | |
| 72 | /** @brief Persistent sd_event loop */ |
| 73 | sd_event& loop; |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | } // namespace manager |
| 77 | } // namespace software |
| 78 | } // namespace phosphor |