FileDescription.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@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/Badge.h>
  28. #include <AK/ByteBuffer.h>
  29. #include <AK/RefCounted.h>
  30. #include <Kernel/FileSystem/FIFO.h>
  31. #include <Kernel/FileSystem/Inode.h>
  32. #include <Kernel/FileSystem/InodeMetadata.h>
  33. #include <Kernel/FileSystem/VirtualFileSystem.h>
  34. #include <Kernel/KBuffer.h>
  35. #include <Kernel/VirtualAddress.h>
  36. namespace Kernel {
  37. class FileDescriptionData {
  38. public:
  39. virtual ~FileDescriptionData() { }
  40. };
  41. class FileDescription : public RefCounted<FileDescription> {
  42. MAKE_SLAB_ALLOCATED(FileDescription)
  43. public:
  44. static KResultOr<NonnullRefPtr<FileDescription>> create(Custody&);
  45. static KResultOr<NonnullRefPtr<FileDescription>> create(File&);
  46. ~FileDescription();
  47. Thread::FileBlocker::BlockFlags should_unblock(Thread::FileBlocker::BlockFlags) const;
  48. bool is_readable() const { return m_readable; }
  49. bool is_writable() const { return m_writable; }
  50. void set_readable(bool b) { m_readable = b; }
  51. void set_writable(bool b) { m_writable = b; }
  52. void set_rw_mode(int options)
  53. {
  54. set_readable(options & O_RDONLY);
  55. set_writable(options & O_WRONLY);
  56. }
  57. KResult close();
  58. off_t seek(off_t, int whence);
  59. KResultOr<size_t> read(UserOrKernelBuffer&, size_t);
  60. KResultOr<size_t> write(const UserOrKernelBuffer& data, size_t);
  61. KResult stat(::stat&);
  62. KResult chmod(mode_t);
  63. bool can_read() const;
  64. bool can_write() const;
  65. ssize_t get_dir_entries(UserOrKernelBuffer& buffer, ssize_t);
  66. KResultOr<NonnullOwnPtr<KBuffer>> read_entire_file();
  67. String absolute_path() const;
  68. bool is_direct() const { return m_direct; }
  69. bool is_directory() const { return m_is_directory; }
  70. File& file() { return *m_file; }
  71. const File& file() const { return *m_file; }
  72. bool is_device() const;
  73. const Device* device() const;
  74. Device* device();
  75. bool is_tty() const;
  76. const TTY* tty() const;
  77. TTY* tty();
  78. bool is_master_pty() const;
  79. const MasterPTY* master_pty() const;
  80. MasterPTY* master_pty();
  81. InodeMetadata metadata() const;
  82. Inode* inode() { return m_inode.ptr(); }
  83. const Inode* inode() const { return m_inode.ptr(); }
  84. Custody* custody() { return m_custody.ptr(); }
  85. const Custody* custody() const { return m_custody.ptr(); }
  86. KResultOr<Region*> mmap(Process&, VirtualAddress, size_t offset, size_t, int prot, bool shared);
  87. bool is_blocking() const { return m_is_blocking; }
  88. void set_blocking(bool b) { m_is_blocking = b; }
  89. bool should_append() const { return m_should_append; }
  90. void set_should_append(bool s) { m_should_append = s; }
  91. u32 file_flags() const { return m_file_flags; }
  92. void set_file_flags(u32);
  93. bool is_socket() const;
  94. Socket* socket();
  95. const Socket* socket() const;
  96. bool is_fifo() const;
  97. FIFO* fifo();
  98. FIFO::Direction fifo_direction() { return m_fifo_direction; }
  99. void set_fifo_direction(Badge<FIFO>, FIFO::Direction direction) { m_fifo_direction = direction; }
  100. OwnPtr<FileDescriptionData>& data() { return m_data; }
  101. void set_original_inode(Badge<VFS>, NonnullRefPtr<Inode>&& inode) { m_inode = move(inode); }
  102. KResult truncate(u64);
  103. off_t offset() const { return m_current_offset; }
  104. KResult chown(uid_t, gid_t);
  105. FileBlockCondition& block_condition();
  106. private:
  107. friend class VFS;
  108. explicit FileDescription(File&);
  109. KResult attach();
  110. void evaluate_block_conditions()
  111. {
  112. block_condition().unblock();
  113. }
  114. RefPtr<Custody> m_custody;
  115. RefPtr<Inode> m_inode;
  116. NonnullRefPtr<File> m_file;
  117. off_t m_current_offset { 0 };
  118. OwnPtr<FileDescriptionData> m_data;
  119. u32 m_file_flags { 0 };
  120. bool m_readable : 1 { false };
  121. bool m_writable : 1 { false };
  122. bool m_is_blocking : 1 { true };
  123. bool m_is_directory : 1 { false };
  124. bool m_should_append : 1 { false };
  125. bool m_direct : 1 { false };
  126. FIFO::Direction m_fifo_direction { FIFO::Direction::Neither };
  127. Lock m_lock { "FileDescription" };
  128. };
  129. }