FileDescription.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include <AK/BufferStream.h>
  2. #include <Kernel/Devices/BlockDevice.h>
  3. #include <Kernel/Devices/CharacterDevice.h>
  4. #include <Kernel/FileSystem/Custody.h>
  5. #include <Kernel/FileSystem/FIFO.h>
  6. #include <Kernel/FileSystem/FileDescription.h>
  7. #include <Kernel/FileSystem/FileSystem.h>
  8. #include <Kernel/FileSystem/InodeFile.h>
  9. #include <Kernel/FileSystem/SharedMemory.h>
  10. #include <Kernel/Net/Socket.h>
  11. #include <Kernel/Process.h>
  12. #include <Kernel/TTY/MasterPTY.h>
  13. #include <Kernel/TTY/TTY.h>
  14. #include <Kernel/UnixTypes.h>
  15. #include <Kernel/VM/MemoryManager.h>
  16. #include <LibC/errno_numbers.h>
  17. NonnullRefPtr<FileDescription> FileDescription::create(Custody& custody)
  18. {
  19. auto description = adopt(*new FileDescription(InodeFile::create(custody.inode())));
  20. description->m_custody = custody;
  21. return description;
  22. }
  23. NonnullRefPtr<FileDescription> FileDescription::create(File& file)
  24. {
  25. return adopt(*new FileDescription(file));
  26. }
  27. FileDescription::FileDescription(File& file)
  28. : m_file(file)
  29. {
  30. if (file.is_inode())
  31. m_inode = static_cast<InodeFile&>(file).inode();
  32. if (is_socket())
  33. socket()->attach(*this);
  34. m_is_directory = metadata().is_directory();
  35. }
  36. FileDescription::~FileDescription()
  37. {
  38. if (is_socket())
  39. socket()->detach(*this);
  40. if (is_fifo())
  41. static_cast<FIFO*>(m_file.ptr())->detach(m_fifo_direction);
  42. m_file->close();
  43. m_inode = nullptr;
  44. }
  45. KResult FileDescription::fstat(stat& buffer)
  46. {
  47. if (is_fifo()) {
  48. memset(&buffer, 0, sizeof(buffer));
  49. buffer.st_mode = 001000;
  50. return KSuccess;
  51. }
  52. if (is_socket()) {
  53. memset(&buffer, 0, sizeof(buffer));
  54. buffer.st_mode = 0140000;
  55. return KSuccess;
  56. }
  57. if (!m_inode)
  58. return KResult(-EBADF);
  59. return metadata().stat(buffer);
  60. }
  61. KResult FileDescription::fchmod(mode_t mode)
  62. {
  63. if (!m_inode)
  64. return KResult(-EBADF);
  65. return VFS::the().chmod(*m_inode, mode);
  66. }
  67. off_t FileDescription::seek(off_t offset, int whence)
  68. {
  69. if (!m_file->is_seekable())
  70. return -EINVAL;
  71. auto metadata = this->metadata();
  72. if (!metadata.is_valid())
  73. return -EIO;
  74. if (metadata.is_socket() || metadata.is_fifo())
  75. return -ESPIPE;
  76. off_t new_offset;
  77. switch (whence) {
  78. case SEEK_SET:
  79. new_offset = offset;
  80. break;
  81. case SEEK_CUR:
  82. new_offset = m_current_offset + offset;
  83. break;
  84. case SEEK_END:
  85. new_offset = metadata.size;
  86. break;
  87. default:
  88. return -EINVAL;
  89. }
  90. if (new_offset < 0)
  91. return -EINVAL;
  92. // FIXME: Return -EINVAL if attempting to seek past the end of a seekable device.
  93. m_current_offset = new_offset;
  94. return m_current_offset;
  95. }
  96. ssize_t FileDescription::read(u8* buffer, ssize_t count)
  97. {
  98. int nread = m_file->read(*this, buffer, count);
  99. if (nread > 0 && m_file->is_seekable())
  100. m_current_offset += nread;
  101. return nread;
  102. }
  103. ssize_t FileDescription::write(const u8* data, ssize_t size)
  104. {
  105. int nwritten = m_file->write(*this, data, size);
  106. if (nwritten > 0 && m_file->is_seekable())
  107. m_current_offset += nwritten;
  108. return nwritten;
  109. }
  110. bool FileDescription::can_write() const
  111. {
  112. return m_file->can_write(*this);
  113. }
  114. bool FileDescription::can_read() const
  115. {
  116. return m_file->can_read(*this);
  117. }
  118. ByteBuffer FileDescription::read_entire_file()
  119. {
  120. // HACK ALERT: (This entire function)
  121. ASSERT(m_file->is_inode());
  122. ASSERT(m_inode);
  123. return m_inode->read_entire(this);
  124. }
  125. ssize_t FileDescription::get_dir_entries(u8* buffer, ssize_t size)
  126. {
  127. if (!is_directory())
  128. return -ENOTDIR;
  129. auto metadata = this->metadata();
  130. if (!metadata.is_valid())
  131. return -EIO;
  132. int size_to_allocate = max(PAGE_SIZE, metadata.size);
  133. auto temp_buffer = ByteBuffer::create_uninitialized(size_to_allocate);
  134. BufferStream stream(temp_buffer);
  135. VFS::the().traverse_directory_inode(*m_inode, [&stream](auto& entry) {
  136. stream << (u32)entry.inode.index();
  137. stream << (u8)entry.file_type;
  138. stream << (u32)entry.name_length;
  139. stream << entry.name;
  140. return true;
  141. });
  142. stream.snip();
  143. if (size < temp_buffer.size())
  144. return -1;
  145. memcpy(buffer, temp_buffer.data(), temp_buffer.size());
  146. return stream.offset();
  147. }
  148. bool FileDescription::is_device() const
  149. {
  150. return m_file->is_device();
  151. }
  152. bool FileDescription::is_tty() const
  153. {
  154. return m_file->is_tty();
  155. }
  156. const TTY* FileDescription::tty() const
  157. {
  158. if (!is_tty())
  159. return nullptr;
  160. return static_cast<const TTY*>(m_file.ptr());
  161. }
  162. TTY* FileDescription::tty()
  163. {
  164. if (!is_tty())
  165. return nullptr;
  166. return static_cast<TTY*>(m_file.ptr());
  167. }
  168. bool FileDescription::is_master_pty() const
  169. {
  170. return m_file->is_master_pty();
  171. }
  172. const MasterPTY* FileDescription::master_pty() const
  173. {
  174. if (!is_master_pty())
  175. return nullptr;
  176. return static_cast<const MasterPTY*>(m_file.ptr());
  177. }
  178. MasterPTY* FileDescription::master_pty()
  179. {
  180. if (!is_master_pty())
  181. return nullptr;
  182. return static_cast<MasterPTY*>(m_file.ptr());
  183. }
  184. int FileDescription::close()
  185. {
  186. return 0;
  187. }
  188. String FileDescription::absolute_path() const
  189. {
  190. if (m_custody)
  191. return m_custody->absolute_path();
  192. return m_file->absolute_path(*this);
  193. }
  194. InodeMetadata FileDescription::metadata() const
  195. {
  196. if (m_inode)
  197. return m_inode->metadata();
  198. return {};
  199. }
  200. KResultOr<Region*> FileDescription::mmap(Process& process, VirtualAddress vaddr, size_t offset, size_t size, int prot)
  201. {
  202. return m_file->mmap(process, *this, vaddr, offset, size, prot);
  203. }
  204. KResult FileDescription::truncate(off_t length)
  205. {
  206. return m_file->truncate(length);
  207. }
  208. bool FileDescription::is_shared_memory() const
  209. {
  210. return m_file->is_shared_memory();
  211. }
  212. SharedMemory* FileDescription::shared_memory()
  213. {
  214. if (!is_shared_memory())
  215. return nullptr;
  216. return static_cast<SharedMemory*>(m_file.ptr());
  217. }
  218. const SharedMemory* FileDescription::shared_memory() const
  219. {
  220. if (!is_shared_memory())
  221. return nullptr;
  222. return static_cast<const SharedMemory*>(m_file.ptr());
  223. }
  224. bool FileDescription::is_fifo() const
  225. {
  226. return m_file->is_fifo();
  227. }
  228. FIFO* FileDescription::fifo()
  229. {
  230. if (!is_fifo())
  231. return nullptr;
  232. return static_cast<FIFO*>(m_file.ptr());
  233. }
  234. bool FileDescription::is_socket() const
  235. {
  236. return m_file->is_socket();
  237. }
  238. Socket* FileDescription::socket()
  239. {
  240. if (!is_socket())
  241. return nullptr;
  242. return static_cast<Socket*>(m_file.ptr());
  243. }
  244. const Socket* FileDescription::socket() const
  245. {
  246. if (!is_socket())
  247. return nullptr;
  248. return static_cast<const Socket*>(m_file.ptr());
  249. }
  250. void FileDescription::set_file_flags(u32 flags)
  251. {
  252. m_is_blocking = !(flags & O_NONBLOCK);
  253. m_should_append = flags & O_APPEND;
  254. m_direct = flags & O_DIRECT;
  255. m_file_flags = flags;
  256. }
  257. KResult FileDescription::chown(uid_t uid, gid_t gid)
  258. {
  259. if (!m_inode)
  260. return KResult(-EINVAL);
  261. return VFS::the().chown(*m_inode, uid, gid);
  262. }