line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::Permissions::Unix; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
20544
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
5
|
1
|
|
|
1
|
|
4
|
use base 'Error::Helper'; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
753
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub new{ |
59
|
0
|
|
|
0
|
1
|
|
my $mode=$_[1]; |
60
|
|
|
|
|
|
|
|
61
|
0
|
0
|
|
|
|
|
if ( ! defined( $mode ) ){ |
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
|
0
|
|
|
|
|
if ( $self->{mode} !~ /^[01246][01234567][01234567][01234567]$/ ){ |
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
|
|
|
|
|
|
|
sub chmod{ |
98
|
0
|
|
|
0
|
1
|
|
my $self=$_[0]; |
99
|
0
|
|
|
|
|
|
my $file=$_[1]; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
$self->errorblank; |
102
|
0
|
0
|
|
|
|
|
if ( $self->error ){ |
103
|
0
|
|
|
|
|
|
return undef; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
#make sure the file is defined |
107
|
0
|
0
|
|
|
|
|
if( ! defined( $file ) ){ |
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
|
0
|
|
|
|
|
if( ! chmod( oct($self->{mode}), $file )){ |
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
|
|
|
|
|
|
|
sub getMode{ |
132
|
0
|
|
|
0
|
1
|
|
my $self=$_[0]; |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
$self->errorblank; |
135
|
0
|
0
|
|
|
|
|
if ( $self->error ){ |
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
|
|
|
|
|
|
|
sub setMode{ |
156
|
0
|
|
|
0
|
1
|
|
my $self=$_[0]; |
157
|
0
|
|
|
|
|
|
my $mode=$_[1]; |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
$self->errorblank; |
160
|
0
|
0
|
|
|
|
|
if ( $self->error ){ |
161
|
0
|
|
|
|
|
|
return undef; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# make sure it is a valid mode |
165
|
0
|
0
|
|
|
|
|
if ( $mode !~ /^[01246][01234567][01234567][01234567]$/ ){ |
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
|
|
|
|
|
|
|
sub setModeFromFile{ |
190
|
0
|
|
|
0
|
1
|
|
my $self=$_[0]; |
191
|
0
|
|
|
|
|
|
my $file=$_[1]; |
192
|
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
|
$self->errorblank; |
194
|
0
|
0
|
|
|
|
|
if ( $self->error ){ |
195
|
0
|
|
|
|
|
|
return undef; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
#make sure the file is defined |
199
|
0
|
0
|
|
|
|
|
if( ! defined( $file ) ){ |
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
|
0
|
|
|
|
|
if ( !defined( $mode ) ){ |
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<< >> |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=head1 BUGS |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
254
|
|
|
|
|
|
|
the web interface at L. 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 |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
L |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=item * CPAN Ratings |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
L |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=item * Search CPAN |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
L |
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
|
|
|
|
|
|
|
1; # End of File::Permissions::Unix |