Commit | Line | Data |
---|---|---|
36477c24 | 1 | package File::stat; |
3b825e41 RK |
2 | use 5.006; |
3 | ||
36477c24 | 4 | use strict; |
b395063c | 5 | use warnings; |
b7c4737f | 6 | use warnings::register; |
448e77d7 | 7 | use Carp; |
36477c24 | 8 | |
b7c4737f BM |
9 | BEGIN { *warnif = \&warnings::warnif } |
10 | ||
17f410f9 GS |
11 | our(@EXPORT, @EXPORT_OK, %EXPORT_TAGS); |
12 | ||
f29a27fe | 13 | our $VERSION = '1.02'; |
b75c8c73 | 14 | |
4f9e7902 | 15 | my @fields; |
36477c24 | 16 | BEGIN { |
17 | use Exporter (); | |
36477c24 | 18 | @EXPORT = qw(stat lstat); |
4f9e7902 | 19 | @fields = qw( $st_dev $st_ino $st_mode |
36477c24 | 20 | $st_nlink $st_uid $st_gid |
21 | $st_rdev $st_size | |
22 | $st_atime $st_mtime $st_ctime | |
23 | $st_blksize $st_blocks | |
24 | ); | |
4f9e7902 BM |
25 | @EXPORT_OK = ( @fields, "stat_cando" ); |
26 | %EXPORT_TAGS = ( FIELDS => [ @fields, @EXPORT ] ); | |
36477c24 | 27 | } |
4f9e7902 | 28 | use vars @fields; |
36477c24 | 29 | |
448e77d7 BM |
30 | use Fcntl qw(S_IRUSR S_IWUSR S_IXUSR); |
31 | ||
32 | BEGIN { | |
33 | # These constants will croak on use if the platform doesn't define | |
34 | # them. It's important to avoid inflicting that on the user. | |
35 | no strict 'refs'; | |
36 | for (qw(suid sgid svtx)) { | |
37 | my $val = eval { &{"Fcntl::S_I\U$_"} }; | |
38 | *{"_$_"} = defined $val ? sub { $_[0] & $val ? 1 : "" } : sub { "" }; | |
39 | } | |
40 | for (qw(SOCK CHR BLK REG DIR FIFO LNK)) { | |
41 | *{"S_IS$_"} = defined eval { &{"Fcntl::S_IF$_"} } | |
42 | ? \&{"Fcntl::S_IS$_"} : sub { "" }; | |
43 | } | |
44 | } | |
45 | ||
46 | # from doio.c | |
47 | sub _ingroup { | |
48 | ||
49 | $^O eq "MacOS" and return 1; | |
50 | ||
51 | my ($gid, $eff) = @_; | |
52 | ||
53 | # I am assuming that since VMS doesn't have getgroups(2), $) will | |
54 | # always only contain a single entry. | |
55 | $^O eq "VMS" and return $_[0] == $); | |
56 | ||
57 | my ($egid, @supp) = split " ", $); | |
58 | my ($rgid) = split " ", $(; | |
59 | ||
60 | $gid == ($eff ? $egid : $rgid) and return 1; | |
61 | grep $gid == $_, @supp and return 1; | |
62 | ||
63 | return ""; | |
64 | } | |
65 | ||
66 | # VMS uses the Unix version of the routine, even though this is very | |
67 | # suboptimal. VMS has a permissions structure that doesn't really fit | |
68 | # into struct stat, and unlike on Win32 the normal -X operators respect | |
69 | # that, but unfortunately by the time we get here we've already lost the | |
70 | # information we need. It looks to me as though if we were to preserve | |
71 | # the st_devnam entry of vmsish.h's fake struct stat (which actually | |
72 | # holds the filename) it might be possible to do this right, but both | |
73 | # getting that value out of the struct (perl's stat doesn't return it) | |
74 | # and interpreting it later would require this module to have an XS | |
75 | # component (at which point we might as well just call Perl_cando and | |
76 | # have done with it). | |
77 | ||
78 | if (grep $^O eq $_, qw/os2 MSWin32 dos/) { | |
79 | ||
80 | # from doio.c | |
4f9e7902 | 81 | *cando = sub { ($_[0] & $_[2][2]) ? 1 : "" }; |
448e77d7 BM |
82 | } |
83 | else { | |
84 | ||
85 | # from doio.c | |
86 | *cando = sub { | |
87 | my ($s, $mode, $eff) = @_; | |
88 | my $uid = $eff ? $> : $<; | |
89 | ||
cf5afd78 | 90 | $^O ne "VMS" and $uid == 0 and return 1; |
448e77d7 | 91 | |
4f9e7902 BM |
92 | my ($stmode, $stuid, $stgid) = @$s[2,4,5]; |
93 | ||
448e77d7 BM |
94 | # This code basically assumes that the rwx bits of the mode are |
95 | # the 0777 bits, but so does Perl_cando. | |
4f9e7902 BM |
96 | if ($stuid == $uid) { |
97 | $stmode & $mode and return 1; | |
448e77d7 | 98 | } |
4f9e7902 BM |
99 | elsif (_ingroup($stgid, $eff)) { |
100 | $stmode & ($mode >> 3) and return 1; | |
448e77d7 BM |
101 | } |
102 | else { | |
4f9e7902 | 103 | $stmode & ($mode >> 6) and return 1; |
448e77d7 BM |
104 | } |
105 | return ""; | |
106 | }; | |
107 | } | |
108 | ||
4f9e7902 BM |
109 | # alias for those who don't like objects |
110 | *stat_cando = \&cando; | |
111 | ||
448e77d7 BM |
112 | my %op = ( |
113 | r => sub { cando($_[0], S_IRUSR, 1) }, | |
114 | w => sub { cando($_[0], S_IWUSR, 1) }, | |
115 | x => sub { cando($_[0], S_IXUSR, 1) }, | |
4f9e7902 | 116 | o => sub { $_[0][4] == $> }, |
448e77d7 BM |
117 | |
118 | R => sub { cando($_[0], S_IRUSR, 0) }, | |
119 | W => sub { cando($_[0], S_IWUSR, 0) }, | |
120 | X => sub { cando($_[0], S_IXUSR, 0) }, | |
4f9e7902 | 121 | O => sub { $_[0][4] == $< }, |
448e77d7 BM |
122 | |
123 | e => sub { 1 }, | |
4f9e7902 BM |
124 | z => sub { $_[0][7] == 0 }, |
125 | s => sub { $_[0][7] }, | |
126 | ||
127 | f => sub { S_ISREG ($_[0][2]) }, | |
128 | d => sub { S_ISDIR ($_[0][2]) }, | |
129 | l => sub { S_ISLNK ($_[0][2]) }, | |
130 | p => sub { S_ISFIFO($_[0][2]) }, | |
131 | S => sub { S_ISSOCK($_[0][2]) }, | |
132 | b => sub { S_ISBLK ($_[0][2]) }, | |
133 | c => sub { S_ISCHR ($_[0][2]) }, | |
134 | ||
135 | u => sub { _suid($_[0][2]) }, | |
136 | g => sub { _sgid($_[0][2]) }, | |
137 | k => sub { _svtx($_[0][2]) }, | |
138 | ||
139 | M => sub { ($^T - $_[0][9] ) / 86400 }, | |
140 | C => sub { ($^T - $_[0][10]) / 86400 }, | |
141 | A => sub { ($^T - $_[0][8] ) / 86400 }, | |
448e77d7 BM |
142 | ); |
143 | ||
b7c4737f BM |
144 | use constant HINT_FILETEST_ACCESS => 0x00400000; |
145 | ||
4f9e7902 | 146 | # we need fallback=>1 or stringifying breaks |
448e77d7 BM |
147 | use overload |
148 | fallback => 1, | |
149 | -X => sub { | |
150 | my ($s, $op) = @_; | |
b7c4737f BM |
151 | |
152 | if (index "rwxRWX", $op) { | |
153 | (caller 0)[8] & HINT_FILETEST_ACCESS | |
154 | and warnif("File::stat ignores use filetest 'access'"); | |
155 | ||
156 | $^O eq "VMS" and warnif("File::stat ignores VMS ACLs"); | |
157 | ||
158 | # It would be nice to have a warning about using -l on a | |
159 | # non-lstat, but that would require an extra member in the | |
160 | # object. | |
161 | } | |
4f9e7902 | 162 | |
448e77d7 BM |
163 | if ($op{$op}) { |
164 | return $op{$op}->($_[0]); | |
165 | } | |
166 | else { | |
167 | croak "-$op is not implemented on a File::stat object"; | |
168 | } | |
169 | }; | |
170 | ||
8cc95fdb | 171 | # Class::Struct forbids use of @ISA |
172 | sub import { goto &Exporter::import } | |
173 | ||
ee28235b | 174 | use Class::Struct qw(struct); |
36477c24 | 175 | struct 'File::stat' => [ |
176 | map { $_ => '$' } qw{ | |
177 | dev ino mode nlink uid gid rdev size | |
178 | atime mtime ctime blksize blocks | |
179 | } | |
180 | ]; | |
181 | ||
182 | sub populate (@) { | |
183 | return unless @_; | |
184 | my $stob = new(); | |
185 | @$stob = ( | |
186 | $st_dev, $st_ino, $st_mode, $st_nlink, $st_uid, $st_gid, $st_rdev, | |
187 | $st_size, $st_atime, $st_mtime, $st_ctime, $st_blksize, $st_blocks ) | |
188 | = @_; | |
189 | return $stob; | |
190 | } | |
191 | ||
14d597e2 | 192 | sub lstat ($) { populate(CORE::lstat(shift)) } |
36477c24 | 193 | |
194 | sub stat ($) { | |
195 | my $arg = shift; | |
196 | my $st = populate(CORE::stat $arg); | |
448e77d7 | 197 | return $st if defined $st; |
2f173a71 | 198 | my $fh; |
199 | { | |
200 | local $!; | |
201 | no strict 'refs'; | |
202 | require Symbol; | |
83716b1e | 203 | $fh = \*{ Symbol::qualify( $arg, caller() )}; |
2f173a71 | 204 | return unless defined fileno $fh; |
205 | } | |
206 | return populate(CORE::stat $fh); | |
36477c24 | 207 | } |
208 | ||
209 | 1; | |
210 | __END__ | |
211 | ||
212 | =head1 NAME | |
213 | ||
2ae324a7 | 214 | File::stat - by-name interface to Perl's built-in stat() functions |
36477c24 | 215 | |
216 | =head1 SYNOPSIS | |
217 | ||
218 | use File::stat; | |
219 | $st = stat($file) or die "No $file: $!"; | |
220 | if ( ($st->mode & 0111) && $st->nlink > 1) ) { | |
221 | print "$file is executable with lotsa links\n"; | |
222 | } | |
223 | ||
224 | use File::stat qw(:FIELDS); | |
225 | stat($file) or die "No $file: $!"; | |
592b6146 | 226 | if ( ($st_mode & 0111) && ($st_nlink > 1) ) { |
36477c24 | 227 | print "$file is executable with lotsa links\n"; |
228 | } | |
229 | ||
230 | =head1 DESCRIPTION | |
231 | ||
232 | This module's default exports override the core stat() | |
233 | and lstat() functions, replacing them with versions that return | |
234 | "File::stat" objects. This object has methods that | |
235 | return the similarly named structure field name from the | |
236 | stat(2) function; namely, | |
237 | dev, | |
238 | ino, | |
239 | mode, | |
240 | nlink, | |
241 | uid, | |
242 | gid, | |
243 | rdev, | |
244 | size, | |
245 | atime, | |
246 | mtime, | |
247 | ctime, | |
248 | blksize, | |
249 | and | |
250 | blocks. | |
251 | ||
252 | You may also import all the structure fields directly into your namespace | |
253 | as regular variables using the :FIELDS import tag. (Note that this still | |
254 | overrides your stat() and lstat() functions.) Access these fields as | |
255 | variables named with a preceding C<st_> in front their method names. | |
256 | Thus, C<$stat_obj-E<gt>dev()> corresponds to $st_dev if you import | |
257 | the fields. | |
258 | ||
259 | To access this functionality without the core overrides, | |
260 | pass the C<use> an empty import list, and then access | |
261 | function functions with their full qualified names. | |
262 | On the other hand, the built-ins are still available | |
263 | via the C<CORE::> pseudo-package. | |
264 | ||
5c4d9947 JH |
265 | =head1 BUGS |
266 | ||
267 | As of Perl 5.8.0 after using this module you cannot use the implicit | |
268 | C<$_> or the special filehandle C<_> with stat() or lstat(), trying | |
269 | to do so leads into strange errors. The workaround is for C<$_> to | |
270 | be explicit | |
271 | ||
272 | my $stat_obj = stat $_; | |
273 | ||
274 | and for C<_> to explicitly populate the object using the unexported | |
275 | and undocumented populate() function with CORE::stat(): | |
276 | ||
277 | my $stat_obj = File::stat::populate(CORE::stat(_)); | |
278 | ||
36477c24 | 279 | =head1 NOTE |
280 | ||
8cc95fdb | 281 | While this class is currently implemented using the Class::Struct |
36477c24 | 282 | module to build a struct-like class, you shouldn't rely upon this. |
283 | ||
284 | =head1 AUTHOR | |
285 | ||
286 | Tom Christiansen |