File stat()vs access()来检查目录的权限 [英] File stat() vs access() to check permissions on a directory

查看:156
本文介绍了File stat()vs access()来检查目录的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功使用 stat()& access()分别确定用户是否具有读取或读取/写入目录的权限。

我的问题是:
- 是否有首选方法?我看到很多使用 stat 的例子,但是出于我的目的,访问似乎更加轻量级并且服务于用途。

- 是否有任何问题例如 - 安全)有一个还是另一个?
- 任何问题w /我的方法?

这里是一些伪代码(从无内存编译重新创建):

  //使用access():
bool readAccessPermission = false;
bool writeAccessPermission = false;
$ b $ if(mode =='r'){
if(access(directory,R_OK)== 0)
readAccessPermission = true;

else {
if(access(directory,R_OK& W_OK)== 0)
readAccessPermission = true;
writeAccessPermission = true;
}


//使用stat函数
//假设我已经调用stat(目录)并且有对象


bool readAccessPermission = false;
bool writeAccessPermission = false;

var retmode =((stats.mode)&(0777));如果(modeString =='r'){
if((retmode)&(consts.S_IRUSR)){
readAccessPermission = false;

if $($)

$ b $ {
if((retmode)&(consts.S_IRUSR)){
readAccessPermission = true; ((retmode)& consts.S_IWUSR)){
writeAccessPermission = true;

if




$ div class =h2_lin>解决方案

要么等同于您的需求。 access()是一个简洁的包装器,如果你不打算对你填充的stat结构进行任何操作。

<请注意,在这样做时你正在创造一场比赛。在调用 stat()/ access()时以及实际尝试和使用目录时,权限可能会发生变化。地狱,该目录甚至可以删除,并在那个时候重新创建。

最好是试图打开你所需要的,并检查 EPERM 。检查 stat() access()不能保证后续操作不会返回EPERM。 >

I have successfully used both stat() & access() separately to determine if a user has either read or read/write access to a directory.

My question is: - Is there a preferred method ? I see a lot of examples using stat, but for my purpose, access seems to be more lightweight and serves purpose.
- Are there any issues (e.g. - security) w/ one or the other ? - Any issues w/ my approach ?

Here is some pseudo code (re-creating from memory w/o compiling) :

       // Using access():
    bool readAccessPermission = false; 
    bool writeAccessPermission = false;

    if (mode == 'r'){
            if (access(directory, R_OK) == 0)
                    readAccessPermission = true;                        
    }
    else{
            if (access(directory, R_OK && W_OK) == 0)
                    readAccessPermission = true;
                    writeAccessPermission = true;
    }


    // vs. using stat function
    // assume I already called stat(directory) and have the object


    bool readAccessPermission = false; 
    bool writeAccessPermission = false;

    var retmode = ((stats.mode) & (0777));

    if (modeString == 'r'){ 
        if ((retmode) & (consts.S_IRUSR)){
            readAccessPermission = false; 
        }    
    } 
    else{ 
        if ((retmode) & (consts.S_IRUSR)){
            readAccessPermission = true; 

            if ((retmode) & consts.S_IWUSR)){               
                writeAccessPermission = true; 
            }
        }
    }

解决方案

Either is equivalent for your needs. access() is a cleaner wrapper if you're not going to do anything with the stat structure that you populate.

Just be mindful that you are creating a race when doing this. The permissions can change between calling stat()/access() and when you actually try and use the directory. Hell, the directory could even be deleted and recreated in that time.

It's better to just try and open what you need and check for EPERM. Checking stat() or access() will not guarantee that a subsequent operation won't return EPERM.

这篇关于File stat()vs access()来检查目录的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