File stat() vs access() to check permissions on a directory

前端 未结 3 1861
南旧
南旧 2021-01-02 13:55

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

<
3条回答
  •  被撕碎了的回忆
    2021-01-02 14:41

    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.

提交回复
热议问题