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