6 use warnings::register;
8 use constant _IS_CYGWIN => $^O eq "cygwin";
10 BEGIN { *warnif = \&warnings::warnif }
12 our(@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
14 our $VERSION = '1.09';
17 our ( $st_dev, $st_ino, $st_mode,
18 $st_nlink, $st_uid, $st_gid,
20 $st_atime, $st_mtime, $st_ctime,
21 $st_blksize, $st_blocks
26 @EXPORT = qw(stat lstat);
27 @fields = qw( $st_dev $st_ino $st_mode
28 $st_nlink $st_uid $st_gid
30 $st_atime $st_mtime $st_ctime
31 $st_blksize $st_blocks
33 @EXPORT_OK = ( @fields, "stat_cando" );
34 %EXPORT_TAGS = ( FIELDS => [ @fields, @EXPORT ] );
37 use Fcntl qw(S_IRUSR S_IWUSR S_IXUSR);
40 # These constants will croak on use if the platform doesn't define
41 # them. It's important to avoid inflicting that on the user.
43 for (qw(suid sgid svtx)) {
44 my $val = eval { &{"Fcntl::S_I\U$_"} };
45 *{"_$_"} = defined $val ? sub { $_[0] & $val ? 1 : "" } : sub { "" };
47 for (qw(SOCK CHR BLK REG DIR LNK)) {
48 *{"S_IS$_"} = defined eval { &{"Fcntl::S_IF$_"} }
49 ? \&{"Fcntl::S_IS$_"} : sub { "" };
51 # FIFO flag and macro don't quite follow the S_IF/S_IS pattern above
53 *{"S_ISFIFO"} = defined &Fcntl::S_IFIFO
54 ? \&Fcntl::S_ISFIFO : sub { "" };
61 # I am assuming that since VMS doesn't have getgroups(2), $) will
62 # always only contain a single entry.
63 $^O eq "VMS" and return $_[0] == $);
65 my ($egid, @supp) = split " ", $);
66 my ($rgid) = split " ", $(;
68 $gid == ($eff ? $egid : $rgid) and return 1;
69 grep $gid == $_, @supp and return 1;
74 # VMS uses the Unix version of the routine, even though this is very
75 # suboptimal. VMS has a permissions structure that doesn't really fit
76 # into struct stat, and unlike on Win32 the normal -X operators respect
77 # that, but unfortunately by the time we get here we've already lost the
78 # information we need. It looks to me as though if we were to preserve
79 # the st_devnam entry of vmsish.h's fake struct stat (which actually
80 # holds the filename) it might be possible to do this right, but both
81 # getting that value out of the struct (perl's stat doesn't return it)
82 # and interpreting it later would require this module to have an XS
83 # component (at which point we might as well just call Perl_cando and
86 if (grep $^O eq $_, qw/os2 MSWin32 dos/) {
89 *cando = sub { ($_[0][2] & $_[1]) ? 1 : "" };
95 my ($s, $mode, $eff) = @_;
96 my $uid = $eff ? $> : $<;
97 my ($stmode, $stuid, $stgid) = @$s[2,4,5];
99 # This code basically assumes that the rwx bits of the mode are
100 # the 0777 bits, but so does Perl_cando.
102 if (_IS_CYGWIN ? _ingroup(544, $eff) : ($uid == 0 && $^O ne "VMS")) {
103 # If we're root on unix
104 # not testing for executable status => all file tests are true
105 return 1 if !($mode & 0111);
106 # testing for executable status =>
107 # for a file, any x bit will do
108 # for a directory, always true
109 return 1 if $stmode & 0111 || S_ISDIR($stmode);
113 if ($stuid == $uid) {
114 $stmode & $mode and return 1;
116 elsif (_ingroup($stgid, $eff)) {
117 $stmode & ($mode >> 3) and return 1;
120 $stmode & ($mode >> 6) and return 1;
126 # alias for those who don't like objects
127 *stat_cando = \&cando;
130 r => sub { cando($_[0], S_IRUSR, 1) },
131 w => sub { cando($_[0], S_IWUSR, 1) },
132 x => sub { cando($_[0], S_IXUSR, 1) },
133 o => sub { $_[0][4] == $> },
135 R => sub { cando($_[0], S_IRUSR, 0) },
136 W => sub { cando($_[0], S_IWUSR, 0) },
137 X => sub { cando($_[0], S_IXUSR, 0) },
138 O => sub { $_[0][4] == $< },
141 z => sub { $_[0][7] == 0 },
142 s => sub { $_[0][7] },
144 f => sub { S_ISREG ($_[0][2]) },
145 d => sub { S_ISDIR ($_[0][2]) },
146 l => sub { S_ISLNK ($_[0][2]) },
147 p => sub { S_ISFIFO($_[0][2]) },
148 S => sub { S_ISSOCK($_[0][2]) },
149 b => sub { S_ISBLK ($_[0][2]) },
150 c => sub { S_ISCHR ($_[0][2]) },
152 u => sub { _suid($_[0][2]) },
153 g => sub { _sgid($_[0][2]) },
154 k => sub { _svtx($_[0][2]) },
156 M => sub { ($^T - $_[0][9] ) / 86400 },
157 C => sub { ($^T - $_[0][10]) / 86400 },
158 A => sub { ($^T - $_[0][8] ) / 86400 },
161 use constant HINT_FILETEST_ACCESS => 0x00400000;
163 # we need fallback=>1 or stringifying breaks
169 if (index("rwxRWX", $op) >= 0) {
170 (caller 0)[8] & HINT_FILETEST_ACCESS
171 and warnif("File::stat ignores use filetest 'access'");
173 $^O eq "VMS" and warnif("File::stat ignores VMS ACLs");
175 # It would be nice to have a warning about using -l on a
176 # non-lstat, but that would require an extra member in the
181 return $op{$op}->($_[0]);
184 croak "-$op is not implemented on a File::stat object";
188 # Class::Struct forbids use of @ISA
189 sub import { goto &Exporter::import }
191 use Class::Struct qw(struct);
192 struct 'File::stat' => [
193 map { $_ => '$' } qw{
194 dev ino mode nlink uid gid rdev size
195 atime mtime ctime blksize blocks
203 $st_dev, $st_ino, $st_mode, $st_nlink, $st_uid, $st_gid, $st_rdev,
204 $st_size, $st_atime, $st_mtime, $st_ctime, $st_blksize, $st_blocks )
209 sub lstat ($) { populate(CORE::lstat(shift)) }
213 my $st = populate(CORE::stat $arg);
214 return $st if defined $st;
220 $fh = \*{ Symbol::qualify( $arg, caller() )};
221 return unless defined fileno $fh;
223 return populate(CORE::stat $fh);
231 File::stat - by-name interface to Perl's built-in stat() functions
236 $st = stat($file) or die "No $file: $!";
237 if ( ($st->mode & 0111) && $st->nlink > 1) ) {
238 print "$file is executable with lotsa links\n";
242 print "$file is executable\n";
246 if ( $st->cando(S_IRUSR, 1) ) {
247 print "My effective uid can read $file\n";
250 use File::stat qw(:FIELDS);
251 stat($file) or die "No $file: $!";
252 if ( ($st_mode & 0111) && ($st_nlink > 1) ) {
253 print "$file is executable with lotsa links\n";
258 This module's default exports override the core stat()
259 and lstat() functions, replacing them with versions that return
260 "File::stat" objects. This object has methods that
261 return the similarly named structure field name from the
262 stat(2) function; namely,
278 As of version 1.02 (provided with perl 5.12) the object provides C<"-X">
279 overloading, so you can call filetest operators (C<-f>, C<-x>, and so
280 on) on it. It also provides a C<< ->cando >> method, called like
282 $st->cando( ACCESS, EFFECTIVE )
284 where I<ACCESS> is one of C<S_IRUSR>, C<S_IWUSR> or C<S_IXUSR> from the
285 L<Fcntl|Fcntl> module, and I<EFFECTIVE> indicates whether to use
286 effective (true) or real (false) ids. The method interprets the C<mode>,
287 C<uid> and C<gid> fields, and returns whether or not the current process
288 would be allowed the specified access.
290 If you don't want to use the objects, you may import the C<< ->cando >>
291 method into your namespace as a regular function called C<stat_cando>.
292 This takes an arrayref containing the return values of C<stat> or
293 C<lstat> as its first argument, and interprets it for you.
295 You may also import all the structure fields directly into your namespace
296 as regular variables using the :FIELDS import tag. (Note that this still
297 overrides your stat() and lstat() functions.) Access these fields as
298 variables named with a preceding C<st_> in front their method names.
299 Thus, C<$stat_obj-E<gt>dev()> corresponds to $st_dev if you import
302 To access this functionality without the core overrides,
303 pass the C<use> an empty import list, and then access
304 function functions with their full qualified names.
305 On the other hand, the built-ins are still available
306 via the C<CORE::> pseudo-package.
310 As of Perl 5.8.0 after using this module you cannot use the implicit
311 C<$_> or the special filehandle C<_> with stat() or lstat(), trying
312 to do so leads into strange errors. The workaround is for C<$_> to
315 my $stat_obj = stat $_;
317 and for C<_> to explicitly populate the object using the unexported
318 and undocumented populate() function with CORE::stat():
320 my $stat_obj = File::stat::populate(CORE::stat(_));
326 =item -%s is not implemented on a File::stat object
328 The filetest operators C<-t>, C<-T> and C<-B> are not implemented, as
329 they require more information than just a stat buffer.
335 These can all be disabled with
337 no warnings "File::stat";
341 =item File::stat ignores use filetest 'access'
343 You have tried to use one of the C<-rwxRWX> filetests with C<use
344 filetest 'access'> in effect. C<File::stat> will ignore the pragma, and
345 just use the information in the C<mode> member as usual.
347 =item File::stat ignores VMS ACLs
349 VMS systems have a permissions structure that cannot be completely
350 represented in a stat buffer, and unlike on other systems the builtin
351 filetest operators respect this. The C<File::stat> overloads, however,
352 do not, since the information required is not available.
358 While this class is currently implemented using the Class::Struct
359 module to build a struct-like class, you shouldn't rely upon this.