简体   繁体   中英

How to have read permission in a recent created folder and file in C?

I've created aa 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,


:s

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


And I already have the 0_CREAT at the open

open (str1, O_RDWR | O_CREAT | O_TRUNC);

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:

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM