FileWatcher.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "FileWatcher.h"
  28. #include <AK/Debug.h>
  29. #include <AK/Function.h>
  30. #include <AK/LexicalPath.h>
  31. #include <AK/Noncopyable.h>
  32. #include <AK/NonnullRefPtr.h>
  33. #include <AK/RefCounted.h>
  34. #include <AK/Result.h>
  35. #include <AK/String.h>
  36. #include <Kernel/API/InodeWatcherEvent.h>
  37. #include <LibCore/DirIterator.h>
  38. #include <LibCore/Notifier.h>
  39. #include <fcntl.h>
  40. #include <string.h>
  41. #include <sys/stat.h>
  42. #include <unistd.h>
  43. namespace Core {
  44. // Only supported in serenity mode because we use `watch_file`
  45. #ifdef __serenity__
  46. static String get_child_path_from_inode_index(const String& path, unsigned child_inode_index)
  47. {
  48. DirIterator iterator(path, Core::DirIterator::SkipDots);
  49. if (iterator.has_error()) {
  50. return {};
  51. }
  52. while (iterator.has_next()) {
  53. auto child_full_path = String::formatted("{}/{}", path, iterator.next_path());
  54. struct stat st;
  55. if (lstat(child_full_path.characters(), &st)) {
  56. return {};
  57. }
  58. if (st.st_ino == child_inode_index) {
  59. return child_full_path;
  60. }
  61. }
  62. return {};
  63. }
  64. BlockingFileWatcher::BlockingFileWatcher(const String& path)
  65. : m_path(path)
  66. {
  67. m_watcher_fd = watch_file(path.characters(), path.length());
  68. VERIFY(m_watcher_fd != -1);
  69. }
  70. BlockingFileWatcher::~BlockingFileWatcher()
  71. {
  72. close(m_watcher_fd);
  73. }
  74. Optional<FileWatcherEvent> BlockingFileWatcher::wait_for_event()
  75. {
  76. InodeWatcherEvent event {};
  77. int rc = read(m_watcher_fd, &event, sizeof(event));
  78. if (rc <= 0)
  79. return {};
  80. FileWatcherEvent result;
  81. if (event.type == InodeWatcherEvent::Type::ChildAdded)
  82. result.type = FileWatcherEvent::Type::ChildAdded;
  83. else if (event.type == InodeWatcherEvent::Type::ChildRemoved)
  84. result.type = FileWatcherEvent::Type::ChildRemoved;
  85. else if (event.type == InodeWatcherEvent::Type::Modified)
  86. result.type = FileWatcherEvent::Type::Modified;
  87. else
  88. return {};
  89. if (result.type == FileWatcherEvent::Type::ChildAdded || result.type == FileWatcherEvent::Type::ChildRemoved) {
  90. auto child_path = get_child_path_from_inode_index(m_path, event.inode_index);
  91. if (!LexicalPath(child_path).is_valid())
  92. return {};
  93. result.child_path = child_path;
  94. }
  95. return result;
  96. }
  97. Result<NonnullRefPtr<FileWatcher>, String> FileWatcher::watch(const String& path)
  98. {
  99. auto watch_fd = watch_file(path.characters(), path.length());
  100. if (watch_fd < 0) {
  101. return String::formatted("Could not watch file '{}' : {}", path.characters(), strerror(errno));
  102. }
  103. fcntl(watch_fd, F_SETFD, FD_CLOEXEC);
  104. if (watch_fd < 0) {
  105. return String::formatted("Could not watch file '{}' : {}", path.characters(), strerror(errno));
  106. }
  107. dbgln_if(FILE_WATCHER_DEBUG, "Started watcher for file '{}'", path.characters());
  108. auto notifier = Notifier::construct(watch_fd, Notifier::Event::Read);
  109. return adopt(*new FileWatcher(move(notifier), move(path)));
  110. }
  111. FileWatcher::FileWatcher(NonnullRefPtr<Notifier> notifier, const String& path)
  112. : m_notifier(move(notifier))
  113. , m_path(path)
  114. {
  115. m_notifier->on_ready_to_read = [this] {
  116. InodeWatcherEvent event {};
  117. int rc = read(m_notifier->fd(), &event, sizeof(event));
  118. if (rc <= 0)
  119. return;
  120. FileWatcherEvent result;
  121. if (event.type == InodeWatcherEvent::Type::ChildAdded) {
  122. result.type = FileWatcherEvent::Type::ChildAdded;
  123. } else if (event.type == InodeWatcherEvent::Type::ChildRemoved) {
  124. result.type = FileWatcherEvent::Type::ChildRemoved;
  125. } else if (event.type == InodeWatcherEvent::Type::Modified) {
  126. result.type = FileWatcherEvent::Type::Modified;
  127. } else {
  128. warnln("Unknown event type {} returned by the watch_file descriptor for {}", (unsigned)event.type, m_path.characters());
  129. return;
  130. }
  131. if (result.type == FileWatcherEvent::Type::ChildAdded || result.type == FileWatcherEvent::Type::ChildRemoved) {
  132. auto child_path = get_child_path_from_inode_index(m_path, event.inode_index);
  133. if (!LexicalPath(child_path).is_valid())
  134. return;
  135. result.child_path = child_path;
  136. }
  137. on_change(result);
  138. };
  139. }
  140. FileWatcher::~FileWatcher()
  141. {
  142. m_notifier->on_ready_to_read = nullptr;
  143. close(m_notifier->fd());
  144. dbgln_if(FILE_WATCHER_DEBUG, "Ended watcher for file '{}'", m_path.characters());
  145. }
  146. #endif
  147. }