【问题标题】:open()'s "mode" argument won't set the correct permissions for the fileopen() 的“模式”参数不会为文件设置正确的权限
【发布时间】:2020-08-22 23:59:00
【问题描述】:

我的目的是打开两个文件,其中第二个是全新的,具有与第一个文件相同的权限。因此,为了测试我的代码,我将第一个文件权限更改为“777”。然后我开始运行我的程序。令我惊讶的是,新生文件2的权限是错误的!它们设置为 755。更奇怪的是,当我将第一个文件设置为“111”并再次尝试时,结果现在是“1204”。 有人可以向我解释一下这种奇怪的行为吗?

这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char *args[]) {
    struct stat stats;
    int fd1, fd2;
    fd1 = open("testfile.txt", O_RDONLY);
        /* Error check*/
        if (fd1 == -1) {
            /* Error handling */
            perror("Opening");
            printf("Unable to open file: %s\n", "testfile.txt");
            printf("ERROR: %s\n", strerror(errno));
            return 1;
        }
    if(fstat(fd1, &stats) == -1)
    {
        printf("Error while getting stats: %s\n", strerror(errno));
        exit(-1);
    }

    //Receives the output file as a main argument . . .
    if (argc > 1)
    {
        //(stats.st_mode = Gets the mask of the first file)
        fd2 = open(args[1], O_WRONLY|O_CREAT, stats.st_mode);
        /* Error check*/
        if (fd2 == -1) {
            /* Error handling */
            perror("Opening");
            printf("Unable to open file: %s\n",args[1]);
            printf("ERROR: %s\n", strerror(errno));
            return 1;
        }
    }
    //. . . if it doesn't it creates a standard one warning you about it
    else
    {
        fd2 = open("Nope.txt", O_WRONLY|O_CREAT, stats.st_mode);
        /* Error check*/
        if (fd2 == -1) {
            /* Error handling */
            perror("Opening");
            printf("Unable to open file: %s\n",args[1]);
            printf("ERROR: %s\n", strerror(errno));
            return 1;
        }
        printf("Standard file created\n");
    }
    close(fd1);
    close(fd2);
    return 0;
}

我尽量让它整洁:)

【问题讨论】:

  • 您是否在所有正确的位置正确地在不同的碱基之间进行转换? oct1204和dec644一样,都是rw- r-- r--,比较典型的权限。
  • 是否允许覆盖卷的groupother 权限?
  • 您好,有趣的是,st_mode 字段还包含其他标志。要将其转换为open 可以使用的文件模式,可能会感兴趣:stackoverflow.com/questions/11669504/…
  • @Thomas Jager 是的,八进制;
  • @Weather Vane 对不起,我什至不知道这个比赛的音量是什么意思;

标签: c file permissions system-calls


【解决方案1】:

来自关于 O_CREATE 部分的 open(2) 手册页:

有效模式由进程的umask以通常的方式修改:在没有默认ACL的情况下,创建文件的模式是(mode & ~umask)。

如果您在 bash 中键入 umask,您可以看到使用的值以及从您提供的模式中清除了哪些位。

【讨论】:

  • 现在一切都说得通了! (波浪线等于 NOT 运算符对吗?)
  • 奇怪的是,同样的问题在 mkdir() 指令中再次发生。我的默认 umask 设置为 022。我的程序使用以下指令创建目录 hello:mkdir("hello", 644),但运行后我发现权限是1204。怎么会?
  • uname 或多或少是一种在用户创建文件时配置“默认最大权限”的方法。这样,程序就可以创建具有777 权限的文件来表示“我希望这个文件在用户允许的范围内可以访问”。用户配置将充分调整权限。您可以修改您的 umask 设置,以允许您运行的程序创建具有更多权限的文件。
  • 是的,“波浪线”(波浪线 :))是 BITWISE NOT(因此 umask 中的所有位都被反转了)。
  • 好的,但是如果我的 umask 是 0022 (~umask = 7755),如果后面跟着一个 BITWISE AND 644,结果不应该是 644 而不是 1204(这是我的最终许可新目录最终得到)?
猜你喜欢
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 2013-05-16
相关资源
最近更新 更多