FileWatcher.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "FileWatcher.h"
  8. #include <AK/Debug.h>
  9. #include <AK/LexicalPath.h>
  10. #include <AK/NonnullRefPtr.h>
  11. #include <AK/String.h>
  12. #include <Kernel/API/InodeWatcherEvent.h>
  13. #include <Kernel/API/InodeWatcherFlags.h>
  14. #include <LibCore/Notifier.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. namespace Core {
  20. // Only supported in serenity mode because we use InodeWatcher syscalls
  21. #ifdef __serenity__
  22. static Optional<FileWatcherEvent> get_event_from_fd(int fd, HashMap<unsigned, String> const& wd_to_path)
  23. {
  24. u8 buffer[MAXIMUM_EVENT_SIZE];
  25. int rc = read(fd, &buffer, MAXIMUM_EVENT_SIZE);
  26. if (rc == 0) {
  27. return {};
  28. } else if (rc < 0) {
  29. dbgln_if(FILE_WATCHER_DEBUG, "get_event_from_fd: Reading from wd {} failed: {}", fd, strerror(errno));
  30. return {};
  31. }
  32. InodeWatcherEvent* event = reinterpret_cast<InodeWatcherEvent*>(buffer);
  33. FileWatcherEvent result;
  34. auto it = wd_to_path.find(event->watch_descriptor);
  35. if (it == wd_to_path.end()) {
  36. dbgln_if(FILE_WATCHER_DEBUG, "get_event_from_fd: Got an event for a non-existent wd {}?!", event->watch_descriptor);
  37. return {};
  38. }
  39. String const& path = it->value;
  40. switch (event->type) {
  41. case InodeWatcherEvent::Type::ChildCreated:
  42. result.type = FileWatcherEvent::Type::ChildCreated;
  43. break;
  44. case InodeWatcherEvent::Type::ChildDeleted:
  45. result.type = FileWatcherEvent::Type::ChildDeleted;
  46. break;
  47. case InodeWatcherEvent::Type::Deleted:
  48. result.type = FileWatcherEvent::Type::Deleted;
  49. break;
  50. case InodeWatcherEvent::Type::ContentModified:
  51. result.type = FileWatcherEvent::Type::ContentModified;
  52. break;
  53. case InodeWatcherEvent::Type::MetadataModified:
  54. result.type = FileWatcherEvent::Type::MetadataModified;
  55. break;
  56. default:
  57. warnln("Unknown event type {} returned by the watch_file descriptor for {}", static_cast<unsigned>(event->type), path);
  58. return {};
  59. }
  60. // We trust that the kernel only sends the name when appropriate.
  61. if (event->name_length > 0) {
  62. String child_name { event->name, event->name_length - 1 };
  63. result.event_path = LexicalPath::join(path, child_name).string();
  64. } else {
  65. result.event_path = path;
  66. }
  67. dbgln_if(FILE_WATCHER_DEBUG, "get_event_from_fd: got event from wd {} on '{}' type {}", fd, result.event_path, result.type);
  68. return result;
  69. }
  70. static String canonicalize_path(String path)
  71. {
  72. if (!path.is_empty() && path[0] == '/')
  73. return LexicalPath::canonicalized_path(move(path));
  74. char* cwd = getcwd(nullptr, 0);
  75. VERIFY(cwd);
  76. return LexicalPath::join({ cwd, strlen(cwd) }, move(path)).string();
  77. }
  78. ErrorOr<bool> FileWatcherBase::add_watch(String path, FileWatcherEvent::Type event_mask)
  79. {
  80. String canonical_path = canonicalize_path(move(path));
  81. if (m_path_to_wd.find(canonical_path) != m_path_to_wd.end()) {
  82. dbgln_if(FILE_WATCHER_DEBUG, "add_watch: path '{}' is already being watched", canonical_path);
  83. return false;
  84. }
  85. auto kernel_mask = InodeWatcherEvent::Type::Invalid;
  86. if (has_flag(event_mask, FileWatcherEvent::Type::ChildCreated))
  87. kernel_mask |= InodeWatcherEvent::Type::ChildCreated;
  88. if (has_flag(event_mask, FileWatcherEvent::Type::ChildDeleted))
  89. kernel_mask |= InodeWatcherEvent::Type::ChildDeleted;
  90. if (has_flag(event_mask, FileWatcherEvent::Type::Deleted))
  91. kernel_mask |= InodeWatcherEvent::Type::Deleted;
  92. if (has_flag(event_mask, FileWatcherEvent::Type::ContentModified))
  93. kernel_mask |= InodeWatcherEvent::Type::ContentModified;
  94. if (has_flag(event_mask, FileWatcherEvent::Type::MetadataModified))
  95. kernel_mask |= InodeWatcherEvent::Type::MetadataModified;
  96. int wd = inode_watcher_add_watch(m_watcher_fd, canonical_path.characters(), canonical_path.length(), static_cast<unsigned>(kernel_mask));
  97. if (wd < 0)
  98. return Error::from_errno(errno);
  99. m_path_to_wd.set(canonical_path, wd);
  100. m_wd_to_path.set(wd, canonical_path);
  101. dbgln_if(FILE_WATCHER_DEBUG, "add_watch: watching path '{}' on InodeWatcher {} wd {}", canonical_path, m_watcher_fd, wd);
  102. return true;
  103. }
  104. ErrorOr<bool> FileWatcherBase::remove_watch(String path)
  105. {
  106. String canonical_path = canonicalize_path(move(path));
  107. auto it = m_path_to_wd.find(canonical_path);
  108. if (it == m_path_to_wd.end()) {
  109. dbgln_if(FILE_WATCHER_DEBUG, "remove_watch: path '{}' is not being watched", canonical_path);
  110. return false;
  111. }
  112. if (inode_watcher_remove_watch(m_watcher_fd, it->value) < 0)
  113. return Error::from_errno(errno);
  114. m_path_to_wd.remove(it);
  115. m_wd_to_path.remove(it->value);
  116. dbgln_if(FILE_WATCHER_DEBUG, "remove_watch: stopped watching path '{}' on InodeWatcher {}", canonical_path, m_watcher_fd);
  117. return true;
  118. }
  119. BlockingFileWatcher::BlockingFileWatcher(InodeWatcherFlags flags)
  120. : FileWatcherBase(create_inode_watcher(static_cast<unsigned>(flags)))
  121. {
  122. VERIFY(m_watcher_fd != -1);
  123. dbgln_if(FILE_WATCHER_DEBUG, "BlockingFileWatcher created with InodeWatcher {}", m_watcher_fd);
  124. }
  125. BlockingFileWatcher::~BlockingFileWatcher()
  126. {
  127. close(m_watcher_fd);
  128. }
  129. Optional<FileWatcherEvent> BlockingFileWatcher::wait_for_event()
  130. {
  131. dbgln_if(FILE_WATCHER_DEBUG, "BlockingFileWatcher::wait_for_event()");
  132. auto maybe_event = get_event_from_fd(m_watcher_fd, m_wd_to_path);
  133. if (!maybe_event.has_value())
  134. return maybe_event;
  135. auto event = maybe_event.release_value();
  136. if (event.type == FileWatcherEvent::Type::Deleted) {
  137. auto result = remove_watch(event.event_path);
  138. if (result.is_error()) {
  139. dbgln_if(FILE_WATCHER_DEBUG, "wait_for_event: {}", result.error());
  140. }
  141. }
  142. return event;
  143. }
  144. ErrorOr<NonnullRefPtr<FileWatcher>> FileWatcher::create(InodeWatcherFlags flags)
  145. {
  146. auto watcher_fd = create_inode_watcher(static_cast<unsigned>(flags | InodeWatcherFlags::CloseOnExec));
  147. if (watcher_fd < 0)
  148. return Error::from_errno(errno);
  149. auto notifier = Notifier::construct(watcher_fd, Notifier::Event::Read);
  150. return adopt_ref(*new FileWatcher(watcher_fd, move(notifier)));
  151. }
  152. FileWatcher::FileWatcher(int watcher_fd, NonnullRefPtr<Notifier> notifier)
  153. : FileWatcherBase(watcher_fd)
  154. , m_notifier(move(notifier))
  155. {
  156. m_notifier->on_ready_to_read = [this] {
  157. auto maybe_event = get_event_from_fd(m_notifier->fd(), m_wd_to_path);
  158. if (maybe_event.has_value()) {
  159. auto event = maybe_event.value();
  160. on_change(event);
  161. if (event.type == FileWatcherEvent::Type::Deleted) {
  162. auto result = remove_watch(event.event_path);
  163. if (result.is_error()) {
  164. dbgln_if(FILE_WATCHER_DEBUG, "on_ready_to_read: {}", result.error());
  165. }
  166. }
  167. }
  168. };
  169. }
  170. FileWatcher::~FileWatcher()
  171. {
  172. m_notifier->on_ready_to_read = nullptr;
  173. close(m_notifier->fd());
  174. dbgln_if(FILE_WATCHER_DEBUG, "Stopped watcher at fd {}", m_notifier->fd());
  175. }
  176. #else
  177. // FIXME: Implement Core::FileWatcher for linux, macOS, and *BSD
  178. FileWatcher::~FileWatcher() { }
  179. FileWatcher::FileWatcher(int watcher_fd, NonnullRefPtr<Notifier> notifier)
  180. : FileWatcherBase(watcher_fd)
  181. , m_notifier(move(notifier))
  182. {
  183. }
  184. ErrorOr<NonnullRefPtr<FileWatcher>> FileWatcher::create(InodeWatcherFlags)
  185. {
  186. return Error::from_errno(ENOTSUP);
  187. }
  188. ErrorOr<bool> FileWatcherBase::add_watch(String, FileWatcherEvent::Type)
  189. {
  190. return Error::from_errno(ENOTSUP);
  191. }
  192. ErrorOr<bool> FileWatcherBase::remove_watch(String)
  193. {
  194. return Error::from_errno(ENOTSUP);
  195. }
  196. #endif
  197. }