bool INotifyEventPublisher::addMonitor(const std::string& path, bool recursive) { if (!isPathMonitored(path)) { int watch = ::inotify_add_watch(getHandle(), path.c_str(), IN_ALL_EVENTS); if (watch == -1) { LOG(ERROR) << "Could not add inotfy watch on: " << path; return false; } // Keep a list of the watch descriptors descriptors_.push_back(watch); // Keep a map of the path -> watch descriptor path_descriptors_[path] = watch; // Keep a map of the opposite (descriptor -> path) descriptor_paths_[watch] = path; } if (recursive && isDirectory(path).ok()) { std::vector<std::string> children; // Get a list of children of this directory (requesed recursive watches). listDirectoriesInDirectory(path, children); for (const auto& child : children) { addMonitor(child, recursive); } } return true; }
bool INotifyEventPublisher::addMonitor(const std::string& path, uint32_t mask, bool recursive, bool add_watch) { if (!isPathMonitored(path)) { int watch = ::inotify_add_watch( getHandle(), path.c_str(), ((mask == 0) ? kFileDefaultMasks : mask)); if (add_watch && watch == -1) { LOG(WARNING) << "Could not add inotify watch on: " << path; return false; } // Keep a list of the watch descriptors descriptors_.push_back(watch); // Keep a map of the path -> watch descriptor path_descriptors_[path] = watch; // Keep a map of the opposite (descriptor -> path) descriptor_paths_[watch] = path; } if (recursive && isDirectory(path).ok()) { std::vector<std::string> children; // Get a list of children of this directory (requested recursive watches). listDirectoriesInDirectory(path, children, true); boost::system::error_code ec; for (const auto& child : children) { auto canonicalized = fs::canonical(child, ec).string() + '/'; addMonitor(canonicalized, mask, false); } } return true; }