tags:

views:

1288

answers:

6

Hi guys, I'm writing a program that writes output to a file. If this file doesn't exist, I want to create it.

Currently, I'm using the following flags when calling open: O_WRONLY | O_CREATE

However, when this creates the file, it doesn't give me any permissions to write to it...

How can I use open so that it creates a file if it doesn't exist, but will create it with necessary permissions when needed?

Thanks!

+9  A: 

You probably need the third argument. For example:

open('path',O_WRONLY|O_CREAT,0640);
Randy Proctor
That was exactly it! Thank you!
samoz
I have a slight preference for @David's use of the flag constants (S_IRUSR | S_IWUSER | S_IRGRP | S_IROTH) rather than hard coded perms, but otherwise a good answer.
Paul Tomblin
Yes, his answer is better. Mine is merely a quick pointer to the third argument (my string is not even valid!).
Randy Proctor
A: 

You can specify 0644 as the third parameter:

open("file", O_WRONLY | O_CREATE, 0644);

I think that will give you the permissions you need.

ryanday
+1  A: 

On Linux there's a third argument you can use to pass permissions. S_IWUSR should be the flag to give you write permissions, but in practice you'll probably want to use more flags than just that one (bitwise or'd together). Check the manpage for a list of the permission flags.

Dan Olson
A: 

From the manual:

O_CREAT

If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, the file shall be created; the user ID of the file shall be set to the effective user ID of the process; the group ID of the file shall be set to the group ID of the file's parent directory or to the effective group ID of the process; and the access permission bits (see ) of the file mode shall be set to the value of the third argument taken as type mode_t modified as follows: a bitwise AND is performed on the file-mode bits and the corresponding bits in the complement of the process' file mode creation mask. Thus, all bits in the file mode whose corresponding bit in the file mode creation mask is set are cleared. When bits other than the file permission bits are set, the effect is unspecified. The third argument does not affect whether the file is open for reading, writing, or for both. Implementations shall provide a way to initialize the file's group ID to the group ID of the parent directory. Implementations may, but need not, provide an implementation-defined way to initialize the file's group ID to the effective group ID of the calling process.

So it seems you need to pass a third argument specifying the desired file permissions.

David Hanak
+7  A: 

Just use the optional third argument to open:

int open(const char* pathname, int flags, mode_t mode);

so like this:

open("blahblah", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSER | S_IRGRP | S_IROTH);

See man open(2).

David Zaslavsky
A: 

Note that under POSIX (Unix, Linux, MacOS, etc) you can open and create a file with any permissions you choose, including 0 (no permission for anyone), and yet still write to the file if opened for writing.

Jonathan Leffler