cmFileMonitor.cxx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmFileMonitor.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmsys/SystemTools.hxx"
  6. #include <cassert>
  7. #include <stddef.h>
  8. #include <unordered_map>
  9. #include <utility>
  10. namespace {
  11. void on_directory_change(uv_fs_event_t* handle, const char* filename,
  12. int events, int status);
  13. void on_fs_close(uv_handle_t* handle);
  14. } // namespace
  15. class cmIBaseWatcher
  16. {
  17. public:
  18. cmIBaseWatcher() = default;
  19. virtual ~cmIBaseWatcher() = default;
  20. virtual void Trigger(const std::string& pathSegment, int events,
  21. int status) const = 0;
  22. virtual std::string Path() const = 0;
  23. virtual uv_loop_t* Loop() const = 0;
  24. virtual void StartWatching() = 0;
  25. virtual void StopWatching() = 0;
  26. virtual std::vector<std::string> WatchedFiles() const = 0;
  27. virtual std::vector<std::string> WatchedDirectories() const = 0;
  28. };
  29. class cmVirtualDirectoryWatcher : public cmIBaseWatcher
  30. {
  31. public:
  32. ~cmVirtualDirectoryWatcher() override { cmDeleteAll(this->Children); }
  33. cmIBaseWatcher* Find(const std::string& ps)
  34. {
  35. const auto i = this->Children.find(ps);
  36. return (i == this->Children.end()) ? nullptr : i->second;
  37. }
  38. void Trigger(const std::string& pathSegment, int events,
  39. int status) const final
  40. {
  41. if (pathSegment.empty()) {
  42. for (const auto& i : this->Children) {
  43. i.second->Trigger(std::string(), events, status);
  44. }
  45. } else {
  46. const auto i = this->Children.find(pathSegment);
  47. if (i != this->Children.end()) {
  48. i->second->Trigger(std::string(), events, status);
  49. }
  50. }
  51. }
  52. void StartWatching() override
  53. {
  54. for (const auto& i : this->Children) {
  55. i.second->StartWatching();
  56. }
  57. }
  58. void StopWatching() override
  59. {
  60. for (const auto& i : this->Children) {
  61. i.second->StopWatching();
  62. }
  63. }
  64. std::vector<std::string> WatchedFiles() const final
  65. {
  66. std::vector<std::string> result;
  67. for (const auto& i : this->Children) {
  68. for (const auto& j : i.second->WatchedFiles()) {
  69. result.push_back(j);
  70. }
  71. }
  72. return result;
  73. }
  74. std::vector<std::string> WatchedDirectories() const override
  75. {
  76. std::vector<std::string> result;
  77. for (const auto& i : this->Children) {
  78. for (const auto& j : i.second->WatchedDirectories()) {
  79. result.push_back(j);
  80. }
  81. }
  82. return result;
  83. }
  84. void Reset()
  85. {
  86. cmDeleteAll(this->Children);
  87. this->Children.clear();
  88. }
  89. void AddChildWatcher(const std::string& ps, cmIBaseWatcher* watcher)
  90. {
  91. assert(!ps.empty());
  92. assert(this->Children.find(ps) == this->Children.end());
  93. assert(watcher);
  94. this->Children.emplace(std::make_pair(ps, watcher));
  95. }
  96. private:
  97. std::unordered_map<std::string, cmIBaseWatcher*> Children; // owned!
  98. };
  99. // Root of all the different (on windows!) root directories:
  100. class cmRootWatcher : public cmVirtualDirectoryWatcher
  101. {
  102. public:
  103. cmRootWatcher(uv_loop_t* loop)
  104. : mLoop(loop)
  105. {
  106. assert(loop);
  107. }
  108. std::string Path() const final
  109. {
  110. assert(false);
  111. return std::string();
  112. }
  113. uv_loop_t* Loop() const final { return this->mLoop; }
  114. private:
  115. uv_loop_t* const mLoop; // no ownership!
  116. };
  117. // Real directories:
  118. class cmRealDirectoryWatcher : public cmVirtualDirectoryWatcher
  119. {
  120. public:
  121. cmRealDirectoryWatcher(cmVirtualDirectoryWatcher* p, const std::string& ps)
  122. : Parent(p)
  123. , PathSegment(ps)
  124. {
  125. assert(p);
  126. assert(!ps.empty());
  127. p->AddChildWatcher(ps, this);
  128. }
  129. ~cmRealDirectoryWatcher() override
  130. {
  131. // Handle is freed via uv_handle_close callback!
  132. }
  133. void StartWatching() final
  134. {
  135. if (!this->Handle) {
  136. this->Handle = new uv_fs_event_t;
  137. uv_fs_event_init(this->Loop(), this->Handle);
  138. this->Handle->data = this;
  139. uv_fs_event_start(this->Handle, &on_directory_change, Path().c_str(), 0);
  140. }
  141. cmVirtualDirectoryWatcher::StartWatching();
  142. }
  143. void StopWatching() final
  144. {
  145. if (this->Handle) {
  146. uv_fs_event_stop(this->Handle);
  147. if (!uv_is_closing(reinterpret_cast<uv_handle_t*>(this->Handle))) {
  148. uv_close(reinterpret_cast<uv_handle_t*>(this->Handle), &on_fs_close);
  149. }
  150. this->Handle = nullptr;
  151. }
  152. cmVirtualDirectoryWatcher::StopWatching();
  153. }
  154. uv_loop_t* Loop() const final { return this->Parent->Loop(); }
  155. std::vector<std::string> WatchedDirectories() const override
  156. {
  157. std::vector<std::string> result = { Path() };
  158. for (const auto& j : cmVirtualDirectoryWatcher::WatchedDirectories()) {
  159. result.push_back(j);
  160. }
  161. return result;
  162. }
  163. protected:
  164. cmVirtualDirectoryWatcher* const Parent;
  165. const std::string PathSegment;
  166. private:
  167. uv_fs_event_t* Handle = nullptr; // owner!
  168. };
  169. // Root directories:
  170. class cmRootDirectoryWatcher : public cmRealDirectoryWatcher
  171. {
  172. public:
  173. cmRootDirectoryWatcher(cmRootWatcher* p, const std::string& ps)
  174. : cmRealDirectoryWatcher(p, ps)
  175. {
  176. }
  177. std::string Path() const final { return this->PathSegment; }
  178. };
  179. // Normal directories below root:
  180. class cmDirectoryWatcher : public cmRealDirectoryWatcher
  181. {
  182. public:
  183. cmDirectoryWatcher(cmRealDirectoryWatcher* p, const std::string& ps)
  184. : cmRealDirectoryWatcher(p, ps)
  185. {
  186. }
  187. std::string Path() const final
  188. {
  189. return this->Parent->Path() + this->PathSegment + "/";
  190. }
  191. };
  192. class cmFileWatcher : public cmIBaseWatcher
  193. {
  194. public:
  195. cmFileWatcher(cmRealDirectoryWatcher* p, const std::string& ps,
  196. cmFileMonitor::Callback cb)
  197. : Parent(p)
  198. , PathSegment(ps)
  199. , CbList({ std::move(cb) })
  200. {
  201. assert(p);
  202. assert(!ps.empty());
  203. p->AddChildWatcher(ps, this);
  204. }
  205. void StartWatching() final {}
  206. void StopWatching() final {}
  207. void AppendCallback(cmFileMonitor::Callback const& cb)
  208. {
  209. this->CbList.push_back(cb);
  210. }
  211. std::string Path() const final
  212. {
  213. return this->Parent->Path() + this->PathSegment;
  214. }
  215. std::vector<std::string> WatchedDirectories() const final { return {}; }
  216. std::vector<std::string> WatchedFiles() const final
  217. {
  218. return { this->Path() };
  219. }
  220. void Trigger(const std::string& ps, int events, int status) const final
  221. {
  222. assert(ps.empty());
  223. assert(status == 0);
  224. static_cast<void>(ps);
  225. const std::string path = this->Path();
  226. for (const auto& cb : this->CbList) {
  227. cb(path, events, status);
  228. }
  229. }
  230. uv_loop_t* Loop() const final { return this->Parent->Loop(); }
  231. private:
  232. cmRealDirectoryWatcher* Parent;
  233. const std::string PathSegment;
  234. std::vector<cmFileMonitor::Callback> CbList;
  235. };
  236. namespace {
  237. void on_directory_change(uv_fs_event_t* handle, const char* filename,
  238. int events, int status)
  239. {
  240. const cmIBaseWatcher* const watcher =
  241. static_cast<const cmIBaseWatcher*>(handle->data);
  242. const std::string pathSegment(filename ? filename : "");
  243. watcher->Trigger(pathSegment, events, status);
  244. }
  245. void on_fs_close(uv_handle_t* handle)
  246. {
  247. delete reinterpret_cast<uv_fs_event_t*>(handle);
  248. }
  249. } // namespace
  250. cmFileMonitor::cmFileMonitor(uv_loop_t* l)
  251. : Root(new cmRootWatcher(l))
  252. {
  253. }
  254. cmFileMonitor::~cmFileMonitor()
  255. {
  256. delete this->Root;
  257. }
  258. void cmFileMonitor::MonitorPaths(const std::vector<std::string>& paths,
  259. Callback const& cb)
  260. {
  261. for (const auto& p : paths) {
  262. std::vector<std::string> pathSegments;
  263. cmsys::SystemTools::SplitPath(p, pathSegments, true);
  264. const size_t segmentCount = pathSegments.size();
  265. if (segmentCount < 2) { // Expect at least rootdir and filename
  266. continue;
  267. }
  268. cmVirtualDirectoryWatcher* currentWatcher = this->Root;
  269. for (size_t i = 0; i < segmentCount; ++i) {
  270. assert(currentWatcher);
  271. const bool fileSegment = (i == segmentCount - 1);
  272. const bool rootSegment = (i == 0);
  273. assert(
  274. !(fileSegment &&
  275. rootSegment)); // Can not be both filename and root part of the path!
  276. const std::string& currentSegment = pathSegments[i];
  277. if (currentSegment.empty()) {
  278. continue;
  279. }
  280. cmIBaseWatcher* nextWatcher = currentWatcher->Find(currentSegment);
  281. if (!nextWatcher) {
  282. if (rootSegment) { // Root part
  283. assert(currentWatcher == this->Root);
  284. nextWatcher = new cmRootDirectoryWatcher(this->Root, currentSegment);
  285. assert(currentWatcher->Find(currentSegment) == nextWatcher);
  286. } else if (fileSegment) { // File part
  287. assert(currentWatcher != this->Root);
  288. nextWatcher = new cmFileWatcher(
  289. dynamic_cast<cmRealDirectoryWatcher*>(currentWatcher),
  290. currentSegment, cb);
  291. assert(currentWatcher->Find(currentSegment) == nextWatcher);
  292. } else { // Any normal directory in between
  293. nextWatcher = new cmDirectoryWatcher(
  294. dynamic_cast<cmRealDirectoryWatcher*>(currentWatcher),
  295. currentSegment);
  296. assert(currentWatcher->Find(currentSegment) == nextWatcher);
  297. }
  298. } else {
  299. if (fileSegment) {
  300. auto filePtr = dynamic_cast<cmFileWatcher*>(nextWatcher);
  301. assert(filePtr);
  302. filePtr->AppendCallback(cb);
  303. continue;
  304. }
  305. }
  306. currentWatcher = dynamic_cast<cmVirtualDirectoryWatcher*>(nextWatcher);
  307. }
  308. }
  309. this->Root->StartWatching();
  310. }
  311. void cmFileMonitor::StopMonitoring()
  312. {
  313. this->Root->StopWatching();
  314. this->Root->Reset();
  315. }
  316. std::vector<std::string> cmFileMonitor::WatchedFiles() const
  317. {
  318. std::vector<std::string> result;
  319. if (this->Root) {
  320. result = this->Root->WatchedFiles();
  321. }
  322. return result;
  323. }
  324. std::vector<std::string> cmFileMonitor::WatchedDirectories() const
  325. {
  326. std::vector<std::string> result;
  327. if (this->Root) {
  328. result = this->Root->WatchedDirectories();
  329. }
  330. return result;
  331. }