FileDescription.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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::fstat(stat& buffer)
  72. {
  73. if (is_fifo()) {
  74. memset(&buffer, 0, sizeof(buffer));
  75. buffer.st_mode = S_IFIFO;
  76. return KSuccess;
  77. }
  78. if (is_socket()) {
  79. memset(&buffer, 0, sizeof(buffer));
  80. buffer.st_mode = S_IFSOCK;
  81. return KSuccess;
  82. }
  83. if (!m_inode)
  84. return KResult(-EBADF);
  85. return metadata().stat(buffer);
  86. }
  87. off_t FileDescription::seek(off_t offset, int whence)
  88. {
  89. LOCKER(m_lock);
  90. if (!m_file->is_seekable())
  91. return -ESPIPE;
  92. off_t new_offset;
  93. switch (whence) {
  94. case SEEK_SET:
  95. new_offset = offset;
  96. break;
  97. case SEEK_CUR:
  98. new_offset = m_current_offset + offset;
  99. break;
  100. case SEEK_END:
  101. if (!metadata().is_valid())
  102. return -EIO;
  103. new_offset = metadata().size;
  104. break;
  105. default:
  106. return -EINVAL;
  107. }
  108. if (new_offset < 0)
  109. return -EINVAL;
  110. // FIXME: Return -EINVAL if attempting to seek past the end of a seekable device.
  111. m_current_offset = new_offset;
  112. return m_current_offset;
  113. }
  114. KResultOr<size_t> FileDescription::read(u8* buffer, size_t count)
  115. {
  116. LOCKER(m_lock);
  117. Checked<size_t> new_offset = m_current_offset;
  118. new_offset += count;
  119. if (new_offset.has_overflow())
  120. return -EOVERFLOW;
  121. SmapDisabler disabler;
  122. auto nread_or_error = m_file->read(*this, offset(), buffer, count);
  123. if (!nread_or_error.is_error() && m_file->is_seekable())
  124. m_current_offset += nread_or_error.value();
  125. return nread_or_error;
  126. }
  127. KResultOr<size_t> FileDescription::write(const u8* data, size_t size)
  128. {
  129. LOCKER(m_lock);
  130. Checked<size_t> new_offset = m_current_offset;
  131. new_offset += size;
  132. if (new_offset.has_overflow())
  133. return -EOVERFLOW;
  134. SmapDisabler disabler;
  135. auto nwritten_or_error = m_file->write(*this, offset(), data, size);
  136. if (!nwritten_or_error.is_error() && m_file->is_seekable())
  137. m_current_offset += nwritten_or_error.value();
  138. return nwritten_or_error;
  139. }
  140. bool FileDescription::can_write() const
  141. {
  142. return m_file->can_write(*this, offset());
  143. }
  144. bool FileDescription::can_read() const
  145. {
  146. return m_file->can_read(*this, offset());
  147. }
  148. KResultOr<KBuffer> FileDescription::read_entire_file()
  149. {
  150. // HACK ALERT: (This entire function)
  151. ASSERT(m_file->is_inode());
  152. ASSERT(m_inode);
  153. return m_inode->read_entire(this);
  154. }
  155. ssize_t FileDescription::get_dir_entries(u8* buffer, ssize_t size)
  156. {
  157. LOCKER(m_lock, Lock::Mode::Shared);
  158. if (!is_directory())
  159. return -ENOTDIR;
  160. auto metadata = this->metadata();
  161. if (!metadata.is_valid())
  162. return -EIO;
  163. if (size < 0)
  164. return -EINVAL;
  165. size_t size_to_allocate = max(static_cast<size_t>(PAGE_SIZE), static_cast<size_t>(metadata.size));
  166. auto temp_buffer = ByteBuffer::create_uninitialized(size_to_allocate);
  167. BufferStream stream(temp_buffer);
  168. KResult result = VFS::the().traverse_directory_inode(*m_inode, [&stream](auto& entry) {
  169. stream << (u32)entry.inode.index();
  170. stream << (u8)entry.file_type;
  171. stream << (u32)entry.name_length;
  172. stream << entry.name;
  173. return true;
  174. });
  175. if (result.is_error())
  176. return result;
  177. stream.snip();
  178. if (static_cast<size_t>(size) < temp_buffer.size())
  179. return -EINVAL;
  180. copy_to_user(buffer, temp_buffer.data(), temp_buffer.size());
  181. return stream.offset();
  182. }
  183. bool FileDescription::is_device() const
  184. {
  185. return m_file->is_device();
  186. }
  187. const Device* FileDescription::device() const
  188. {
  189. if (!is_device())
  190. return nullptr;
  191. return static_cast<const Device*>(m_file.ptr());
  192. }
  193. Device* FileDescription::device()
  194. {
  195. if (!is_device())
  196. return nullptr;
  197. return static_cast<Device*>(m_file.ptr());
  198. }
  199. bool FileDescription::is_tty() const
  200. {
  201. return m_file->is_tty();
  202. }
  203. const TTY* FileDescription::tty() const
  204. {
  205. if (!is_tty())
  206. return nullptr;
  207. return static_cast<const TTY*>(m_file.ptr());
  208. }
  209. TTY* FileDescription::tty()
  210. {
  211. if (!is_tty())
  212. return nullptr;
  213. return static_cast<TTY*>(m_file.ptr());
  214. }
  215. bool FileDescription::is_master_pty() const
  216. {
  217. return m_file->is_master_pty();
  218. }
  219. const MasterPTY* FileDescription::master_pty() const
  220. {
  221. if (!is_master_pty())
  222. return nullptr;
  223. return static_cast<const MasterPTY*>(m_file.ptr());
  224. }
  225. MasterPTY* FileDescription::master_pty()
  226. {
  227. if (!is_master_pty())
  228. return nullptr;
  229. return static_cast<MasterPTY*>(m_file.ptr());
  230. }
  231. KResult FileDescription::close()
  232. {
  233. if (m_file->ref_count() > 1)
  234. return KSuccess;
  235. return m_file->close();
  236. }
  237. String FileDescription::absolute_path() const
  238. {
  239. if (m_custody)
  240. return m_custody->absolute_path();
  241. return m_file->absolute_path(*this);
  242. }
  243. InodeMetadata FileDescription::metadata() const
  244. {
  245. if (m_inode)
  246. return m_inode->metadata();
  247. return {};
  248. }
  249. KResultOr<Region*> FileDescription::mmap(Process& process, VirtualAddress vaddr, size_t offset, size_t size, int prot, bool shared)
  250. {
  251. LOCKER(m_lock);
  252. return m_file->mmap(process, *this, vaddr, offset, size, prot, shared);
  253. }
  254. KResult FileDescription::truncate(u64 length)
  255. {
  256. LOCKER(m_lock);
  257. return m_file->truncate(length);
  258. }
  259. bool FileDescription::is_fifo() const
  260. {
  261. return m_file->is_fifo();
  262. }
  263. FIFO* FileDescription::fifo()
  264. {
  265. if (!is_fifo())
  266. return nullptr;
  267. return static_cast<FIFO*>(m_file.ptr());
  268. }
  269. bool FileDescription::is_socket() const
  270. {
  271. return m_file->is_socket();
  272. }
  273. Socket* FileDescription::socket()
  274. {
  275. if (!is_socket())
  276. return nullptr;
  277. return static_cast<Socket*>(m_file.ptr());
  278. }
  279. const Socket* FileDescription::socket() const
  280. {
  281. if (!is_socket())
  282. return nullptr;
  283. return static_cast<const Socket*>(m_file.ptr());
  284. }
  285. void FileDescription::set_file_flags(u32 flags)
  286. {
  287. LOCKER(m_lock);
  288. m_is_blocking = !(flags & O_NONBLOCK);
  289. m_should_append = flags & O_APPEND;
  290. m_direct = flags & O_DIRECT;
  291. m_file_flags = flags;
  292. }
  293. KResult FileDescription::chmod(mode_t mode)
  294. {
  295. LOCKER(m_lock);
  296. return m_file->chmod(*this, mode);
  297. }
  298. KResult FileDescription::chown(uid_t uid, gid_t gid)
  299. {
  300. LOCKER(m_lock);
  301. return m_file->chown(*this, uid, gid);
  302. }
  303. }