Why are the file permissions changed when creating a file with the open system call on Linux?

后端 未结 3 1517
-上瘾入骨i
-上瘾入骨i 2020-12-11 14:34

I am creating a file with full permission (777) using the open system call, but when I do ls -l I can see only permission as (755). Could you pleas

3条回答
  •  爱一瞬间的悲伤
    2020-12-11 14:59

    umask will return the original value of the mask, so to reset it temporarily you just need to do

    #include 
    #include 
    
    mode_t old_mask = umask(0);
    ...
    umask(old_mask);
    

    though it would be perhaps preferable to use fchmod after opening the file - this will avoid the problem of changing process-global state:

    fd = open("test", O_CREAT | O_RDWR | O_APPEND, 0777);
    if (fd < 0) { ... }
    int rv = fchmod(fd, 0777);
    if (rv < 0) { /* fchmod errored */ }
    

提交回复
热议问题