cmFileMonitor.cxx 9.7 KB

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