Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: Permission Denied with open() (GCC for windows)

Tags:

c

gcc

So, I just write:

int fdes = open(path, O_WRONLY | O_CREAT | O_TRUNC);

And if the file that the path referred to doesn't exist, this code works. But if it does, the code returns with errno 13 : Permission Denied. I never use the O_EXCL mode at all.

I have searched for the solution to this problem a long time and I am really confused. Can you explain the problem?

I use the GCC for windows(4.5.2). Your answer will be appreciated.

like image 513
DeathKing Avatar asked Sep 18 '11 03:09

DeathKing


1 Answers

  1. If you use O_CREAT, you should supply a mode as the third argument to open().
  2. If the file does exist, you must have write permission on the file. The error indicates (strongly suggests) that the file is created without write permission. This might be a consequence of not creating it with a sensible mode.

Note that (on Unix at least) you can create a file for writing with mode 0444 (or even 0); the permissions affect everyone else, but not the process that created the file while it uses the file descriptor that created the file.

like image 104
Jonathan Leffler Avatar answered Sep 22 '22 13:09

Jonathan Leffler