views:

684

answers:

6

Hi, I've created a a folder and after I open a file inside of that folder a write on it. It happens that after that I try to open the file but I have no permissions thus I have to change it manually.

/* str1 has tha name of the folder */
/* str the bytes I want to write in the file inside the folder*/
...

   mkdir(str1,0777);    
   if (filefd < 0) { 

      strncpy(auxstr, str, MAX_MSG + 1);
      strcat(str1,"\\");
      strcat(str1, auxstr);
      filefd = open (str1, O_RDWR | O_CREAT | O_TRUNC);

      nbytes -= (strlen(str) + 1);
      memcpy(auxstr, &str[strlen(str)+1], nbytes); 
      memcpy(str, auxstr, nbytes);

   }

   /*write to the file */
   if ((nwritten = write(filefd, str, nbytes)) != nbytes) {
       printf ("can't write on file\n");
       break;
   }

What should I change in order to have permissions to open the created file?

Thanks a lot,

+5  A: 

You are forgetting the third argument to open().

The third argument to open() with O_CREAT is precisely the permissions the newly created file will have.

References:

CesarB
A: 

:s

with = 0_CREATE I STILL have the problem of no having permissions to read the file. I have to set them manually

A: 

And I already have the 0_CREAT at the open

open (str1, O_RDWR | O_CREAT | O_TRUNC);

+3  A: 

What CesarB is trying to tell you is that you need to supply the permissions as third argument:

filefd = open (str1, O_RDWR | O_CREAT | O_TRUNC, 0777);

And please use "add comment" to reply instead of creating a new reply to your own question.

Wimmel
+3  A: 

SECURITY CONCERNS

The question uses 0777 permission on a directory - don't! In the ordinary course of events, you should not create a directory with 0777 permission. You might consider using 01777 like you'd find on /tmp; roughly speaking, that ensures that the people removing a file from the directory have permission to modify the file. But you should not grant everyone permission to remove any file from a directory. And claiming that the umask setting will protect you, while probably correct, is still not a good excuse for messing up the lives of those who don't know how to set it safely. Use 0755 or perhaps 0775 but not 0777.

One of the answers uses 0777 permission on a file - don't! The argument is similar to the argument for directories, with the added caveat that most people do not create executables when creating files (linker writers would be an exception to this general rule), and regardless of whether the resulting file is meant to be executable, it is still an astoundingly bad idea to allow anyone whatsoever to modify a program. Use 0644 or 0664; there is seldom a good reason for using 0666, and even less often a good reason for using 0777.

Jonathan Leffler
The whole point of the umask is that you can use 0666/0777 on open()/mkdir() and the permission the user wants (group-writable or not, for instance) will be honored. People who do not know how to set a umask safely will most probably have it at a default 0022 or 0002.
CesarB
That depends on the system; many Unixes distribute with umask 0 unless you set it to something else. On mkdir(), I'll allow 0777, but object to it. On open(), there is seldom a good reason to use 0777; I'll allow 0666, but object to it too. I think publicly writable is a bad, bad, bad, bad default.
Jonathan Leffler
A: 

The problem is that you're using the wrong path separator - you're trying to use a backslash '\\' to separate the components. This is the path separator for Windows. Since you appear to be using a *nix-based operating system, you should use forward slash '/' as a path separator. In fact, even on Windows you should use forward slash, since it's more portable - Windows automatically converts forward slashes to backslashes.

As a result, you're trying to create a file named "foo\bar" in the current directory, and that's probably failing since you don't have permissions to create a file in the current directory.

Adam Rosenfield