12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/ADT/ScopeExit.h" 14 #include "llvm/Support/AlignOf.h" 15 #include "llvm/Support/Errno.h" 16 #include "llvm/Support/Error.h" 17 #include "llvm/Support/Path.h" 19 #include <condition_variable> 27 #include <sys/epoll.h> 28 #include <sys/inotify.h> 34 using namespace clang;
41 struct SemaphorePipe {
45 SemaphorePipe(
int pipefd[2])
46 : FDRead(pipefd[0]), FDWrite(pipefd[1]), OwnsFDs(
true) {}
47 SemaphorePipe(
const SemaphorePipe &) =
delete;
48 void operator=(
const SemaphorePipe &) =
delete;
49 SemaphorePipe(SemaphorePipe &&other)
50 : FDRead(other.FDRead), FDWrite(other.FDWrite),
51 OwnsFDs(other.OwnsFDs)
54 other.OwnsFDs =
false;
61 llvm::sys::RetryAfterSignal(-1, write, FDWrite,
"A", 1);
75 int InotifyPollingStopperFDs[2];
76 if (pipe2(InotifyPollingStopperFDs, O_CLOEXEC) == -1)
78 return SemaphorePipe(InotifyPollingStopperFDs);
85 std::condition_variable NonEmpty;
86 std::queue<DirectoryWatcher::Event> Events;
92 std::unique_lock<std::mutex> L(Mtx);
93 Events.emplace(K, Filename);
95 NonEmpty.notify_one();
101 std::unique_lock<std::mutex> L(Mtx);
105 if (!Events.empty()) {
110 NonEmpty.wait(L, [
this]() {
return !Events.empty(); });
117 DirectoryWatcherLinux(
118 llvm::StringRef WatchedDirPath,
120 bool WaitForInitialSync,
int InotifyFD,
int InotifyWD,
121 SemaphorePipe &&InotifyPollingStopSignal);
123 ~DirectoryWatcherLinux()
override {
125 InotifyPollingThread.join();
126 EventsReceivingThread.join();
127 inotify_rm_watch(InotifyFD, InotifyWD);
128 llvm::sys::RetryAfterSignal(-1, close, InotifyFD);
132 const std::string WatchedDirPath;
142 std::function<void(llvm::ArrayRef<Event>,
bool)> Receiver;
145 void InotifyPollingLoop();
146 std::thread InotifyPollingThread;
149 SemaphorePipe InotifyPollingStopSignal;
160 std::thread EventsReceivingThread;
164 void EventReceivingLoop();
168 Queue.push_back(DirectoryWatcher::Event::EventKind::WatcherGotInvalidated,
170 InotifyPollingStopSignal.signal();
174 void DirectoryWatcherLinux::InotifyPollingLoop() {
177 constexpr
size_t EventBufferLength =
178 30 * (
sizeof(
struct inotify_event) + NAME_MAX + 1);
187 alignas(
struct inotify_event) char buffer[EventBufferLength];
189 auto ManagedBuffer = std::make_unique<Buffer>();
190 char *
const Buf = ManagedBuffer->buffer;
192 const int EpollFD = epoll_create1(EPOLL_CLOEXEC);
197 auto EpollFDGuard = llvm::make_scope_exit([EpollFD]() { close(EpollFD); });
199 struct epoll_event EventSpec;
200 EventSpec.events = EPOLLIN;
201 EventSpec.data.fd = InotifyFD;
202 if (epoll_ctl(EpollFD, EPOLL_CTL_ADD, InotifyFD, &EventSpec) == -1) {
207 EventSpec.data.fd = InotifyPollingStopSignal.FDRead;
208 if (epoll_ctl(EpollFD, EPOLL_CTL_ADD, InotifyPollingStopSignal.FDRead,
214 std::array<struct epoll_event, 2> EpollEventBuffer;
217 const int EpollWaitResult = llvm::sys::RetryAfterSignal(
218 -1, epoll_wait, EpollFD, EpollEventBuffer.data(),
219 EpollEventBuffer.size(), -1 );
220 if (EpollWaitResult == -1) {
227 for (
int i = 0; i < EpollWaitResult; ++i) {
228 if (EpollEventBuffer[i].data.fd == InotifyPollingStopSignal.FDRead) {
236 ssize_t NumRead = llvm::sys::RetryAfterSignal(-1, read, InotifyFD, Buf,
238 for (
char *
P = Buf;
P < Buf + NumRead;) {
239 if (
P +
sizeof(
struct inotify_event) > Buf + NumRead) {
241 llvm_unreachable(
"an incomplete inotify_event was read");
245 struct inotify_event *Event =
reinterpret_cast<struct inotify_event *
>(
P);
246 P +=
sizeof(
struct inotify_event) + Event->len;
248 if (Event->mask & (IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_DELETE) &&
251 llvm_unreachable(
"expected a filename from inotify");
255 if (Event->mask & (IN_CREATE | IN_MOVED_TO | IN_MODIFY)) {
256 Queue.push_back(DirectoryWatcher::Event::EventKind::Modified,
258 }
else if (Event->mask & (IN_DELETE | IN_MOVED_FROM)) {
259 Queue.push_back(DirectoryWatcher::Event::EventKind::Removed,
261 }
else if (Event->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) {
262 Queue.push_back(DirectoryWatcher::Event::EventKind::WatchedDirRemoved,
266 }
else if (Event->mask & IN_IGNORED) {
271 llvm_unreachable(
"Unknown event type.");
278 void DirectoryWatcherLinux::InitialScan() {
283 void DirectoryWatcherLinux::EventReceivingLoop() {
286 this->Receiver(Event,
false);
288 DirectoryWatcher::Event::EventKind::WatcherGotInvalidated) {
295 DirectoryWatcherLinux::DirectoryWatcherLinux(
296 StringRef WatchedDirPath,
298 bool WaitForInitialSync,
int InotifyFD,
int InotifyWD,
299 SemaphorePipe &&InotifyPollingStopSignal)
300 : WatchedDirPath(WatchedDirPath), InotifyFD(InotifyFD),
301 InotifyWD(InotifyWD), Receiver(Receiver),
302 InotifyPollingStopSignal(
std::move(InotifyPollingStopSignal)) {
304 InotifyPollingThread = std::thread([
this]() { InotifyPollingLoop(); });
308 if (WaitForInitialSync) {
310 EventsReceivingThread = std::thread([
this]() { EventReceivingLoop(); });
312 EventsReceivingThread = std::thread([
this]() {
316 EventReceivingLoop();
326 bool WaitForInitialSync) {
328 llvm::report_fatal_error(
329 "DirectoryWatcher::create can not accept an empty Path.");
331 const int InotifyFD = inotify_init1(IN_CLOEXEC);
333 return llvm::make_error<llvm::StringError>(
334 std::string(
"inotify_init1() error: ") + strerror(errno),
335 llvm::inconvertibleErrorCode());
337 const int InotifyWD = inotify_add_watch(
338 InotifyFD, Path.str().c_str(),
339 IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MODIFY |
340 IN_MOVED_FROM | IN_MOVE_SELF | IN_MOVED_TO | IN_ONLYDIR | IN_IGNORED
341 #ifdef IN_EXCL_UNLINK 346 return llvm::make_error<llvm::StringError>(
347 std::string(
"inotify_add_watch() error: ") + strerror(errno),
348 llvm::inconvertibleErrorCode());
352 if (!InotifyPollingStopper)
353 return llvm::make_error<llvm::StringError>(
354 std::string(
"SemaphorePipe::create() error: ") + strerror(errno),
355 llvm::inconvertibleErrorCode());
357 return std::make_unique<DirectoryWatcherLinux>(
358 Path, Receiver, WaitForInitialSync, InotifyFD, InotifyWD,
359 std::move(*InotifyPollingStopper));
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
std::vector< DirectoryWatcher::Event > getAsFileEvents(const std::vector< std::string > &Scan)
Create event with EventKind::Added for every element in Scan.
std::vector< std::string > scanDirectory(StringRef Path)
static llvm::Expected< std::unique_ptr< DirectoryWatcher > > create(llvm::StringRef Path, std::function< void(llvm::ArrayRef< DirectoryWatcher::Event > Events, bool IsInitial)> Receiver, bool WaitForInitialSync)
llvm fatal_error if
Dataflow Directional Tag Classes.
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
Provides notifications for file changes in a directory.