libc_core/
fcntl.rs

1//! This module provides the `libc` types for FCNTL (file control).
2
3use num_enum::TryFromPrimitive;
4
5/// 当前目录的文件描述符
6pub const AT_FDCWD: isize = -100;
7
8/// 不跟随符号链接 (do not follow symbolic links)
9pub const AT_SYMLINK_NOFOLLOW: u32 = 0x100;
10
11/// 删除目录而非文件 (remove directory instead of file)
12pub const AT_REMOVEDIR: u32 = 0x200;
13
14/// 跟随符号链接 (follow symbolic links)
15pub const AT_SYMLINK_FOLLOW: u32 = 0x400;
16
17/// 使用有效权限而非实际权限 (use effective access rights instead of real)
18pub const AT_EACCESS: u32 = 0x200; // 注意:与 AT_REMOVEDIR 值相同
19
20/// 文件描述符控制命令
21///
22/// MUSL: <https://github.com/bminor/musl/blob/c47ad25ea3b484e10326f933e927c0bc8cded3da/arch/generic/bits/fcntl.h#L22>
23/// TODO: 根据不同的平台实现不同的命令
24#[repr(u32)]
25#[derive(Debug, Clone, PartialEq, TryFromPrimitive)]
26pub enum FcntlCmd {
27    /// dup
28    DUPFD = 0,
29    /// get close_on_exec
30    GETFD = 1,
31    /// set/clear close_on_exec
32    SETFD = 2,
33    /// get file->f_flags
34    GETFL = 3,
35    /// set file->f_flags
36    SETFL = 4,
37    /// Get record locking info.
38    GETLK = 5,
39    /// Set record locking info (non-blocking).
40    SETLK = 6,
41    /// Set record locking info (blocking).
42    SETLKW = 7,
43    /// like F_DUPFD, but additionally set the close-on-exec flag
44    DUPFDCLOEXEC = 0x406,
45}
46
47#[cfg(any(
48    target_arch = "riscv64",
49    target_arch = "loongarch64",
50    target_arch = "x86_64"
51))]
52bitflags! {
53    /// 文件打开标志,对应 Linux 的 open(2) 系统调用选项。
54    #[derive(Debug, Clone, Copy)]
55    pub struct OpenFlags: usize {
56        /// 只读(Read Only)
57        const RDONLY      = 0;
58        /// 只写(Write Only)
59        const WRONLY      = 1;
60        /// 读写(Read and Write)
61        const RDWR        = 2;
62        /// 访问模式掩码(Access mode mask)
63        const ACCMODE     = 3;
64        /// 文件不存在时创建(Create file if it does not exist)
65        const CREAT       = 0o100;
66        /// 与 O_CREAT 一起使用,文件存在时报错(Error if file exists)
67        const EXCL        = 0o200;
68        /// 不将设备设为控制终端(Do not make device a controlling terminal)
69        const NOCTTY      = 0o400;
70        /// 打开文件时清空内容(Truncate file to zero length)
71        const TRUNC       = 0o1000;
72        /// 每次写入都追加到末尾(Append on each write)
73        const APPEND      = 0o2000;
74        /// 非阻塞模式(Non-blocking I/O)
75        const NONBLOCK    = 0o4000;
76        /// 数据写入后立即同步(Synchronize data writes)
77        const DSYNC       = 0o10000;
78        /// 数据和元数据写入后同步(Synchronize all writes)
79        const SYNC        = 0o4010000;
80        /// 同步读操作(Same as O_SYNC)
81        const RSYNC       = 0o4010000;
82        /// 打开目标必须为目录(Fail if not a directory)
83        const DIRECTORY   = 0o200000;
84        /// 不跟随符号链接(Do not follow symlinks)
85        const NOFOLLOW    = 0o400000;
86        /// 执行 exec 时关闭(Close on exec)
87        const CLOEXEC     = 0o2000000;
88        /// 启用异步 I/O(Enable signal-driven I/O)
89        const ASYNC       = 0o20000;
90        /// 绕过页缓存直接 I/O(Direct disk access)
91        const DIRECT      = 0o40000;
92        /// 启用大文件支持(Enable large files)
93        const LARGEFILE   = 0o100000;
94        /// 不更新 atime(Do not update access time)
95        const NOATIME     = 0o1000000;
96        /// 仅解析路径,不打开目标(Open just the path)
97        const PATH        = 0o10000000;
98        /// 创建匿名临时文件(Unnamed temporary file)
99        const TMPFILE     = 0o20200000;
100    }
101
102}
103
104#[cfg(target_arch = "aarch64")]
105bitflags! {
106    /// 文件打开标志,对应 Linux 的 open(2) 系统调用选项。
107    #[derive(Debug, Clone, Copy)]
108    pub struct OpenFlags: usize {
109        /// 只读(Read Only)
110        const RDONLY = 0;
111        /// 只写(Write Only)
112        const WRONLY = 1;
113        /// 读写(Read and Write)
114        const RDWR = 2;
115        /// 访问模式掩码(用于提取读写模式)
116        const ACCMODE = 3;
117        /// 文件不存在时创建(Create if not exist)
118        const CREAT = 0o100;
119        /// 与 O_CREAT 配合使用,文件存在时报错(Exclusive)
120        const EXCL = 0o200;
121        /// 不将设备设为控制终端(No controlling TTY)
122        const NOCTTY = 0o400;
123        /// 打开时截断为 0 长度(Truncate)
124        const TRUNC = 0o1000;
125        /// 写操作追加到文件末尾(Append)
126        const APPEND = 0o2000;
127        /// 非阻塞模式(Non-blocking)
128        const NONBLOCK = 0o4000;
129        /// 数据同步写入(Data sync)
130        const DSYNC = 0o10000;
131        /// 数据+元数据同步写入(Full sync)
132        const SYNC = 0o4010000;
133        /// 同步读操作(Same as O_SYNC)
134        const RSYNC = 0o4010000;
135        /// 必须是目录(Directory only)
136        const DIRECTORY = 0o40000;
137        /// 不跟随符号链接(No follow symlinks)
138        const NOFOLLOW = 0o100000;
139        /// 执行 exec 时自动关闭(Close-on-exec)
140        const CLOEXEC = 0o2000000;
141        /// 启用异步 I/O 信号(Async notify)
142        const ASYNC = 0o20000;
143        /// 直接 I/O,绕过缓存(Direct access)
144        const DIRECT = 0o200000;
145        /// 启用大文件支持(Large file)
146        const LARGEFILE = 0o400000;
147        /// 不更新访问时间(No atime update)
148        const NOATIME = 0o1000000;
149        /// 只解析路径,不打开目标(Path only)
150        const PATH = 0o10000000;
151        /// 创建临时匿名文件(Unnamed temporary file)
152        const TMPFILE = 0o20040000;
153    }
154}