FileDescription.h 5.0 KB

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