如何在C中最近创建的文件夹和文件中具有读取权限?

发布于 2024-07-09 09:29:22 字数 887 浏览 5 评论 0原文

我创建了一个文件夹,打开该文件夹内的文件后,在其上写入内容。 碰巧之后我尝试打开该文件,但我没有权限,因此我必须手动更改它。

/* 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;
   }

我应该更改什么才能有权打开创建的文件?

非常感谢,


:s

with = 0_CREATE 我仍然遇到没有权限读取文件的问题。 我必须手动设置它们


而且我已经在打开时打开了 0_CREAT

(str1, O_RDWR | O_CREAT | O_TRUNC);

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,


: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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

难得心□动 2024-07-16 09:29:22

您忘记了 open() 的第三个参数。

O_CREATopen() 的第三个参数正是新创建的文件将具有的权限。

参考文献:

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:

放肆 2024-07-16 09:29:22

CesarB 试图告诉您的是,您需要提供权限作为第三个参数:

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

并且请使用“添加评论”来回复,而不是为您自己的问题创建新回复。

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.

南汐寒笙箫 2024-07-16 09:29:22

安全问题

该问题对目录使用 0777 权限 - 不要! 在正常情况下,您不应创建具有 0777 权限的目录。 您可以考虑使用 01777,就像您在 /tmp 上找到的那样; 粗略地说,这确保了从目录中删除文件的人有权修改该文件。 但您不应授予每个人从目录中删除任何文件的权限。 声称 umask 设置会保护您,虽然可能是正确的,但仍然不是扰乱那些不知道如何安全设置的人的生活的好借口。 使用 0755 或可能 0775,但不要使用 0777。

答案之一是对文件使用 0777 权限 - 不要! 该参数与目录的参数类似,但附加了大多数人不这样做的警告在创建文件时创建可执行文件(链接器编写器是此一般规则的一个例外),并且无论生成的文件是否是可执行的,允许任何人修改程序仍然是一个非常糟糕的主意。 使用0644或0664; 很少有使用 0666 的充分理由,更不用说使用 0777 的充分理由。

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.

触ぅ动初心 2024-07-16 09:29:22

问题是您使用了错误的路径分隔符 - 您尝试使用反斜杠 '\\' 来分隔组件。 这是 Windows 的路径分隔符。 由于您似乎使用的是基于 *nix 的操作系统,因此您应该使用正斜杠 '/' 作为路径分隔符。 事实上,即使在 Windows 上,您也应该使用正斜杠,因为它更便携 - Windows 会自动将正斜杠转换为反斜杠。

因此,您尝试在当前目录中创建一个名为“foo\bar”的文件,这可能会失败,因为您没有在当前目录中创建文件的权限。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文