openat与open的区别及用法示例
从2.6.16
版本开始,GNU/Linux
引入opeant
系统调用:
#define _XOPEN_SOURCE 700 /* Or define _POSIX_C_SOURCE >= 200809 */
#include <fcntl.h>
int openat(int dirfd , const char * pathname , int flags , ... /* mode_t mode */);
Returns file descriptor on success, or –1 on error
同open
相比,多了一个dirfd
参数。关于它的用法,参考以下解释:
If pathname specifies a relative pathname, then it is interpreted relative to the directory referred to by the open file descriptor dirfd, rather than relative to the process’s current working directory.
If pathname specifies a relative pathname, and dirfd contains the special value AT_FDCWD , then pathname is interpreted relative to the process’s current working directory (i.e., the same behavior as open(2)).
If pathname specifies an absolute pathname, then dirfd is ignored.
总结起来,如果pathname
是绝对路径,则dirfd
参数没用。如果pathname
是相对路径,并且dirfd
的值不是AT_FDCWD
,则pathname
的参照物是相对于dirfd
指向的目录,而不是进程的当前工作目录;反之,如果dirfd
的值是AT_FDCWD
,pathname
则是相对于进程当前工作目录的相对路径,此时等同于open
。参考kernel
代码则一目了然:
引入openat
(及其它at
结尾的函数)有以下两个原因:
First, openat() allows an application to avoid race conditions that could occur when using open(2) to open files in directories other than the current working directory. These race conditions result from the fact that some component of the directory prefix given to open(2) could be changed in parallel with the call to open(2). Such races can be avoided by opening a file descriptor for the target directory, and then specifying that file descriptor as the dirfd argument of openat().
Second, openat() allows the implementation of a per-thread “current working directory”, via file descriptor(s) maintained by the application. (This functionality can also be obtained by tricks based on the use of /proc/self/fd/dirfd, but less efficiently.)
引入openat是方便一个进程内的各线程可拥有不同的当前目录,传统的chdir会影响整个进程,而使用openat只需要每个线程在初始化时打开一个目录(调用open),然后就可以以openat在“当前目录”操作文件了,如:
int dirfd = open("/tmp"); // 相当于chdir到“/tmp”
int filefd = openat(dirfd, "myfile"); // 在/tmp目录下打开“myfile”文件
用法示例:
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h> void creat_at(char *dir_path, char *relative_path)
{
int dir_fd;
int fd;
int flags;
mode_t mode; dir_fd = open(dir_path, O_RDONLY); //fd参数是通过打开相对路径名所在的目录来获取。
if (dir_fd < )
{
perror("open");
exit(EXIT_FAILURE);
} flags = O_CREAT | O_TRUNC | O_RDWR;
mode = ; //-rw-r-----
fd = openat(dir_fd, relative_path, flags, mode);
if (fd < )
{
perror("openat");
exit(EXIT_FAILURE);
} write(fd, "HELLO", ); close(fd);
close(dir_fd);
} int main()
{
creat_at("../03.文件IO", "log.txt");
return ;
}
借用dirfd,将DIR*转换成int类型的文件描述符
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h> int main()
{
DIR *dir;
int dirfd2;
int fd;
int n; dir = opendir("../03.文件IO");
if(NULL == dir)
{
perror("open dir error");
return -;
}
dirfd2 = dirfd(dir);
if(- == dirfd2)
{
perror("dirfd error");
return -;
} fd = openat(dirfd2,"output.log",O_CREAT|O_RDWR|O_TRUNC, \
S_IRWXU|S_IRWXG|S_IRWXO);
if(- == fd)
{
perror("opeat error");
return -;
}
n = write(fd,"Hello world!\n",); close(fd);
closedir(dir); return ; }
参考资料:
openat(2) – Linux man page
The Linux programming interface
http://blog.csdn.net/wang1902568721/article/details/47796173
http://bbs.chinaunix.net/thread-2081442-1-1.html
openat与open的区别及用法示例的更多相关文章
- Linux find 用法示例
Linux中find常见用法示例 ·find path -option [ -print ] [ -exec -ok command ] {} \; find命令的参数 ...
- GROUP BY,WHERE,HAVING之间的区别和用法
GROUP BY,WHERE,HAVING之间的区别和用法 分类: Oracle学习2009-11-01 23:40 21963人阅读 评论(6) 收藏 举报 mathmanagersql数据库m ...
- [转]Linux中find常见用法示例
Linux中find常见用法示例[转]·find path -option [ -print ] [ -exec -ok command ] {} \;find命令的参 ...
- oracle中to_date详细用法示例(oracle日期格式转换)
这篇文章主要介绍了oracle中to_date详细用法示例,包括期和字符转换函数用法.字符串和时间互转.求某天是星期几.两个日期间的天数.月份差等用法 TO_DATE格式(以时间:2007-11-02 ...
- SQL Server连接查询之Cross Apply和Outer Apply的区别及用法(转载)
先简单了解下cross apply的语法以及会产生什么样的结果集吧!示例表: SELECT * FROM tableA CROSS APPLY tableB 两张表直接连接,不需要任何的关联条件,产生 ...
- Java容器类List、ArrayList、Vector及map、HashTable、HashMap的区别与用法
Java容器类List.ArrayList.Vector及map.HashTable.HashMap的区别与用法 ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数 ...
- 转 Java中final、finally、finalize的区别与用法
Java中final.finally.finalize的区别与用法 1.简单区别:final用于声明属性,方法和类,分别表示属性不可交变,方法不可覆盖,类不可继承.finally是异常处理语句结构 ...
- Position属性四个值:static、fixed、absolute和relative的区别和用法
Position属性四个值:static.fixed.absolute和relative的区别和用法 在用CSS+DIV进行布局的时候,一直对position的四个属性值relative,absolu ...
- Python中内置数据类型list,tuple,dict,set的区别和用法
Python中内置数据类型list,tuple,dict,set的区别和用法 Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, ...
随机推荐
- P2168 [NOI2015]荷马史诗 k叉哈夫曼树
思路:哈夫曼编码 提交:1次(参考题解) 题解:类似合并果子$QwQ$ 取出前$k$小(注意如果叶子结点不满的话要补全),合并起来再扔回堆里去. #include<cstdio> #inc ...
- 002_linux驱动之_register_chrdev注册字符设备
(一)解析:register_chrdev函数和unregister_chrdev函数 (二)register_chrdev函数原型 int register_chrdev(unsigned int ...
- oracle数据库GROUP BY 子句
1.GROUP BY子句 在SELECT 列表中所有未包含在组函数中的列都应该包含在GROUP BY 子句中. 如下: SELECT deptno,AVG(sal) from emp GROUP BY ...
- WPF中,Grid与Table的区别(英文)-转载
原文地址:http://blog.csdn.net/johnsuna/article/details/1742799 How is Grid Different from Table?Table an ...
- 前端逼死强迫症系列之css
一.编写css样式 1.ID选择器 由于ID唯一,所以也是写多遍. <head> <style> #i1{ background-color: #2459a2; height: ...
- 2019巅峰极客CTF-web1(LOL英雄联盟)
今晚有空 以后随缘写博客了 好好沉淀 web1当天做出的队伍很少 其实不难 折腾到最后就差一步 可惜 0x01 读取文件 截图没留了 只留了代码部分. 有个页面 有上传和下载功能 起初 ...
- ArcGIS Server“无法创建站点,计算机不具有有效的的许可”
问题描述 ArcGIS Server10.5安装过程中,所有授权和破解均已完成,但是最后一步创建站点的时候显示创建失败,会出现如下图所示的问题:既“无法创建站点,计算机不具有有效的的许可…”,经历了卸 ...
- Git 命令行使用
一.git简介: Linux创建了Linux,但是Linux的发展壮大是由世界各地的热心志愿者参与编写的?那么那么多份的代码是怎么合并的呢?之前是在2002年以前,世界各地的志愿者把源代码文件通过di ...
- Nginx内置变量及正则语法
对于很多Nginx初学者来说,配置文件是必须要看懂的.但是当公司的Nginx配置文件放在你面前的时候你总会被一些带着"$"符号和一大推看不懂的的正则给正懵逼.没错带着"$ ...
- Flutter子组件调用父组件方法修改父组件参数
子组件调用父级组件方法的主要实现是父组件给子组件传入一个方法,然后在子组件中调用父级方法来修改父级的参数.看一下效果图 父级组件实现 在父级组件中写一个_editParentText的方法来修改组件中 ...