filetest
$can_perhaps_read = –r "file"; # use the mode bits
{
use filetest "access"; # intuit harder
$can_really_read = –r "file";
}
$can_perhaps_read = –r "file"; # use the mode bits againThis lexically scoped pragma tells the compiler to change the
behavior of the unary file test operators –r, –w,
–x, –R, –W, and
–X, documented in Chapter 3. The default behavior for these file tests is to use
the mode bits returned by the stat
family of calls. However, this may not always be the right thing to do,
such as when a filesystem understands ACLs (access control lists). In
environments such as AFS where this matters, the filetest pragma may help the permission
operators to return results more consistent with other tools.
There may be a slight performance decrease in the affected file test
operators under filetest, since on some
systems the extended functionality needs to be emulated.
Warning: any notion of using file tests for security purposes is a lost cause from the start. There is a window open for race conditions, because there’s no way to guarantee that the permissions will not change between the test and the real operation. If you are the least bit serious about security, you won’t use file test operators to decide whether something will work. Instead, just go ahead and try the real operation, then test for whether that operation succeeded. (You should be doing that anyway.) See the section Handling Timing Glitches in Chapter 20.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access