ConfigFile.pm

filter: Statementstmt 18.3% 26 / 142 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 29.1% 7 / 24 Podpod 50.0% 8 / 16 totaltotal 14.4% 41 / 283 CC 50 SCAR 39.5
CC
50
Cov
11%
new
132.0
joinwrap
110.0
commit
56.0
CRAP 52.1
↑ Summary AliasFile.pm »
1 package Unix::ConfigFile;
2  
3 # $Id: ConfigFile.pm,v 1.6 2000/05/02 15:49:19 ssnodgra Exp $
4  
5 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $LOCKEXT);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Text::Tabs;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 require Exporter;
14  
15 1 @ISA = qw(Exporter);
16 # Items to export into callers namespace by default. Note: do not export
17 # names by default without a very good reason. Use EXPORT_OK instead.
18 # Do not simply export all your public functions/methods/constants.
19 1 @EXPORT = qw(
20
21 );
22 1 $VERSION = '0.06';
23  
24 # Package variables
25 1 my $SALTCHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/.";
26  
27 # Preloaded methods go here.
28  
29 # Create a new ConfigFile (or, more likely, a ConfigFile subclass) object.
30 # Opens the specified file and calls the read method (which will be located
31 # in the subclass package) to initialize the object data structures
32 0 ▶sub new {
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
33 0 my ($pkg, $filename, %opt) = @_;
34  
35 # Initialize the object reference
36 0 my $this = {
37 filename => $filename,
38 handle => undef,
39 locked => 0,
40 lockfh => undef,
41 lockfile => "$filename.lock",
42 locking => "dotlock",
43 mode => "r+",
44 seq => [ ]
45 };
46 0 bless $this, $pkg;
47  
48 # Set options
49 0 ▶ $this->lockfile($opt{lockfile}) if defined $opt{lockfile};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"lockfile"} 0 0 0
50 0 ▶ $this->locking($opt{locking}) if defined $opt{locking};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"locking"} 0 0 0
51 0 ▶ $this->mode($opt{mode}) if defined $opt{mode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"mode"} 0 0 0
52  
53 # Get a filehandle
54 0 my $fh = new IO::File $this->filename, $this->mode;
55 0 ▶ return undef unless defined($fh);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 0 0
56 0 $this->fh($fh);
57  
58 # Do file locking - this must happen before read is called or we could
59 # end up with stale data in memory
60 0 ▶ if ($this->mode eq "r") {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->mode eq 'r') { } 0 0 0
61 0 ▶ $this->lock("shared") or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$this->lock("shared") or return undef 0 0 0
62 }
63 else {
64 0 ▶ $this->lock() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$this->lock or return undef 0 0 0
65 }
66  
67 # Initialize object structure from the file
68 0 ▶ if (exists $opt{readopts}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{'readopts'}) { } 0 0 0
69 0 ▶ $this->read($this->fh, $opt{readopts}) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$this->read($this->fh, $opt{"readopts"}) or return undef 0 0 0
70 }
71 else {
72 0 ▶ $this->read($this->fh) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$this->read($this->fh) or return undef 0 0 0
73 }
74 0 return $this;
75 }
76  
77  
78 # Commit in-memory changes to disk
79 0 ▶sub commit {
Subroutine commit: not called CC=7 SCAR=40.3
Pod documented
80 0 my ($this, %opt) = @_;
81  
82 0 ▶ return 0 if $this->mode eq "r";
Branch: 1 branch
BranchTrueFalseTotal
if $this->mode eq "r" 0 0 0
83 0 my $tempname = $this->filename . ".tmp." . $$;
84 0 ▶ my $fh = new IO::File ">$tempname" or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new(">$tempname") or return 0 0 0 0
85 0 my ($mode, $uid, $gid) = (stat $this->fh)[2,4,5];
86 0 chown $uid, $gid, $tempname;
87 0 chmod $mode, $tempname;
88 0 ▶ if (exists $opt{writeopts}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{'writeopts'}) { } 0 0 0
89 0 ▶ $this->write($fh, $opt{writeopts}) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->write($fh, $opt{"writeopts"}) or return 0 0 0 0
90 }
91 else {
92 0 ▶ $this->write($fh) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->write($fh) or return 0 0 0 0
93 }
94 0 undef $fh;
95 0 ▶ if (defined $opt{backup}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"backup"}) 0 0 0
96 0 rename $this->filename, $this->filename . $opt{backup};
97 }
98 0 return rename $tempname, $this->filename;
99 }
100  
101  
102 # This method is absolutely necessary to prevent leftover lock files
103 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
104 0 my $this = shift;
105  
106 0 ▶ $this->unlock() or croak "Can't unlock file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$this->unlock or croak("Can't unlock file: $!") 0 0 0
107 0 $this->fh->close();
108 }
109  
110  
111 # Filename accessor
112 0 ▶sub filename {
Subroutine filename: not called CC=2 SCAR=17.9
Pod undocumented
113 0 my $this = shift;
114 0 ▶ @_ ? $this->{filename} = shift : $this->{filename};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
115 }
116  
117  
118 # Filehandle accessor
119 0 ▶sub fh {
Subroutine fh: not called CC=2 SCAR=17.9
Pod undocumented
120 0 my $this = shift;
121 0 ▶ @_ ? $this->{handle} = shift : $this->{handle};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
122 }
123  
124  
125 # Locking method accessor
126 0 ▶sub locking {
Subroutine locking: not called CC=3 SCAR=24.8
Pod undocumented
127 0 my $this = shift;
128  
129 0 ▶ return $this->{locking} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
130 0 my $lockmethod = shift;
131 0 ▶ return undef unless grep { $lockmethod eq $_ } qw(flock dotlock none);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$lockmethod eq $_;} "flock", "dotlock", "none" 0 0 0
132 0 $this->{locking} = $lockmethod;
133 }
134  
135  
136 # Lock filehandle accessor
137 0 ▶sub lockfh {
Subroutine lockfh: not called CC=2 SCAR=17.9
Pod undocumented
138 0 my $this = shift;
139 0 ▶ @_ ? $this->{lockfh} = shift : $this->{lockfh};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
140 }
141  
142  
143 # Lock file name accessor
144 0 ▶sub lockfile {
Subroutine lockfile: not called CC=2 SCAR=17.9
Pod undocumented
145 0 my $this = shift;
146 0 ▶ @_ ? $this->{lockfile} = shift : $this->{lockfile};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
147 }
148  
149  
150 # Mode accessor
151 0 ▶sub mode {
Subroutine mode: not called CC=3 SCAR=24.8
Pod undocumented
152 0 my $this = shift;
153 0 ▶ return $this->{mode} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
154 0 my $mode = shift;
155 0 ▶ return undef unless grep { $mode eq $_ } qw(r r+ w);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$mode eq $_;} "r", "r+", "w" 0 0 0
156 0 $this->{mode} = $mode;
157 }
158  
159  
160 # Obtain a lock on the file. You can pass "shared" to request a shared lock;
161 # the default is exclusive. This function is somewhat inconsistent at the
162 # moment since it will block with the flock method but return an error if the
163 # dotlock method fails.
164 0 ▶sub lock {
Subroutine lock: not called CC=7 SCAR=40.3
Pod undocumented
165 0 my $this = shift;
166  
167 0 ▶ return 1 if ($this->locking eq "none");
Branch: 1 branch
BranchTrueFalseTotal
if $this->locking eq "none" 0 0 0
168 0 ▶ return 0 if $this->{locked};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"locked"} 0 0 0
169 0 ▶ if ($this->locking eq "flock") {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->locking eq 'flock') { } 0 0 0
elsif ($this->locking eq 'dotlock') { } 0 0 0
170 0 ▶ @_ ? flock $this->fh, LOCK_SH : flock $this->fh, LOCK_EX;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
171 }
172 elsif ($this->locking eq "dotlock") {
173 # We only support exclusive locks with dotlock
174 0 my $fh = new IO::File $this->lockfile, O_CREAT|O_EXCL|O_RDWR;
175 0 ▶ return 0 unless defined($fh);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 0 0
176 0 $this->lockfh($fh);
177 }
178 0 $this->{locked} = 1;
179 }
180  
181  
182 # Unlock the file
183 0 ▶sub unlock {
Subroutine unlock: not called CC=6 SCAR=37.4
Pod undocumented
184 0 my $this = shift;
185  
186 # NOTE: Originally I wasn't unlinking the lock file unless the lock
187 # filehandle was defined. This led to the rather unexpected discovery
188 # the Perl would sometimes destroy the filehandle before destroying
189 # the object during program shutdown. Thus, we now check if locked
190 # is set, which happens only if a lock is successfully acquired.
191 # This also prevents us from unlinking someone else's lock file.
192  
193 0 ▶ return 1 if ($this->locking eq "none");
Branch: 1 branch
BranchTrueFalseTotal
if $this->locking eq "none" 0 0 0
194 0 ▶ return 0 unless $this->{locked};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"locked"} 0 0 0
195 0 $this->{locked} = 0;
196 0 ▶ if ($this->locking eq "flock") {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->locking eq 'flock') { } 0 0 0
elsif ($this->locking eq 'dotlock') { } 0 0 0
197 0 flock $this->fh, LOCK_UN;
198 0 return 1;
199 }
200 elsif ($this->locking eq "dotlock") {
201 0 ▶ $this->lockfh->close() if defined($this->lockfh);
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->lockfh 0 0 0
202 0 my $result = unlink $this->lockfile;
203 0 return ($result == 1);
204 }
205 }
206  
207  
208 # Encrypts a plaintext password with a random salt
209 # This is provided for use with the subclasses
210 0 ▶sub encpass {
Subroutine encpass: not called CC=1 SCAR=6.9
Pod documented
211 0 my ($this, $pass) = @_;
212  
213 0 my $salt = substr($SALTCHARS, int(rand(length($SALTCHARS))), 1) .
214 substr($SALTCHARS, int(rand(length($SALTCHARS))), 1);
215 0 crypt($pass, $salt);
216 }
217  
218  
219 # Return the file sequence
220 0 ▶sub sequence {
Subroutine sequence: not called CC=1 SCAR=6.9
Pod documented
221 0 my $this = shift;
222 0 return @{$this->{seq}};
223 }
224  
225  
226 # Append information to the file sequence
227 0 ▶sub seq_append {
Subroutine seq_append: not called CC=1 SCAR=6.9
Pod documented
228 0 my $this = shift;
229 0 push @{$this->{seq}}, @_;
230 }
231  
232  
233 # Insert information into the file sequence before the given data
234 0 ▶sub seq_insert {
Subroutine seq_insert: not called CC=3 SCAR=24.8
Pod documented
235 0 my $this = shift;
236 0 my $data = shift;
237  
238 0 ▶ for (my $i = 0; $i < @{$this->{seq}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$this->{"seq"};}) 0 0 0
239 0 ▶ if ($this->{seq}[$i] eq $data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"seq"}[$i] eq $data) 0 0 0
240 0 splice @{$this->{seq}}, $i, 0, @_;
241 0 return 1;
242 }
243 }
244 0 return 0;
245 }
246  
247  
248 # Remove the specified data from the file sequence
249 0 ▶sub seq_remove {
Subroutine seq_remove: not called CC=3 SCAR=24.8
Pod documented
250 0 my ($this, $data) = @_;
251  
252 0 ▶ for (my $i = 0; $i < @{$this->{seq}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$this->{"seq"};}) 0 0 0
253 0 ▶ if ($this->{seq}[$i] eq $data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"seq"}[$i] eq $data) 0 0 0
254 0 splice @{$this->{seq}}, $i, 1;
255 0 return 1;
256 }
257 }
258 0 return 0;
259 }
260  
261  
262 # Joinwrap is a utility function that happens to be useful in several modules
263 # This thing was a bitch to get working 100% right, so use caution. :-)
264 0 ▶sub joinwrap {
Subroutine joinwrap: not called CC=10 SCAR=47.0
Pod documented
265 0 my ($this, $linelen, $head, $indent, $delim, $tail, @list) = @_;
266  
267 0 my $result = "";
268 0 my $line = 0;
269 0 $linelen -= length(expand($tail));
270 0 ▶ while (@list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list) 0 0 0
271 0 ▶ my $curline = $result ? $indent : $head;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 0 0 0
272 0 $curline =~ s/%n/$line/;
273 0 my $appended = 0;
274 0 ▶ while (@list && length(expand($curline . $delim . $list[0])) <= $linelen) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list and length expand($curline . $delim . $list[0]) <= $linelen) 0 0 0
Condition0%0/3
@list and length expand($curline . $delim . $list[0]) <= $linelen
!ll&&!rl&&r
000
MC/DC0%0/2
@list and length expand($curline . $delim . $list[0]) <= $linelen
@list length expand($curline . $delim . $list[0]) <= $linelen
Truth table0%0/3
A and B
A= @listB= length expand($curline . $delim . $list[0]) <= $linelen
ABresult
0X0
100
111
275 0 ▶ $curline .= $delim if $appended;
Branch: 1 branch
BranchTrueFalseTotal
if $appended 0 0 0
276 0 $curline .= shift @list;
277 0 $appended++;
278 }
279 # Special case - element is longer than linelen
280 0 ▶ $curline .= shift @list unless $appended;
Branch: 1 branch
BranchTrueFalseTotal
unless $appended 0 0 0
281 # Append newline if this isn't the first line
282 0 ▶ $result .= "\n" if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 0 0 0
283 0 $result .= $curline;
284 # Append tail unless this is the last line
285 0 ▶ $result .= $tail if @list;
Branch: 1 branch
BranchTrueFalseTotal
if @list 0 0 0
286 0 $line++;
287 }
288 0 ▶ $result ? $result : $head;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 0 0 0
289 }
290  
291  
292 # Autoload methods go after =cut, and are processed by the autosplit program.
293  
294 1 1;
295 __END__
↑ Summary AliasFile.pm »