diff --git a/kernel/include/kernel/OpenFileDescriptorSet.h b/kernel/include/kernel/OpenFileDescriptorSet.h index 7fb6b59958..6344a818a4 100644 --- a/kernel/include/kernel/OpenFileDescriptorSet.h +++ b/kernel/include/kernel/OpenFileDescriptorSet.h @@ -52,6 +52,7 @@ namespace Kernel BAN::ErrorOr read_dir_entries(int fd, struct dirent* list, size_t list_len); + BAN::ErrorOr file_of(int) const; BAN::ErrorOr path_of(int) const; BAN::ErrorOr> inode_of(int); BAN::ErrorOr flags_of(int) const; @@ -59,15 +60,16 @@ namespace Kernel private: struct OpenFileDescription : public BAN::RefCounted { - OpenFileDescription(BAN::RefPtr inode, BAN::String path, off_t offset, int flags) - : inode(inode) - , path(BAN::move(path)) + OpenFileDescription(VirtualFileSystem::File file, off_t offset, int flags) + : file(BAN::move(file)) , offset(offset) , flags(flags) { } - BAN::RefPtr inode; - BAN::String path; + BAN::RefPtr inode() const { return file.inode; } + BAN::StringView path() const { return file.canonical_path.sv(); } + + VirtualFileSystem::File file; off_t offset { 0 }; int flags { 0 }; diff --git a/kernel/kernel/OpenFileDescriptorSet.cpp b/kernel/kernel/OpenFileDescriptorSet.cpp index b7baa09e9e..6c3521ea22 100644 --- a/kernel/kernel/OpenFileDescriptorSet.cpp +++ b/kernel/kernel/OpenFileDescriptorSet.cpp @@ -38,7 +38,11 @@ namespace Kernel auto& open_file = other.m_open_files[fd]; - auto result = BAN::RefPtr::create(open_file->inode, open_file->path, open_file->offset, open_file->flags); + VirtualFileSystem::File temp_file; + temp_file.inode = open_file->inode(); + TRY(temp_file.canonical_path.append(open_file->path())); + + auto result = BAN::RefPtr::create(BAN::move(temp_file), open_file->offset, open_file->flags); if (result.is_error()) { @@ -48,10 +52,10 @@ namespace Kernel m_open_files[fd] = result.release_value(); - if (m_open_files[fd]->path == ""_sv) + if (m_open_files[fd]->path() == ""_sv) { - ASSERT(m_open_files[fd]->inode->is_pipe()); - static_cast(m_open_files[fd]->inode.ptr())->clone_writing(); + ASSERT(m_open_files[fd]->inode()->is_pipe()); + static_cast(m_open_files[fd]->inode().ptr())->clone_writing(); } } @@ -75,7 +79,7 @@ namespace Kernel TRY(file.inode->truncate(0)); int fd = TRY(get_free_fd()); - m_open_files[fd] = TRY(BAN::RefPtr::create(file.inode, BAN::move(file.canonical_path), 0, flags)); + m_open_files[fd] = TRY(BAN::RefPtr::create(BAN::move(file), 0, flags)); return fd; } @@ -140,7 +144,7 @@ namespace Kernel auto socket = TRY(NetworkManager::get().create_socket(sock_domain, sock_type, 0777, m_credentials.euid(), m_credentials.egid())); int fd = TRY(get_free_fd()); - m_open_files[fd] = TRY(BAN::RefPtr::create(socket, ""_sv, 0, O_RDWR | extra_flags)); + m_open_files[fd] = TRY(BAN::RefPtr::create(VirtualFileSystem::File(socket, ""_sv), 0, O_RDWR | extra_flags)); return fd; } @@ -149,8 +153,8 @@ namespace Kernel TRY(get_free_fd_pair(fds)); auto pipe = TRY(Pipe::create(m_credentials)); - m_open_files[fds[0]] = TRY(BAN::RefPtr::create(pipe, ""_sv, 0, O_RDONLY)); - m_open_files[fds[1]] = TRY(BAN::RefPtr::create(pipe, ""_sv, 0, O_WRONLY)); + m_open_files[fds[0]] = TRY(BAN::RefPtr::create(VirtualFileSystem::File(pipe, ""_sv), 0, O_RDONLY)); + m_open_files[fds[1]] = TRY(BAN::RefPtr::create(VirtualFileSystem::File(pipe, ""_sv), 0, O_WRONLY)); return {}; } @@ -162,10 +166,10 @@ namespace Kernel int result = TRY(get_free_fd()); m_open_files[result] = m_open_files[fildes]; - if (m_open_files[result]->path == ""_sv) + if (m_open_files[result]->path() == ""_sv) { - ASSERT(m_open_files[result]->inode->is_pipe()); - static_cast(m_open_files[result]->inode.ptr())->clone_writing(); + ASSERT(m_open_files[result]->inode()->is_pipe()); + static_cast(m_open_files[result]->inode().ptr())->clone_writing(); } return result; @@ -185,10 +189,10 @@ namespace Kernel m_open_files[fildes2] = m_open_files[fildes]; m_open_files[fildes2]->flags &= ~O_CLOEXEC; - if (m_open_files[fildes2]->path == ""_sv) + if (m_open_files[fildes2]->path() == ""_sv) { - ASSERT(m_open_files[fildes2]->inode->is_pipe()); - static_cast(m_open_files[fildes2]->inode.ptr())->clone_writing(); + ASSERT(m_open_files[fildes2]->inode()->is_pipe()); + static_cast(m_open_files[fildes2]->inode().ptr())->clone_writing(); } return fildes; @@ -234,7 +238,7 @@ namespace Kernel base_offset = m_open_files[fd]->offset; break; case SEEK_END: - base_offset = m_open_files[fd]->inode->size(); + base_offset = m_open_files[fd]->inode()->size(); break; default: return BAN::Error::from_errno(EINVAL); @@ -258,7 +262,7 @@ namespace Kernel BAN::ErrorOr OpenFileDescriptorSet::truncate(int fd, off_t length) { TRY(validate_fd(fd)); - return m_open_files[fd]->inode->truncate(length); + return m_open_files[fd]->inode()->truncate(length); } static void read_stat_from_inode(BAN::RefPtr inode, struct stat* out) @@ -281,7 +285,7 @@ namespace Kernel BAN::ErrorOr OpenFileDescriptorSet::fstat(int fd, struct stat* out) const { TRY(validate_fd(fd)); - read_stat_from_inode(m_open_files[fd]->inode, out); + read_stat_from_inode(m_open_files[fd]->inode(), out); return {}; } @@ -319,10 +323,10 @@ namespace Kernel { TRY(validate_fd(fd)); - if (m_open_files[fd]->path == ""_sv) + if (m_open_files[fd]->path() == ""_sv) { - ASSERT(m_open_files[fd]->inode->is_pipe()); - static_cast(m_open_files[fd]->inode.ptr())->close_writing(); + ASSERT(m_open_files[fd]->inode()->is_pipe()); + static_cast(m_open_files[fd]->inode().ptr())->close_writing(); } m_open_files[fd].clear(); @@ -351,9 +355,9 @@ namespace Kernel { TRY(validate_fd(fd)); auto& open_file = m_open_files[fd]; - if ((open_file->flags & O_NONBLOCK) && !open_file->inode->can_read()) + if ((open_file->flags & O_NONBLOCK) && !open_file->inode()->can_read()) return 0; - size_t nread = TRY(open_file->inode->read(open_file->offset, buffer)); + size_t nread = TRY(open_file->inode()->read(open_file->offset, buffer)); open_file->offset += nread; return nread; } @@ -362,11 +366,11 @@ namespace Kernel { TRY(validate_fd(fd)); auto& open_file = m_open_files[fd]; - if ((open_file->flags & O_NONBLOCK) && !open_file->inode->can_write()) + if ((open_file->flags & O_NONBLOCK) && !open_file->inode()->can_write()) return 0; if (open_file->flags & O_APPEND) - open_file->offset = open_file->inode->size(); - size_t nwrite = TRY(open_file->inode->write(open_file->offset, buffer)); + open_file->offset = open_file->inode()->size(); + size_t nwrite = TRY(open_file->inode()->write(open_file->offset, buffer)); open_file->offset += nwrite; return nwrite; } @@ -377,19 +381,25 @@ namespace Kernel auto& open_file = m_open_files[fd]; if (!(open_file->flags & O_RDONLY)) return BAN::Error::from_errno(EACCES); - return TRY(open_file->inode->list_next_inodes(open_file->offset++, list, list_len)); + return TRY(open_file->inode()->list_next_inodes(open_file->offset++, list, list_len)); + } + + BAN::ErrorOr OpenFileDescriptorSet::file_of(int fd) const + { + TRY(validate_fd(fd)); + return m_open_files[fd]->file; } BAN::ErrorOr OpenFileDescriptorSet::path_of(int fd) const { TRY(validate_fd(fd)); - return m_open_files[fd]->path.sv(); + return m_open_files[fd]->path(); } BAN::ErrorOr> OpenFileDescriptorSet::inode_of(int fd) { TRY(validate_fd(fd)); - return m_open_files[fd]->inode; + return m_open_files[fd]->inode(); } BAN::ErrorOr OpenFileDescriptorSet::flags_of(int fd) const