FileDescription.h 5.0 KB

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