我正在使用 open
创建文件并设置其权限,然后我使用 stat
获取文件权限....权限不匹配。
下面程序的结果是:
mode from open (600) and stat (100600) are different
如何比较 open(2)
设置的模式(权限)与 stat(2)
检索的模式(权限)?
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int
main(int argc, char **argv, char **env) {
const char *path = "/tmp/test";
const mode_t mode = S_IRUSR | S_IWUSR;
if (open(path, O_RDWR | O_CREAT | O_EXCL, mode) == -1)
err(1, "open for '%s' failed", path);
struct stat sb;
if (stat(path, &sb) != 0)
err(2, "stat failed");
if (mode != sb.st_mode)
printf("mode from open (%o) and stat (%o) are different\n",
mode, sb.st_mode);
return 0;
}
谢谢
最佳答案
这是因为 st_mode
成员不仅包含访问权限,还包含许多其他标志(例如,您可以检查文件是否是符号链接(symbolic link))。 Docs here .
关于c - 如何匹配open和stat mode_t?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24314397/