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