Manage/Perms.pm

filter: Statementstmt 4.0% 17 / 423 Branchbran 0.0% 0 / 172 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 41.6% 5 / 12 Podpod 100% 7 / 7 totaltotal 4.7% 29 / 614 CC 87 SCAR 55.1
CC
87
Cov
3%
setPerms
420.0
downSync
342.0
upSync
306.0
CRAP 246.4
« Manage/Ownership.pm ↑ Summary Manage/Remove.pm »
1 package Sys::Config::Manage::Perms;
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 File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use String::ShellQuote;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Sys::Config::Manage::Perms - Handles file permissions for files in a configuration directory.
12  
13 =head1 VERSION
14  
15 Version 0.0.0
16  
17 =cut
18  
19 2 our $VERSION = '0.0.0';
20  
21  
22 =head1 SYNOPSIS
23  
24 use Sys::Config::Manage::Perms;
25  
26 my $foo = Sys::Config::Manage::Perms->new();
27 ...
28  
29 =head1 METHODS
30  
31 =head2 new
32  
33 This initiates the object.
34  
35 One argument is required and it is a hash reference.
36  
37 =head3 args hash ref
38  
39 =head4 scm
40  
41 This is a initiated Sys::Config::Manage object.
42  
43 =head4 default
44  
45 Default value to use for the mode.
46  
47 If not specified, it defaults to '0644'.
48  
49 $foo=Sys::Config::Manage::Perms->new(\%args);
50 if($foo->error){
51 warn('error:'.$foo->error.': '.$foo->errorString);
52 }
53  
54 =cut
55  
56 0 ▶sub new{
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
57 0 my %args;
58 0 ▶ if(defined($_[1])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
59 0 %args= %{$_[1]};
60 };
61 0 my $method='new';
62  
63 0 my $self = {
64 module=>'Sys-Config-Manage-Perms',
65 perror=>undef,
66 error=>undef,
67 errorString=>"",
68 default=>'0644',
69 };
70 0 bless $self;
71  
72 #make sure we have a Sys::Config::Manage
73 0 ▶ if(!defined( $args{scm} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"scm"}) 0 0 0
74 0 $self->{perror}=1;
75 0 $self->{error}=1;
76 0 $self->{errorString}='Nothing passed for the Sys::Config::Manage object';
77 0 $self->warn;
78 0 return $self;
79 }
80  
81 #make sure that it is really a Sys::Config::Manage object
82 0 ▶ if( ref( $args{scm} ) ne "Sys::Config::Manage" ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"scm"} ne "Sys::Config::Manage") 0 0 0
83 0 $self->{perror}=1;
84 0 $self->{error}=2;
85 0 $self->{errorString}='$args{scm} is not a Sys::Config::Manage object';
86 0 $self->warn;
87 0 return $self;
88 }
89  
90 #figures out what the defualt is
91 0 ▶ if ( defined( $args{default} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"default"}) 0 0 0
92 #make sure the perms are sane
93 0 ▶ if ( $args{default} !~ /^[01246][01234567][01234567][01234567]$/ ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"default"} =~ /^[01246][01234567][01234567][01234567]$/) 0 0 0
94 0 $self->{perror}=1;
95 0 $self->{error}=12;
96 0 $self->{errorString}='"'.$args{default}.'" does not appear to be a valid value';
97 0 $self->warn;
98 0 return $self;
99 }
100 0 $self->{default}=$args{default};
101 }
102  
103 0 $self->{scm}=$args{scm};
104  
105 0 return $self;
106 }
107  
108 =head2 downSync
109  
110 This syncs the file permissions down from the configuration
111 directory to the system.
112  
113 Two arguments can be used.
114  
115 The first is the configuration directory. If not specified, it will
116 be automaticallly choosen.
117  
118 The second is the files to sync. If not specifiedm, all files will
119 be synced.
120  
121 #sync the specified files
122 $foo->downSync( $configDir, \@files);
123 if($foo->error){
124 warn('error:'.$foo->error.': '.$foo->errorString);
125 }
126  
127 #syncs all the files
128 $foo->downSync( $configDir );
129 if($foo->error){
130 warn('error:'.$foo->error.': '.$foo->errorString);
131 }
132  
133 =cut
134  
135 0 ▶sub downSync{
Subroutine downSync: not called CC=18 SCAR=58.3
Pod documented
136 0 my $self=$_[0];
137 0 my $configDir=$_[1];
138 0 my @files;
139 0 ▶ if (defined($_[2])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[2]) 0 0 0
140 0 @files=@{$_[2]};
141 }
142 0 my $method='downSync';
143  
144 #blank any previous errors
145 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
146 0 return undef;
147 }
148
149 #make sure we have a directory to use
150 0 ▶ if (!defined($configDir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $configDir) 0 0 0
151 0 $configDir=$self->{scm}->selectConfigDir;
152 0 ▶ if ($self->{scm}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
153 0 $self->{error}=5;
154 $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
155 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
156 0 $self->warn;
157 0 return undef;
158 }
159 }
160
161 #make sure the config directory is valid
162 0 my $valid=$self->{scm}->validConfigDirName($configDir);
163 0 ▶ if ($self->{scm}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
164 0 $self->{error}=6;
165 $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
166 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
167 0 $self->warn;
168 0 return undef;
169 }
170 0 ▶ if (defined( $valid )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid) 0 0 0
171 0 $self->{error}=3;
172 0 $self->{errorString}='The configuration directory name '.$valid;
173 0 $self->warn;
174 0 return undef;
175 }
176
177 #makes sure it exists
178 0 ▶ if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"scm"}{"baseDir"} . '/' . $configDir) 0 0 0
179 0 $self->{error}=4;
180 0 $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
181 0 $self->warn;
182 0 return undef;
183 }
184  
185 #checks and make sure all the files exist
186 0 my $int=0;
187 0 ▶ if(defined( $files[$int] )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files[$int]) { } 0 0 0
188 0 my @allfiles=$self->{scm}->listConfigFiles($configDir);
189 0 ▶ if( $self->{scm}->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
190 0 $self->{error}='16';
191 $self->{errorString}='Sys::Config::Manage->listConfigFiles errored error="'.
192 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
193 0 $self->warn;
194 0 return undef;
195 }
196
197 #make sure each file asked to be synced is tracked
198 0 ▶ while(defined( $files[$int] )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files[$int]) 0 0 0
199  
200 0 my $matched=0;
201 0 my $int2=0;
202 0 ▶ while(defined( $allfiles[$int2] )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $allfiles[$int2]) 0 0 0
203 0 ▶ if( $files[$int] eq $allfiles[$int2] ){
Branch: 1 branch
BranchTrueFalseTotal
if ($files[$int] eq $allfiles[$int2]) 0 0 0
204 0 $matched=1;
205 }
206  
207 0 $int2++;
208 }
209  
210 0 ▶ if(! $matched){
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 0 0 0
211 0 $self->{error}='8';
212 0 $self->{errorString}='"'.$files[$int].'" does not exist under the configuration directory, "'.$configDir.'",';
213 0 $self->warn;
214 0 return undef;
215 }
216  
217 0 $int++;
218 }
219  
220 }else{
221 #if we get here, no files have been specified so we do them all
222 0 @files=$self->{scm}->listConfigFiles($configDir);
223 0 ▶ if( $self->{scm}->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
224 0 $self->{error}='16';
225 $self->{errorString}='Sys::Config::Manage->listConfigFiles errored error="'.
226 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
227 0 $self->warn;
228 0 return undef;
229 }
230 }
231  
232 #process each file
233 0 $int=0;
234 0 ▶ while( defined( $files[$int] ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files[$int]) 0 0 0
235 #get the perms for the file we will set it on
236 0 my $perms=$self->getPerms( $configDir, $files[$int] );
237 0 ▶ if( $self->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error) 0 0 0
238 0 warn($self->{module}.' '.$method.': Sys::Config::Manage::Perms->getPerms errored');
239 0 return undef;
240 }
241  
242 #try to chmod it
243 0 ▶ if(!chmod( oct($perms), $files[$int] )){
Branch: 1 branch
BranchTrueFalseTotal
unless (chmod oct $perms, $files[$int]) 0 0 0
244 0 $self->{error}='17';
245 0 $self->{errorString}='chmod( '.$perms.', "'.$files[$int].'") errored';
246 0 $self->warn;
247 0 return undef;
248 }
249
250 0 $int++;
251 }
252  
253 0 return 1;
254 }
255  
256 =head2 getPerms
257  
258 This retrieves the mode for a file.
259  
260 Two arguments are taken.The first is the configuration directory,
261 which if not defined is automatically chosen. The second is the
262 file in question.
263  
264 my $mode=$foo->getPerms( $configDir, $file );
265 if($foo->error){
266 warn('error:'.$foo->error.': '.$foo->errorString);
267 }
268  
269 =cut
270  
271 0 ▶sub getPerms{
Subroutine getPerms: not called CC=11 SCAR=48.8
Pod documented
272 0 my $self=$_[0];
273 0 my $configDir=$_[1];
274 0 my $file=$_[2];
275 0 my $method='getPerms';
276  
277 #blank any previous errors
278 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
279 0 return undef;
280 }
281  
282 #make sure we have a directory to use
283 0 ▶ if (!defined($configDir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $configDir) 0 0 0
284 0 $configDir=$self->{scm}->selectConfigDir;
285 0 ▶ if ($self->{scm}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
286 0 $self->{error}=5;
287 $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
288 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
289 0 $self->warn;
290 0 return undef;
291 }
292 }
293
294 #make sure the config directory is valid
295 0 my $valid=$self->{scm}->validConfigDirName($configDir);
296 0 ▶ if ($self->{scm}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
297 0 $self->{error}=6;
298 $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
299 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
300 0 $self->warn;
301 0 return undef;
302 }
303 0 ▶ if (defined( $valid )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid) 0 0 0
304 0 $self->{error}=3;
305 0 $self->{errorString}='The configuration directory name '.$valid;
306 0 $self->warn;
307 0 return undef;
308 }
309
310 #makes sure it exists
311 0 ▶ if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"scm"}{"baseDir"} . '/' . $configDir) 0 0 0
312 0 $self->{error}=4;
313 0 $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
314 0 $self->warn;
315 0 return undef;
316 }
317  
318 #make value for the file is specified
319 0 ▶ if (!defined( $file )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 0 0
320 0 $self->{error}=7;
321 0 $self->{errorString}='No file specified';
322 0 $self->warn;
323 0 return undef;
324 }
325
326 #make sure the file exists, under the config dir
327 0 ▶ if (! -f $self->{scm}->{baseDir}.'/'.$configDir.'/'.$file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->{"scm"}{"baseDir"} . '/' . $configDir . '/' . $file) 0 0 0
328 0 $self->{error}=8;
329 0 $self->{errorString}='The file does not exist in the configuration directory';
330 0 $self->warn;
331 0 return undef;
332 }
333  
334 #figure out what the perms file is
335 0 my ($name,$path,$suffix) = fileparse($file);
336 0 my $permsfile=$self->{scm}->{baseDir}.'/'.$configDir.'/'.$path.'/.SysConfigManage/Perms/'.$name;
337  
338 #make sure the file has some perms
339 0 ▶ if (! -f $permsfile ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $permsfile) 0 0 0
340 0 return $self->{default};
341 }
342  
343 #read the file
344 0 my $fh;
345 0 ▶ if ( ! open( $fh, '<', $permsfile ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $permsfile) 0 0 0
346 0 $self->{error}=15;
347 0 $self->{errorString}='Unable to open "'.$permsfile.'"';
348 0 $self->warn;
349 0 return undef;
350 }
351 0 my $perms=<$fh>;
352 0 chomp($perms);
353 0 close( $fh );
354  
355 0 return $perms;
356 }
357  
358 =head2 listConfigFiles
359  
360 This lists the various config files that are being
361 tracked that actually have a value specified.
362  
363 Not all files returned by Sys::Config::Manage->listConfigFiles
364 will have a value specified.
365  
366 my @files=$foo->listConfigFiles;
367 if($foo->error){
368 warn('error:'.$foo->error.': '.$foo->errorString);
369 }
370  
371 =cut
372  
373 0 ▶sub listConfigFiles{
Subroutine listConfigFiles: not called CC=9 SCAR=45.0
Pod documented
374 0 my $self=$_[0];
375 0 my $configDir=$_[1];
376 0 my $method='listConfigFiles';
377  
378 #blank any previous errors
379 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
380 0 return undef;
381 }
382  
383 #make sure we have a directory to use
384 0 ▶ if (!defined($configDir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $configDir) 0 0 0
385 0 $configDir=$self->{scm}->selectConfigDir;
386 0 ▶ if ($self->{scm}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
387 0 $self->{error}=5;
388 $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
389 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
390 0 $self->warn;
391 0 return undef;
392 }
393 }
394  
395 #make sure the config directory is valid
396 0 my $valid=$self->{scm}->validConfigDirName($configDir);
397 0 ▶ if ($self->{scm}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
398 0 $self->{error}=6;
399 $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
400 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
401 0 $self->warn;
402 0 return undef;
403 }
404 0 ▶ if (defined( $valid )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid) 0 0 0
405 0 $self->{error}=3;
406 0 $self->{errorString}='The configuration directory name '.$valid;
407 0 $self->warn;
408 0 return undef;
409 }
410  
411 #makes sure it exists
412 0 ▶ if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"scm"}{"baseDir"} . '/' . $configDir) 0 0 0
413 0 $self->{error}=4;
414 0 $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
415 0 $self->warn;
416 0 return undef;
417 }
418  
419 #holds what will be returned
420 0 my @found;
421  
422 #get a list of files
423 0 my @files=$self->{scm}->listConfigFiles($configDir);
424  
425
426 #process every found file
427 0 my $int=0;
428 0 ▶ while (defined( $files[$int] )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files[$int]) 0 0 0
429 my ($name,$path,$suffix)=fileparse($self->{scm}->{baseDir}
430 0 .'/'.$configDir.'/'.$files[$int]);
431  
432  
433 0 my $permsFile=$path.'/.SysConfigManage/Perms/'.$name;
434 0 $permsFile=~s/\/\/*/\//g;
435  
436 #make sure the perms file exists and if so add it
437 0 ▶ if ( -f $permsFile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $permsFile) 0 0 0
438 0 push(@found, $files[$int]);
439 }
440  
441 0 $int++;
442 }
443  
444 0 return @found;
445 }
446  
447 =head2 setPerms
448  
449 This sets the permissions for a file. This does require the numeric value.
450  
451 Three arguments are taken.
452  
453 The first one is the configuration directory to use. If none is
454 specified, it will automatically be choosen.
455  
456 The second is the config file to add the perms for.
457  
458 The third numeric value for the permissions.
459  
460 $foo->setPerms($configDir, $file, '0640');
461 if($foo->error){
462 warn('error:'.$foo->error.': '.$foo->errorString);
463 }
464  
465 =cut
466  
467 0 ▶sub setPerms{
Subroutine setPerms: not called CC=20 SCAR=60.4
Pod documented
468 0 my $self=$_[0];
469 0 my $configDir=$_[1];
470 0 my $file=$_[2];
471 0 my $perms=$_[3];
472 0 my $method='setPerms';
473  
474 #blank any previous errors
475 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
476 0 return undef;
477 }
478  
479 #make sure we have a directory to use
480 0 ▶ if (!defined($configDir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $configDir) 0 0 0
481 0 $configDir=$self->{scm}->selectConfigDir;
482 0 ▶ if ($self->{scm}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
483 0 $self->{error}=5;
484 $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
485 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
486 0 $self->warn;
487 0 return undef;
488 }
489 }
490  
491 #make sure the config directory is valid
492 0 my $valid=$self->{scm}->validConfigDirName($configDir);
493 0 ▶ if ($self->{scm}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
494 0 $self->{error}=6;
495 $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
496 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
497 0 $self->warn;
498 0 return undef;
499 }
500 0 ▶ if (defined( $valid )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid) 0 0 0
501 0 $self->{error}=3;
502 0 $self->{errorString}='The configuration directory name '.$valid;
503 0 $self->warn;
504 0 return undef;
505 }
506  
507 #makes sure it exists
508 0 ▶ if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"scm"}{"baseDir"} . '/' . $configDir) 0 0 0
509 0 $self->{error}=4;
510 0 $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
511 0 $self->warn;
512 0 return undef;
513 }
514  
515 #make value for the file is specified
516 0 ▶ if (!defined( $file )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 0 0
517 0 $self->{error}=7;
518 0 $self->{errorString}='No file specified';
519 0 $self->warn;
520 0 return undef;
521 }
522  
523 #make sure the file exists, under the config dir
524 0 ▶ if (! -f $self->{scm}->{baseDir}.'/'.$configDir.'/'.$file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->{"scm"}{"baseDir"} . '/' . $configDir . '/' . $file) 0 0 0
525 0 $self->{error}=8;
526 0 $self->{errorString}='The file does not exist in the configuration directory';
527 0 $self->warn;
528 0 return undef;
529 }
530  
531 #make sure we have a some perms passed
532 0 ▶ if ( ! defined( $perms ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $perms) 0 0 0
533 0 $self->{error}=11;
534 0 $self->{errorString}='No value for the permissions specified';
535 0 $self->warn;
536 0 return undef;
537 }
538  
539 #make sure the perms are sane
540 0 ▶ if ( $perms !~ /^[01246][01234567][01234567][01234567]$/ ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($perms =~ /^[01246][01234567][01234567][01234567]$/) 0 0 0
541 0 $self->{error}=12;
542 0 $self->{errorString}='"'.$perms.'" does not appear to be a valid value';
543 0 warn($self->{module}.' '.$method.':'.$self->error.': '.$self->errorString);
544 0 return undef;
545 }
546  
547 #creates the .SysConfigManage if needed
548 0 my ($name,$path,$suffix) = fileparse($file);
549 0 my $scmd=$self->{scm}->{baseDir}.'/'.$configDir.'/'.$path.'/.SysConfigManage';
550 0 ▶ if ( ! -d $scmd ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $scmd) 0 0 0
551 0 ▶ if ( ! mkdir( $scmd ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $scmd) 0 0 0
552 0 $self->{error}=13;
553 0 $self->{errorString}='Unable to create "'.$scmd.'"';
554 0 $self->warn;
555 0 return undef;
556 }
557 }
558  
559 #makes sure that the perms dir exists
560 0 my $permsdir=$scmd.'/Perms';
561 0 ▶ if ( ! -d $permsdir ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $permsdir) 0 0 0
562 0 ▶ if ( ! mkdir( $permsdir ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $permsdir) 0 0 0
563 0 $self->{error}=14;
564 0 $self->{errorString}='Unable to create "'.$permsdir.'"';
565 0 $self->warn;
566 0 return undef;
567 }
568 }
569  
570 #this is the file that will store the perms
571 0 my $permsfile=$permsdir.'/'.$name;
572  
573 #check if it exists
574 0 my $exists=0;
575 0 ▶ if ( -f $permsfile ){
Branch: 1 branch
BranchTrueFalseTotal
if (-f $permsfile) 0 0 0
576 0 $exists=1;
577 }
578  
579 0 my $fh;
580 0 ▶ if ( ! open( $fh, '>', $permsfile ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, ">", $permsfile) 0 0 0
581 0 $self->{error}=15;
582 0 $self->{errorString}='Unable to open "'.$permsfile.'"';
583 0 $self->warn;
584 0 return undef;
585 }
586 0 print $fh $perms;
587 0 close( $fh );
588  
589 #add it if it, if it did not exist previously
590 0 ▶ if ( ! $exists ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($exists) 0 0 0
591 0 ▶ if (defined( $self->{scm}->{addCommand} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"scm"}{"addCommand"}) 0 0 0
592 0 my $command=$self->{scm}->{addCommand};
593 0 my $newfile=shell_quote($permsfile);
594
595 0 $command=~s/\%\%\%file\%\%\%/$newfile/g;
596 0 system($command);
597 0 my $exit = $?<<8;
598 0 ▶ if ($exit ne '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit ne 0) 0 0 0
599 0 $self->{error}=16;
600 0 $self->{errorString}='The add command failed. command="'.$command.'" exit="'.$exit.'"';
601 0 $self->warn;
602 0 return undef;
603 }
604 }
605 }
606
607 0 return 1;
608 }
609  
610 =head2 setPermsFromFile
611  
612 This sets the permissions for a file, from a already existing file.
613  
614 Three arguments are taken.
615  
616 The first one is the configuration directory to use. If none is
617 specified, it will automatically be choosen.
618  
619 The second is the config file to add the perms for.
620  
621 $foo->setPermsFromFile($configDir, $file);
622 if($foo->error){
623 warn('error:'.$foo->error.': '.$foo->errorString);
624 }
625  
626 =cut
627  
628 0 ▶sub setPermsFromFile{
Subroutine setPermsFromFile: not called CC=12 SCAR=50.5
Pod documented
629 0 my $self=$_[0];
630 0 my $configDir=$_[1];
631 0 my $file=$_[2];
632 0 my $method='setPermsFromFile';
633  
634 #blank any previous errors
635 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
636 0 return undef;
637 }
638  
639 #make sure we have a directory to use
640 0 ▶ if (!defined($configDir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $configDir) 0 0 0
641 0 $configDir=$self->{scm}->selectConfigDir;
642 0 ▶ if ($self->{scm}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
643 0 $self->{error}=5;
644 $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
645 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
646 0 $self->warn;
647 0 return undef;
648 }
649 }
650  
651 #make sure the config directory is valid
652 0 my $valid=$self->{scm}->validConfigDirName($configDir);
653 0 ▶ if ($self->{scm}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
654 0 $self->{error}=6;
655 $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
656 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
657 0 $self->warn;
658 0 return undef;
659 }
660 0 ▶ if (defined( $valid )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid) 0 0 0
661 0 $self->{error}=3;
662 0 $self->{errorString}='The configuration directory name '.$valid;
663 0 $self->warn;
664 0 return undef;
665 }
666  
667 #makes sure it exists
668 0 ▶ if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"scm"}{"baseDir"} . '/' . $configDir) 0 0 0
669 0 $self->{error}=4;
670 0 $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
671 0 $self->warn;
672 0 return undef;
673 }
674  
675 #make value for the file is specified
676 0 ▶ if (!defined( $file )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 0 0
677 0 $self->{error}=7;
678 0 $self->{errorString}='No file specified';
679 0 $self->warn;
680 0 return undef;
681 }
682  
683 #make sure the file exists, under the config dir
684 0 ▶ if (! -f $self->{scm}->{baseDir}.'/'.$configDir.'/'.$file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->{"scm"}{"baseDir"} . '/' . $configDir . '/' . $file) 0 0 0
685 0 $self->{error}=8;
686 0 $self->{errorString}='The file does not exist in the configuration directory';
687 0 $self->warn;
688 0 return undef;
689 }
690  
691 #make sure the file exists, on the fs
692 0 ▶ if (! -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 0 0
693 0 $self->{error}=9;
694 0 $self->{errorString}='The file does not exist in the configuration directory';
695 0 $self->warn;
696 0 return undef;
697 }
698  
699 #make sure it is not under the base directory
700 0 ▶ if ( ! $self->{scm}->notUnderBase($file) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"scm"}->notUnderBase($file)) 0 0 0
701 0 $self->{error}=10;
702 0 $self->{errorString}='"'.$file.'" exists under the base directory, "'.$self->{scm}->{baseDir}.'"';
703 0 $self->warn;
704 0 return undef;
705 }
706  
707 #stat the file
708 0 my $mode = (stat($file))[2] & 07777;
709 0 $mode=sprintf("%04o", $mode);
710
711 0 $self->setPerms($configDir, $file, $mode);
712 0 ▶ if ( $self->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error) 0 0 0
713 0 warn($self->{module}.' '.$method.': : setPerm errored');
714 0 return undef;
715 }
716  
717 0 return 1;
718 }
719  
720 =head2 upSync
721  
722 This syncs the file permissions up from the file system to
723 configuration directory.
724  
725 Two arguments can be used.
726  
727 The first is the configuration directory. If not specified, it will
728 be automaticallly choosen.
729  
730 The second is the files to sync. If not specifiedm, all files will
731 be synced.
732  
733 #sync the specified files
734 $foo->upSync( $configDir, \@files);
735 if($foo->error){
736 warn('error:'.$foo->error.': '.$foo->errorString);
737 }
738  
739 #syncs all the files
740 $foo->upSync( $configDir );
741 if($foo->error){
742 warn('error:'.$foo->error.': '.$foo->errorString);
743 }
744  
745 =cut
746  
747 0 ▶sub upSync{
Subroutine upSync: not called CC=17 SCAR=57.2
Pod documented
748 0 my $self=$_[0];
749 0 my $configDir=$_[1];
750 0 my @files;
751 0 ▶ if (defined($_[2])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[2]) 0 0 0
752 0 @files=@{$_[2]};
753 }
754 0 my $method='downSync';
755
756 #blank any previous errors
757 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
758 0 return undef;
759 }
760
761 #make sure we have a directory to use
762 0 ▶ if (!defined($configDir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $configDir) 0 0 0
763 0 $configDir=$self->{scm}->selectConfigDir;
764 0 ▶ if ($self->{scm}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
765 0 $self->{error}=5;
766 $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
767 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
768 0 $self->warn;
769 0 return undef;
770 }
771 }
772
773 #make sure the config directory is valid
774 0 my $valid=$self->{scm}->validConfigDirName($configDir);
775 0 ▶ if ($self->{scm}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
776 0 $self->{error}=6;
777 $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
778 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
779 0 $self->warn;
780 0 return undef;
781 }
782 0 ▶ if (defined( $valid )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid) 0 0 0
783 0 $self->{error}=3;
784 0 $self->{errorString}='The configuration directory name '.$valid;
785 0 $self->warn;
786 0 return undef;
787 }
788
789 #makes sure it exists
790 0 ▶ if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"scm"}{"baseDir"} . '/' . $configDir) 0 0 0
791 0 $self->{error}=4;
792 0 $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
793 0 $self->warn;
794 0 return undef;
795 }
796  
797 #checks and make sure all the files exist
798 0 my $int=0;
799 0 ▶ if(defined( $files[$int] )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files[$int]) { } 0 0 0
800 0 my @allfiles=$self->{scm}->listConfigFiles($configDir);
801 0 ▶ if( $self->{scm}->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
802 0 $self->{error}='16';
803 $self->{errorString}='Sys::Config::Manage->listConfigFiles errored error="'.
804 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
805 0 $self->warn;
806 0 return undef;
807 }
808
809 #make sure each file asked to be synced is tracked
810 0 ▶ while(defined( $files[$int] )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files[$int]) 0 0 0
811  
812 0 my $matched=0;
813 0 my $int2=0;
814 0 ▶ while(defined( $allfiles[$int2] )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $allfiles[$int2]) 0 0 0
815 0 ▶ if( $files[$int] eq $allfiles[$int2] ){
Branch: 1 branch
BranchTrueFalseTotal
if ($files[$int] eq $allfiles[$int2]) 0 0 0
816 0 $matched=1;
817 }
818  
819 0 $int2++;
820 }
821  
822 0 ▶ if(! $matched){
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 0 0 0
823 0 $self->{error}='8';
824 0 $self->{errorString}='"'.$files[$int].'" does not exist under the configuration directory, "'.$configDir.'",';
825 0 $self->warn;
826 0 return undef;
827 }
828  
829 0 $int++;
830 }
831  
832 }else{
833 #if we get here, no files have been specified so we do them all
834 0 @files=$self->{scm}->listConfigFiles($configDir);
835 0 ▶ if( $self->{scm}->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"scm"}->error) 0 0 0
836 0 $self->{error}='16';
837 $self->{errorString}='Sys::Config::Manage->listConfigFiles errored error="'.
838 0 $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
839 0 $self->warn;
840 0 return undef;
841 }
842 }
843  
844 #process each file
845 0 $int=0;
846 0 ▶ while( defined( $files[$int] ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files[$int]) 0 0 0
847 #try to sync it from what it currently is
848 0 $self->setPermsFromFile( $configDir, $files[$int] );
849 0 ▶ if( $self->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error) 0 0 0
850 0 warn( $self->{module}.' '.$method.': $self->setPermsFromFile( $configDir, $files[$int] ) errored' );
851 0 return undef;
852 }
853
854 0 $int++;
855 }
856  
857 0 return 1;
858 }
859  
860 =head1 ERROR CODES
861  
862 =head2 1
863  
864 Nothing passed for the Sys::Config::Manage object.
865  
866 =head2 2
867  
868 $args{scm} is not a Sys::Config::Manage object.
869  
870 =head2 3
871  
872 Invalid configuration directory name.
873  
874 =head2 4
875  
876 The configuration directory does not exist.
877  
878 =head2 5
879  
880 Sys::Config::Manage->selectConfigDir errored.
881  
882 =head2 6
883  
884 Sys::Config::Manage->validConfigDirName errored.
885  
886 =head2 7
887  
888 No filename specified.
889  
890 =head2 8
891  
892 The specified file does not exist under the configuration directory.
893  
894 =head2 9
895  
896 The specified file does not exist on the file system.
897  
898 =head2 10
899  
900 The file specified file is under the base directory.
901  
902 =head2 11
903  
904 No value for the permissions specified.
905  
906 =head2 12
907  
908 The value specified for the permissions does not appear to be valid.
909  
910 Validity is checked via the regexp below.
911  
912 /^[01246][01234567][01234567][01234567]$/
913  
914 =head2 13
915  
916 The ".SysConfigManage" directory does not exist and could not be created.
917  
918 =head2 14
919  
920 The ".SysConfigManage/Perms" directory does not exist and could not be created.
921  
922 =head2 15
923  
924 Unable to opens the permissions file.
925  
926 =head2 16
927  
928 The add command failed.
929  
930 =head1 AUTHOR
931  
932 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
933  
934 =head1 BUGS
935  
936 Please report any bugs or feature requests to C<bug-sys-config-manage-perms at rt.cpan.org>, or through
937 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sys-Config-Manage-Perms>. I will be notified, and then you'll
938 automatically be notified of progress on your bug as I make changes.
939  
940 =head1 SUPPORT
941  
942 You can find documentation for this module with the perldoc command.
943  
944 perldoc Sys::Config::Manage::Perms
945  
946  
947 You can also look for information at:
948  
949 =over 4
950  
951 =item * RT: CPAN's request tracker
952  
953 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Sys-Config-Manage>
954  
955 =item * AnnoCPAN: Annotated CPAN documentation
956  
957 L<http://annocpan.org/dist/Sys-Config-Manage>
958  
959 =item * CPAN Ratings
960  
961 L<http://cpanratings.perl.org/d/Sys-Config-Manage>
962  
963 =item * Search CPAN
964  
965 L<http://search.cpan.org/dist/Sys-Config-Manage/>
966  
967 =back
968  
969  
970 =head1 ACKNOWLEDGEMENTS
971  
972  
973 =head1 LICENSE AND COPYRIGHT
974  
975 Copyright 2011 Zane C. Bowers-Hadley.
976  
977 This program is free software; you can redistribute it and/or modify it
978 under the terms of either: the GNU General Public License as published
979 by the Free Software Foundation; or the Artistic License.
980  
981 See http://dev.perl.org/licenses/ for more information.
982  
983  
984 =cut
985  
986 2 1; # End of Sys::Config::Manage::Perms
« Manage/Ownership.pm ↑ Summary Manage/Remove.pm »