summaryrefslogtreecommitdiffstats
path: root/sync_watch.hpp
blob: 126c50c2bcd5a2eafbbcbe887810e0cefa42f07d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once

#include <systemd/sd-event.h>

#include <experimental/filesystem>
#include <functional>
#include <map>

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(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 Adds an inotify watch to the specified file or directory path
     *
     *  @param[in] path - The path to the file or directory
     */
    void addInotifyWatch(const fs::path& path);

    /** @brief Map of file descriptors, watch descriptors, and file paths */
    using fd = int;
    using wd = int;
    fd inotifyFd;
    std::map<wd, fs::path> fileMap;

    /** @brief The callback function for processing the inotify event */
    std::function<int(int, fs::path&)> syncCallback;

    /** @brief Persistent sd_event loop */
    sd_event& loop;
};

} // namespace manager
} // namespace software
} // namespace phosphor
OpenPOWER on IntegriCloud