FileDescription.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #include <AK/MemoryStream.h>
  27. #include <Kernel/Debug.h>
  28. #include <Kernel/Devices/BlockDevice.h>
  29. #include <Kernel/Devices/CharacterDevice.h>
  30. #include <Kernel/FileSystem/Custody.h>
  31. #include <Kernel/FileSystem/FIFO.h>
  32. #include <Kernel/FileSystem/FileDescription.h>
  33. #include <Kernel/FileSystem/FileSystem.h>
  34. #include <Kernel/FileSystem/InodeFile.h>
  35. #include <Kernel/Net/Socket.h>
  36. #include <Kernel/Process.h>
  37. #include <Kernel/TTY/MasterPTY.h>
  38. #include <Kernel/TTY/TTY.h>
  39. #include <Kernel/UnixTypes.h>
  40. #include <Kernel/VM/MemoryManager.h>
  41. #include <LibC/errno_numbers.h>
  42. namespace Kernel {
  43. KResultOr<NonnullRefPtr<FileDescription>> FileDescription::create(Custody& custody)
  44. {
  45. auto description = adopt(*new FileDescription(InodeFile::create(custody.inode())));
  46. description->m_custody = custody;
  47. auto result = description->attach();
  48. if (result.is_error()) {
  49. dbgln_if(FILEDESCRIPTION_DEBUG, "Failed to create file description for custody: {}", result);
  50. return result;
  51. }
  52. return description;
  53. }
  54. KResultOr<NonnullRefPtr<FileDescription>> FileDescription::create(File& file)
  55. {
  56. auto description = adopt(*new FileDescription(file));
  57. auto result = description->attach();
  58. if (result.is_error()) {
  59. dbgln_if(FILEDESCRIPTION_DEBUG, "Failed to create file description for file: {}", result);
  60. return result;
  61. }
  62. return description;
  63. }
  64. FileDescription::FileDescription(File& file)
  65. : m_file(file)
  66. {
  67. if (file.is_inode())
  68. m_inode = static_cast<InodeFile&>(file).inode();
  69. m_is_directory = metadata().is_directory();
  70. }
  71. FileDescription::~FileDescription()
  72. {
  73. m_file->detach(*this);
  74. if (is_fifo())
  75. static_cast<FIFO*>(m_file.ptr())->detach(m_fifo_direction);
  76. // FIXME: Should this error path be observed somehow?
  77. (void)m_file->close();
  78. if (m_inode)
  79. m_inode->detach(*this);
  80. }
  81. KResult FileDescription::attach()
  82. {
  83. if (m_inode) {
  84. auto result = m_inode->attach(*this);
  85. if (result.is_error())
  86. return result;
  87. }
  88. return m_file->attach(*this);
  89. }
  90. Thread::FileBlocker::BlockFlags FileDescription::should_unblock(Thread::FileBlocker::BlockFlags block_flags) const
  91. {
  92. u32 unblock_flags = (u32)Thread::FileBlocker::BlockFlags::None;
  93. if (((u32)block_flags & (u32)Thread::FileBlocker::BlockFlags::Read) && can_read())
  94. unblock_flags |= (u32)Thread::FileBlocker::BlockFlags::Read;
  95. if (((u32)block_flags & (u32)Thread::FileBlocker::BlockFlags::Write) && can_write())
  96. unblock_flags |= (u32)Thread::FileBlocker::BlockFlags::Write;
  97. // TODO: Implement Thread::FileBlocker::BlockFlags::Exception
  98. if ((u32)block_flags & (u32)Thread::FileBlocker::BlockFlags::SocketFlags) {
  99. auto* sock = socket();
  100. VERIFY(sock);
  101. if (((u32)block_flags & (u32)Thread::FileBlocker::BlockFlags::Accept) && sock->can_accept())
  102. unblock_flags |= (u32)Thread::FileBlocker::BlockFlags::Accept;
  103. if (((u32)block_flags & (u32)Thread::FileBlocker::BlockFlags::Connect) && sock->setup_state() == Socket::SetupState::Completed)
  104. unblock_flags |= (u32)Thread::FileBlocker::BlockFlags::Connect;
  105. }
  106. return (Thread::FileBlocker::BlockFlags)unblock_flags;
  107. }
  108. KResult FileDescription::stat(::stat& buffer)
  109. {
  110. LOCKER(m_lock);
  111. // FIXME: This is a little awkward, why can't we always forward to File::stat()?
  112. if (m_inode)
  113. return metadata().stat(buffer);
  114. return m_file->stat(buffer);
  115. }
  116. off_t FileDescription::seek(off_t offset, int whence)
  117. {
  118. LOCKER(m_lock);
  119. if (!m_file->is_seekable())
  120. return -ESPIPE;
  121. off_t new_offset;
  122. switch (whence) {
  123. case SEEK_SET:
  124. new_offset = offset;
  125. break;
  126. case SEEK_CUR:
  127. if (Checked<off_t>::addition_would_overflow(m_current_offset, offset))
  128. return -EOVERFLOW;
  129. new_offset = m_current_offset + offset;
  130. break;
  131. case SEEK_END:
  132. if (!metadata().is_valid())
  133. return -EIO;
  134. new_offset = metadata().size;
  135. break;
  136. default:
  137. return -EINVAL;
  138. }
  139. if (new_offset < 0)
  140. return -EINVAL;
  141. // FIXME: Return -EINVAL if attempting to seek past the end of a seekable device.
  142. m_current_offset = new_offset;
  143. m_file->did_seek(*this, new_offset);
  144. if (m_inode)
  145. m_inode->did_seek(*this, new_offset);
  146. evaluate_block_conditions();
  147. return m_current_offset;
  148. }
  149. KResultOr<size_t> FileDescription::read(UserOrKernelBuffer& buffer, size_t count)
  150. {
  151. LOCKER(m_lock);
  152. if (Checked<off_t>::addition_would_overflow(m_current_offset, count))
  153. return EOVERFLOW;
  154. auto nread_or_error = m_file->read(*this, offset(), buffer, count);
  155. if (!nread_or_error.is_error()) {
  156. if (m_file->is_seekable())
  157. m_current_offset += nread_or_error.value();
  158. evaluate_block_conditions();
  159. }
  160. return nread_or_error;
  161. }
  162. KResultOr<size_t> FileDescription::write(const UserOrKernelBuffer& data, size_t size)
  163. {
  164. LOCKER(m_lock);
  165. if (Checked<off_t>::addition_would_overflow(m_current_offset, size))
  166. return EOVERFLOW;
  167. auto nwritten_or_error = m_file->write(*this, offset(), data, size);
  168. if (!nwritten_or_error.is_error()) {
  169. if (m_file->is_seekable())
  170. m_current_offset += nwritten_or_error.value();
  171. evaluate_block_conditions();
  172. }
  173. return nwritten_or_error;
  174. }
  175. bool FileDescription::can_write() const
  176. {
  177. return m_file->can_write(*this, offset());
  178. }
  179. bool FileDescription::can_read() const
  180. {
  181. return m_file->can_read(*this, offset());
  182. }
  183. KResultOr<NonnullOwnPtr<KBuffer>> FileDescription::read_entire_file()
  184. {
  185. // HACK ALERT: (This entire function)
  186. VERIFY(m_file->is_inode());
  187. VERIFY(m_inode);
  188. return m_inode->read_entire(this);
  189. }
  190. ssize_t FileDescription::get_dir_entries(UserOrKernelBuffer& buffer, ssize_t size)
  191. {
  192. LOCKER(m_lock, Lock::Mode::Shared);
  193. if (!is_directory())
  194. return -ENOTDIR;
  195. auto metadata = this->metadata();
  196. if (!metadata.is_valid())
  197. return -EIO;
  198. if (size < 0)
  199. return -EINVAL;
  200. size_t size_to_allocate = max(static_cast<size_t>(PAGE_SIZE), static_cast<size_t>(metadata.size));
  201. auto temp_buffer = ByteBuffer::create_uninitialized(size_to_allocate);
  202. OutputMemoryStream stream { temp_buffer };
  203. KResult result = VFS::the().traverse_directory_inode(*m_inode, [&stream, this](auto& entry) {
  204. stream << (u32)entry.inode.index().value();
  205. stream << m_inode->fs().internal_file_type_to_directory_entry_type(entry);
  206. stream << (u32)entry.name.length();
  207. stream << entry.name.bytes();
  208. return true;
  209. });
  210. if (result.is_error())
  211. return result;
  212. if (stream.handle_recoverable_error())
  213. return -EINVAL;
  214. if (!buffer.write(stream.bytes()))
  215. return -EFAULT;
  216. return stream.size();
  217. }
  218. bool FileDescription::is_device() const
  219. {
  220. return m_file->is_device();
  221. }
  222. const Device* FileDescription::device() const
  223. {
  224. if (!is_device())
  225. return nullptr;
  226. return static_cast<const Device*>(m_file.ptr());
  227. }
  228. Device* FileDescription::device()
  229. {
  230. if (!is_device())
  231. return nullptr;
  232. return static_cast<Device*>(m_file.ptr());
  233. }
  234. bool FileDescription::is_tty() const
  235. {
  236. return m_file->is_tty();
  237. }
  238. const TTY* FileDescription::tty() const
  239. {
  240. if (!is_tty())
  241. return nullptr;
  242. return static_cast<const TTY*>(m_file.ptr());
  243. }
  244. TTY* FileDescription::tty()
  245. {
  246. if (!is_tty())
  247. return nullptr;
  248. return static_cast<TTY*>(m_file.ptr());
  249. }
  250. bool FileDescription::is_master_pty() const
  251. {
  252. return m_file->is_master_pty();
  253. }
  254. const MasterPTY* FileDescription::master_pty() const
  255. {
  256. if (!is_master_pty())
  257. return nullptr;
  258. return static_cast<const MasterPTY*>(m_file.ptr());
  259. }
  260. MasterPTY* FileDescription::master_pty()
  261. {
  262. if (!is_master_pty())
  263. return nullptr;
  264. return static_cast<MasterPTY*>(m_file.ptr());
  265. }
  266. KResult FileDescription::close()
  267. {
  268. if (m_file->ref_count() > 1)
  269. return KSuccess;
  270. return m_file->close();
  271. }
  272. String FileDescription::absolute_path() const
  273. {
  274. if (m_custody)
  275. return m_custody->absolute_path();
  276. return m_file->absolute_path(*this);
  277. }
  278. InodeMetadata FileDescription::metadata() const
  279. {
  280. if (m_inode)
  281. return m_inode->metadata();
  282. return {};
  283. }
  284. KResultOr<Region*> FileDescription::mmap(Process& process, const Range& range, size_t offset, int prot, bool shared)
  285. {
  286. LOCKER(m_lock);
  287. return m_file->mmap(process, *this, range, offset, prot, shared);
  288. }
  289. KResult FileDescription::truncate(u64 length)
  290. {
  291. LOCKER(m_lock);
  292. return m_file->truncate(length);
  293. }
  294. bool FileDescription::is_fifo() const
  295. {
  296. return m_file->is_fifo();
  297. }
  298. FIFO* FileDescription::fifo()
  299. {
  300. if (!is_fifo())
  301. return nullptr;
  302. return static_cast<FIFO*>(m_file.ptr());
  303. }
  304. bool FileDescription::is_socket() const
  305. {
  306. return m_file->is_socket();
  307. }
  308. Socket* FileDescription::socket()
  309. {
  310. if (!is_socket())
  311. return nullptr;
  312. return static_cast<Socket*>(m_file.ptr());
  313. }
  314. const Socket* FileDescription::socket() const
  315. {
  316. if (!is_socket())
  317. return nullptr;
  318. return static_cast<const Socket*>(m_file.ptr());
  319. }
  320. void FileDescription::set_file_flags(u32 flags)
  321. {
  322. LOCKER(m_lock);
  323. m_is_blocking = !(flags & O_NONBLOCK);
  324. m_should_append = flags & O_APPEND;
  325. m_direct = flags & O_DIRECT;
  326. m_file_flags = flags;
  327. }
  328. KResult FileDescription::chmod(mode_t mode)
  329. {
  330. LOCKER(m_lock);
  331. return m_file->chmod(*this, mode);
  332. }
  333. KResult FileDescription::chown(uid_t uid, gid_t gid)
  334. {
  335. LOCKER(m_lock);
  336. return m_file->chown(*this, uid, gid);
  337. }
  338. FileBlockCondition& FileDescription::block_condition()
  339. {
  340. return m_file->block_condition();
  341. }
  342. }