std.os.posix 包
功能介绍
os.posix 包主要适配 POSIX 系统接口。
本包提供多平台统一操控能力,目前支持 Linux 平台,macOS 平台,Windows 平台与 OHOS 平台。
API 列表
函数
| 函数名 | 功能 | 支持平台 |
|---|---|---|
| open(String, Int32) | 打开文件并为其返回新的文件描述符,或在失败时返回 -1。 | Linux Windows macOS OHOS |
| open(String, Int32, UInt32) | 打开文件并为其返回新的文件描述符,或在失败时返回 -1。 | Linux Windows macOS OHOS |
| access(String, Int32) | 判断某个文件是否具有某种权限,具有返回 0,否则返回 -1。 | Linux Windows macOS OHOS |
| chdir(String) | 通过指定路径的方式,更改调用进程的当前工作目录。 | Linux Windows macOS OHOS |
| chmod(String, UInt32) | 修改文件访问权限。 | Linux Windows macOS OHOS |
| chown(String, UInt32, UInt32) | 修改文件所有者和文件所有者所属组。 | Linux macOS OHOS |
| close(Int32) | 关闭文件,close 将会触发数据写回磁盘,并释放文件占用的资源。 | Linux Windows macOS OHOS |
| creat(String, UInt32) | 创建文件并为其返回文件描述符,或在失败时返回 -1。 | Linux Windows macOS OHOS |
| dup(Int32) | 用于复制旧 fd 参数指定的文件描述符并返回。 | Linux Windows macOS OHOS |
| dup2(Int32, Int32) | 用于复制 oldfd 参数指定的文件描述符,并将其返回到 newfd 参数。 | Linux Windows macOS OHOS |
| faccessat(Int32, String, Int32, Int32) | 判断 fd 对应的文件是否具有某种权限,具有返回 0,否则返回 -1。 | Linux macOS OHOS |
| fchdir(Int32) | 通过指定文件路径的描述符,更改调用进程的当前工作目录。 | Linux macOS OHOS |
| fchmod(Int32, UInt32) | 修改文件描述符对应的文件访问权限。 | Linux Windows macOS OHOS |
| fchmodat(Int32, String, UInt32, Int32) | 修改文件描述符对应的文件访问权限。 | Linux Windows macOS OHOS |
| fchown(Int32, UInt32, UInt32) | 修改 fd 对应的文件所有者和文件所有者所属组。 | Linux macOS OHOS |
| fchownat(Int32, String, UInt32, UInt32, Int32) | 修改文件描述符对应的文件所有者和文件所有者所属组。 | Linux macOS OHOS |
| getcwd() | 获取当前执行进程工作目录的绝对路径。 | Linux Windows macOS OHOS |
| getgid() | 获取用户组 ID。 | Linux macOS OHOS |
| getgroups(Int32, CPointer<UInt32>) | 获取当前用户所属组的代码。 | Linux macOS OHOS |
| gethostname() | 获取主机名称,此名称通常是 TCP/IP 网络上主机的名称。 | Linux macOS OHOS |
| getlogin() | 获取当前登录名。 | Linux macOS OHOS |
| getos() | 从 /proc/version 文件中获取 Linux 系统的信息。 | Linux |
| getpgid(Int32) | 获取 pid 指定的进程的 PGID,如果 pid 为零,返回调用进程的进程 ID。 | Linux macOS OHOS |
| getpgrp() | 获取调用进程的父进程 ID。 | Linux macOS OHOS |
| getpid() | 获取调用进程的进程 ID(PID)。 | Linux Windows macOS OHOS |
| getppid() | 获取调用进程的父进程 ID。 | Linux macOS OHOS |
| getuid() | 获取调用进程的真实用户 ID。 | Linux macOS OHOS |
| isBlk(String) | 检查传入对象是否为块设备,并返回布尔类型。 | Linux Windows macOS OHOS |
| isChr(String) | 检查传入对象是否为字符设备,返回布尔类型。 | Linux Windows macOS OHOS |
| isDir(String) | 检查传入对象是否为文件夹,返回布尔类型。 | Linux Windows macOS OHOS |
| isFIFO(String) | 检查传入对象是否为 FIFO 文件,返回布尔类型。 | Linux macOS OHOS |
| isLnk(String) | 检查传入对象是否为软链路,返回布尔类型。 | Linux macOS OHOS |
| isReg(String) | 检查传入对象是否为普通文件,返回布尔类型。 | Linux Windows macOS OHOS |
| isSock(String) | 检查传入对象是否为套接字文件,返回布尔类型。 | Linux macOS OHOS |
| isType(String, UInt32) | 检查文件是否为指定模式的文件。 | Linux macOS OHOS |
| isatty(Int32) | 用于测试文件描述符是否引用终端,成功时返回 true,否则返回 false。 | Linux Windows macOS OHOS |
| kill(Int32, Int32) | 系统调用可用于向任何进程组或进程发送任何信号。 | Linux macOS OHOS |
| killpg(Int32, Int32) | 将信号 sig 发送到进程组 pgrp,如果 pgrp 为 0,则 killpg() 将信号发送到调用进程的进程组。 | Linux macOS OHOS |
| lchown(String, UInt32, UInt32) | 修改文件链接本身所有者和所有者所属组。 | Linux macOS |
| link(String, String) | 为存在的文件创建链接,一个文件可以有多个指向其 i-node 的目录条目。 | Linux macOS OHOS |
| linkat(Int32, String, Int32, String, Int32) | 创建相对于目录文件描述符的文件链接。 | Linux macOS OHOS |
| lseek(Int32, Int64, Int32) | 当文件进行读或写时,读或写位置相应增加。 | Linux Windows macOS OHOS |
| nice(Int32) | 更改当前线程的优先级。 | Linux macOS OHOS |
| open64(String, Int32) | 打开文件并为其返回新的文件描述符,或在失败时返回 -1。 | Linux OHOS |
| open64(String, Int32, UInt32) | 打开文件并为其返回新的文件描述符,或在失败时返回 -1。 | Linux OHOS |
| openat(Int32, String, Int32) | 打开文件并为其返回新的文件描述符,或在失败时返回 -1。 | Linux macOS OHOS |
| openat(Int32, String, Int32, UInt32) | 打开文件并为其返回新的文件描述符,或在失败时返回 -1。 | Linux macOS OHOS |
| openat64(Int32, String, Int32) | 打开文件并为其返回新的文件描述符,或在失败时返回 -1。 | Linux macOS OHOS |
| openat64(Int32, String, Int32, UInt32) | 打开文件并为其返回新的文件描述符,或在失败时返回 -1。 | Linux macOS OHOS |
| pread(Int32, CPointer<UInt8>, UIntNative, Int32) | 将 fd 指向的文件的 nbyte 字节传输到 buffer 指向的内存中。 | Linux macOS OHOS |
| pwrite(Int32, CPointer<UInt8>, UIntNative, Int32) | 将 buffer 指向的内存中 nbyte 字节从指定偏移位置开始写入到 fd 指向的文件。 | Linux macOS OHOS |
| read(Int32, CPointer<UInt8>, UIntNative) | 将 fd 指向的文件的 nbyte 字节传输到 buffer 指向的内存中。 | Linux Windows macOS OHOS |
| remove(String) | 删除文件或目录。 | Linux Windows macOS OHOS |
| rename(String, String) | 重命名文件,如果需要将会移动文件所在目录。 | Linux Windows macOS OHOS |
| renameat(Int32, String, Int32, String) | 重命名文件,如果需要将会移动文件所在目录。 | Linux macOS OHOS |
| setgid(UInt32) | 设置调用进程的有效组 ID,需要适当的权限。 | Linux macOS |
| sethostname(String) | 设置主机名,仅超级用户可以调用。 | Linux macOS |
| setpgid(Int32, Int32) | 此函数将参数 pid 指定的组 ID 设置为参数 pgrp 指定的组 ID。 | Linux macOS OHOS |
| setpgrp() | 将当前进程所属的组 ID 设置为当前进程的进程 ID,此函数等同于调用 setpgid(0, 0)。 | Linux macOS OHOS |
| setuid(UInt32) | 设置调用进程的有效用户 ID,需要适当的权限。 | Linux macOS |
| symlink(String, String) | 创建一个名为 symPath 链接到 path 所指定的文件。 | Linux macOS OHOS |
| symlinkat(String, Int32, String) | 创建一个名为 symPath 链接到 path 与 fd 所指定的文件。 | Linux macOS OHOS |
| ttyname(Int32) | 返回终端名称。 | Linux Windows macOS OHOS |
| umask(UInt32) | 设置权限掩码。 | Linux Windows macOS OHOS |
| unlink(String) | 从文件系统中删除文件。 | Linux macOS OHOS |
| unlinkat(Int32, String, Int32) | 从文件系统中删除文件。 | Linux macOS OHOS |
| write(Int32, CPointer<UInt8>, UIntNative) | 将 buffer 指向的内存中 nbyte 字节写入到 fd 指向的文件。 | Linux Windows macOS OHOS |
常量
| 常量名 | 功能 | 支持平台 |
|---|---|---|
| AT_EMPTY_PATH | 表示在文件系统中空路径(即没有指定任何文件或目录)时返回的文件描述符,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux Windows OHOS |
| AT_REMOVEDIR | 如果指定了 AT_REMOVEDIR 标志,则对 pathname 执行等效于 rmdir(2) 的操作,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux Windows macOS OHOS |
| O_CLOEXEC | 在某些多线程程序中,使用此标志是必不可少的。因为在一个线程同时打开文件描述符,而另一个线程执行 fork(2) 加 execve(2) 场景下使用单独的 fcntl(2) F_SETFD 操作设置 FD_CLOEXEC 标志并不足以避免竞争条件,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux macOS OHOS |
| O_DIRECTORY | 如果 pathname 指定的文件不是目录,则打开文件失败,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux macOS OHOS |
| O_CREAT | 如果要打开的文件不存在,则自动创建该文件,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux Windows macOS OHOS |
| O_DSYNC | 每次写入都会等待物理 I/O 完成,但如果写操作不影响读取刚写入的数据,则不等待文件属性更新,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux macOS OHOS |
| O_EXCL | 如同时设置 O_CREAT,则此指令检查文件是否存在。如果文件不存在,则创建文件。否则,打开文件出错。此外,如果同时设置了 O_CREAT 和 O_EXCL,并且要打开的文件是符号链接,则打开文件失败,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux Windows macOS OHOS |
| O_NOCTTY | 如要打开的文件是终端设备,则该文件不会成为这个进程的控制终端,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux macOS OHOS |
| O_NOFOLLOW | 如 pathname 指定的文件是单符号连接,则打开文件失败,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux macOS OHOS |
| O_NONBLOCK | 以非阻塞的方式打开文件,即 I/O 操作不会导致调用进程等待,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux macOS OHOS |
| O_SYNC | 同步打开文件,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux macOS OHOS |
| O_RDONLY | 以只读方式打开文件,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux Windows macOS OHOS |
| O_RDWR | 以读写模式打开文件,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux Windows macOS OHOS |
| O_WRONLY | 以只写方式打开文件,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux Windows macOS OHOS |
| O_APPEND | 读取或写入文件时,数据将从文件末尾移动。即写入的数据将附加到文件的末尾,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux Windows macOS OHOS |
| O_RSYNC | 此标志仅影响读取操作,必须与 O_SYNC 或 O_DSYNC 结合使用。如果有必要,它将导致读取调用阻塞,直到正在读取的数据(可能还有元数据)刷新到磁盘,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux OHOS |
| O_TRUNC | 如果文件存在且打开可写,则此标志将文件长度清除为 0,文件中以前存储的数据消失,适用函数 open、open64、openat、openat64,所属函数参数 oflag。 | Linux Windows macOS OHOS |
| R_OK | 测试文件读权限,适用函数 access,faccessat,所属函数参数 mode。 | Linux Windows macOS OHOS |
| W_OK | 测试文件写权限,适用函数 access,faccessat,所属函数参数 mode。 | Linux Windows macOS OHOS |
| X_OK | 测试文件执行权限,适用函数 access,faccessat,所属函数参数 mode。 | Linux Windows macOS OHOS |
| F_OK | 测试文件是否存在,适用函数 access,faccessat,所属函数参数 mode。 | Linux Windows macOS OHOS |
| SEEK_SET | 偏移参数表示新的读写位置,适用函数 lseek,所属函数参数 whence。 | Linux Windows macOS OHOS |
| SEEK_CUR | 向当前读或写位置添加偏移量,适用函数 lseek,所属函数参数 whence。 | Linux Windows macOS OHOS |
| SEEK_END | 将读写位置设置为文件末尾,并添加偏移量,适用函数 lseek,所属函数参数 whence。 | Linux Windows macOS OHOS |
| SIGABRT | 异常终止,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGBUS | 硬件故障,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGFPE | 算术错误,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGKILL | 终止,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGCONT | 暂停过程的继续,默认操作继续或忽略,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGHUP | 连接已断开,默认操作已终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGINT | 终端中断字符,默认动作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGQUIT | 终端退出字符,默认动作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGILL | 硬件指令无效,默认动作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGTRAP | 硬件故障,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGIOT | 硬件故障,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGIO | 异步 IO,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGPIPE | 写入未读进程的管道,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGALRM | 计时器到期,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGPWR | 电源故障或重启,系统调用无效,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows OHOS |
| SIGSEGV | 内存引用无效,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGSTOP | 停止,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGTERM | 终止,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGSTKFLT | 协处理器堆栈故障,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows OHOS |
| SIGCHLD | 子进程状态更改,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGTSTP | 终端停止符号,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGTTIN | 后台读取控件 tty,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGTTOU | 后台写控制 tty,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGURG | 紧急情况(套接字),忽略默认操作,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGUSR1 | 用户定义的信号,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGUSR2 | 用户定义的信号,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGVTALRM | 虚拟时间警报,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGPROF | 摘要超时,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGWINCH | 终端窗口大小更改,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGXCPU | CPU 占用率超过上限,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| SIGXFSZ | 文件长度超过上限,默认操作终止,适用函数 kill,killpg,所属函数参数 sig。 | Linux Windows macOS OHOS |
| S_IRUSR | 表示文件所有者具有读权限,适用函数 open,open64,openat,openat64,chmod(mode),fchmod(mode),fchmodat(mode),creat, 所属函数参数 flag。 | Linux Windows macOS OHOS |
| S_IWUSR | 表示文件所有者具有写权限,适用函数 open,open64,openat,openat64,chmod(mode),fchmod(mode),fchmodat(mode),creat, 所属函数参数 flag。 | Linux Windows macOS OHOS |
| S_IRGRP | 表示文件用户组具有读权限,适用函数 open,open64,openat,openat64,chmod(mode),fchmod(mode),fchmodat(mode),creat, 所属函数参数 flag。 | Linux Windows macOS OHOS |
| S_IWGRP | 表示文件用户组具有写权限,适用函数 open,open64,openat,openat64,chmod(mode),fchmod(mode),fchmodat(mode),creat, 所属函数参数 flag。 | Linux Windows macOS OHOS |
| S_IFREG | 文件类型为一般文件,适用函数 isType, 所属函数参数 mode。 | Linux Windows macOS OHOS |
| S_IFBLK | 文件类型为块设备,适用函数 isType, 所属函数参数 mode。 | Linux Windows macOS OHOS |
| S_IFDIR | 文件类型为目录,适用函数 isType, 所属函数参数 mode。 | Linux Windows macOS OHOS |
| S_IFCHR | 文件类型为字符设备,适用函数 isType, 所属函数参数 mode。 | Linux Windows macOS OHOS |
| S_IFIFO | 文件类型为 FIFO 文件,适用函数 isType, 所属函数参数 mode。 | Linux Windows macOS OHOS |
| S_IFLNK | 文件类型为软连接,适用函数 isType, 所属函数参数 mode。 | Linux Windows macOS OHOS |
| S_IFSOCK | 文件类型为套接字文件,适用函数 isType, 所属函数参数 mode。 | Linux Windows macOS OHOS |
| S_IROTH | 表示其他用户对文件具有读权限,适用函数 open,open64,openat,openat64,chmod(mode),fchmod(mode),fchmodat(mode),creat, 所属函数参数 flag。 | Linux Windows macOS OHOS |
| S_IRWXG | 表示文件用户组具有读、写、执行权限,适用函数 open,open64,openat,openat64,chmod(mode),fchmod(mode),fchmodat(mode),creat, 所属函数参数 flag。 | Linux Windows macOS OHOS |
| S_IRWXU | 表示文件所有者具有读、写和执行权限,适用函数 open,open64,openat,openat64,chmod(mode),fchmod(mode),fchmodat(mode),creat, 所属函数参数 flag。 | Linux Windows macOS OHOS |
| S_IWOTH | 表示其他用户对文件具有写权限,适用函数 open,open64,openat,openat64,chmod(mode),fchmod(mode),fchmodat(mode),creat, 所属函数参数 flag。 | Linux Windows macOS OHOS |
| S_IXOTH | 表示其他用户对文件具有执行权限,适用函数 open,open64,openat,openat64,chmod(mode),fchmod(mode),fchmodat(mode),creat, 所属函数参数 flag。 | Linux Windows macOS OHOS |
| S_IRWXO | 表示其他用户对文件具有读、写和执行权限,适用函数 open,open64,openat,openat64,chmod(mode),fchmod(mode),fchmodat(mode),creat, 所属函数参数 flag。 | Linux Windows macOS OHOS |
| S_IXGRP | 表示文件用户组具有执行权限,适用函数 open,open64,openat,openat64,chmod(mode),fchmod(mode),fchmodat(mode),creat, 所属函数参数 flag。 | Linux Windows macOS OHOS |
| S_IXUSR | 表示文件所有者具有执行权限,适用函数 open,open64,openat,openat64,chmod(mode),fchmod(mode),fchmodat(mode),creat, 所属函数参数 flag。 | Linux Windows macOS OHOS |