This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Replace #20763 with
[perl5.git] / lib / filetest.pm
CommitLineData
5ff3f7a4
GS
1package filetest;
2
2af1ab88 3our $VERSION = '1.01';
b75c8c73 4
5ff3f7a4
GS
5=head1 NAME
6
7filetest - Perl pragma to control the filetest permission operators
8
9=head1 SYNOPSIS
3cb6de81 10
5ff3f7a4
GS
11 $can_perhaps_read = -r "file"; # use the mode bits
12 {
13 use filetest 'access'; # intuit harder
14 $can_really_read = -r "file";
15 }
16 $can_perhaps_read = -r "file"; # use the mode bits again
17
18=head1 DESCRIPTION
19
20This pragma tells the compiler to change the behaviour of the filetest
feaeca78 21permission operators, C<-r> C<-w> C<-x> C<-R> C<-W> C<-X>
80d06f2d 22(see L<perlfunc>).
5ff3f7a4 23
feaeca78 24The default behaviour is to use the mode bits as returned by the stat()
5ff3f7a4
GS
25family of calls. This, however, may not be the right thing to do if
26for example various ACL (access control lists) schemes are in use.
27For such environments, C<use filetest> may help the permission
28operators to return results more consistent with other tools.
29
30Each "use filetest" or "no filetest" affects statements to the end of
31the enclosing block.
32
33There may be a slight performance decrease in the filetests
34when C<use filetest> is in effect, because in some systems
35the extended functionality needs to be emulated.
36
6ca796d8 37B<NOTE>: using the file tests for security purposes is a lost cause
80d06f2d
JH
38from the start: there is a window open for race conditions (who is to
39say that the permissions will not change between the test and the real
40operation?). Therefore if you are serious about security, just try
9b488eb8
HS
41the real operation and test for its success - think in terms of atomic
42operations.
5ff3f7a4
GS
43
44=head2 subpragma access
45
46Currently only one subpragma, C<access> is implemented. It enables
47(or disables) the use of access() or similar system calls. This
48extended filetest functionality is used only when the argument of the
49operators is a filename, not when it is a filehandle.
50
51=cut
52
9cfe5470 53$filetest::hint_bits = 0x00400000; # HINT_FILETEST_ACCESS
d5448623 54
5ff3f7a4
GS
55sub import {
56 if ( $_[1] eq 'access' ) {
d5448623 57 $^H |= $filetest::hint_bits;
5ff3f7a4
GS
58 } else {
59 die "filetest: the only implemented subpragma is 'access'.\n";
60 }
61}
62
63sub unimport {
64 if ( $_[1] eq 'access' ) {
d5448623 65 $^H &= ~$filetest::hint_bits;
5ff3f7a4
GS
66 } else {
67 die "filetest: the only implemented subpragma is 'access'.\n";
68 }
69}
70
711;