xref: /freebsd/contrib/llvm-project/clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
1 //===- DirectoryWatcher-windows.cpp - Windows-platform directory watching -===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "DirectoryScanner.h"
10 #include "clang/DirectoryWatcher/DirectoryWatcher.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Support/ConvertUTF.h"
13 #include "llvm/Support/Path.h"
14 #include "llvm/Support/Windows/WindowsSupport.h"
15 #include <condition_variable>
16 #include <mutex>
17 #include <queue>
18 #include <string>
19 #include <thread>
20 #include <vector>
21 
22 namespace {
23 
24 using DirectoryWatcherCallback =
25     std::function<void(llvm::ArrayRef<clang::DirectoryWatcher::Event>, bool)>;
26 
27 using namespace llvm;
28 using namespace clang;
29 
30 class DirectoryWatcherWindows : public clang::DirectoryWatcher {
31   OVERLAPPED Overlapped;
32 
33   std::vector<DWORD> Notifications;
34 
35   std::thread WatcherThread;
36   std::thread HandlerThread;
37   std::function<void(ArrayRef<DirectoryWatcher::Event>, bool)> Callback;
38   SmallString<MAX_PATH> Path;
39   HANDLE Terminate;
40 
41   std::mutex Mutex;
42   bool WatcherActive = false;
43   std::condition_variable Ready;
44 
45   class EventQueue {
46     std::mutex M;
47     std::queue<DirectoryWatcher::Event> Q;
48     std::condition_variable CV;
49 
50   public:
51     void emplace(DirectoryWatcher::Event::EventKind Kind, StringRef Path) {
52       {
53         std::unique_lock<std::mutex> L(M);
54         Q.emplace(Kind, Path);
55       }
56       CV.notify_one();
57     }
58 
59     DirectoryWatcher::Event pop_front() {
60       std::unique_lock<std::mutex> L(M);
61       while (true) {
62         if (!Q.empty()) {
63           DirectoryWatcher::Event E = Q.front();
64           Q.pop();
65           return E;
66         }
67         CV.wait(L, [this]() { return !Q.empty(); });
68       }
69     }
70   } Q;
71 
72 public:
73   DirectoryWatcherWindows(HANDLE DirectoryHandle, bool WaitForInitialSync,
74                           DirectoryWatcherCallback Receiver);
75 
76   ~DirectoryWatcherWindows() override;
77 
78   void InitialScan();
79   void WatcherThreadProc(HANDLE DirectoryHandle);
80   void NotifierThreadProc(bool WaitForInitialSync);
81 };
82 
83 DirectoryWatcherWindows::DirectoryWatcherWindows(
84     HANDLE DirectoryHandle, bool WaitForInitialSync,
85     DirectoryWatcherCallback Receiver)
86     : Callback(Receiver), Terminate(INVALID_HANDLE_VALUE) {
87   // Pre-compute the real location as we will be handing over the directory
88   // handle to the watcher and performing synchronous operations.
89   {
90     DWORD Size = GetFinalPathNameByHandleW(DirectoryHandle, NULL, 0, 0);
91     std::unique_ptr<WCHAR[]> Buffer{new WCHAR[Size]};
92     Size = GetFinalPathNameByHandleW(DirectoryHandle, Buffer.get(), Size, 0);
93     Buffer[Size] = L'\0';
94     llvm::sys::windows::UTF16ToUTF8(Buffer.get(), Size, Path);
95   }
96 
97   size_t EntrySize = sizeof(FILE_NOTIFY_INFORMATION) + MAX_PATH * sizeof(WCHAR);
98   Notifications.resize((4 * EntrySize) / sizeof(DWORD));
99 
100   memset(&Overlapped, 0, sizeof(Overlapped));
101   Overlapped.hEvent =
102       CreateEventW(NULL, /*bManualReset=*/FALSE, /*bInitialState=*/FALSE, NULL);
103   assert(Overlapped.hEvent && "unable to create event");
104 
105   Terminate =
106       CreateEventW(NULL, /*bManualReset=*/TRUE, /*bInitialState=*/FALSE, NULL);
107 
108   WatcherThread = std::thread([this, DirectoryHandle]() {
109     this->WatcherThreadProc(DirectoryHandle);
110   });
111 
112   if (WaitForInitialSync)
113     InitialScan();
114 
115   HandlerThread = std::thread([this, WaitForInitialSync]() {
116     this->NotifierThreadProc(WaitForInitialSync);
117   });
118 }
119 
120 DirectoryWatcherWindows::~DirectoryWatcherWindows() {
121   // Signal the Watcher to exit.
122   SetEvent(Terminate);
123   HandlerThread.join();
124   WatcherThread.join();
125   CloseHandle(Terminate);
126   CloseHandle(Overlapped.hEvent);
127 }
128 
129 void DirectoryWatcherWindows::InitialScan() {
130   std::unique_lock<std::mutex> lock(Mutex);
131   Ready.wait(lock, [this] { return this->WatcherActive; });
132 
133   Callback(getAsFileEvents(scanDirectory(Path.data())), /*IsInitial=*/true);
134 }
135 
136 void DirectoryWatcherWindows::WatcherThreadProc(HANDLE DirectoryHandle) {
137   while (true) {
138     // We do not guarantee subdirectories, but macOS already provides
139     // subdirectories, might as well as ...
140     BOOL WatchSubtree = TRUE;
141     DWORD NotifyFilter = FILE_NOTIFY_CHANGE_FILE_NAME
142                        | FILE_NOTIFY_CHANGE_DIR_NAME
143                        | FILE_NOTIFY_CHANGE_SIZE
144                        | FILE_NOTIFY_CHANGE_LAST_WRITE
145                        | FILE_NOTIFY_CHANGE_CREATION;
146 
147     DWORD BytesTransferred;
148     if (!ReadDirectoryChangesW(DirectoryHandle, Notifications.data(),
149                                Notifications.size() * sizeof(DWORD),
150                                WatchSubtree, NotifyFilter, &BytesTransferred,
151                                &Overlapped, NULL)) {
152       Q.emplace(DirectoryWatcher::Event::EventKind::WatcherGotInvalidated,
153                 "");
154       break;
155     }
156 
157     if (!WatcherActive) {
158       std::unique_lock<std::mutex> lock(Mutex);
159       WatcherActive = true;
160     }
161     Ready.notify_one();
162 
163     HANDLE Handles[2] = { Terminate, Overlapped.hEvent };
164     switch (WaitForMultipleObjects(2, Handles, FALSE, INFINITE)) {
165     case WAIT_OBJECT_0: // Terminate Request
166     case WAIT_FAILED:   // Failure
167       Q.emplace(DirectoryWatcher::Event::EventKind::WatcherGotInvalidated,
168                 "");
169       (void)CloseHandle(DirectoryHandle);
170       return;
171     case WAIT_TIMEOUT:  // Spurious wakeup?
172       continue;
173     case WAIT_OBJECT_0 + 1: // Directory change
174       break;
175     }
176 
177     if (!GetOverlappedResult(DirectoryHandle, &Overlapped, &BytesTransferred,
178                              FALSE)) {
179       Q.emplace(DirectoryWatcher::Event::EventKind::WatchedDirRemoved,
180                 "");
181       Q.emplace(DirectoryWatcher::Event::EventKind::WatcherGotInvalidated,
182                 "");
183       break;
184     }
185 
186     // There was a buffer underrun on the kernel side.  We may have lost
187     // events, please re-synchronize.
188     if (BytesTransferred == 0) {
189       Q.emplace(DirectoryWatcher::Event::EventKind::WatcherGotInvalidated,
190                 "");
191       break;
192     }
193 
194     for (FILE_NOTIFY_INFORMATION *I =
195             (FILE_NOTIFY_INFORMATION *)Notifications.data();
196          I;
197          I = I->NextEntryOffset
198               ? (FILE_NOTIFY_INFORMATION *)((CHAR *)I + I->NextEntryOffset)
199               : NULL) {
200       DirectoryWatcher::Event::EventKind Kind =
201           DirectoryWatcher::Event::EventKind::WatcherGotInvalidated;
202       switch (I->Action) {
203       case FILE_ACTION_ADDED:
204       case FILE_ACTION_MODIFIED:
205       case FILE_ACTION_RENAMED_NEW_NAME:
206         Kind = DirectoryWatcher::Event::EventKind::Modified;
207         break;
208       case FILE_ACTION_REMOVED:
209       case FILE_ACTION_RENAMED_OLD_NAME:
210         Kind = DirectoryWatcher::Event::EventKind::Removed;
211         break;
212       }
213 
214       SmallString<MAX_PATH> filename;
215       sys::windows::UTF16ToUTF8(I->FileName, I->FileNameLength / sizeof(WCHAR),
216                                 filename);
217       Q.emplace(Kind, filename);
218     }
219   }
220 
221   (void)CloseHandle(DirectoryHandle);
222 }
223 
224 void DirectoryWatcherWindows::NotifierThreadProc(bool WaitForInitialSync) {
225   // If we did not wait for the initial sync, then we should perform the
226   // scan when we enter the thread.
227   if (!WaitForInitialSync)
228     this->InitialScan();
229 
230   while (true) {
231     DirectoryWatcher::Event E = Q.pop_front();
232     Callback(E, /*IsInitial=*/false);
233     if (E.Kind == DirectoryWatcher::Event::EventKind::WatcherGotInvalidated)
234       break;
235   }
236 }
237 
238 auto error(DWORD ErrorCode) {
239   DWORD Flags = FORMAT_MESSAGE_ALLOCATE_BUFFER
240               | FORMAT_MESSAGE_FROM_SYSTEM
241               | FORMAT_MESSAGE_IGNORE_INSERTS;
242 
243   LPSTR Buffer;
244   if (!FormatMessageA(Flags, NULL, ErrorCode,
245                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&Buffer,
246                       0, NULL)) {
247     return make_error<llvm::StringError>("error " + utostr(ErrorCode),
248                                          inconvertibleErrorCode());
249   }
250   std::string Message{Buffer};
251   LocalFree(Buffer);
252   return make_error<llvm::StringError>(Message, inconvertibleErrorCode());
253 }
254 
255 } // namespace
256 
257 llvm::Expected<std::unique_ptr<DirectoryWatcher>>
258 clang::DirectoryWatcher::create(StringRef Path,
259                                 DirectoryWatcherCallback Receiver,
260                                 bool WaitForInitialSync) {
261   if (Path.empty())
262     llvm::report_fatal_error(
263         "DirectoryWatcher::create can not accept an empty Path.");
264 
265   if (!sys::fs::is_directory(Path))
266     llvm::report_fatal_error(
267         "DirectoryWatcher::create can not accept a filepath.");
268 
269   SmallVector<wchar_t, MAX_PATH> WidePath;
270   if (sys::windows::UTF8ToUTF16(Path, WidePath))
271     return llvm::make_error<llvm::StringError>(
272         "unable to convert path to UTF-16", llvm::inconvertibleErrorCode());
273 
274   DWORD DesiredAccess = FILE_LIST_DIRECTORY;
275   DWORD ShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
276   DWORD CreationDisposition = OPEN_EXISTING;
277   DWORD FlagsAndAttributes = FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED;
278 
279   HANDLE DirectoryHandle =
280       CreateFileW(WidePath.data(), DesiredAccess, ShareMode,
281                   /*lpSecurityAttributes=*/NULL, CreationDisposition,
282                   FlagsAndAttributes, NULL);
283   if (DirectoryHandle == INVALID_HANDLE_VALUE)
284     return error(GetLastError());
285 
286   // NOTE: We use the watcher instance as a RAII object to discard the handles
287   // for the directory in case of an error.  Hence, this is early allocated,
288   // with the state being written directly to the watcher.
289   return std::make_unique<DirectoryWatcherWindows>(
290       DirectoryHandle, WaitForInitialSync, Receiver);
291 }
292