我正在使用
下面程序的结果是:
open
创建文件并设置其权限,然后使用stat
获取文件的权限...但是权限不匹配。下面程序的结果是:
我如何比较由来自open(600)和stat(100600)的mode不同
open(2)
设置和由stat(2)
取回的mode(权限)?
#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
确实提供了更多的信息,但问题是如何将st_mode
与open
中使用的t_mode
进行比较。一个代码示例会非常有用。 - user4543220777
来屏蔽噪音。顺便说一句,我不确定为什么你想要测试模式是否相等。最好使用提供的宏来测试各个标志位。 - The Paramagnetic Croissant0777
解释为rwx
。出于可移植性的考虑,您可以将其替换为所有S_I*
宏按位OR在一起的形式。 - The Paramagnetic Croissant#define RWX_UGO (S_IRWXU | S_IRWXG | S_IRWXO)
,但仍然无法理解。您能提供一下代码吗? - user454322#define RWX <所有9个宏按位或>
,然后if ((mode & RWX) == (st.st_mode & RWX)) { ... }
等。 - The Paramagnetic Croissant