2023-07-07 23:11:37 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <BAN/Array.h>
|
|
|
|
#include <kernel/FS/Inode.h>
|
2024-08-01 15:35:02 +03:00
|
|
|
#include <kernel/FS/VirtualFileSystem.h>
|
2023-07-07 23:11:37 +03:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
namespace Kernel
|
|
|
|
{
|
|
|
|
|
|
|
|
class OpenFileDescriptorSet
|
|
|
|
{
|
|
|
|
BAN_NON_COPYABLE(OpenFileDescriptorSet);
|
|
|
|
|
|
|
|
public:
|
|
|
|
OpenFileDescriptorSet(const Credentials&);
|
|
|
|
~OpenFileDescriptorSet();
|
|
|
|
|
2023-07-19 17:47:12 +03:00
|
|
|
OpenFileDescriptorSet& operator=(OpenFileDescriptorSet&&);
|
|
|
|
|
2023-07-07 23:11:37 +03:00
|
|
|
BAN::ErrorOr<void> clone_from(const OpenFileDescriptorSet&);
|
|
|
|
|
2024-08-28 16:36:10 +03:00
|
|
|
BAN::ErrorOr<int> open(VirtualFileSystem::File&&, int flags);
|
2023-07-07 23:11:37 +03:00
|
|
|
BAN::ErrorOr<int> open(BAN::StringView absolute_path, int flags);
|
|
|
|
|
2024-02-01 23:39:09 +02:00
|
|
|
BAN::ErrorOr<int> socket(int domain, int type, int protocol);
|
|
|
|
|
2023-07-07 23:11:37 +03:00
|
|
|
BAN::ErrorOr<void> pipe(int fds[2]);
|
|
|
|
|
2023-08-17 12:03:29 +03:00
|
|
|
BAN::ErrorOr<int> dup(int);
|
2023-07-07 23:11:37 +03:00
|
|
|
BAN::ErrorOr<int> dup2(int, int);
|
|
|
|
|
2023-09-04 12:57:09 +03:00
|
|
|
BAN::ErrorOr<int> fcntl(int fd, int cmd, int extra);
|
|
|
|
|
2024-05-23 14:49:23 +03:00
|
|
|
BAN::ErrorOr<off_t> seek(int fd, off_t offset, int whence);
|
2023-07-07 23:11:37 +03:00
|
|
|
BAN::ErrorOr<off_t> tell(int) const;
|
|
|
|
|
2024-05-28 01:08:04 +03:00
|
|
|
BAN::ErrorOr<void> truncate(int fd, off_t length);
|
|
|
|
|
2023-07-07 23:11:37 +03:00
|
|
|
BAN::ErrorOr<void> close(int);
|
|
|
|
void close_all();
|
|
|
|
void close_cloexec();
|
|
|
|
|
2023-10-20 05:07:44 +03:00
|
|
|
BAN::ErrorOr<size_t> read(int fd, BAN::ByteSpan);
|
|
|
|
BAN::ErrorOr<size_t> write(int fd, BAN::ConstByteSpan);
|
2023-07-07 23:11:37 +03:00
|
|
|
|
2024-05-22 20:17:39 +03:00
|
|
|
BAN::ErrorOr<size_t> read_dir_entries(int fd, struct dirent* list, size_t list_len);
|
2023-07-07 23:11:37 +03:00
|
|
|
|
2024-09-14 21:15:58 +03:00
|
|
|
BAN::ErrorOr<VirtualFileSystem::File> file_of(int) const;
|
2023-07-07 23:11:37 +03:00
|
|
|
BAN::ErrorOr<BAN::StringView> path_of(int) const;
|
2023-07-23 18:52:33 +03:00
|
|
|
BAN::ErrorOr<BAN::RefPtr<Inode>> inode_of(int);
|
2023-09-29 17:23:42 +03:00
|
|
|
BAN::ErrorOr<int> flags_of(int) const;
|
2023-07-07 23:11:37 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct OpenFileDescription : public BAN::RefCounted<OpenFileDescription>
|
|
|
|
{
|
2024-09-14 20:10:21 +03:00
|
|
|
OpenFileDescription(VirtualFileSystem::File file, off_t offset, int flags)
|
|
|
|
: file(BAN::move(file))
|
2023-07-07 23:11:37 +03:00
|
|
|
, offset(offset)
|
|
|
|
, flags(flags)
|
|
|
|
{ }
|
|
|
|
|
2024-09-14 20:10:21 +03:00
|
|
|
BAN::RefPtr<Inode> inode() const { return file.inode; }
|
|
|
|
BAN::StringView path() const { return file.canonical_path.sv(); }
|
|
|
|
|
|
|
|
VirtualFileSystem::File file;
|
2023-07-07 23:11:37 +03:00
|
|
|
off_t offset { 0 };
|
2023-07-10 16:06:36 +03:00
|
|
|
int flags { 0 };
|
2023-07-07 23:11:37 +03:00
|
|
|
|
|
|
|
friend class BAN::RefPtr<OpenFileDescription>;
|
|
|
|
};
|
|
|
|
|
|
|
|
BAN::ErrorOr<void> validate_fd(int) const;
|
|
|
|
BAN::ErrorOr<int> get_free_fd() const;
|
|
|
|
BAN::ErrorOr<void> get_free_fd_pair(int fds[2]) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Credentials& m_credentials;
|
|
|
|
|
|
|
|
BAN::Array<BAN::RefPtr<OpenFileDescription>, OPEN_MAX> m_open_files;
|
|
|
|
};
|
|
|
|
|
2024-01-24 14:43:46 +02:00
|
|
|
}
|