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/BufferStream.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. BufferStream 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;
  165. return true;
  166. });
  167. if (result.is_error())
  168. return result;
  169. stream.snip();
  170. if (static_cast<size_t>(size) < temp_buffer.size())
  171. return -EINVAL;
  172. if (!buffer.write(temp_buffer.data(), temp_buffer.size()))
  173. return -EFAULT;
  174. return stream.offset();
  175. }
  176. bool FileDescription::is_device() const
  177. {
  178. return m_file->is_device();
  179. }
  180. const Device* FileDescription::device() const
  181. {
  182. if (!is_device())
  183. return nullptr;
  184. return static_cast<const Device*>(m_file.ptr());
  185. }
  186. Device* FileDescription::device()
  187. {
  188. if (!is_device())
  189. return nullptr;
  190. return static_cast<Device*>(m_file.ptr());
  191. }
  192. bool FileDescription::is_tty() const
  193. {
  194. return m_file->is_tty();
  195. }
  196. const TTY* FileDescription::tty() const
  197. {
  198. if (!is_tty())
  199. return nullptr;
  200. return static_cast<const TTY*>(m_file.ptr());
  201. }
  202. TTY* FileDescription::tty()
  203. {
  204. if (!is_tty())
  205. return nullptr;
  206. return static_cast<TTY*>(m_file.ptr());
  207. }
  208. bool FileDescription::is_master_pty() const
  209. {
  210. return m_file->is_master_pty();
  211. }
  212. const MasterPTY* FileDescription::master_pty() const
  213. {
  214. if (!is_master_pty())
  215. return nullptr;
  216. return static_cast<const MasterPTY*>(m_file.ptr());
  217. }
  218. MasterPTY* FileDescription::master_pty()
  219. {
  220. if (!is_master_pty())
  221. return nullptr;
  222. return static_cast<MasterPTY*>(m_file.ptr());
  223. }
  224. KResult FileDescription::close()
  225. {
  226. if (m_file->ref_count() > 1)
  227. return KSuccess;
  228. return m_file->close();
  229. }
  230. String FileDescription::absolute_path() const
  231. {
  232. if (m_custody)
  233. return m_custody->absolute_path();
  234. return m_file->absolute_path(*this);
  235. }
  236. InodeMetadata FileDescription::metadata() const
  237. {
  238. if (m_inode)
  239. return m_inode->metadata();
  240. return {};
  241. }
  242. KResultOr<Region*> FileDescription::mmap(Process& process, VirtualAddress vaddr, size_t offset, size_t size, int prot, bool shared)
  243. {
  244. LOCKER(m_lock);
  245. return m_file->mmap(process, *this, vaddr, offset, size, prot, shared);
  246. }
  247. KResult FileDescription::truncate(u64 length)
  248. {
  249. LOCKER(m_lock);
  250. return m_file->truncate(length);
  251. }
  252. bool FileDescription::is_fifo() const
  253. {
  254. return m_file->is_fifo();
  255. }
  256. FIFO* FileDescription::fifo()
  257. {
  258. if (!is_fifo())
  259. return nullptr;
  260. return static_cast<FIFO*>(m_file.ptr());
  261. }
  262. bool FileDescription::is_socket() const
  263. {
  264. return m_file->is_socket();
  265. }
  266. Socket* FileDescription::socket()
  267. {
  268. if (!is_socket())
  269. return nullptr;
  270. return static_cast<Socket*>(m_file.ptr());
  271. }
  272. const Socket* FileDescription::socket() const
  273. {
  274. if (!is_socket())
  275. return nullptr;
  276. return static_cast<const Socket*>(m_file.ptr());
  277. }
  278. void FileDescription::set_file_flags(u32 flags)
  279. {
  280. LOCKER(m_lock);
  281. m_is_blocking = !(flags & O_NONBLOCK);
  282. m_should_append = flags & O_APPEND;
  283. m_direct = flags & O_DIRECT;
  284. m_file_flags = flags;
  285. }
  286. KResult FileDescription::chmod(mode_t mode)
  287. {
  288. LOCKER(m_lock);
  289. return m_file->chmod(*this, mode);
  290. }
  291. KResult FileDescription::chown(uid_t uid, gid_t gid)
  292. {
  293. LOCKER(m_lock);
  294. return m_file->chown(*this, uid, gid);
  295. }
  296. }