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/Mutex.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 llvm::make_unique<llvm::AlignedCharArray<
alignof(
struct inotify_event),
188 EventBufferLength>>();
189 char *
const Buf = ManagedBuffer->buffer;
191 const int EpollFD = epoll_create1(EPOLL_CLOEXEC);
196 auto EpollFDGuard = llvm::make_scope_exit([EpollFD]() { close(EpollFD); });
198 struct epoll_event EventSpec;
199 EventSpec.events = EPOLLIN;
200 EventSpec.data.fd = InotifyFD;
201 if (epoll_ctl(EpollFD, EPOLL_CTL_ADD, InotifyFD, &EventSpec) == -1) {
206 EventSpec.data.fd = InotifyPollingStopSignal.FDRead;
207 if (epoll_ctl(EpollFD, EPOLL_CTL_ADD, InotifyPollingStopSignal.FDRead,
213 std::array<struct epoll_event, 2> EpollEventBuffer;
216 const int EpollWaitResult = llvm::sys::RetryAfterSignal(
217 -1, epoll_wait, EpollFD, EpollEventBuffer.data(),
218 EpollEventBuffer.size(), -1 );
219 if (EpollWaitResult == -1) {
226 for (
int i = 0;
i < EpollWaitResult; ++
i) {
227 if (EpollEventBuffer[
i].data.fd == InotifyPollingStopSignal.FDRead) {
235 ssize_t NumRead = llvm::sys::RetryAfterSignal(-1, read, InotifyFD, Buf,
237 for (
char *
P = Buf;
P < Buf + NumRead;) {
238 if (
P +
sizeof(
struct inotify_event) > Buf + NumRead) {
240 llvm_unreachable(
"an incomplete inotify_event was read");
244 struct inotify_event *Event =
reinterpret_cast<struct inotify_event *
>(
P);
245 P +=
sizeof(
struct inotify_event) + Event->len;
247 if (Event->mask & (IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_DELETE) &&
250 llvm_unreachable(
"expected a filename from inotify");
254 if (Event->mask & (IN_CREATE | IN_MOVED_TO | IN_MODIFY)) {
255 Queue.push_back(DirectoryWatcher::Event::EventKind::Modified,
257 }
else if (Event->mask & (IN_DELETE | IN_MOVED_FROM)) {
258 Queue.push_back(DirectoryWatcher::Event::EventKind::Removed,
260 }
else if (Event->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) {
261 Queue.push_back(DirectoryWatcher::Event::EventKind::WatchedDirRemoved,
265 }
else if (Event->mask & IN_IGNORED) {
270 llvm_unreachable(
"Unknown event type.");
277 void DirectoryWatcherLinux::InitialScan() {
282 void DirectoryWatcherLinux::EventReceivingLoop() {
285 this->Receiver(Event,
false);
287 DirectoryWatcher::Event::EventKind::WatcherGotInvalidated) {
294 DirectoryWatcherLinux::DirectoryWatcherLinux(
295 StringRef WatchedDirPath,
297 bool WaitForInitialSync,
int InotifyFD,
int InotifyWD,
298 SemaphorePipe &&InotifyPollingStopSignal)
299 : WatchedDirPath(WatchedDirPath), InotifyFD(InotifyFD),
300 InotifyWD(InotifyWD), Receiver(Receiver),
301 InotifyPollingStopSignal(
std::move(InotifyPollingStopSignal)) {
303 InotifyPollingThread = std::thread([
this]() { InotifyPollingLoop(); });
307 if (WaitForInitialSync) {
309 EventsReceivingThread = std::thread([
this]() { EventReceivingLoop(); });
311 EventsReceivingThread = std::thread([
this]() {
315 EventReceivingLoop();
325 bool WaitForInitialSync) {
329 const int InotifyFD = inotify_init1(IN_CLOEXEC);
333 const int InotifyWD = inotify_add_watch(
334 InotifyFD, Path.str().c_str(),
335 IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MODIFY |
336 IN_MOVED_FROM | IN_MOVE_SELF | IN_MOVED_TO | IN_ONLYDIR | IN_IGNORED
337 #ifdef IN_EXCL_UNLINK 346 if (!InotifyPollingStopper)
349 return llvm::make_unique<DirectoryWatcherLinux>(
350 Path, Receiver, WaitForInitialSync, InotifyFD, InotifyWD,
351 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)
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.
static std::unique_ptr< DirectoryWatcher > create(llvm::StringRef Path, std::function< void(llvm::ArrayRef< DirectoryWatcher::Event > Events, bool IsInitial)> Receiver, bool WaitForInitialSync)
Returns nullptr if.
Provides notifications for file changes in a directory.