FDTable

Struct FDTable 

Source
pub struct FDTable {
    files: SpinLock<Vec<Option<Arc<dyn File>>>>,
    fd_flags: SpinLock<Vec<FdFlags>>,
    max_fds: usize,
}
Expand description

文件描述符表

§并发安全

内部使用 SpinLock 保护,支持多线程访问。

Fields§

§files: SpinLock<Vec<Option<Arc<dyn File>>>>

文件描述符数组 None 表示该 FD 未使用

§fd_flags: SpinLock<Vec<FdFlags>>

文件描述符标志数组(与 files 索引对应) 默认值为 FdFlags::empty()

§max_fds: usize

最大文件描述符数量

Implementations§

Source§

impl FDTable

Source

pub fn new() -> Self

创建新的文件描述符表

Source

pub fn alloc(&self, file: Arc<dyn File>) -> Result<usize, FsError>

分配一个新的文件描述符(默认无 FD 标志)

Source

pub fn alloc_with_flags( &self, file: Arc<dyn File>, flags: FdFlags, ) -> Result<usize, FsError>

分配一个新的文件描述符并指定 FD 标志

Source

pub fn install_at(&self, fd: usize, file: Arc<dyn File>) -> Result<(), FsError>

在指定的 FD 位置安装文件(默认无 FD 标志)

Source

pub fn install_at_with_flags( &self, fd: usize, file: Arc<dyn File>, flags: FdFlags, ) -> Result<(), FsError>

在指定的 FD 位置安装文件并指定 FD 标志

Source

pub fn get(&self, fd: usize) -> Result<Arc<dyn File>, FsError>

获取文件对象

Source

pub fn close(&self, fd: usize) -> Result<(), FsError>

关闭文件描述符

Source

pub fn dup(&self, old_fd: usize) -> Result<usize, FsError>

复制文件描述符

返回新的 fd,与 old_fd 指向同一个 Arc<dyn File> (共享 offset)。

Source

pub fn dup_from( &self, old_fd: usize, min_fd: usize, flags: FdFlags, ) -> Result<usize, FsError>

复制文件描述符,新 fd >= min_fd(F_DUPFD 语义)

返回新的 fd,与 old_fd 指向同一个 Arc<dyn File> (共享 offset)。 新分配的 fd 是 >= min_fd 的最小未使用文件描述符。

Source

pub fn dup2(&self, old_fd: usize, new_fd: usize) -> Result<usize, FsError>

复制文件描述符到指定位置

如果 new_fd 已打开,先关闭它。

Source

pub fn dup3( &self, old_fd: usize, new_fd: usize, flags: OpenFlags, ) -> Result<usize, FsError>

复制文件描述符到指定位置(dup3 语义)

如果 new_fd 已打开,先关闭它。 与 dup2 不同,dup3 不允许 old_fd == new_fd。

§参数
  • flags: 可以包含 O_CLOEXEC,用于设置新 FD 的 CLOEXEC 标志
Source

pub fn clone_table(&self) -> Self

克隆整个文件描述符表(用于 fork)

所有 Arc<dyn File> 引用计数递增,父子进程共享文件对象。 FD 标志也会被复制。

Source

pub fn close_exec(&self)

关闭所有带有 CLOEXEC 标志的文件(用于 exec)

Source

pub fn get_fd_flags(&self, fd: usize) -> Result<FdFlags, FsError>

获取文件描述符标志 (F_GETFD)

Source

pub fn set_fd_flags(&self, fd: usize, flags: FdFlags) -> Result<(), FsError>

设置文件描述符标志 (F_SETFD)

Trait Implementations§

Source§

impl Debug for FDTable

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for FDTable

§

impl !RefUnwindSafe for FDTable

§

impl Send for FDTable

§

impl Sync for FDTable

§

impl Unpin for FDTable

§

impl !UnwindSafe for FDTable

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.