jiamo jiamo - 1 year ago 96
Linux Question

linux file attributes

Use open syscall to write and create a file ,there's no attributes with the file. fedora16 gcc-4.6.3

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main()
char * str= "helloworld";
int fd = open("test.db",O_WRONLY|O_CREAT|O_TRUNC|O_APPEND);


return 0;

ll test.db

----------. 1 jiamo jiamo 14 Apr 17 11:34 test.db

While it don't create file with the default file attributes such like
touch test.db

umask : 0002

if drop the O_TRUNC
int fd = open("test1.db",O_WRONLY|O_CREAT|O_APPEND)

the file attributs is :

----rwx---. 1 jiamo jiamo 14 Apr 17 12:29 test1.db

Answer Source

Add the required permissions to the open() syscall:

int fd = open("test.db",O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 0666);

From the documentation:

mode must be specified when O_CREAT is in the flags, and is ignored otherwise.
The argument mode specifies the permissions to use in case a new file is created.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download