【发布时间】:2016-07-11 11:11:03
【问题描述】:
我发现使用 stat 和 access 来检查文件是否可执行可能会产生不同的结果。谁能给我解释一下这两种方法的区别?
使用 C:
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h> /* Definition of AT_* constants */
#include <assert.h>
int main(){
const int access_ret = access("start.r",X_OK);
const int eaccess_ret = eaccess("start.r",X_OK);
const int faccessat_ret=faccessat(AT_FDCWD,"start.r",X_OK,AT_EACCESS);
printf("%d,\t%d,\t%d",access_ret, eaccess_ret, faccessat_ret);
}
使用 bash:
$ stat start.r
File: 'start.r'
Size: 2769 Blocks: 8 IO Block: 4096 regular file
Device: 29h/41d Inode: 58066679 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ ci) Gid: ( 100/ users)
Access: 2016-03-22 22:29:31.234892605 -0400
Modify: 2010-08-06 03:40:03.000000000 -0400
Change: 2016-02-28 20:39:10.244094828 -0500
Birth: -
$ [[ -x start.r ]]; echo $?
0
使用python:
$ python3 -c '
> import os
> print(oct(os.stat("start.r").st_mode))
> print(os.access("start.r",os.X_OK,effective_ids=False))
> print(os.access("start.r",os.X_OK,effective_ids=True))
> '
0o100644
True
True
【问题讨论】:
-
在我对 perms 644 的本地文件进行的测试中,我从您的
[[ ...]]; echo $?测试中得到了1(正如我们俩所期望的那样)。也许用uname ; echo $SHELL的结果编辑你的 q ?祝你好运! -
@shellter 我有一些行为异常的文件。
$ uname ; echo $SHELL Linux /bin/bash
标签: python linux bash permissions