FileDescription.cpp 11 KB

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