SimpleKernel 1.17.0
Loading...
Searching...
No Matches
filesystem::FileDescriptorTable Class Reference

进程级文件描述符表 More...

#include <file_descriptor.hpp>

Collaboration diagram for filesystem::FileDescriptorTable:
Collaboration graph

Public Member Functions

auto Alloc (vfs::File *file) -> Expected< int >
 分配一个最小可用 fd 并关联 File
 
auto Get (int fd) -> vfs::File *
 获取 fd 对应的 File 对象
 
auto Free (int fd) -> Expected< void >
 释放 fd
 
auto Dup (int old_fd, int new_fd=-1) -> Expected< int >
 复制文件描述符(用于 dup/dup2)
 
auto CloseAll () -> Expected< void >
 关闭所有文件描述符
 
auto SetupStandardFiles (vfs::File *stdin_file, vfs::File *stdout_file, vfs::File *stderr_file) -> Expected< void >
 设置标准文件描述符
 
auto GetOpenCount () const -> int
 获取已打开文件描述符数量
 
构造/析构函数
 FileDescriptorTable ()
 
 FileDescriptorTable (const FileDescriptorTable &)=delete
 
 FileDescriptorTable (FileDescriptorTable &&other)
 
auto operator= (const FileDescriptorTable &) -> FileDescriptorTable &=delete
 
auto operator= (FileDescriptorTable &&other) -> FileDescriptorTable &
 
 ~FileDescriptorTable ()
 

Static Public Attributes

static constexpr int kMaxFd = 64
 最大文件描述符数
 
static constexpr int kStdinFd = 0
 标准文件描述符
 
static constexpr int kStdoutFd = 1
 
static constexpr int kStderrFd = 2
 

Private Attributes

std::array< vfs::File *, kMaxFdtable_
 
int open_count_ {0}
 
SpinLock lock_ {"fd_table"}
 

Detailed Description

进程级文件描述符表

每个进程(TaskControlBlock)持有一个 FdTable, 将整数 fd 映射到 File 对象。 fd 0/1/2 预留给 stdin/stdout/stderr。

Definition at line 23 of file file_descriptor.hpp.

Constructor & Destructor Documentation

◆ FileDescriptorTable() [1/3]

filesystem::FileDescriptorTable::FileDescriptorTable ( )

Definition at line 11 of file file_descriptor.cpp.

◆ FileDescriptorTable() [2/3]

filesystem::FileDescriptorTable::FileDescriptorTable ( const FileDescriptorTable )
delete

◆ FileDescriptorTable() [3/3]

filesystem::FileDescriptorTable::FileDescriptorTable ( FileDescriptorTable &&  other)

Definition at line 21 of file file_descriptor.cpp.

22 : open_count_(other.open_count_), lock_{"fd_table"} {
23 LockGuard guard(other.lock_);
24
25 for (int i = 0; i < kMaxFd; ++i) {
26 table_[i] = other.table_[i];
27 other.table_[i] = nullptr;
28 }
29
30 other.open_count_ = 0;
31}
RAII 风格的锁守卫模板类
Definition spinlock.hpp:131
static constexpr int kMaxFd
最大文件描述符数

◆ ~FileDescriptorTable()

filesystem::FileDescriptorTable::~FileDescriptorTable ( )

Definition at line 14 of file file_descriptor.cpp.

14 {
15 CloseAll().or_else([](auto&& err) {
16 klog::Warn("Failed to close all files in destructor: {}", err.message());
17 return Expected<void>{};
18 });
19}
auto CloseAll() -> Expected< void >
关闭所有文件描述符
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
auto Warn(etl::format_string< Args... > fmt, Args &&... args) -> void
以 WARN 级别记录日志
Here is the call graph for this function:

Member Function Documentation

◆ Alloc()

auto filesystem::FileDescriptorTable::Alloc ( vfs::File file) -> Expected<int>

分配一个最小可用 fd 并关联 File

Parameters
file要关联的 File 对象
Returns
Expected<int> 分配到的 fd
Postcondition
返回的 fd >= 0 且 fd < kMaxFd

