1 #pragma once
2 #include <functional>
3 #include <memory>
4 #include <sdeventplus/source/event.hpp>
5 #include <sdeventplus/source/io.hpp>
6 #include <string>
7 
8 namespace phosphor::certs
9 {
10 /** @class Watch
11  *
12  *  @brief Adds inotify watch on certificate directory
13  *
14  *  The inotify watch is hooked up with sd-event, so that on call back,
15  *  appropriate actions related to a certificate upload can be taken.
16  */
17 class Watch
18 {
19   public:
20     using Callback = std::function<void()>;
21     /** @brief ctor - hook inotify watch with sd-event
22      *
23      *  @param[in] loop - sd-event object
24      *  @param[in] cb - The callback function for processing
25      *                             certificate upload
26      */
27     Watch(sdeventplus::Event& event, std::string& certFile, Callback cb);
28     Watch(const Watch&) = delete;
29     Watch& operator=(const Watch&) = delete;
30     Watch(Watch&&) = delete;
31     Watch& operator=(Watch&&) = delete;
32 
33     /** @brief dtor - remove inotify watch and close fd's
34      */
35     ~Watch();
36 
37     /** @brief start watch on the specified path
38      */
39     void startWatch();
40 
41     /** @brief stop watch on the specified path
42      */
43     void stopWatch();
44 
45   private:
46     /** @brief certificate upload directory watch descriptor */
47     int wd = -1;
48 
49     /** @brief inotify file descriptor */
50     int fd = -1;
51 
52     /** @brief SDEventPlus IO pointer added to event loop */
53     std::unique_ptr<sdeventplus::source::IO> ioPtr = nullptr;
54 
55     /** @brief sd-event object */
56     sdeventplus::Event& event;
57 
58     /** @brief callback method to be called */
59     Callback callback;
60 
61     /** @brief Certificate directory to watch */
62     std::string watchDir;
63 
64     /** @brief Certificate file to watch */
65     std::string watchFile;
66 
67     /** @brief Certificate file with path */
68     std::string certFile;
69 };
70 } // namespace phosphor::certs
71