| 1 | # Copyright (c) 2014, cPanel, Inc. | ||||||||||
| 2 | # All rights reserved. | ||||||||||
| 3 | # http://cpanel.net/ | ||||||||||
| 4 | # | ||||||||||
| 5 | # This is free software; you can redistribute it and/or modify it under the same | ||||||||||
| 6 | # terms as Perl itself. See the LICENSE file for further details. | ||||||||||
| 7 | |||||||||||
| 8 | package Filesys::POSIX::Userland::Test; | ||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use Filesys::POSIX::Bits; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use Filesys::POSIX::Module (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 1 | my @METHODS = qw( | |||||||||
| 17 | exists is_file is_dir is_link is_char is_block is_fifo | ||||||||||
| 18 | is_readable is_writable is_executable | ||||||||||
| 19 | is_setuid is_setgid | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | 1 | Filesys::POSIX::Module->export_methods( __PACKAGE__, @METHODS ); | |||||||||
| 23 | |||||||||||
| 24 | =head1 NAME | ||||||||||
| 25 | |||||||||||
| 26 | Filesys::POSIX::Userland::Test - Inode conditional tests | ||||||||||
| 27 | |||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||
| 29 | |||||||||||
| 30 | use Filesys::POSIX; | ||||||||||
| 31 | use Filesys::POSIX::Mem; | ||||||||||
| 32 | use Filesys::POSIX::Userland::Test; | ||||||||||
| 33 | |||||||||||
| 34 | my $fs = Filesys::POSIX->new(Filesys::POSIX::Real->new, | ||||||||||
| 35 | 'noatime' => 1 | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | $fs->touch('foo'); | ||||||||||
| 39 | $fs->is_file('foo'); # returns 1 | ||||||||||
| 40 | $fs->is_dir('foo'); # returns 0 | ||||||||||
| 41 | |||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||
| 43 | |||||||||||
| 44 | This runtime addon module provides a series of boolean tests in the vein of | ||||||||||
| 45 | L<test(1)> that allow introspection of the nature of files without explicitly | ||||||||||
| 46 | having to write boilerplate wrappers around C<$fs-E<gt>stat>. | ||||||||||
| 47 | |||||||||||
| 48 | This module exposes the inode-level tests in the L<Filesys::POSIX::Inode> base | ||||||||||
| 49 | class at a higher, file-oriented level. | ||||||||||
| 50 | |||||||||||
| 51 | =head1 TESTS | ||||||||||
| 52 | |||||||||||
| 53 | =over | ||||||||||
| 54 | |||||||||||
| 55 | =item C<$FS-E<gt>exists($path)> | ||||||||||
| 56 | |||||||||||
| 57 | Returns true if an inode indicated by C<$path> exists. | ||||||||||
| 58 | |||||||||||
| 59 | =cut | ||||||||||
| 60 | |||||||||||
| 61 | 13 | ▶ | sub exists { | ||||||||
|
Subroutine
exists: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 62 | 13 | my ( $self, $path ) = @_; | |||||||||
| 63 | |||||||||||
| 64 | 13 | my $inode = eval { $self->stat($path) }; | |||||||||
| 65 | |||||||||||
| 66 | 13 | ▶ | return 0 unless $inode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 12 | return 1; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | =item C<$fs-E<gt>is_file($path)> | ||||||||||
| 71 | |||||||||||
| 72 | Returns true if an inode indicated by C<$path> exists and is a regular file | ||||||||||
| 73 | (C<$S_IFREG>). | ||||||||||
| 74 | |||||||||||
| 75 | =cut | ||||||||||
| 76 | |||||||||||
| 77 | 6 | ▶ | sub is_file { | ||||||||
|
Subroutine
is_file: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 78 | 6 | my ( $self, $path ) = @_; | |||||||||
| 79 | |||||||||||
| 80 | 6 | my $inode = eval { $self->stat($path) }; | |||||||||
| 81 | |||||||||||
| 82 | 6 | ▶ | return 0 unless $inode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 5 | ▶ | return 0 unless $inode->file; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 1 | return 1; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | =item C<$fs-E<gt>is_dir($path)> | ||||||||||
| 88 | |||||||||||
| 89 | Returns true if an inode indicated by C<$path> exists and is a directory | ||||||||||
| 90 | (C<$S_IFDIR>). | ||||||||||
| 91 | |||||||||||
| 92 | =cut | ||||||||||
| 93 | |||||||||||
| 94 | 6 | ▶ | sub is_dir { | ||||||||
|
Subroutine
is_dir: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 95 | 6 | my ( $self, $path ) = @_; | |||||||||
| 96 | |||||||||||
| 97 | 6 | my $inode = eval { $self->stat($path) }; | |||||||||
| 98 | |||||||||||
| 99 | 6 | ▶ | return 0 unless $inode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 5 | ▶ | return 0 unless $inode->dir; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 1 | return 1; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | =item C<$fs-E<gt>is_link($path)> | ||||||||||
| 105 | |||||||||||
| 106 | Returns true if an inode indicated by C<$path> exists and is a symlink | ||||||||||
| 107 | (C<$S_IFLNK>). | ||||||||||
| 108 | |||||||||||
| 109 | =cut | ||||||||||
| 110 | |||||||||||
| 111 | 7 | ▶ | sub is_link { | ||||||||
|
Subroutine
is_link: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 112 | 7 | my ( $self, $path ) = @_; | |||||||||
| 113 | |||||||||||
| 114 | 7 | my $inode = eval { $self->lstat($path) }; | |||||||||
| 115 | |||||||||||
| 116 | 7 | ▶ | return 0 unless $inode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 6 | ▶ | return 0 unless $inode->link; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 1 | return 1; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | =item C<$fs-E<gt>is_char($path)> | ||||||||||
| 122 | |||||||||||
| 123 | Returns true if an inode indicated by C<$path> exists and is a character device | ||||||||||
| 124 | (C<$S_IFCHR>). | ||||||||||
| 125 | |||||||||||
| 126 | =cut | ||||||||||
| 127 | |||||||||||
| 128 | 6 | ▶ | sub is_char { | ||||||||
|
Subroutine
is_char: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 129 | 6 | my ( $self, $path ) = @_; | |||||||||
| 130 | |||||||||||
| 131 | 6 | my $inode = eval { $self->stat($path) }; | |||||||||
| 132 | |||||||||||
| 133 | 6 | ▶ | return 0 unless $inode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 5 | ▶ | return 0 unless $inode->char; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 1 | return 1; | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | =item C<$fs-E<gt>is_block($path)> | ||||||||||
| 139 | |||||||||||
| 140 | Returns true if an inode indicated by C<$path> exists and is a block device | ||||||||||
| 141 | (C<$S_IFBLK>). | ||||||||||
| 142 | |||||||||||
| 143 | =cut | ||||||||||
| 144 | |||||||||||
| 145 | 6 | ▶ | sub is_block { | ||||||||
|
Subroutine
is_block: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 146 | 6 | my ( $self, $path ) = @_; | |||||||||
| 147 | |||||||||||
| 148 | 6 | my $inode = eval { $self->stat($path) }; | |||||||||
| 149 | |||||||||||
| 150 | 6 | ▶ | return 0 unless $inode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | 5 | ▶ | return 0 unless $inode->block; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 1 | return 1; | |||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | =item C<$fs-E<gt>is_fifo($path)> | ||||||||||
| 156 | |||||||||||
| 157 | Returns true if an inode indicated by C<$path> exists and is a FIFO | ||||||||||
| 158 | (C<$S_IFIFO>). | ||||||||||
| 159 | |||||||||||
| 160 | =cut | ||||||||||
| 161 | |||||||||||
| 162 | 6 | ▶ | sub is_fifo { | ||||||||
|
Subroutine
is_fifo: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 163 | 6 | my ( $self, $path ) = @_; | |||||||||
| 164 | |||||||||||
| 165 | 6 | my $inode = eval { $self->stat($path) }; | |||||||||
| 166 | |||||||||||
| 167 | 6 | ▶ | return 0 unless $inode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | 5 | ▶ | return 0 unless $inode->fifo; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 169 | 1 | return 1; | |||||||||
| 170 | } | ||||||||||
| 171 | |||||||||||
| 172 | =item C<$fs-E<gt>is_readable($path)> | ||||||||||
| 173 | |||||||||||
| 174 | Returns true if an inode indicated by C<$path> exists and has a readable bit set | ||||||||||
| 175 | in the inode mode permissions field (C<$S_IRUSR | $S_IRGRP | $S_IROTH>). | ||||||||||
| 176 | |||||||||||
| 177 | =cut | ||||||||||
| 178 | |||||||||||
| 179 | 6 | ▶ | sub is_readable { | ||||||||
|
Subroutine
is_readable: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 180 | 6 | my ( $self, $path ) = @_; | |||||||||
| 181 | |||||||||||
| 182 | 6 | my $inode = eval { $self->stat($path) }; | |||||||||
| 183 | |||||||||||
| 184 | 6 | ▶ | return 0 unless $inode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 185 | 5 | ▶ | return 0 unless $inode->readable; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 186 | 1 | return 1; | |||||||||
| 187 | } | ||||||||||
| 188 | |||||||||||
| 189 | =item C<$fs-E<gt>is_writable($path)> | ||||||||||
| 190 | |||||||||||
| 191 | Returns true if an inode indicated by C<$path> exists and has a writable bit set | ||||||||||
| 192 | in the inode mode permissions field (C<$S_IWUSR | $S_IWGRP | $S_IWOTH>). | ||||||||||
| 193 | |||||||||||
| 194 | =cut | ||||||||||
| 195 | |||||||||||
| 196 | 6 | ▶ | sub is_writable { | ||||||||
|
Subroutine
is_writable: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 197 | 6 | my ( $self, $path ) = @_; | |||||||||
| 198 | |||||||||||
| 199 | 6 | my $inode = eval { $self->stat($path) }; | |||||||||
| 200 | |||||||||||
| 201 | 6 | ▶ | return 0 unless $inode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 202 | 5 | ▶ | return 0 unless $inode->writable; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | 1 | return 1; | |||||||||
| 204 | } | ||||||||||
| 205 | |||||||||||
| 206 | =item C<$fs-E<gt>is_executable($path)> | ||||||||||
| 207 | |||||||||||
| 208 | Returns true if an inode indicated by C<$path> exists and has an executable bit | ||||||||||
| 209 | set in the inode mode permissions field (C<$S_IXUSR | $S_IXGRP | $S_IXOTH>). | ||||||||||
| 210 | |||||||||||
| 211 | =cut | ||||||||||
| 212 | |||||||||||
| 213 | 6 | ▶ | sub is_executable { | ||||||||
|
Subroutine
is_executable: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 214 | 6 | my ( $self, $path ) = @_; | |||||||||
| 215 | |||||||||||
| 216 | 6 | my $inode = eval { $self->stat($path) }; | |||||||||
| 217 | |||||||||||
| 218 | 6 | ▶ | return 0 unless $inode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 219 | 5 | ▶ | return 0 unless $inode->executable; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 220 | 1 | return 1; | |||||||||
| 221 | } | ||||||||||
| 222 | |||||||||||
| 223 | =item C<$fs-E<gt>is_setuid($path)> | ||||||||||
| 224 | |||||||||||
| 225 | Returns true if an inode indicated by C<$path> exists and has a setuid bit | ||||||||||
| 226 | set in the inode mode permissions field (C<$S_SUID>). | ||||||||||
| 227 | |||||||||||
| 228 | =cut | ||||||||||
| 229 | |||||||||||
| 230 | 6 | ▶ | sub is_setuid { | ||||||||
|
Subroutine
is_setuid: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 231 | 6 | my ( $self, $path ) = @_; | |||||||||
| 232 | |||||||||||
| 233 | 6 | my $inode = eval { $self->stat($path) }; | |||||||||
| 234 | |||||||||||
| 235 | 6 | ▶ | return 0 unless $inode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 236 | 5 | ▶ | return 0 unless $inode->setuid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 237 | 1 | return 1; | |||||||||
| 238 | } | ||||||||||
| 239 | |||||||||||
| 240 | =item C<$fs-E<gt>is_setgid($path)> | ||||||||||
| 241 | |||||||||||
| 242 | Returns true if an inode indicated by C<$path> exists and has a setgid bit | ||||||||||
| 243 | set in the inode permissions field (C<$S_SGID>). | ||||||||||
| 244 | |||||||||||
| 245 | =cut | ||||||||||
| 246 | |||||||||||
| 247 | 6 | ▶ | sub is_setgid { | ||||||||
|
Subroutine
is_setgid: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 248 | 6 | my ( $self, $path ) = @_; | |||||||||
| 249 | |||||||||||
| 250 | 6 | my $inode = eval { $self->stat($path) }; | |||||||||
| 251 | |||||||||||
| 252 | 6 | ▶ | return 0 unless $inode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 253 | 5 | ▶ | return 0 unless $inode->setgid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 254 | 1 | return 1; | |||||||||
| 255 | } | ||||||||||
| 256 | |||||||||||
| 257 | =back | ||||||||||
| 258 | |||||||||||
| 259 | =cut | ||||||||||
| 260 | |||||||||||
| 261 | =head1 SEE ALSO | ||||||||||
| 262 | |||||||||||
| 263 | =over | ||||||||||
| 264 | |||||||||||
| 265 | =item L<Filesys::POSIX::Inode> | ||||||||||
| 266 | |||||||||||
| 267 | =item L<Filesys::POSIX::Bits> | ||||||||||
| 268 | |||||||||||
| 269 | =back | ||||||||||
| 270 | |||||||||||
| 271 | =cut | ||||||||||
| 272 | |||||||||||
| 273 | 1 | 1; | |||||||||
| 274 | |||||||||||
| 275 | __END__ | ||||||||||