12 : table_{}, open_count_(0), lock_{
"fd_table"} {}
16 klog::Warn(
"Failed to close all files in destructor: {}", err.message());
22 : open_count_(other.open_count_), lock_{
"fd_table"} {
25 for (
int i = 0; i <
kMaxFd; ++i) {
26 table_[i] = other.table_[i];
27 other.table_[i] =
nullptr;
30 other.open_count_ = 0;
37 CloseAll().or_else([](
auto&& err) {
38 klog::Warn(
"Failed to close all files in move assignment: {}",
44 auto* first_lock = (
this < &other) ? &lock_ : &other.lock_;
45 auto* second_lock = (
this < &other) ? &other.lock_ : &lock_;
49 for (
int i = 0; i < kMaxFd; ++i) {
50 table_[i] = other.table_[i];
51 other.table_[i] =
nullptr;
54 open_count_ = other.open_count_;
55 other.open_count_ = 0;
62 if (file ==
nullptr) {
69 for (
int fd = 3; fd < kMaxFd; ++fd) {
70 if (table_[fd] ==
nullptr) {
81 if (fd < 0 || fd >= kMaxFd) {
90 if (fd < 0 || fd >= kMaxFd) {
96 if (table_[fd] ==
nullptr) {
100 table_[fd] =
nullptr;
107 if (old_fd < 0 || old_fd >= kMaxFd) {
114 if (file ==
nullptr) {
118 if (new_fd >= 0 && new_fd < kMaxFd) {
120 if (table_[new_fd] !=
nullptr) {
121 table_[new_fd] =
nullptr;
125 table_[new_fd] = file;
132 for (
int fd = kStderrFd + 1; fd < kMaxFd; ++fd) {
133 if (table_[fd] ==
nullptr) {
147 for (
int fd = 0; fd < kMaxFd; ++fd) {
148 if (table_[fd] !=
nullptr) {
151 table_[fd] =
nullptr;
166 if (table_[kStdinFd] ==
nullptr && stdin_file !=
nullptr) {
169 if (table_[kStdoutFd] ==
nullptr && stdout_file !=
nullptr) {
172 if (table_[kStderrFd] ==
nullptr && stderr_file !=
nullptr) {
176 table_[kStdinFd] = stdin_file;
177 table_[kStdoutFd] = stdout_file;
178 table_[kStderrFd] = stderr_file;
auto SetupStandardFiles(vfs::File *stdin_file, vfs::File *stdout_file, vfs::File *stderr_file) -> Expected< void >
设置标准文件描述符
auto CloseAll() -> Expected< void >
关闭所有文件描述符
auto GetOpenCount() const -> int
获取已打开文件描述符数量
auto Alloc(vfs::File *file) -> Expected< int >
分配一个最小可用 fd 并关联 File
auto Get(int fd) -> vfs::File *
获取 fd 对应的 File 对象
auto operator=(const FileDescriptorTable &) -> FileDescriptorTable &=delete
auto Dup(int old_fd, int new_fd=-1) -> Expected< int >
复制文件描述符(用于 dup/dup2)
auto Free(int fd) -> Expected< void >
释放 fd
static constexpr int kMaxFd
最大文件描述符数
std::array< vfs::File *, kMaxFd > table_
std::expected< T, Error > Expected
std::expected 别名模板
auto Warn(etl::format_string< Args... > fmt, Args &&... args) -> void
以 WARN 级别记录日志
File — 打开的文件实例(每次 open 产生一个)