FileDescription.h 5.0 KB

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