FileDescription.cpp 8.9 KB

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