FileWatcher.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. namespace Core {
  43. // Only supported in serenity mode because we use `watch_file`
  44. #ifdef __serenity__
  45. static String get_child_path_from_inode_index(const String& path, unsigned child_inode_index)
  46. {
  47. DirIterator iterator(path, Core::DirIterator::SkipDots);
  48. if (iterator.has_error()) {
  49. return {};
  50. }
  51. while (iterator.has_next()) {
  52. auto child_full_path = String::formatted("{}/{}", path, iterator.next_path());
  53. struct stat st;
  54. if (lstat(child_full_path.characters(), &st)) {
  55. return {};
  56. }
  57. if (st.st_ino == child_inode_index) {
  58. return child_full_path;
  59. }
  60. }
  61. return {};
  62. }
  63. BlockingFileWatcher::BlockingFileWatcher(const String& path)
  64. : m_path(path)
  65. {
  66. m_watcher_fd = watch_file(path.characters(), path.length());
  67. ASSERT(m_watcher_fd != -1);
  68. }
  69. BlockingFileWatcher::~BlockingFileWatcher()
  70. {
  71. close(m_watcher_fd);
  72. }
  73. Optional<FileWatcherEvent> BlockingFileWatcher::wait_for_event()
  74. {
  75. InodeWatcherEvent event {};
  76. int rc = read(m_watcher_fd, &event, sizeof(event));
  77. if (rc <= 0)
  78. return {};
  79. FileWatcherEvent result;
  80. if (event.type == InodeWatcherEvent::Type::ChildAdded)
  81. result.type = FileWatcherEvent::Type::ChildAdded;
  82. else if (event.type == InodeWatcherEvent::Type::ChildRemoved)
  83. result.type = FileWatcherEvent::Type::ChildRemoved;
  84. else if (event.type == InodeWatcherEvent::Type::Modified)
  85. result.type = FileWatcherEvent::Type::Modified;
  86. else
  87. return {};
  88. if (result.type == FileWatcherEvent::Type::ChildAdded || result.type == FileWatcherEvent::Type::ChildRemoved) {
  89. auto child_path = get_child_path_from_inode_index(m_path, event.inode_index);
  90. if (!LexicalPath(child_path).is_valid())
  91. return {};
  92. result.child_path = child_path;
  93. }
  94. return result;
  95. }
  96. Result<NonnullRefPtr<FileWatcher>, String> FileWatcher::watch(const String& path)
  97. {
  98. auto watch_fd = watch_file(path.characters(), path.length());
  99. if (watch_fd < 0) {
  100. return String::formatted("Could not watch file '{}' : {}", path.characters(), strerror(errno));
  101. }
  102. fcntl(watch_fd, F_SETFD, FD_CLOEXEC);
  103. if (watch_fd < 0) {
  104. return String::formatted("Could not watch file '{}' : {}", path.characters(), strerror(errno));
  105. }
  106. dbgln_if(FILE_WATCHER_DEBUG, "Started watcher for file '{}'", path.characters());
  107. auto notifier = Notifier::construct(watch_fd, Notifier::Event::Read);
  108. return adopt(*new FileWatcher(move(notifier), move(path)));
  109. }
  110. FileWatcher::FileWatcher(NonnullRefPtr<Notifier> notifier, const String& path)
  111. : m_notifier(move(notifier))
  112. , m_path(path)
  113. {
  114. m_notifier->on_ready_to_read = [this] {
  115. InodeWatcherEvent event {};
  116. int rc = read(m_notifier->fd(), &event, sizeof(event));
  117. if (rc <= 0)
  118. return;
  119. FileWatcherEvent result;
  120. if (event.type == InodeWatcherEvent::Type::ChildAdded) {
  121. result.type = FileWatcherEvent::Type::ChildAdded;
  122. } else if (event.type == InodeWatcherEvent::Type::ChildRemoved) {
  123. result.type = FileWatcherEvent::Type::ChildRemoved;
  124. } else if (event.type == InodeWatcherEvent::Type::Modified) {
  125. result.type = FileWatcherEvent::Type::Modified;
  126. } else {
  127. warnln("Unknown event type {} returned by the watch_file descriptor for {}", (unsigned)event.type, m_path.characters());
  128. return;
  129. }
  130. if (result.type == FileWatcherEvent::Type::ChildAdded || result.type == FileWatcherEvent::Type::ChildRemoved) {
  131. auto child_path = get_child_path_from_inode_index(m_path, event.inode_index);
  132. if (!LexicalPath(child_path).is_valid())
  133. return;
  134. result.child_path = child_path;
  135. }
  136. on_change(result);
  137. };
  138. }
  139. FileWatcher::~FileWatcher()
  140. {
  141. m_notifier->on_ready_to_read = nullptr;
  142. close(m_notifier->fd());
  143. dbgln_if(FILE_WATCHER_DEBUG, "Ended watcher for file '{}'", m_path.characters());
  144. }
  145. #endif
  146. }