TmpFS.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/Optional.h>
  28. #include <Kernel/FileSystem/FileSystem.h>
  29. #include <Kernel/FileSystem/Inode.h>
  30. #include <Kernel/KBuffer.h>
  31. class TmpFSInode;
  32. class TmpFS final : public FS {
  33. friend class TmpFSInode;
  34. public:
  35. virtual ~TmpFS() override;
  36. static NonnullRefPtr<TmpFS> create();
  37. virtual bool initialize() override;
  38. virtual const char* class_name() const override { return "TmpFS"; }
  39. virtual bool supports_watchers() const override { return true; }
  40. virtual InodeIdentifier root_inode() const override;
  41. virtual RefPtr<Inode> get_inode(InodeIdentifier) const override;
  42. virtual KResultOr<NonnullRefPtr<Inode>> create_inode(InodeIdentifier parent_id, const String& name, mode_t, off_t size, dev_t, uid_t, gid_t) override;
  43. virtual KResult create_directory(InodeIdentifier parent_id, const String& name, mode_t, uid_t, gid_t) override;
  44. private:
  45. TmpFS();
  46. RefPtr<TmpFSInode> m_root_inode;
  47. HashMap<unsigned, NonnullRefPtr<TmpFSInode>> m_inodes;
  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, u8* buffer, FileDescription*) const override;
  61. virtual InodeMetadata metadata() const override;
  62. virtual bool traverse_as_directory(Function<bool(const FS::DirectoryEntry&)>) 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 u8* buffer, FileDescription*) override;
  66. virtual KResult add_child(InodeIdentifier child_id, const StringView& name, mode_t) override;
  67. virtual KResult remove_child(const StringView& name) override;
  68. virtual size_t directory_entry_count() const override;
  69. virtual KResult chmod(mode_t) override;
  70. virtual KResult chown(uid_t, gid_t) override;
  71. virtual KResult truncate(u64) override;
  72. virtual int set_atime(time_t) override;
  73. virtual int set_ctime(time_t) override;
  74. virtual int set_mtime(time_t) override;
  75. virtual void one_ref_left() override;
  76. private:
  77. TmpFSInode(TmpFS& fs, InodeMetadata metadata, InodeIdentifier parent);
  78. static NonnullRefPtr<TmpFSInode> create(TmpFS&, InodeMetadata metadata, InodeIdentifier parent);
  79. static NonnullRefPtr<TmpFSInode> create_root(TmpFS&);
  80. InodeMetadata m_metadata;
  81. InodeIdentifier m_parent;
  82. Optional<KBuffer> m_content;
  83. struct Child {
  84. FS::DirectoryEntry entry;
  85. NonnullRefPtr<TmpFSInode> inode;
  86. };
  87. HashMap<String, Child> m_children;
  88. };