【发布时间】:2014-08-10 10:50:33
【问题描述】:
我正在使用open 创建一个文件并设置其权限,然后我使用stat 获取文件权限......权限不匹配。
下面程序的结果是:
open (600) 和 stat (100600) 的模式不同
如何比较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;
}
谢谢
【问题讨论】:
标签: c unix posix file-permissions