CfgTie/filever.pm

filter: Statementstmt 21.1% 18 / 85 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 30.0% 3 / 10 Podpod 37.5% 3 / 8 totaltotal 14.9% 24 / 161 CC 26 SCAR 35.8
CC
26
Cov
13%
Rotate
72.0
Roll
42.0
Unroll
30.0
CRAP 35.9
« CfgTie/CfgArgs.pm ↑ Summary CfgTie/TieUser.pm »
1 #Copyright 1998-2001, Randall Maas. All rights reserved. This program is free
2 #software; you can redistribute it and/or modify it under the same terms as
3 #PERL itself.
4  
5 package CfgTie::filever;
6  
7 =head1 NAME
8  
9 CfgTie::filever -- a simple module for substituting newer versions into a file system.
10  
11 =head1 SYNOPSIS
12  
13 This module allows a newer version of file to be safely placed into the file system.
14  
15 =head1 DESCRIPTION
16  
17 This is a set of utilities for manipulating files.
18  
19 =head2 C<File's_Rotate> (I<$Old, $New>)
20  
21 This is the file space equivalent to the variable exchange or swap. The
22 I<old> file will be renamed (with a .old extension) and the I<new> file will
23 be renamed to I<old>. This is extremely useful for many semi-critical
24 functions, where modifying the file directly will cause unpredictable results.
25 Instead, the preferred method is to modify I<old> sending the results to
26 I<new> in the background and then doing a I<very> quick switcheroo.
27  
28 It preserves the permissions and ownership of the original file.
29  
30 If this routine fails, it will make an attempt to restore things to their
31 original state and return.
32  
33 B<Return Value>: 0 on success, -1 on error;
34  
35 =head2 C<Roll(>I<path>,I<depth>,I<sep>C<)>
36  
37 This convert the specified files into a backup file (the original file is
38 renamed, so you had better have something to put there).
39  
40 =over 1
41  
42 =item I<Depth>
43  
44 (optional) Controlls the number of backup copies
45  
46 =item I<Sep>
47  
48 (optional) Controlls the seperator between the main name and the backup
49 number
50  
51 =back
52  
53 The defaults for I<depth> and I<sep> are controlled by following two variables
54 in the package:
55  
56 =over 1
57  
58 =item C<RollDepth>
59  
60 Controlls the number of of older versions that are kept around as backup
61 copies.
62 I<[Default: 4]>
63  
64 =item C<RollSep>
65  
66 Controls the separator between the file name and the backup number.
67 I<[Default: >C<~>I<]>
68  
69 =back
70  
71 =head2 C<RCS_path (>I<RCSObj>,I<path>C<)>
72  
73 For the given RCS object, this sets the working directory and the file.
74  
75 =head2 find_by_user
76  
77 This function attempts to locate all of the files in the system that are owned
78 by the specified users. It takes the following parameters:
79  
80 =over 1
81  
82 =item C<Base>
83  
84 C<Base> can be a string to the base path or a reference to a list of base
85 paths to search.
86  
87 =back
88  
89 Return Value:
90  
91 =over 1
92  
93 =item C<undef> if there was an error
94  
95 =item otherwise the list of file that matched
96  
97 =back
98  
99 =head1 Author
100  
101 Randall Maas (L<randym@acm.org>, L<http://www.hamline.edu/~rcmaas/>)
102  
103 =cut
104  
105 1 my $RollDepth = 4;
106 1 my $RollSep = '~';
107  
108 sub Rotate
109 0 ▶{
Subroutine Rotate: not called CC=8 SCAR=42.8
Pod undocumented
110 0 my($Old, $New) = @_;
111 0 ▶ if (!defined $Old) {die "Rotate: old not defined, but $New is!\n";}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Old) 0 0 0
112 0 ▶ if ($Old eq $New) {return;}
Branch: 1 branch
BranchTrueFalseTotal
if ($Old eq $New) 0 0 0
113  
114 0 my @S =stat $Old;
115 #If the old file does exist we need to do a bit of work (we use stat
116 #since the other bits of information are relevant too)
117 0 ▶ if (scalar @S)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @S) { } 0 0 0
118 {
119 #Migrate us to some backup copies, use one more than default so we
120 #can unroll
121 0 &Roll($Old,$RollDepth+1);
122  
123 # Modify the permissions of the new file to match that of the old one.
124 0 ▶ if (!chmod($S[2], $New) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (chmod $S[2], $New and chown $S[4], $S[5], $New and rename $New, $Old) 0 0 0
Condition0%0/6
chmod $S[2], $New and chown $S[4], $S[5], $New and rename $New, $Old
!ll&&!rl&&r
000
chmod $S[2], $New and chown $S[4], $S[5], $New
!ll&&!rl&&r
000
MC/DC0%0/3
chmod $S[2], $New and chown $S[4], $S[5], $New and rename $New, $Old
chmod $S[2], $New chown $S[4], $S[5], $New rename $New, $Old
Truth table0%0/4
A and B and C
A= chmod $S[2], $NewB= chown $S[4], $S[5], $NewC= rename $New, $Old
ABCresult
0XX0
10X0
1100
1111
125  
126 # Modify the ownership of the new file to match that of the old one.
127 !chown($S[4], $S[5], $New)||
128  
129 !rename($New, $Old))
130 {
131 0 die "bad things happened: $New doesn't exist? $!\n";
132 0 &Unroll($Old,$RollDepth+1);
133 0 return -1;
134 }
135 }
136 else
137 {
138 0 ▶ rename ($New,$Old) || return -1;
Branch: 1 branch
BranchTrueFalseTotal
rename $New, $Old or return -1 0 0 0
139 }
140 0 0;
141 }
142  
143  
144 sub Roll
145 0 ▶{
Subroutine Roll: not called CC=6 SCAR=37.4
Pod documented
146 0 my ($File,$Num,$Sep) =@_;
147 0 ▶ if (!defined $File) {die "File not defined!\n";}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $File) 0 0 0
148 0 ▶ if (!defined $Sep) {$Sep = $RollSep;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Sep) 0 0 0
149 0 ▶ if (!defined $Num) {$Num = $RollDepth;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Num) 0 0 0
150  
151 0 ▶ if ($Num < 1) {return;}
Branch: 1 branch
BranchTrueFalseTotal
if ($Num < 1) 0 0 0
152  
153 0 my $Base = $File.$Sep;
154 0 ▶ for (my $I=$Num-1; $I; $I--)
Branch: 1 branch
BranchTrueFalseTotal
if ($I) 0 0 0
155 0 {rename $Base.$I,$Base.($I+1);}
156  
157 0 link $File, $Base."1";
158 #rename $File, $Base."1";
159 }
160  
161 sub Unroll
162 0 ▶{
Subroutine Unroll: not called CC=5 SCAR=34.0
Pod undocumented
163 0 my ($File,$Num,$Sep) =@_;
164 0 ▶ if (!defined $Sep) {$Sep = $RollSep;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Sep) 0 0 0
165 0 ▶ if (!defined $Num) {$Num = $RollDepth;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Num) 0 0 0
166  
167 0 ▶ if ($Num < 1) {return;}
Branch: 1 branch
BranchTrueFalseTotal
if ($Num < 1) 0 0 0
168  
169 0 my $Base = $File.$Sep;
170 0 rename $Base.'1',$File;
171 0 ▶ for (my $I=1; $I < $RollDepth; $I++)
Branch: 1 branch
BranchTrueFalseTotal
if ($I < $RollDepth) 0 0 0
172 0 {rename $Base.($I+1),$Base.$I;}
173 }
174  
175 sub RCS_path($$)
176 0 ▶{
Subroutine RCS_path: not called CC=5 SCAR=34.0
Pod documented
177 0 my ($RCSObj, $Path) = @_;
178 0 ▶ if ($Path =~ /^(.+)\/([^\/]+)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($Path =~ m[^(.+)/([^/]+)$]) { } 0 0 0
179 {
180 0 $RCSObj->workdir($1);
181 0 $RCSObj->file($2);
182  
183 #Make the working directory and archive directory make sense
184 0 my $t = $RCSObj->rcsdir;
185 0 ▶ if (!defined $t || (substr($t,0,1) ne '/' && substr($1,0,1) ne '.'))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $t or substr($t, 0, 1) ne "/" and substr($1, 0, 1) ne ".") 0 0 0
Condition0%0/6
not defined $t or substr($t, 0, 1) ne "/" and substr($1, 0, 1) ne "."
l!l&&r!l&&!r
000
substr($t, 0, 1) ne "/" and substr($1, 0, 1) ne "."
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $t or substr($t, 0, 1) ne "/" and substr($1, 0, 1) ne "."
not defined $t substr($t, 0, 1) ne "/" substr($1, 0, 1) ne "."
Truth table0%0/4
not(A) or B and C
A= not defined $tB= substr($t, 0, 1) ne "/"C= substr($1, 0, 1) ne "."
ABCresult
1XX1
0111
00X0
0100
186 0 {$RCSObj->rcsdir($1.'/'.$t);}
187  
188 }
189 else
190 {
191 0 $RCSObj->file($Path);
192 }
193 }
194  
195 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
196  
197 sub open($)
198 1 ▶{
Subroutine open: called CC=1 SCAR=0.0
Pod undocumented
199 1 local($name)=@_;
200 #Save the environment so we can restore it
201 1 my $E=\%ENV;
202  
203 #Erase the path, and other environment information
204 1 %ENV=();
205 1 $ENV{'PATH'}='/bin:/sbin';
206 1 my $F=eval ('new FileHandle $name');
207  
208 #Restore the environment
209 1 %ENV=%{$E};
210 1 $F;
211 }
212  
213 sub system($)
214 0 ▶{
Subroutine system: not called CC=1 SCAR=6.9
Pod undocumented
215 0 my $F= &filever::open($_[0]."|");
216 0 $F->close;
217 }
218  
219  
220 1 my @UID_paths; # Holder of file paths that match
221  
222 sub UID_worker
223 0 ▶{
Subroutine UID_worker: not called CC=2 SCAR=17.9
Pod undocumented
224 # A worker to check to see if the specified files owner matches any in our
225 # set
226 0 my @S = stat $_;
227 0 ▶ if (exists $UIDs_to_watch{$S[4]}) {push @UID_paths, $File::Find::name;}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $UIDs_to_watch{$S[4]}) 0 0 0
228 }
229  
230 sub find_by_user
231 0 ▶{
Subroutine find_by_user: not called CC=5 SCAR=30.9
Pod documented
232 0 my $Base = shift;
233  
234 0 ▶ if (!scalar @_) {return ();}
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @_) 0 0 0
235  
236 1 ▶ use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
237 0 my %User;
238 0 tie %User, 'user';
239  
240 #Convert all of the user names and such to ids
241 0 %UIDs_to_watch=();
242 0 foreach my $I (@_)
243 {
244 0 ▶ if (exists $User{$I})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $User{$I}) { } 0 0 0
245 0 {$UIDs_to_watch{$User{$I}->{Id}}=1;}
246 else
247 0 {$UIDs_to_watch{$I} = 1;}
248 }
249  
250 #Call find to executie it
251 0 @UID_paths=();
252 0 ▶ if (not ref $Base)
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $Base) { } 0 0 0
253 0 {find(\&UID_worker, $Base);}
254 else
255 0 {find(\&UID_worker, @{$Base});}
256 0 return @UID_paths;
257 }
258  
259 1 1;
« CfgTie/CfgArgs.pm ↑ Summary CfgTie/TieUser.pm »