Definition at line 61 of file file_descriptor.cpp.

61 {
62 if (file == nullptr) {
63 return std::unexpected(Error(ErrorCode::kInvalidArgument));
64 }
65
66 LockGuard guard(lock_);
67
68 // 从 3 开始查找(0/1/2 预留给标准流)
69 for (int fd = 3; fd < kMaxFd; ++fd) {
70 if (table_[fd] == nullptr) {
71 table_[fd] = file;
73 return fd;
74 }
75 }
76
77 return std::unexpected(Error(ErrorCode::kFsFdTableFull));
78}
@ kInvalidArgument
错误类型,用于 std::expected
Definition expected.hpp:343

◆ CloseAll()

auto filesystem::FileDescriptorTable::CloseAll ( ) -> Expected<void>

关闭所有文件描述符

Returns
Expected<void> 成功或错误

Definition at line 144 of file file_descriptor.cpp.

144 {
145 LockGuard guard(lock_);
146
147 for (int fd = 0; fd < kMaxFd; ++fd) {
148 if (table_[fd] != nullptr) {
149 // 注意:这里不调用 VFS Close,因为 File 对象可能在其他地方共享
150 // 实际关闭操作由调用者负责
151 table_[fd] = nullptr;
152 }
153 }
154
155 open_count_ = 0;
156 return {};
157}
Here is the caller graph for this function:

◆ Dup()

auto filesystem::FileDescriptorTable::Dup ( int  old_fd,
int  new_fd = -1 
) -> Expected<int>

复制文件描述符(用于 dup/dup2)

Parameters
old_fd原文件描述符
new_fd目标文件描述符(若为 -1 则分配最小可用)
Returns
Expected<int> 新的文件描述符

Definition at line 106 of file file_descriptor.cpp.

106 {
107 if (old_fd < 0 || old_fd >= kMaxFd) {
108 return std::unexpected(Error(ErrorCode::kFsInvalidFd));
109 }
110
111 LockGuard guard(lock_);
112
113 vfs::File* file = table_[old_fd];
114 if (file == nullptr) {
115 return std::unexpected(Error(ErrorCode::kFsInvalidFd));
116 }
117
118 if (new_fd >= 0 && new_fd < kMaxFd) {
119 // 关闭目标 fd 如果已打开
120 if (table_[new_fd] != nullptr) {
121 table_[new_fd] = nullptr;
122 --open_count_;
123 }
124
125 table_[new_fd] = file;
126 ++open_count_;
127 return new_fd;
128 }
129
130 // 分配新的 fd
131 if (new_fd == -1) {
132 for (int fd = kStderrFd + 1; fd < kMaxFd; ++fd) {
133 if (table_[fd] == nullptr) {
134 table_[fd] = file;
135 ++open_count_;
136 return fd;
137 }
138 }
139 }
140
141 return std::unexpected(Error(ErrorCode::kFsFdTableFull));
142}
File — 打开的文件实例(每次 open 产生一个)
Definition vfs.hpp:65

◆ Free()

auto filesystem::FileDescriptorTable::Free ( int  fd) -> Expected<void>

释放 fd

Parameters
fd要释放的文件描述符
Returns
Expected<void>

Definition at line 89 of file file_descriptor.cpp.

89 {
90 if (fd < 0 || fd >= kMaxFd) {
91 return std::unexpected(Error(ErrorCode::kFsInvalidFd));
92 }
93
94 LockGuard guard(lock_);
95
96 if (table_[fd] == nullptr) {
97 return std::unexpected(Error(ErrorCode::kFsInvalidFd));
98 }
99
100 table_[fd] = nullptr;
101 --open_count_;
102
103 return {};
104}

◆ Get()

auto filesystem::FileDescriptorTable::Get ( int  fd) -> vfs::File*

获取 fd 对应的 File 对象

Parameters
fd文件描述符
Returns
vfs::File* 指针,无效 fd 返回 nullptr
Precondition
0 <= fd < kMaxFd

Definition at line 80 of file file_descriptor.cpp.

80 {
81 if (fd < 0 || fd >= kMaxFd) {
82 return nullptr;
83 }
84
85 LockGuard guard(lock_);
86 return table_[fd];
87}

