177dd8a79SJan Korous //===- DirectoryWatcher-linux.cpp - Linux-platform directory watching -----===//
277dd8a79SJan Korous //
377dd8a79SJan Korous // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
477dd8a79SJan Korous // See https://llvm.org/LICENSE.txt for license information.
577dd8a79SJan Korous // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
677dd8a79SJan Korous //
777dd8a79SJan Korous //===----------------------------------------------------------------------===//
877dd8a79SJan Korous 
977dd8a79SJan Korous #include "DirectoryScanner.h"
1077dd8a79SJan Korous #include "clang/DirectoryWatcher/DirectoryWatcher.h"
1177dd8a79SJan Korous 
1277dd8a79SJan Korous #include "llvm/ADT/STLExtras.h"
1377dd8a79SJan Korous #include "llvm/ADT/ScopeExit.h"
14d2ed9d6bSReid Kleckner #include "llvm/Support/AlignOf.h"
1577dd8a79SJan Korous #include "llvm/Support/Errno.h"
16ef74924fSPuyan Lotfi #include "llvm/Support/Error.h"
17*72390f0cSSimon Pilgrim #include "llvm/Support/MathExtras.h"
1877dd8a79SJan Korous #include "llvm/Support/Path.h"
1977dd8a79SJan Korous #include <atomic>
2077dd8a79SJan Korous #include <condition_variable>
2177dd8a79SJan Korous #include <mutex>
2277dd8a79SJan Korous #include <queue>
2377dd8a79SJan Korous #include <string>
2477dd8a79SJan Korous #include <thread>
2577dd8a79SJan Korous #include <vector>
2677dd8a79SJan Korous 
2777dd8a79SJan Korous #include <fcntl.h>
2877dd8a79SJan Korous #include <sys/epoll.h>
2977dd8a79SJan Korous #include <sys/inotify.h>
3077dd8a79SJan Korous #include <unistd.h>
3177dd8a79SJan Korous 
3277dd8a79SJan Korous namespace {
3377dd8a79SJan Korous 
3477dd8a79SJan Korous using namespace llvm;
3577dd8a79SJan Korous using namespace clang;
3677dd8a79SJan Korous 
3777dd8a79SJan Korous /// Pipe for inter-thread synchronization - for epoll-ing on multiple
3877dd8a79SJan Korous /// conditions. It is meant for uni-directional 1:1 signalling - specifically:
3977dd8a79SJan Korous /// no multiple consumers, no data passing. Thread waiting for signal should
4077dd8a79SJan Korous /// poll the FDRead. Signalling thread should call signal() which writes single
4177dd8a79SJan Korous /// character to FDRead.
4277dd8a79SJan Korous struct SemaphorePipe {
4377dd8a79SJan Korous   // Expects two file-descriptors opened as a pipe in the canonical POSIX
4477dd8a79SJan Korous   // order: pipefd[0] refers to the read end of the pipe. pipefd[1] refers to
4577dd8a79SJan Korous   // the write end of the pipe.
SemaphorePipe__anon93fcd25b0111::SemaphorePipe4677dd8a79SJan Korous   SemaphorePipe(int pipefd[2])
4777dd8a79SJan Korous       : FDRead(pipefd[0]), FDWrite(pipefd[1]), OwnsFDs(true) {}
4877dd8a79SJan Korous   SemaphorePipe(const SemaphorePipe &) = delete;
4977dd8a79SJan Korous   void operator=(const SemaphorePipe &) = delete;
SemaphorePipe__anon93fcd25b0111::SemaphorePipe5077dd8a79SJan Korous   SemaphorePipe(SemaphorePipe &&other)
5177dd8a79SJan Korous       : FDRead(other.FDRead), FDWrite(other.FDWrite),
5277dd8a79SJan Korous         OwnsFDs(other.OwnsFDs) // Someone could have moved from the other
5377dd8a79SJan Korous                                // instance before.
5477dd8a79SJan Korous   {
5577dd8a79SJan Korous     other.OwnsFDs = false;
5677dd8a79SJan Korous   };
5777dd8a79SJan Korous 
signal__anon93fcd25b0111::SemaphorePipe5877dd8a79SJan Korous   void signal() {
59000ba715SJan Korous #ifndef NDEBUG
60000ba715SJan Korous     ssize_t Result =
61000ba715SJan Korous #endif
62000ba715SJan Korous     llvm::sys::RetryAfterSignal(-1, write, FDWrite, "A", 1);
6377dd8a79SJan Korous     assert(Result != -1);
6477dd8a79SJan Korous   }
~SemaphorePipe__anon93fcd25b0111::SemaphorePipe6577dd8a79SJan Korous   ~SemaphorePipe() {
6677dd8a79SJan Korous     if (OwnsFDs) {
6777dd8a79SJan Korous       close(FDWrite);
6877dd8a79SJan Korous       close(FDRead);
6977dd8a79SJan Korous     }
7077dd8a79SJan Korous   }
7177dd8a79SJan Korous   const int FDRead;
7277dd8a79SJan Korous   const int FDWrite;
7377dd8a79SJan Korous   bool OwnsFDs;
7477dd8a79SJan Korous 
create__anon93fcd25b0111::SemaphorePipe7577dd8a79SJan Korous   static llvm::Optional<SemaphorePipe> create() {
7677dd8a79SJan Korous     int InotifyPollingStopperFDs[2];
7777dd8a79SJan Korous     if (pipe2(InotifyPollingStopperFDs, O_CLOEXEC) == -1)
7877dd8a79SJan Korous       return llvm::None;
7977dd8a79SJan Korous     return SemaphorePipe(InotifyPollingStopperFDs);
8077dd8a79SJan Korous   }
8177dd8a79SJan Korous };
8277dd8a79SJan Korous 
8377dd8a79SJan Korous /// Mutex-protected queue of Events.
8477dd8a79SJan Korous class EventQueue {
8577dd8a79SJan Korous   std::mutex Mtx;
8677dd8a79SJan Korous   std::condition_variable NonEmpty;
8777dd8a79SJan Korous   std::queue<DirectoryWatcher::Event> Events;
8877dd8a79SJan Korous 
8977dd8a79SJan Korous public:
push_back(const DirectoryWatcher::Event::EventKind K,StringRef Filename)9077dd8a79SJan Korous   void push_back(const DirectoryWatcher::Event::EventKind K,
9177dd8a79SJan Korous                  StringRef Filename) {
9277dd8a79SJan Korous     {
9377dd8a79SJan Korous       std::unique_lock<std::mutex> L(Mtx);
9477dd8a79SJan Korous       Events.emplace(K, Filename);
9577dd8a79SJan Korous     }
9677dd8a79SJan Korous     NonEmpty.notify_one();
9777dd8a79SJan Korous   }
9877dd8a79SJan Korous 
9977dd8a79SJan Korous   // Blocks on caller thread and uses codition_variable to wait until there's an
10077dd8a79SJan Korous   // event to return.
pop_front_blocking()10177dd8a79SJan Korous   DirectoryWatcher::Event pop_front_blocking() {
10277dd8a79SJan Korous     std::unique_lock<std::mutex> L(Mtx);
10377dd8a79SJan Korous     while (true) {
10477dd8a79SJan Korous       // Since we might have missed all the prior notifications on NonEmpty we
10577dd8a79SJan Korous       // have to check the queue first (under lock).
10677dd8a79SJan Korous       if (!Events.empty()) {
10777dd8a79SJan Korous         DirectoryWatcher::Event Front = Events.front();
10877dd8a79SJan Korous         Events.pop();
10977dd8a79SJan Korous         return Front;
11077dd8a79SJan Korous       }
11177dd8a79SJan Korous       NonEmpty.wait(L, [this]() { return !Events.empty(); });
11277dd8a79SJan Korous     }
11377dd8a79SJan Korous   }
11477dd8a79SJan Korous };
11577dd8a79SJan Korous 
11677dd8a79SJan Korous class DirectoryWatcherLinux : public clang::DirectoryWatcher {
11777dd8a79SJan Korous public:
11877dd8a79SJan Korous   DirectoryWatcherLinux(
11977dd8a79SJan Korous       llvm::StringRef WatchedDirPath,
12077dd8a79SJan Korous       std::function<void(llvm::ArrayRef<Event>, bool)> Receiver,
12177dd8a79SJan Korous       bool WaitForInitialSync, int InotifyFD, int InotifyWD,
12277dd8a79SJan Korous       SemaphorePipe &&InotifyPollingStopSignal);
12377dd8a79SJan Korous 
~DirectoryWatcherLinux()12477dd8a79SJan Korous   ~DirectoryWatcherLinux() override {
12577dd8a79SJan Korous     StopWork();
12677dd8a79SJan Korous     InotifyPollingThread.join();
12777dd8a79SJan Korous     EventsReceivingThread.join();
12877dd8a79SJan Korous     inotify_rm_watch(InotifyFD, InotifyWD);
12977dd8a79SJan Korous     llvm::sys::RetryAfterSignal(-1, close, InotifyFD);
13077dd8a79SJan Korous   }
13177dd8a79SJan Korous 
13277dd8a79SJan Korous private:
13377dd8a79SJan Korous   const std::string WatchedDirPath;
13477dd8a79SJan Korous   // inotify file descriptor
13577dd8a79SJan Korous   int InotifyFD = -1;
13677dd8a79SJan Korous   // inotify watch descriptor
13777dd8a79SJan Korous   int InotifyWD = -1;
13877dd8a79SJan Korous 
13977dd8a79SJan Korous   EventQueue Queue;
14077dd8a79SJan Korous 
14177dd8a79SJan Korous   // Make sure lifetime of Receiver fully contains lifetime of
14277dd8a79SJan Korous   // EventsReceivingThread.
14377dd8a79SJan Korous   std::function<void(llvm::ArrayRef<Event>, bool)> Receiver;
14477dd8a79SJan Korous 
14577dd8a79SJan Korous   // Consumes inotify events and pushes directory watcher events to the Queue.
14677dd8a79SJan Korous   void InotifyPollingLoop();
14777dd8a79SJan Korous   std::thread InotifyPollingThread;
14877dd8a79SJan Korous   // Using pipe so we can epoll two file descriptors at once - inotify and
14977dd8a79SJan Korous   // stopping condition.
15077dd8a79SJan Korous   SemaphorePipe InotifyPollingStopSignal;
15177dd8a79SJan Korous 
15277dd8a79SJan Korous   // Does the initial scan of the directory - directly calling Receiver,
15377dd8a79SJan Korous   // bypassing the Queue. Both InitialScan and EventReceivingLoop use Receiver
15477dd8a79SJan Korous   // which isn't necessarily thread-safe.
15577dd8a79SJan Korous   void InitialScan();
15677dd8a79SJan Korous 
15777dd8a79SJan Korous   // Processing events from the Queue.
15877dd8a79SJan Korous   // In case client doesn't want to do the initial scan synchronously
15977dd8a79SJan Korous   // (WaitForInitialSync=false in ctor) we do the initial scan at the beginning
16077dd8a79SJan Korous   // of this thread.
16177dd8a79SJan Korous   std::thread EventsReceivingThread;
16277dd8a79SJan Korous   // Push event of WatcherGotInvalidated kind to the Queue to stop the loop.
16377dd8a79SJan Korous   // Both InitialScan and EventReceivingLoop use Receiver which isn't
16477dd8a79SJan Korous   // necessarily thread-safe.
16577dd8a79SJan Korous   void EventReceivingLoop();
16677dd8a79SJan Korous 
16777dd8a79SJan Korous   // Stops all the async work. Reentrant.
StopWork()16877dd8a79SJan Korous   void StopWork() {
16977dd8a79SJan Korous     Queue.push_back(DirectoryWatcher::Event::EventKind::WatcherGotInvalidated,
17077dd8a79SJan Korous                     "");
17177dd8a79SJan Korous     InotifyPollingStopSignal.signal();
17277dd8a79SJan Korous   }
17377dd8a79SJan Korous };
17477dd8a79SJan Korous 
InotifyPollingLoop()17577dd8a79SJan Korous void DirectoryWatcherLinux::InotifyPollingLoop() {
17677dd8a79SJan Korous   // We want to be able to read ~30 events at once even in the worst case
17777dd8a79SJan Korous   // (obscenely long filenames).
17877dd8a79SJan Korous   constexpr size_t EventBufferLength =
17977dd8a79SJan Korous       30 * (sizeof(struct inotify_event) + NAME_MAX + 1);
18077dd8a79SJan Korous   // http://man7.org/linux/man-pages/man7/inotify.7.html
18177dd8a79SJan Korous   // Some systems cannot read integer variables if they are not
18277dd8a79SJan Korous   // properly aligned. On other systems, incorrect alignment may
18377dd8a79SJan Korous   // decrease performance. Hence, the buffer used for reading from
18477dd8a79SJan Korous   // the inotify file descriptor should have the same alignment as
18577dd8a79SJan Korous   // struct inotify_event.
18677dd8a79SJan Korous 
187ac868620SJF Bastien   struct Buffer {
188ac868620SJF Bastien     alignas(struct inotify_event) char buffer[EventBufferLength];
189ac868620SJF Bastien   };
1902b3d49b6SJonas Devlieghere   auto ManagedBuffer = std::make_unique<Buffer>();
191d9e55fa5SJF Bastien   char *const Buf = ManagedBuffer->buffer;
19277dd8a79SJan Korous 
19377dd8a79SJan Korous   const int EpollFD = epoll_create1(EPOLL_CLOEXEC);
19477dd8a79SJan Korous   if (EpollFD == -1) {
19577dd8a79SJan Korous     StopWork();
19677dd8a79SJan Korous     return;
19777dd8a79SJan Korous   }
19877dd8a79SJan Korous   auto EpollFDGuard = llvm::make_scope_exit([EpollFD]() { close(EpollFD); });
19977dd8a79SJan Korous 
20077dd8a79SJan Korous   struct epoll_event EventSpec;
20177dd8a79SJan Korous   EventSpec.events = EPOLLIN;
20277dd8a79SJan Korous   EventSpec.data.fd = InotifyFD;
20377dd8a79SJan Korous   if (epoll_ctl(EpollFD, EPOLL_CTL_ADD, InotifyFD, &EventSpec) == -1) {
20477dd8a79SJan Korous     StopWork();
20577dd8a79SJan Korous     return;
20677dd8a79SJan Korous   }
20777dd8a79SJan Korous 
20877dd8a79SJan Korous   EventSpec.data.fd = InotifyPollingStopSignal.FDRead;
20977dd8a79SJan Korous   if (epoll_ctl(EpollFD, EPOLL_CTL_ADD, InotifyPollingStopSignal.FDRead,
21077dd8a79SJan Korous                 &EventSpec) == -1) {
21177dd8a79SJan Korous     StopWork();
21277dd8a79SJan Korous     return;
21377dd8a79SJan Korous   }
21477dd8a79SJan Korous 
21577dd8a79SJan Korous   std::array<struct epoll_event, 2> EpollEventBuffer;
21677dd8a79SJan Korous 
21777dd8a79SJan Korous   while (true) {
21877dd8a79SJan Korous     const int EpollWaitResult = llvm::sys::RetryAfterSignal(
21977dd8a79SJan Korous         -1, epoll_wait, EpollFD, EpollEventBuffer.data(),
22077dd8a79SJan Korous         EpollEventBuffer.size(), /*timeout=*/-1 /*== infinity*/);
22177dd8a79SJan Korous     if (EpollWaitResult == -1) {
22277dd8a79SJan Korous       StopWork();
22377dd8a79SJan Korous       return;
22477dd8a79SJan Korous     }
22577dd8a79SJan Korous 
22677dd8a79SJan Korous     // Multiple epoll_events can be received for a single file descriptor per
22777dd8a79SJan Korous     // epoll_wait call.
228ec2abbafSJan Korous     for (int i = 0; i < EpollWaitResult; ++i) {
229ec2abbafSJan Korous       if (EpollEventBuffer[i].data.fd == InotifyPollingStopSignal.FDRead) {
23077dd8a79SJan Korous         StopWork();
23177dd8a79SJan Korous         return;
23277dd8a79SJan Korous       }
23377dd8a79SJan Korous     }
23477dd8a79SJan Korous 
23577dd8a79SJan Korous     // epoll_wait() always return either error or >0 events. Since there was no
23677dd8a79SJan Korous     // event for stopping, it must be an inotify event ready for reading.
23777dd8a79SJan Korous     ssize_t NumRead = llvm::sys::RetryAfterSignal(-1, read, InotifyFD, Buf,
23877dd8a79SJan Korous                                                   EventBufferLength);
23977dd8a79SJan Korous     for (char *P = Buf; P < Buf + NumRead;) {
24077dd8a79SJan Korous       if (P + sizeof(struct inotify_event) > Buf + NumRead) {
24177dd8a79SJan Korous         StopWork();
24277dd8a79SJan Korous         llvm_unreachable("an incomplete inotify_event was read");
24377dd8a79SJan Korous         return;
24477dd8a79SJan Korous       }
24577dd8a79SJan Korous 
24677dd8a79SJan Korous       struct inotify_event *Event = reinterpret_cast<struct inotify_event *>(P);
24777dd8a79SJan Korous       P += sizeof(struct inotify_event) + Event->len;
24877dd8a79SJan Korous 
24977dd8a79SJan Korous       if (Event->mask & (IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_DELETE) &&
25077dd8a79SJan Korous           Event->len <= 0) {
25177dd8a79SJan Korous         StopWork();
25277dd8a79SJan Korous         llvm_unreachable("expected a filename from inotify");
25377dd8a79SJan Korous         return;
25477dd8a79SJan Korous       }
25577dd8a79SJan Korous 
25677dd8a79SJan Korous       if (Event->mask & (IN_CREATE | IN_MOVED_TO | IN_MODIFY)) {
25777dd8a79SJan Korous         Queue.push_back(DirectoryWatcher::Event::EventKind::Modified,
25877dd8a79SJan Korous                         Event->name);
25977dd8a79SJan Korous       } else if (Event->mask & (IN_DELETE | IN_MOVED_FROM)) {
26077dd8a79SJan Korous         Queue.push_back(DirectoryWatcher::Event::EventKind::Removed,
26177dd8a79SJan Korous                         Event->name);
26277dd8a79SJan Korous       } else if (Event->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) {
26377dd8a79SJan Korous         Queue.push_back(DirectoryWatcher::Event::EventKind::WatchedDirRemoved,
26477dd8a79SJan Korous                         "");
26577dd8a79SJan Korous         StopWork();
26677dd8a79SJan Korous         return;
26777dd8a79SJan Korous       } else if (Event->mask & IN_IGNORED) {
26877dd8a79SJan Korous         StopWork();
26977dd8a79SJan Korous         return;
27077dd8a79SJan Korous       } else {
27177dd8a79SJan Korous         StopWork();
27277dd8a79SJan Korous         llvm_unreachable("Unknown event type.");
27377dd8a79SJan Korous         return;
27477dd8a79SJan Korous       }
27577dd8a79SJan Korous     }
27677dd8a79SJan Korous   }
27777dd8a79SJan Korous }
27877dd8a79SJan Korous 
InitialScan()27977dd8a79SJan Korous void DirectoryWatcherLinux::InitialScan() {
28077dd8a79SJan Korous   this->Receiver(getAsFileEvents(scanDirectory(WatchedDirPath)),
28177dd8a79SJan Korous                  /*IsInitial=*/true);
28277dd8a79SJan Korous }
28377dd8a79SJan Korous 
EventReceivingLoop()28477dd8a79SJan Korous void DirectoryWatcherLinux::EventReceivingLoop() {
28577dd8a79SJan Korous   while (true) {
28677dd8a79SJan Korous     DirectoryWatcher::Event Event = this->Queue.pop_front_blocking();
28777dd8a79SJan Korous     this->Receiver(Event, false);
28877dd8a79SJan Korous     if (Event.Kind ==
28977dd8a79SJan Korous         DirectoryWatcher::Event::EventKind::WatcherGotInvalidated) {
29077dd8a79SJan Korous       StopWork();
29177dd8a79SJan Korous       return;
29277dd8a79SJan Korous     }
29377dd8a79SJan Korous   }
29477dd8a79SJan Korous }
29577dd8a79SJan Korous 
DirectoryWatcherLinux(StringRef WatchedDirPath,std::function<void (llvm::ArrayRef<Event>,bool)> Receiver,bool WaitForInitialSync,int InotifyFD,int InotifyWD,SemaphorePipe && InotifyPollingStopSignal)29677dd8a79SJan Korous DirectoryWatcherLinux::DirectoryWatcherLinux(
29777dd8a79SJan Korous     StringRef WatchedDirPath,
29877dd8a79SJan Korous     std::function<void(llvm::ArrayRef<Event>, bool)> Receiver,
29977dd8a79SJan Korous     bool WaitForInitialSync, int InotifyFD, int InotifyWD,
30077dd8a79SJan Korous     SemaphorePipe &&InotifyPollingStopSignal)
30177dd8a79SJan Korous     : WatchedDirPath(WatchedDirPath), InotifyFD(InotifyFD),
30277dd8a79SJan Korous       InotifyWD(InotifyWD), Receiver(Receiver),
30377dd8a79SJan Korous       InotifyPollingStopSignal(std::move(InotifyPollingStopSignal)) {
30477dd8a79SJan Korous 
30577dd8a79SJan Korous   InotifyPollingThread = std::thread([this]() { InotifyPollingLoop(); });
30677dd8a79SJan Korous   // We have no guarantees about thread safety of the Receiver which is being
30777dd8a79SJan Korous   // used in both InitialScan and EventReceivingLoop. We shouldn't run these
30877dd8a79SJan Korous   // only synchronously.
30977dd8a79SJan Korous   if (WaitForInitialSync) {
31077dd8a79SJan Korous     InitialScan();
31177dd8a79SJan Korous     EventsReceivingThread = std::thread([this]() { EventReceivingLoop(); });
31277dd8a79SJan Korous   } else {
31377dd8a79SJan Korous     EventsReceivingThread = std::thread([this]() {
31477dd8a79SJan Korous       // FIXME: We might want to terminate an async initial scan early in case
31577dd8a79SJan Korous       // of a failure in EventsReceivingThread.
31677dd8a79SJan Korous       InitialScan();
31777dd8a79SJan Korous       EventReceivingLoop();
31877dd8a79SJan Korous     });
31977dd8a79SJan Korous   }
32077dd8a79SJan Korous }
32177dd8a79SJan Korous 
32277dd8a79SJan Korous } // namespace
32377dd8a79SJan Korous 
create(StringRef Path,std::function<void (llvm::ArrayRef<DirectoryWatcher::Event>,bool)> Receiver,bool WaitForInitialSync)324ef74924fSPuyan Lotfi llvm::Expected<std::unique_ptr<DirectoryWatcher>> clang::DirectoryWatcher::create(
32577dd8a79SJan Korous     StringRef Path,
32677dd8a79SJan Korous     std::function<void(llvm::ArrayRef<DirectoryWatcher::Event>, bool)> Receiver,
32777dd8a79SJan Korous     bool WaitForInitialSync) {
3281dcf216fSPuyan Lotfi   if (Path.empty())
3291dcf216fSPuyan Lotfi     llvm::report_fatal_error(
3301dcf216fSPuyan Lotfi         "DirectoryWatcher::create can not accept an empty Path.");
33177dd8a79SJan Korous 
33277dd8a79SJan Korous   const int InotifyFD = inotify_init1(IN_CLOEXEC);
33377dd8a79SJan Korous   if (InotifyFD == -1)
334ef74924fSPuyan Lotfi     return llvm::make_error<llvm::StringError>(
335ef74924fSPuyan Lotfi         std::string("inotify_init1() error: ") + strerror(errno),
336ef74924fSPuyan Lotfi         llvm::inconvertibleErrorCode());
33777dd8a79SJan Korous 
33877dd8a79SJan Korous   const int InotifyWD = inotify_add_watch(
33977dd8a79SJan Korous       InotifyFD, Path.str().c_str(),
34060a0d49eSJan Korous       IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MODIFY |
34160a0d49eSJan Korous       IN_MOVED_FROM | IN_MOVE_SELF | IN_MOVED_TO | IN_ONLYDIR | IN_IGNORED
34257f4bacfSJan Korous #ifdef IN_EXCL_UNLINK
34360a0d49eSJan Korous       | IN_EXCL_UNLINK
34460a0d49eSJan Korous #endif
34560a0d49eSJan Korous       );
34677dd8a79SJan Korous   if (InotifyWD == -1)
347ef74924fSPuyan Lotfi     return llvm::make_error<llvm::StringError>(
348ef74924fSPuyan Lotfi         std::string("inotify_add_watch() error: ") + strerror(errno),
349ef74924fSPuyan Lotfi         llvm::inconvertibleErrorCode());
35077dd8a79SJan Korous 
35177dd8a79SJan Korous   auto InotifyPollingStopper = SemaphorePipe::create();
35277dd8a79SJan Korous 
35377dd8a79SJan Korous   if (!InotifyPollingStopper)
354ef74924fSPuyan Lotfi     return llvm::make_error<llvm::StringError>(
355ef74924fSPuyan Lotfi         std::string("SemaphorePipe::create() error: ") + strerror(errno),
356ef74924fSPuyan Lotfi         llvm::inconvertibleErrorCode());
35777dd8a79SJan Korous 
3582b3d49b6SJonas Devlieghere   return std::make_unique<DirectoryWatcherLinux>(
35977dd8a79SJan Korous       Path, Receiver, WaitForInitialSync, InotifyFD, InotifyWD,
36077dd8a79SJan Korous       std::move(*InotifyPollingStopper));
36177dd8a79SJan Korous }
362