| 1 | package File::Permissions::Unix; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use base 'Error::Helper'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | File::Permissions::Unix - A simple object oriented interface to handling file permissions. | ||||||||||
| 10 | |||||||||||
| 11 | =head1 VERSION | ||||||||||
| 12 | |||||||||||
| 13 | Version 0.1.0 | ||||||||||
| 14 | |||||||||||
| 15 | =cut | ||||||||||
| 16 | |||||||||||
| 17 | 2 | our $VERSION = '0.1.0'; | |||||||||
| 18 | |||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||
| 20 | |||||||||||
| 21 | use File::Permissions::Unix; | ||||||||||
| 22 | |||||||||||
| 23 | my $foo=File::Permissions::Unix->new('0640'); | ||||||||||
| 24 | |||||||||||
| 25 | #chmods a /tmp/foo with 0640 | ||||||||||
| 26 | $foo->chmod('/tmp/foo'); | ||||||||||
| 27 | |||||||||||
| 28 | #do the same thing as above, but check if it worked | ||||||||||
| 29 | $foo->chmod('/tmp/foo'); | ||||||||||
| 30 | if( $foo->error ){ | ||||||||||
| 31 | warn('error:'.$foo->error.': '.$foo->errorString); | ||||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | #copies the mode from /tmp/foo to /tmp/bar | ||||||||||
| 35 | $foo->setModeFromFile('/tmp/foo'); | ||||||||||
| 36 | $foo->chmod('/tmp/bar'); | ||||||||||
| 37 | |||||||||||
| 38 | #prints the current mode | ||||||||||
| 39 | print $foo->getMode."\n"; | ||||||||||
| 40 | |||||||||||
| 41 | =head1 METHODS | ||||||||||
| 42 | |||||||||||
| 43 | =head2 new | ||||||||||
| 44 | |||||||||||
| 45 | This initiates the object. | ||||||||||
| 46 | |||||||||||
| 47 | One arguement is accepted. It is the mode | ||||||||||
| 48 | to intialize the object with. If not specified | ||||||||||
| 49 | it defaults to '0644'. | ||||||||||
| 50 | |||||||||||
| 51 | my $foo=File::Permissions::Unix->new($mode); | ||||||||||
| 52 | if( $foo->error ){ | ||||||||||
| 53 | warn('error:'.$foo->error.': '.$foo->errorString); | ||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | =cut | ||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub new{ | ||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 59 | 0 | my $mode=$_[1]; | |||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | if ( ! defined( $mode ) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 0 | $mode='0644'; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | my $self={ | |||||||||
| 66 | mode=>$mode, | ||||||||||
| 67 | perror=>undef, | ||||||||||
| 68 | error=>undef, | ||||||||||
| 69 | errorString=>'', | ||||||||||
| 70 | }; | ||||||||||
| 71 | 0 | bless $self; | |||||||||
| 72 | |||||||||||
| 73 | # make sure it is a valid mode | ||||||||||
| 74 | 0 | ▶ | if ( $self->{mode} !~ /^[01246][01234567][01234567][01234567]$/ ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 0 | $self->{error}=1; | |||||||||
| 76 | 0 | $self->{perror}=1; | |||||||||
| 77 | 0 | $self->{errorString}=''; | |||||||||
| 78 | 0 | return $self; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 0 | return $self; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | =head2 chmod | ||||||||||
| 85 | |||||||||||
| 86 | This chmods a file with the current mode. | ||||||||||
| 87 | |||||||||||
| 88 | One argument is required and it the file/directory/etc in question. | ||||||||||
| 89 | |||||||||||
| 90 | $foo->chmod($file); | ||||||||||
| 91 | if( $foo->error ){ | ||||||||||
| 92 | warn('error:'.$foo->error.': '.$foo->errorString); | ||||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | =cut | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | sub chmod{ | ||||||||
|
Subroutine
chmod: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 98 | 0 | my $self=$_[0]; | |||||||||
| 99 | 0 | my $file=$_[1]; | |||||||||
| 100 | |||||||||||
| 101 | 0 | $self->errorblank; | |||||||||
| 102 | 0 | ▶ | if ( $self->error ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | return undef; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | #make sure the file is defined | ||||||||||
| 107 | 0 | ▶ | if( ! defined( $file ) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 0 | $self->{error}=2; | |||||||||
| 109 | 0 | $self->{errorString}='No file specified'; | |||||||||
| 110 | 0 | return undef; | |||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | #try to chmod the file | ||||||||||
| 114 | 0 | ▶ | if( ! chmod( oct($self->{mode}), $file )){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | $self->{error}=4; | |||||||||
| 116 | 0 | $self->{errorString}='Unable to chmod "'.$file.'" with "'.$self->{mode}.'"'; | |||||||||
| 117 | 0 | return undef; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 0 | return 1; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | =head2 getMode | ||||||||||
| 124 | |||||||||||
| 125 | This returns the current mode. | ||||||||||
| 126 | |||||||||||
| 127 | my $mode=$foo->getMode; | ||||||||||
| 128 | |||||||||||
| 129 | =cut | ||||||||||
| 130 | |||||||||||
| 131 | 0 | ▶ | sub getMode{ | ||||||||
|
Subroutine
getMode: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 132 | 0 | my $self=$_[0]; | |||||||||
| 133 | |||||||||||
| 134 | 0 | $self->errorblank; | |||||||||
| 135 | 0 | ▶ | if ( $self->error ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | return undef; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 0 | return $self->{mode}; | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | =head2 setMode | ||||||||||
| 143 | |||||||||||
| 144 | This changes the currently set mode. | ||||||||||
| 145 | |||||||||||
| 146 | One argument is accepted and it is the current mode. | ||||||||||
| 147 | |||||||||||
| 148 | $foo->setMode('0640')'; | ||||||||||
| 149 | if($foo->error){ | ||||||||||
| 150 | warn('error:'.$foo->error.': '.$foo->errorString); | ||||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | =cut | ||||||||||
| 154 | |||||||||||
| 155 | 0 | ▶ | sub setMode{ | ||||||||
|
Subroutine
setMode: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 156 | 0 | my $self=$_[0]; | |||||||||
| 157 | 0 | my $mode=$_[1]; | |||||||||
| 158 | |||||||||||
| 159 | 0 | $self->errorblank; | |||||||||
| 160 | 0 | ▶ | if ( $self->error ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | 0 | return undef; | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | # make sure it is a valid mode | ||||||||||
| 165 | 0 | ▶ | if ( $mode !~ /^[01246][01234567][01234567][01234567]$/ ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | 0 | $self->{error}=1; | |||||||||
| 167 | 0 | $self->{errorString}='"'.$mode.'" is not a valid mode'; | |||||||||
| 168 | 0 | return $self; | |||||||||
| 169 | } | ||||||||||
| 170 | |||||||||||
| 171 | 0 | $self->{mode}=$mode; | |||||||||
| 172 | |||||||||||
| 173 | 0 | return 1; | |||||||||
| 174 | } | ||||||||||
| 175 | |||||||||||
| 176 | =head2 setModeFromFile | ||||||||||
| 177 | |||||||||||
| 178 | This sets the current mode from a file. | ||||||||||
| 179 | |||||||||||
| 180 | One argument is required and it the file/directory/etc in question. | ||||||||||
| 181 | |||||||||||
| 182 | $foo->setModeFromFile($file); | ||||||||||
| 183 | if( $foo->error ){ | ||||||||||
| 184 | warn('error:'.$foo->error.': '.$foo->errorString); | ||||||||||
| 185 | } | ||||||||||
| 186 | |||||||||||
| 187 | =cut | ||||||||||
| 188 | |||||||||||
| 189 | 0 | ▶ | sub setModeFromFile{ | ||||||||
|
Subroutine
setModeFromFile: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 190 | 0 | my $self=$_[0]; | |||||||||
| 191 | 0 | my $file=$_[1]; | |||||||||
| 192 | |||||||||||
| 193 | 0 | $self->errorblank; | |||||||||
| 194 | 0 | ▶ | if ( $self->error ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 195 | 0 | return undef; | |||||||||
| 196 | } | ||||||||||
| 197 | |||||||||||
| 198 | #make sure the file is defined | ||||||||||
| 199 | 0 | ▶ | if( ! defined( $file ) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 200 | 0 | $self->{error}=2; | |||||||||
| 201 | 0 | $self->{errorString}='No file specified'; | |||||||||
| 202 | 0 | return undef; | |||||||||
| 203 | } | ||||||||||
| 204 | |||||||||||
| 205 | #stat the file and get it | ||||||||||
| 206 | 0 | my $mode = (stat($file))[2] & 07777; | |||||||||
| 207 | 0 | ▶ | if ( !defined( $mode ) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 208 | 0 | $self->{error}=5; | |||||||||
| 209 | 0 | $self->{errorString}='Failed to stat the file "'.$file.'"'; | |||||||||
| 210 | 0 | return $self; | |||||||||
| 211 | } | ||||||||||
| 212 | 0 | $mode=sprintf("%04o", $mode); | |||||||||
| 213 | |||||||||||
| 214 | 0 | $self->{mode}=$mode; | |||||||||
| 215 | |||||||||||
| 216 | 0 | return 1; | |||||||||
| 217 | } | ||||||||||
| 218 | |||||||||||
| 219 | =head1 ERROR CODES | ||||||||||
| 220 | |||||||||||
| 221 | =head2 1 | ||||||||||
| 222 | |||||||||||
| 223 | Invalid mode. | ||||||||||
| 224 | |||||||||||
| 225 | This means it did not match the regexp below. | ||||||||||
| 226 | |||||||||||
| 227 | /^[01246][01234567][01234567][01234567]$/ | ||||||||||
| 228 | |||||||||||
| 229 | =head2 2 | ||||||||||
| 230 | |||||||||||
| 231 | No file specified. | ||||||||||
| 232 | |||||||||||
| 233 | =head2 3 | ||||||||||
| 234 | |||||||||||
| 235 | The file does not exist. | ||||||||||
| 236 | |||||||||||
| 237 | This has been depreciated as it introduces a possible race condition. | ||||||||||
| 238 | |||||||||||
| 239 | =head2 4 | ||||||||||
| 240 | |||||||||||
| 241 | Failed to chmod the file. | ||||||||||
| 242 | |||||||||||
| 243 | =head2 5 | ||||||||||
| 244 | |||||||||||
| 245 | Failed too stat the file. | ||||||||||
| 246 | |||||||||||
| 247 | =head1 AUTHOR | ||||||||||
| 248 | |||||||||||
| 249 | Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >> | ||||||||||
| 250 | |||||||||||
| 251 | =head1 BUGS | ||||||||||
| 252 | |||||||||||
| 253 | Please report any bugs or feature requests to C<bug-file-permissions-unix at rt.cpan.org>, or through | ||||||||||
| 254 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Permissions-Unix>. I will be notified, and then you'll | ||||||||||
| 255 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 256 | |||||||||||
| 257 | |||||||||||
| 258 | |||||||||||
| 259 | |||||||||||
| 260 | =head1 SUPPORT | ||||||||||
| 261 | |||||||||||
| 262 | You can find documentation for this module with the perldoc command. | ||||||||||
| 263 | |||||||||||
| 264 | perldoc File::Permissions::Unix | ||||||||||
| 265 | |||||||||||
| 266 | |||||||||||
| 267 | You can also look for information at: | ||||||||||
| 268 | |||||||||||
| 269 | =over 4 | ||||||||||
| 270 | |||||||||||
| 271 | =item * RT: CPAN's request tracker | ||||||||||
| 272 | |||||||||||
| 273 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Permissions-Unix> | ||||||||||
| 274 | |||||||||||
| 275 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 276 | |||||||||||
| 277 | L<http://annocpan.org/dist/File-Permissions-Unix> | ||||||||||
| 278 | |||||||||||
| 279 | =item * CPAN Ratings | ||||||||||
| 280 | |||||||||||
| 281 | L<http://cpanratings.perl.org/d/File-Permissions-Unix> | ||||||||||
| 282 | |||||||||||
| 283 | =item * Search CPAN | ||||||||||
| 284 | |||||||||||
| 285 | L<http://search.cpan.org/dist/File-Permissions-Unix/> | ||||||||||
| 286 | |||||||||||
| 287 | =back | ||||||||||
| 288 | |||||||||||
| 289 | |||||||||||
| 290 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 291 | |||||||||||
| 292 | |||||||||||
| 293 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 294 | |||||||||||
| 295 | Copyright 2011 Zane C. Bowers-Hadley. | ||||||||||
| 296 | |||||||||||
| 297 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 298 | under the terms of either: the GNU General Public License as published | ||||||||||
| 299 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 300 | |||||||||||
| 301 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 302 | |||||||||||
| 303 | |||||||||||
| 304 | =cut | ||||||||||
| 305 | |||||||||||
| 306 | 2 | 1; # End of File::Permissions::Unix | |||||||||