cmFileMonitor.cxx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 <iostream>
  8. #include <set>
  9. #include <unordered_map>
  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. uv_close(reinterpret_cast<uv_handle_t*>(this->Handle), &on_fs_close);
  148. this->Handle = nullptr;
  149. }
  150. cmVirtualDirectoryWatcher::StopWatching();
  151. }
  152. uv_loop_t* Loop() const final { return this->Parent->Loop(); }
  153. std::vector<std::string> WatchedDirectories() const override
  154. {
  155. std::vector<std::string> result = { Path() };
  156. for (const auto& j : cmVirtualDirectoryWatcher::WatchedDirectories()) {
  157. result.push_back(j);
  158. }
  159. return result;
  160. }
  161. protected:
  162. cmVirtualDirectoryWatcher* const Parent;
  163. const std::string PathSegment;
  164. private:
  165. uv_fs_event_t* Handle = nullptr; // owner!
  166. };
  167. // Root directories:
  168. class cmRootDirectoryWatcher : public cmRealDirectoryWatcher
  169. {
  170. public:
  171. cmRootDirectoryWatcher(cmRootWatcher* p, const std::string& ps)
  172. : cmRealDirectoryWatcher(p, ps)
  173. {
  174. }
  175. std::string Path() const final { return this->PathSegment; }
  176. };
  177. // Normal directories below root:
  178. class cmDirectoryWatcher : public cmRealDirectoryWatcher
  179. {
  180. public:
  181. cmDirectoryWatcher(cmRealDirectoryWatcher* p, const std::string& ps)
  182. : cmRealDirectoryWatcher(p, ps)
  183. {
  184. }
  185. std::string Path() const final
  186. {
  187. return this->Parent->Path() + this->PathSegment + "/";
  188. }
  189. };
  190. class cmFileWatcher : public cmIBaseWatcher
  191. {
  192. public:
  193. cmFileWatcher(cmRealDirectoryWatcher* p, const std::string& ps,
  194. cmFileMonitor::Callback cb)
  195. : Parent(p)
  196. , PathSegment(ps)
  197. , CbList({ std::move(cb) })
  198. {
  199. assert(p);
  200. assert(!ps.empty());
  201. p->AddChildWatcher(ps, this);
  202. }
  203. void StartWatching() final {}
  204. void StopWatching() final {}
  205. void AppendCallback(cmFileMonitor::Callback const& cb)
  206. {
  207. this->CbList.push_back(cb);
  208. }
  209. std::string Path() const final
  210. {
  211. return this->Parent->Path() + this->PathSegment;
  212. }
  213. std::vector<std::string> WatchedDirectories() const final { return {}; }
  214. std::vector<std::string> WatchedFiles() const final
  215. {
  216. return { this->Path() };
  217. }
  218. void Trigger(const std::string& ps, int events, int status) const final
  219. {
  220. assert(ps.empty());
  221. assert(status == 0);
  222. static_cast<void>(ps);
  223. const std::string path = this->Path();
  224. for (const auto& cb : this->CbList) {
  225. cb(path, events, status);
  226. }
  227. }
  228. uv_loop_t* Loop() const final { return this->Parent->Loop(); }
  229. private:
  230. cmRealDirectoryWatcher* Parent;
  231. const std::string PathSegment;
  232. std::vector<cmFileMonitor::Callback> CbList;
  233. };
  234. namespace {
  235. void on_directory_change(uv_fs_event_t* handle, const char* filename,
  236. int events, int status)
  237. {
  238. const cmIBaseWatcher* const watcher =
  239. static_cast<const cmIBaseWatcher*>(handle->data);
  240. const std::string pathSegment(filename ? filename : "");
  241. watcher->Trigger(pathSegment, events, status);
  242. }
  243. void on_fs_close(uv_handle_t* handle)
  244. {
  245. delete reinterpret_cast<uv_fs_event_t*>(handle);
  246. }
  247. } // namespace
  248. cmFileMonitor::cmFileMonitor(uv_loop_t* l)
  249. : Root(new cmRootWatcher(l))
  250. {
  251. }
  252. cmFileMonitor::~cmFileMonitor()
  253. {
  254. delete this->Root;
  255. }
  256. void cmFileMonitor::MonitorPaths(const std::vector<std::string>& paths,
  257. Callback const& cb)
  258. {
  259. for (const auto& p : paths) {
  260. std::vector<std::string> pathSegments;
  261. cmsys::SystemTools::SplitPath(p, pathSegments, true);
  262. const size_t segmentCount = pathSegments.size();
  263. if (segmentCount < 2) { // Expect at least rootdir and filename
  264. continue;
  265. }
  266. cmVirtualDirectoryWatcher* currentWatcher = this->Root;
  267. for (size_t i = 0; i < segmentCount; ++i) {
  268. assert(currentWatcher);
  269. const bool fileSegment = (i == segmentCount - 1);
  270. const bool rootSegment = (i == 0);
  271. assert(
  272. !(fileSegment &&
  273. rootSegment)); // Can not be both filename and root part of the path!
  274. const std::string& currentSegment = pathSegments[i];
  275. if (currentSegment.empty()) {
  276. continue;
  277. }
  278. cmIBaseWatcher* nextWatcher = currentWatcher->Find(currentSegment);
  279. if (!nextWatcher) {
  280. if (rootSegment) { // Root part
  281. assert(currentWatcher == this->Root);
  282. nextWatcher = new cmRootDirectoryWatcher(this->Root, currentSegment);
  283. assert(currentWatcher->Find(currentSegment) == nextWatcher);
  284. } else if (fileSegment) { // File part
  285. assert(currentWatcher != this->Root);
  286. nextWatcher = new cmFileWatcher(
  287. dynamic_cast<cmRealDirectoryWatcher*>(currentWatcher),
  288. currentSegment, cb);
  289. assert(currentWatcher->Find(currentSegment) == nextWatcher);
  290. } else { // Any normal directory in between
  291. nextWatcher = new cmDirectoryWatcher(
  292. dynamic_cast<cmRealDirectoryWatcher*>(currentWatcher),
  293. currentSegment);
  294. assert(currentWatcher->Find(currentSegment) == nextWatcher);
  295. }
  296. } else {
  297. if (fileSegment) {
  298. auto filePtr = dynamic_cast<cmFileWatcher*>(nextWatcher);
  299. assert(filePtr);
  300. filePtr->AppendCallback(cb);
  301. continue;
  302. }
  303. }
  304. currentWatcher = dynamic_cast<cmVirtualDirectoryWatcher*>(nextWatcher);
  305. }
  306. }
  307. this->Root->StartWatching();
  308. }
  309. void cmFileMonitor::StopMonitoring()
  310. {
  311. this->Root->StopWatching();
  312. this->Root->Reset();
  313. }
  314. std::vector<std::string> cmFileMonitor::WatchedFiles() const
  315. {
  316. std::vector<std::string> result;
  317. if (this->Root) {
  318. result = this->Root->WatchedFiles();
  319. }
  320. return result;
  321. }
  322. std::vector<std::string> cmFileMonitor::WatchedDirectories() const
  323. {
  324. std::vector<std::string> result;
  325. if (this->Root) {
  326. result = this->Root->WatchedDirectories();
  327. }
  328. return result;
  329. }