InodeWatcher.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 KResultOr<NonnullOwnPtr<WatchDescription>> create(int wd, Inode& inode, unsigned event_mask)
  23. {
  24. auto description = adopt_own_if_nonnull(new (nothrow) WatchDescription(wd, inode, event_mask));
  25. if (description)
  26. return description.release_nonnull();
  27. return ENOMEM;
  28. }
  29. private:
  30. WatchDescription(int wd, Inode& inode, unsigned event_mask)
  31. : wd(wd)
  32. , inode(inode)
  33. , event_mask(event_mask)
  34. {
  35. }
  36. };
  37. class InodeWatcher final : public File {
  38. public:
  39. static KResultOr<NonnullRefPtr<InodeWatcher>> try_create();
  40. virtual ~InodeWatcher() override;
  41. virtual bool can_read(const FileDescription&, size_t) const override;
  42. virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override;
  43. // Can't write to an inode watcher.
  44. virtual bool can_write(const FileDescription&, size_t) const override { return true; }
  45. virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EIO; }
  46. virtual KResult close() override;
  47. virtual String absolute_path(const FileDescription&) const override;
  48. virtual StringView class_name() const override { return "InodeWatcher"; };
  49. virtual bool is_inode_watcher() const override { return true; }
  50. void notify_inode_event(Badge<Inode>, InodeIdentifier, InodeWatcherEvent::Type, String const& name = {});
  51. KResultOr<int> register_inode(Inode&, unsigned event_mask);
  52. KResult unregister_by_wd(int);
  53. void unregister_by_inode(Badge<Inode>, InodeIdentifier);
  54. private:
  55. explicit InodeWatcher() { }
  56. mutable Mutex m_lock;
  57. struct Event {
  58. int wd { 0 };
  59. InodeWatcherEvent::Type type { InodeWatcherEvent::Type::Invalid };
  60. String path;
  61. };
  62. CircularQueue<Event, 32> m_queue;
  63. Checked<int> m_wd_counter { 1 };
  64. // NOTE: These two hashmaps provide two different ways of reaching the same
  65. // watch description, so they will overlap.
  66. HashMap<int, NonnullOwnPtr<WatchDescription>> m_wd_to_watches;
  67. HashMap<InodeIdentifier, WatchDescription*> m_inode_to_watches;
  68. };
  69. }