InodeWatcher.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Badge.h>
  9. #include <AK/Checked.h>
  10. #include <AK/CircularQueue.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/NonnullOwnPtr.h>
  13. #include <Kernel/API/InodeWatcherEvent.h>
  14. #include <Kernel/FileSystem/File.h>
  15. #include <Kernel/Forward.h>
  16. namespace Kernel {
  17. // A specific description of a watch.
  18. struct WatchDescription {
  19. int wd;
  20. Inode& inode;
  21. unsigned event_mask;
  22. static ErrorOr<NonnullOwnPtr<WatchDescription>> create(int wd, Inode& inode, unsigned event_mask)
  23. {
  24. return adopt_nonnull_own_or_enomem(new (nothrow) WatchDescription(wd, inode, event_mask));
  25. }
  26. private:
  27. WatchDescription(int wd, Inode& inode, unsigned event_mask)
  28. : wd(wd)
  29. , inode(inode)
  30. , event_mask(event_mask)
  31. {
  32. }
  33. };
  34. class InodeWatcher final : public File {
  35. public:
  36. static ErrorOr<NonnullLockRefPtr<InodeWatcher>> try_create();
  37. virtual ~InodeWatcher() override;
  38. virtual bool can_read(OpenFileDescription const&, u64) const override;
  39. virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
  40. // Can't write to an inode watcher.
  41. virtual bool can_write(OpenFileDescription const&, u64) const override { return true; }
  42. virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) override { return EIO; }
  43. virtual ErrorOr<void> close() override;
  44. virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(OpenFileDescription const&) const override;
  45. virtual StringView class_name() const override { return "InodeWatcher"sv; };
  46. virtual bool is_inode_watcher() const override { return true; }
  47. void notify_inode_event(Badge<Inode>, InodeIdentifier, InodeWatcherEvent::Type, StringView name = {});
  48. ErrorOr<int> register_inode(Inode&, unsigned event_mask);
  49. ErrorOr<void> unregister_by_wd(int);
  50. void unregister_by_inode(Badge<Inode>, InodeIdentifier);
  51. private:
  52. explicit InodeWatcher() { }
  53. mutable Mutex m_lock;
  54. struct Event {
  55. int wd { 0 };
  56. InodeWatcherEvent::Type type { InodeWatcherEvent::Type::Invalid };
  57. OwnPtr<KString> path;
  58. };
  59. CircularQueue<Event, 32> m_queue;
  60. Checked<int> m_wd_counter { 1 };
  61. // NOTE: These two hashmaps provide two different ways of reaching the same
  62. // watch description, so they will overlap.
  63. HashMap<int, NonnullOwnPtr<WatchDescription>> m_wd_to_watches;
  64. HashMap<InodeIdentifier, WatchDescription*> m_inode_to_watches;
  65. };
  66. }