Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

O_RDWR permission denied

I created this file

char *output = "big";
creat(output, O_RDWR);

When I'm trying to read the file

 cat big

I'm getting permission denied. Whats wrong with my code? How to create a file with read and write permission mode?

with ls -l, the permission of big looked like this

----------

what does this mean?

like image 567
19285496 Avatar asked Dec 09 '25 00:12

19285496


1 Answers

You have misinterpeted the mode argument. From the man page:

          mode specifies the permissions to use in case a new file is cre‐
          ated.  This argument must be supplied when O_CREAT is  specified
          in  flags;  if  O_CREAT  is not specified, then mode is ignored.
          The effective permissions are modified by the process's umask in
          the   usual  way:  The  permissions  of  the  created  file  are
          (mode & ~umask).  Note that this mode  only  applies  to  future
          accesses of the newly created file; the open() call that creates
          a read-only file may well return a read/write file descriptor.

and also

   creat()    is    equivalent    to    open()   with   flags   equal   to
   O_CREAT|O_WRONLY|O_TRUNC.

So, a more appropriate call might look like:

int fd = creat(output, 0644); /*-rw-r--r-- */

If you want to open it O_RDWR though, then just use open():

int fd = open(output, O_CREAT|O_RDWR|O_TRUNC, 0644);
like image 158
FatalError Avatar answered Dec 11 '25 15:12

FatalError



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!