◆ GetOpenCount()

auto filesystem::FileDescriptorTable::GetOpenCount ( ) const -> int

获取已打开文件描述符数量

Returns
int 已打开 fd 数量

Definition at line 183 of file file_descriptor.cpp.

183{ return open_count_; }

◆ operator=() [1/2]

auto filesystem::FileDescriptorTable::operator= ( const FileDescriptorTable ) -> FileDescriptorTable &=delete
delete

◆ operator=() [2/2]

auto filesystem::FileDescriptorTable::operator= ( FileDescriptorTable &&  other) -> FileDescriptorTable&

Definition at line 33 of file file_descriptor.cpp.

34 {
35 if (this != &other) {
36 // 先关闭当前的所有文件
37 CloseAll().or_else([](auto&& err) {
38 klog::Warn("Failed to close all files in move assignment: {}",
39 err.message());
40 return Expected<void>{};
41 });
42
43 // 使用地址确定加锁顺序,避免死锁
44 auto* first_lock = (this < &other) ? &lock_ : &other.lock_;
45 auto* second_lock = (this < &other) ? &other.lock_ : &lock_;
46 LockGuard guard1(*first_lock);
47 LockGuard guard2(*second_lock);
48
49 for (int i = 0; i < kMaxFd; ++i) {
50 table_[i] = other.table_[i];
51 other.table_[i] = nullptr;
52 }
53
54 open_count_ = other.open_count_;
55 other.open_count_ = 0;
56 }
57
58 return *this;
59}
Here is the call graph for this function:

◆ SetupStandardFiles()

auto filesystem::FileDescriptorTable::SetupStandardFiles ( vfs::File stdin_file,
vfs::File stdout_file,
vfs::File stderr_file 
) -> Expected<void>

设置标准文件描述符

Parameters
stdin_filestdin 文件对象
stdout_filestdout 文件对象
stderr_filestderr 文件对象
Returns
Expected<void> 成功或错误

Definition at line 159 of file file_descriptor.cpp.

162 {
163 LockGuard guard(lock_);
164
165 // 仅对之前未占用的槽位增加计数
166 if (table_[kStdinFd] == nullptr && stdin_file != nullptr) {
167 ++open_count_;
168 }
169 if (table_[kStdoutFd] == nullptr && stdout_file != nullptr) {
170 ++open_count_;
171 }
172 if (table_[kStderrFd] == nullptr && stderr_file != nullptr) {
173 ++open_count_;
174 }
175
176 table_[kStdinFd] = stdin_file;
177 table_[kStdoutFd] = stdout_file;
178 table_[kStderrFd] = stderr_file;
179
180 return {};
181}
static constexpr int kStdinFd
标准文件描述符

Member Data Documentation

◆ kMaxFd

constexpr int filesystem::FileDescriptorTable::kMaxFd = 64
staticconstexpr

最大文件描述符数

Definition at line 26 of file file_descriptor.hpp.

◆ kStderrFd

constexpr int filesystem::FileDescriptorTable::kStderrFd = 2
staticconstexpr

Definition at line 31 of file file_descriptor.hpp.

◆ kStdinFd

constexpr int filesystem::FileDescriptorTable::kStdinFd = 0
staticconstexpr

标准文件描述符

Definition at line 29 of file file_descriptor.hpp.

◆ kStdoutFd

constexpr int filesystem::FileDescriptorTable::kStdoutFd = 1
staticconstexpr

Definition at line 30 of file file_descriptor.hpp.

◆ lock_

SpinLock filesystem::FileDescriptorTable::lock_ {"fd_table"}
private

Definition at line 101 of file file_descriptor.hpp.

101{"fd_table"};

◆ open_count_

int filesystem::FileDescriptorTable::open_count_ {0}
private

Definition at line 100 of file file_descriptor.hpp.

100{0};

◆ table_

std::array<vfs::File*, kMaxFd> filesystem::FileDescriptorTable::table_
private

Definition at line 99 of file file_descriptor.hpp.


The documentation for this class was generated from the following files: