Your code looks correct and should do what you want. You should check whether your program actually did create the file: if it did exist already, creat()
would succeed without error and update the timestamp, but not change the permissions.
creat system call fail to set permissions
-
17-07-2023 - |
Question
I need to use creat
system call to create file (it is used in legacy code and I'm not allowed to change this; example below is simplified one but the problem is just the same). I have the following code:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
umask(0);
int hndl = creat("abc.dat", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); // == 0660
close(hndl);
}
File is created as expected (I checked the timestamp also), but the permissions are different from 0660:
$ gcc creat.c && ./a.out && ll | grep abc
-rwx---r-x 1 ulysses ulysses 0 May 7 19:42 abc.dat*
which are 0705. I tried to use 0660 literally and had the same result. Am I missing something?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow