我创建使用下面的代码文件:的open()未设置文件权限正确
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
const char* filename = "./test.out";
int fd;
if(-1 == (fd = open(filename, O_CREAT|O_RDWR, 0666)))
{
perror("Error");
errno = 0;
}
else
puts("File opened");
if(-1 == (close(fd)))
{
perror("Error");
errno = 0;
}
else
puts("File closed");
return 0;
}
我指定的mode
参数作为0666
,应授予读取,写入访问给大家。然而,ls -l
显示
-rw-r--r-- 1 kmehta users 0 2012-01-29 16:29 test.out
正如你所看到的,写权限仅仅授予文件的所有者。我不知道为什么其他人没有正确授予权限。 chmod a+w test.out
虽然正确设置权限。
代码编译为gcc -Wall test.c
规格:GCC v 4.5.0在openSUSE 11.3 64个
检查umask。检查umask。 – wildplasser 2012-01-29 22:40:07
你是否尝试使用常量来表示标志,例如S_IRUSR,S_IRGRP等? – dasblinkenlight 2012-01-29 22:41:39
@dasblinkenlight使用常量没有帮助。这是一个umask问题,现在打开文件后使用fchmod,如答案 – jitihsk 2012-01-29 22:59:18