读书人

st_mode关于读权限的有关问题。

发布时间: 2013-07-16 22:38:05 作者: rapoo

st_mode关于读权限的问题。。。
如何通过文件的stat中的字段st_mode判断,当前用户对这个文件的读权限,比如我现在得到的
st_mode是一个数,我怎么做个判断判断我当期的这个用户对于这个文件具有读权限?
[解决办法]
C:\WINDOWS\system32>cacls /?
显示或者修改文件的访问控制列表(ACL)

CACLS filename [/T] [/M] [/S[:SDDL]] [/E] [/C] [/G user:perm] [/R user [...]]
[/P user:perm [...]] [/D user [...]]
filename Displays ACLs.
/T 更改当前目录及其所有子目录中指定
文件的 ACL。
/M 将装入卷的 ACL 更改到目录。
/S 显示 DACL 的 SDDL 字符串。
/S:SDDL 用 SDDL 字符串中指定的 ACL 替换
(与 /E、/G、/R、/P 或 /D 一起使用时无效)。
/E 编辑 ACL 而不替换。
/C 在出现拒绝访问错误时继续。
/G user:perm 赋予指定用户访问权限。
Perm 可以是: R 读取
W 写入
C 更改(写入)
F 完全控制
/R user 撤销指定用户的访问权限(仅在与 /E 一起使用时合法)。


/P user:perm 替换指定用户的访问权限。
Perm 可以是: N 无
R 读取
W 写入
C 更改(写入)
F 完全控制
/D user 拒绝指定用户的访问。
在命令中可以使用通配符指定多个文件。也可以在命令中指定
多个用户。

缩写:
CI - 容器继承。
ACE 会由目录继承。
OI - 对象继承。
ACE 会由文件继承。
IO - 只继承。
ACE 不适用于当前文件/目录。

[解决办法]
if(st.st_mode & S_IRUSR)
{
...
}

S_IRUSR(S_IREAD) 00400 文件所有者具可读取权限
S_IWUSR(S_IWRITE)00200 文件所有者具可写入权限
S_IXUSR(S_IEXEC) 00100 文件所有者具可执行权限
S_IRGRP 00040 用户组具可读取权限
S_IWGRP 00020 用户组具可写入权限
S_IXGRP 00010 用户组具可执行权限
S_IROTH 00004 其他用户具可读取权限
S_IWOTH 00002 其他用户具可写入权限
S_IXOTH 00001 其他用户具可执行权限
[解决办法]
_access, _waccess
Determine file-access permission.

int _access( const char *path, int mode );

int _waccess( const wchar_t *path, int mode );

Routine Required Header Optional Headers Compatibility
_access <io.h> <errno.h> Win 95, Win NT
_waccess <wchar.h> or <io.h> <errno.h> Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version


MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

Each of these functions returns 0 if the file has the given mode. The function returns 1 if the named file does not exist or is not accessible in the given mode; in this case, errno is set as follows:

EACCES

Access denied: file’s permission setting does not allow specified access.

ENOENT

Filename or path not found.

Parameters

path

File or directory path

mode

Permission setting

Remarks

When used with files, the _access function determines whether the specified file exists and can be accessed as specified by the value of mode. When used with directories, _access determines only whether the specified directory exists; in Windows NT, all directories have read and write access.

mode Value Checks File For
00 Existence only
02 Write permission
04 Read permission
06 Read and write permission


_waccess is a wide-character version of _access; the path argument to _waccess is a wide-character string. _waccess and _access behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_taccess _access _access _waccess


Example

/* ACCESS.C: This example uses _access to check the
* file named "ACCESS.C" to see if it exists and if
* writing is allowed.
*/

#include <io.h>
#include <stdio.h>
#include <stdlib.h>

void main( void )
{
/* Check for existence */
if( (_access( "ACCESS.C", 0 )) != -1 )
{
printf( "File ACCESS.C exists\n" );


/* Check for write permission */
if( (_access( "ACCESS.C", 2 )) != -1 )
printf( "File ACCESS.C has write permission\n" );
}
}


Output

File ACCESS.C exists
File ACCESS.C has write permission


File Handling Routines

See Also _chmod, _fstat, _open, _stat

[解决办法]
if(st_mode & _S_IREAD)
可读
else
不可读
[解决办法]
是什么样的用户,你需要另外判断。

引用:
Quote: 引用:

if(st.st_mode & S_IRUSR)
{
...
}

S_IRUSR(S_IREAD) 00400 文件所有者具可读取权限
S_IWUSR(S_IWRITE)00200 文件所有者具可写入权限
S_IXUSR(S_IEXEC) 00100 文件所有者具可执行权限
S_IRGRP 00040 用户组具可读取权限
S_IWGRP 00020 用户组具可写入权限
S_IXGRP 00010 用户组具可执行权限
S_IROTH 00004 其他用户具可读取权限
S_IWOTH 00002 其他用户具可写入权限
S_IXOTH 00001 其他用户具可执行权限


S_IRGRP 00040 用户组具可读取权限,为啥不是用,用户组?

[解决办法]
不用stat改用access
[解决办法]
geteuid获得你的有效用户ID,和stat返回的st_uid比较.

组权限同理:geteuid和st_gid

其他用户直接看st_mode就可以了

读书人网 >C语言

热点推荐