sync_manager: Create sync watch class
Create a watch class to monitor the files and directories
specified in the synclist file.
Store the file descriptors and file names in a map to be
able to know the full path of the file that triggered the
event. The watch descriptor number does not change so it
can be a single variable.
Change-Id: I211225ddc012af85d9be39ae5d40b8258d73435d
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/sync_watch.hpp b/sync_watch.hpp
new file mode 100644
index 0000000..a7f8557
--- /dev/null
+++ b/sync_watch.hpp
@@ -0,0 +1,66 @@
+#pragma once
+
+#include <experimental/filesystem>
+#include <functional>
+#include <systemd/sd-event.h>
+
+namespace phosphor
+{
+namespace software
+{
+namespace manager
+{
+
+namespace fs = std::experimental::filesystem;
+
+/** @class SyncWatch
+ *
+ * @brief Adds inotify watch on persistent files to be synced
+ *
+ * The inotify watch is hooked up with sd-event, so that on call back,
+ * appropriate actions related to syncing files can be taken.
+ */
+class SyncWatch
+{
+ public:
+ /** @brief ctor - hook inotify watch with sd-event
+ *
+ * @param[in] loop - sd-event object
+ * @param[in] syncCallback - The callback function for processing
+ * files
+ */
+ SyncWatch(sd_event& loop, std::function<int(fs::path&)> syncCallback);
+
+ SyncWatch(const SyncWatch&) = delete;
+ SyncWatch& operator=(const SyncWatch&) = delete;
+ SyncWatch(SyncWatch&&) = default;
+ SyncWatch& operator=(SyncWatch&&) = default;
+
+ /** @brief dtor - remove inotify watch and close fd's
+ */
+ ~SyncWatch();
+
+ private:
+ /** @brief sd-event callback
+ *
+ * @param[in] s - event source, floating (unused) in our case
+ * @param[in] fd - inotify fd
+ * @param[in] revents - events that matched for fd
+ * @param[in] userdata - pointer to SyncWatch object
+ * @returns 0 on success, -1 on fail
+ */
+ static int callback(sd_event_source* s, int fd, uint32_t revents,
+ void* userdata);
+
+ /** @brief Map of file descriptors, watch descriptors, and file paths */
+ using fd = int;
+ using wd = int;
+ std::map<fd, std::map<wd, fs::path>> fileMap;
+
+ /** @brief The callback function for processing the inotify event */
+ std::function<int(fs::path&)> syncCallback;
+};
+
+} // namespace manager
+} // namespace software
+} // namespace phosphor