blob: a1d8240504ec109170b73f8a8645ce85f1d292c8 [file]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_UTILS_INOTIFY_WATCHER_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_UTILS_INOTIFY_WATCHER_H_
#include <atomic>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "boost/asio.hpp" // NOLINT
namespace milotic_tlbmc {
class InotifyWatcher : public std::enable_shared_from_this<InotifyWatcher> {
public:
using EventCallback = std::function<void(
absl::string_view dir_path, absl::string_view filename, uint32_t mask)>;
static absl::StatusOr<std::shared_ptr<InotifyWatcher>> Create(
std::shared_ptr<boost::asio::io_context> io_context,
EventCallback callback);
~InotifyWatcher();
absl::Status WatchDirectory(std::string dir_path, uint32_t event_mask);
void Stop();
private:
InotifyWatcher(std::shared_ptr<boost::asio::io_context> io_context,
EventCallback callback, int fd);
void StartAsyncRead();
void HandleRead(const boost::system::error_code& error,
std::size_t bytes_transferred);
std::atomic<int> inotify_fd_ = -1;
absl::Mutex mutex_;
absl::flat_hash_map<int, std::string> watch_descriptors_
ABSL_GUARDED_BY(mutex_);
std::shared_ptr<boost::asio::io_context> io_context_;
boost::asio::posix::stream_descriptor stream_descriptor_;
std::vector<uint8_t> read_buffer_;
EventCallback callback_;
std::atomic<bool> stopped_{false};
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_UTILS_INOTIFY_WATCHER_H_