TmpFS.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/HashMap.h>
  28. #include <AK/Optional.h>
  29. #include <Kernel/FileSystem/FileSystem.h>
  30. #include <Kernel/FileSystem/Inode.h>
  31. #include <Kernel/KBuffer.h>
  32. namespace Kernel {
  33. class TmpFSInode;
  34. class TmpFS final : public FS {
  35. friend class TmpFSInode;
  36. public:
  37. virtual ~TmpFS() override;
  38. static NonnullRefPtr<TmpFS> create();
  39. virtual bool initialize() override;
  40. virtual const char* class_name() const override { return "TmpFS"; }
  41. virtual bool supports_watchers() const override { return true; }
  42. virtual NonnullRefPtr<Inode> root_inode() const override;
  43. private:
  44. TmpFS();
  45. RefPtr<TmpFSInode> m_root_inode;
  46. HashMap<unsigned, NonnullRefPtr<TmpFSInode>> m_inodes;
  47. RefPtr<Inode> get_inode(InodeIdentifier identifier) const;
  48. void register_inode(TmpFSInode&);
  49. void unregister_inode(InodeIdentifier);
  50. unsigned m_next_inode_index { 1 };
  51. unsigned next_inode_index();
  52. };
  53. class TmpFSInode final : public Inode {
  54. friend class TmpFS;
  55. public:
  56. virtual ~TmpFSInode() override;
  57. TmpFS& fs() { return static_cast<TmpFS&>(Inode::fs()); }
  58. const TmpFS& fs() const { return static_cast<const TmpFS&>(Inode::fs()); }
  59. // ^Inode
  60. virtual ssize_t read_bytes(off_t, ssize_t, UserOrKernelBuffer& buffer, FileDescription*) const override;
  61. virtual InodeMetadata metadata() const override;
  62. virtual KResult traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)>) const override;
  63. virtual RefPtr<Inode> lookup(StringView name) override;
  64. virtual void flush_metadata() override;
  65. virtual ssize_t write_bytes(off_t, ssize_t, const UserOrKernelBuffer& buffer, FileDescription*) override;
  66. virtual KResultOr<NonnullRefPtr<Inode>> create_child(const String& name, mode_t, dev_t, uid_t, gid_t) override;
  67. virtual KResult add_child(Inode&, const StringView& name, mode_t) override;
  68. virtual KResult remove_child(const StringView& name) override;
  69. virtual KResultOr<size_t> directory_entry_count() const override;
  70. virtual KResult chmod(mode_t) override;
  71. virtual KResult chown(uid_t, gid_t) override;
  72. virtual KResult truncate(u64) override;
  73. virtual int set_atime(time_t) override;
  74. virtual int set_ctime(time_t) override;
  75. virtual int set_mtime(time_t) override;
  76. virtual void one_ref_left() override;
  77. private:
  78. TmpFSInode(TmpFS& fs, InodeMetadata metadata, InodeIdentifier parent);
  79. static NonnullRefPtr<TmpFSInode> create(TmpFS&, InodeMetadata metadata, InodeIdentifier parent);
  80. static NonnullRefPtr<TmpFSInode> create_root(TmpFS&);
  81. void notify_watchers();
  82. InodeMetadata m_metadata;
  83. InodeIdentifier m_parent;
  84. OwnPtr<KBuffer> m_content;
  85. struct Child {
  86. String name;
  87. NonnullRefPtr<TmpFSInode> inode;
  88. };
  89. HashMap<String, Child> m_children;
  90. };
  91. }