How do I get read permission on a newly created folder and file in C?

I created a folder and after I opened a file inside that folder write on it. It happens that after this I try to open the file, but I don't have permission, so I need to manually modify it.

/* 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 to have permission to open the generated file?

Many thanks,


: S

with = 0_CREATE I have a problem with not having permission to access a file. I have to install them manually


And I already have 0_CREAT when open

open (str1, O_RDWR | O_CREAT | O_TRUNC);

+1


source to share


4 answers


You are forgetting the third argument open()

.

The third argument, open()

c O_CREAT

, is exactly the permissions that the newly created file will have.



Literature:

+5


source


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

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

      



And please use "add comment" instead of answering your own question.

+3


source


SECURITY CONCERNS

The question is using permission 0777 in the directory - don't! ... In the normal course of events, you should not create a directory with permission 0777. You can use 01777 as on /tmp

; roughly, which ensures that people deleting a file from a directory have permission to modify the file. But you shouldn't allow all users to delete any files from the directory. And claiming that the setup umask

will protect you, while probably correct, is still not a good excuse to ruin the lives of those who don't know how to set it up safely. Use 0755 or maybe 0775 but not 0777.

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

+3


source


The problem is that you are using the wrong route separator - you are trying to use backslashes '\\'

to separate components. This is the path separator for Windows. Since you appear to be using a nix based operating system, you must use the forward slash '/'

as path separator. In fact, even on Windows, you should use the forward slash as it is more portable - Windows will automatically convert the forward slashes to the backslash.

As a result, you are trying to create a file named "foo \ bar" in the current directory and will probably work because you do not have permission to create the file in the current directory.

0


source







All Articles