open(fd,O_RDWR,0664)生成文件权限问题

雨焰 2014-08-26 06:02:02
我在好多文件中看到open(fd,O_RDWR,0664)这么写的

可是我在查看生成的文件,他们的权限并不是0644。
这个是我的理解错误(并不表示权限)还是什么原因呢?
...全文
1585 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
登陆首发 2014-08-28
  • 打赏
  • 举报
回复
引用 5 楼 mymtom 的回复:
实际生成的文件的权限还要受到进程的权限掩码的影响的 O_CREAT If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, the file is created; the user ID of the file is set to the effective user ID of the process; the group ID of the file is set to the group ID of the file's parent directory or to the effective group ID of the process; and the access permission bits (see <sys/stat.h>) of the file mode are set to the value of the third argument taken as type mode_t modified as follows: a bitwise-AND is performed on the file-mode bits and the corresponding bits in the complement of the process' file mode creation mask. Thus, all bits in the file mode whose corresponding bit in the file mode creation mask is set are cleared. When bits other than the file permission bits are set, the effect is unspecified. The third argument does not affect whether the file is open for reading, writing or for both.
+1
mymtom 2014-08-28
  • 打赏
  • 举报
回复
引用 3 楼 f839903061 的回复:
[quote=引用 2 楼 jwj070524 的回复:] 只有当open的第二个参数为O_CREAT时,第三个参数才起作用
非常感谢你的回答。 fd=open( filename,O_WRONLY | O_CREAT,0644); 我这边生成的文件权限是 -rw------- root root 1036800 2014-08-27 09:06 lu 不是-rw-r--r-- 所以很迷惑![/quote] 因为你的umask是0077或者0066啊, 自己用umask命令看看就知道啦 0644 & ~0077 = 0600 UMASK(2) Linux Programmer’s Manual UMASK(2) NAME umask - set file mode creation mask SYNOPSIS #include <sys/types.h> #include <sys/stat.h> mode_t umask(mode_t mask); DESCRIPTION umask() sets the calling process’s file mode creation mask (umask) to mask & 0777 (i.e., only the file permis- sion bits of mask are used), and returns the previous value of the mask. The umask is used by open(2), mkdir(2), and other system calls that create files to modify the permissions placed on newly created files or directories. Specifically, permissions in the umask are turned off from the mode argument to open(2) and mkdir(2). The constants that should be used to specify mask are described under stat(2). The typical default value for the process umask is S_IWGRP | S_IWOTH (octal 022). In the usual case where the mode argument to open(2) is specified as: S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH (octal 0666) when creating a new file, the permissions on the resulting file will be: S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH (because 0666 & ~022 = 0644; i.e., rw-r--r--). RETURN VALUE This system call always succeeds and the previous value of the mask is returned. CONFORMING TO SVr4, 4.3BSD, POSIX.1-2001.
mymtom 2014-08-28
  • 打赏
  • 举报
回复
实际生成的文件的权限还要受到进程的权限掩码的影响的 O_CREAT If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, the file is created; the user ID of the file is set to the effective user ID of the process; the group ID of the file is set to the group ID of the file's parent directory or to the effective group ID of the process; and the access permission bits (see <sys/stat.h>) of the file mode are set to the value of the third argument taken as type mode_t modified as follows: a bitwise-AND is performed on the file-mode bits and the corresponding bits in the complement of the process' file mode creation mask. Thus, all bits in the file mode whose corresponding bit in the file mode creation mask is set are cleared. When bits other than the file permission bits are set, the effect is unspecified. The third argument does not affect whether the file is open for reading, writing or for both.
赵4老师 2014-08-27
  • 打赏
  • 举报
回复
以root身份运行
雨焰 2014-08-27
  • 打赏
  • 举报
回复
引用 2 楼 jwj070524 的回复:
只有当open的第二个参数为O_CREAT时,第三个参数才起作用
非常感谢你的回答。 fd=open( filename,O_WRONLY | O_CREAT,0644); 我这边生成的文件权限是 -rw------- root root 1036800 2014-08-27 09:06 lu 不是-rw-r--r-- 所以很迷惑!
jwj070524 2014-08-26
  • 打赏
  • 举报
回复
只有当open的第二个参数为O_CREAT时,第三个参数才起作用
707wk 2014-08-26
  • 打赏
  • 举报
回复
用root权限运行的吗?

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