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 | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 5 | #include <experimental/filesystem> |
| 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 | |
| 16 | namespace 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 | */ |
| 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; |
| 66 | std::map<fd, std::map<wd, fs::path>> fileMap; |
| 67 | |
| 68 | /** @brief The callback function for processing the inotify event */ |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 69 | std::function<int(int, fs::path&)> syncCallback; |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame^] | 70 | |
| 71 | /** @brief Persistent sd_event loop */ |
| 72 | sd_event& loop; |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } // namespace manager |
| 76 | } // namespace software |
| 77 | } // namespace phosphor |