Dir.pm

filter: Statementstmt 86.5% 200 / 231 Branchbran 64.9% 74 / 114 Conditioncond 55.1% 32 / 58 MC/DCmcdc 31.2% 10 / 32 Subroutinesub 88.8% 24 / 27 Podpod 100% 7 / 7 totaltotal 73.9% 347 / 469 CC 83 SCAR 25.7
CC
83
Cov
76%
__ANON__
42.0
_lock
27.8
visit
18.1
CRAP 13.1
↑ Summary flock-test.pl »
1 package Queue::Dir;
2 # $Id: Dir.pm,v 1.13 2003/03/09 16:18:48 lem Exp $
3  
4 11 require 5.005_62;
5  
6 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use IO::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 11 our $Debug = 0;
15 11 our $hires = 'gettimeofday';
16  
17 11 ▶eval "use Time::HiRes qw(gettimeofday);";
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 0 ▶if ($@) { $hires = 'time' }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 11 11
20  
21 11 ▶use vars qw($a $b);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 11 our $VERSION = 0.01;
24  
25 =pod
26  
27 =head1 NAME
28  
29 Queue::Dir - Manage queue directories where each object is a file
30  
31 =head1 SYNOPSIS
32  
33 use Queue::Dir;
34  
35 my $q = new Queue::File (
36 -id => $my_process_id,
37 -paths => [ '/var/path/to/queue1', ... ],
38 -promiscuous => 1,
39 -sort => 'sortsub',
40 -filter => sub { ... },
41 -lockdir => 'lock',
42 -lockmax => 300,
43 );
44
45 my ($fh, $qid) = $q->store($oid);
46  
47 my $qid = $q->next();
48  
49 my $fh = $q->visit($mode, $qid);
50  
51 my $status = $q->done($qid);
52  
53 my $name = $q->name($qid);
54  
55 =head1 DESCRIPTION
56  
57 C<Queue::Dir> allows the manipulation of objects placed in a
58 queue. The queue is implemented as a directory where each object is
59 stored as a file.
60  
61 =head2 METHODS
62  
63 The following methods are defined:
64  
65 =over 4
66  
67 =item C<my $q = new Queue::File (...)>
68  
69 B<-id> assigns a unique process-id to this queue object. Defaults to
70 something built from the serialization of the object + C<$$> or
71 something similar.
72  
73 B<-paths> specifies a list of paths to use as storage points for the
74 queue files. If more than one are supplied, round-robin will be used
75 to store objects there.
76  
77 When B<-promiscuous> is true (the default), objects stored with any
78 other C<Queue::File> object are accessible. If set to false, only
79 files whose id matches the value for B<-id> are visible.
80  
81 B<-sort> allows for the specification of a sorting function, used to
82 decide the order in which the queue files will be used. The function
83 is invoked in the same fashion as C<sort>, getting two variables
84 (C<$a> and C<$b>) and returning -1, 0 or 1 depending on
85 comparison. C<$a> and C<$b> are hash references whose first element is
86 the queue id of the object and the second element is a the full
87 pathname of such object.
88  
89 The C<sub { ... }> passed in the B<-filter> parameter can control
90 which files in a given directory to consider as queue objects. By
91 default, all files will be considered part of the queue. This function
92 is called with a reference of the invoking object and the full
93 pathname of each file. A true return value causes the given file to be
94 included in the queue. Note that this is only called if
95 B<-promiscuous> is set to a false value.
96  
97 B<-lockdir> and B<-lockmax> control an optional locking mechanism that
98 reduces the chance of multiple collaborating instances of
99 C<Queue::Dir> objects, from picking the same object from the
100 queue. B<-lockdir>, when present, defines the name of the directory
101 (within each queue directory) to use for storing the lock files. The
102 B<-lockmax> parameter, which defaults to 300 seconds, control for how
103 long the locks are honored.
104  
105 Note that locking is disabled by default.
106  
107 =cut
108  
109 sub new
110 23 ▶{
Subroutine new: called CC=6 SCAR=23.5
Pod documented
111 23 my $name = shift;
112 23 ▶ my $class = ref($name) || $name;
Condition33%1/3
ref $name || $name
l!l&&r!l&&!r
0230
MC/DC0%0/2
ref $name || $name
ref $name $name
Truth table33%1/3
A || B
A= ref $nameB= $name
ABresult
1X1
011
000
113  
114 23 ▶ warn "Queue::Dir::new()\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 23 23
115  
116 my %self = validate_with
117 (
118 params => \@_,
119 ignore_case => 1,
120 strip_leading => '-',
121 spec =>
122 {
123 id =>
124 {
125 type => SCALAR,
126 default => hostname . $$,
127 },
128 paths =>
129 {
130 type => ARRAYREF,
131 callbacks =>
132 {
133 23 ▶ directory => sub { $_ = shift; @$_ == grep { -d } @$_; }
Subroutine __ANON__: called CC=1 SCAR=0.0
134 }
135 },
136 promiscuous =>
137 {
138 type => SCALAR | BOOLEAN,
139 default => 1,
140 },
141 sort =>
142 {
143 type => SCALAR,
144 default => 'Queue::Dir::_sort',
145 },
146 lockdir =>
147 {
148 type => SCALAR,
149 default => undef,
150 },
151 lockmax =>
152 {
153 type => SCALAR,
154 default => 300,
155 callbacks =>
156 {
157 0 ▶ numeric => sub { shift =~ /^\d+$/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 ▶ positive => sub { shift > 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
159 },
160 },
161 filter =>
162 {
163 type => CODEREF,
164 default => sub
165 0 ▶ {
Subroutine __ANON__: not called CC=6 SCAR=37.4
166 0 my $self = shift;
167 0 my $long = shift;
168
169 0 ▶ return 0 unless $long;
Branch: 1 branch
BranchTrueFalseTotal
unless $long 0 0 0
170  
171 0 my ($path, $id) = (File::Spec->splitpath($long))[1,2];
172
173 0 for my $p (@{$self->{paths}})
174 {
175 0 ▶ if (substr($p->[0], $path, 0) == 0
Branch: 1 branch
BranchTrueFalseTotal
if (substr($p->[0], $path, 0) == 0 and -f $p->[0] . '/' . $id and $id =~ /^\d+\.\d+\.$self->{"id"}\.\d+$/) 0 0 0
Condition0%0/6
substr($p->[0], $path, 0) == 0 and -f $p->[0] . '/' . $id and $id =~ /^\d+\.\d+\.$self->{"id"}\.\d+$/
!ll&&!rl&&r
000
substr($p->[0], $path, 0) == 0 and -f $p->[0] . '/' . $id
!ll&&!rl&&r
000
MC/DC0%0/3
substr($p->[0], $path, 0) == 0 and -f $p->[0] . '/' . $id and $id =~ /^\d+\.\d+\.$self->{"id"}\.\d+$/
substr($p->[0], $path, 0) == 0 -f $p->[0] . '/' . $id $id =~ /^\d+\.\d+\.$self->{"id"}\.\d+$/
Truth table0%0/4
A and B and C
A= substr($p->[0], $path, 0) == 0B= -f $p->[0] . '/' . $idC= $id =~ /^\d+\.\d+\.$self->{"id"}\.\d+$/
ABCresult
0XX0
10X0
1100
1111
176 and -f $p->[0] . '/' . $id
177 and $id =~ m!^\d+\.\d+\.$self->{id}\.\d+$!)
178 {
179 0 return 1;
180 }
181 }
182
183 0 return 0;
184 },
185 },
186 23 });
187
188 21 @{$self{paths}} = sort { $a cmp $b } @{$self{paths}};
189  
190 21 $_ = [$_, new IO::Dir $_] for @{$self{paths}};
191  
192 21 ▶ if (grep { ! defined $_->[1] } @{$self{paths}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not defined $_->[1];} @{$self{"paths"};}) 0 21 21
193 0 warn "One of the queue paths seems invalid\n";
194 0 return;
195 }
196  
197 # Prime the object with an empty file
198 # inventory.
199 21 $self{_files} = [];
200
201 # We store objects in round-robin.
202 21 $self{_rr} = 0;
203 21 $self{_current} = [0, 0];
204  
205 21 my $self = bless \%self, $class;
206
207 21 ▶ $self->_clean_locks if $self->{lockdir};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lockdir"} 10 11 21
208  
209 21 return $self->_refresh;
210 }
211  
212 62 ▶sub _sort { $a->[0] cmp $b->[0]; }
Subroutine _sort: called CC=1 SCAR=0.0
213 11 ▶sub _timestamp { no strict "refs"; return join '', &$hires; }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _timestamp: called CC=1 SCAR=0.0
214  
215 # Update the inventory of queue
216 # objects, if required.
217 sub _refresh
218 64 ▶{
Subroutine _refresh: called CC=12 SCAR=25.3
219 64 my $self = shift;
220  
221 64 ▶ warn "Queue::Dir::_refresh()\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 64 64
222  
223 # warn "_files ", scalar @{$self->{_files}}, " _current[0] ",
224 # $self->{_current}->[0], "\n";
225  
226 64 ▶ unless (@{$self->{_files}} or $self->{_current}->[0])
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"_files"};} or $self->{"_current"}[0]) 47 17 64
Condition66%2/3
@{$self->{"_files"};} or $self->{"_current"}[0]
l!l&&r!l&&!r
01747
MC/DC50%1/2
@{$self->{"_files"};} or $self->{"_current"}[0]
@{$self->{"_files"};} $self->{"_current"}[0]
Truth table66%2/3
A or B
A= @{$self->{"_files"};}B= $self->{"_current"}[0]
ABresult
1X1
011
000
227 {
228 47 ▶ warn "Queue::Dir::_refresh() running\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 47 47
229  
230 47 for my $p (@{$self->{paths}})
231 {
232 # warn "p\n";
233 48 $p->[1]->rewind;
234 48 ▶ while (defined (my $f = $p->[1]->read))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $f = $p->[1]->read)) 173 48 221
235 {
236 173 ▶ next if $f eq '.' or $f eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $f eq "." or $f eq ".." 96 77 173
Condition100%3/3
$f eq "." or $f eq ".."
l!l&&r!l&&!r
484877
MC/DC100%2/2
$f eq "." or $f eq ".."
$f eq "." $f eq ".."
Truth table100%3/3
A or B
A= $f eq "."B= $f eq ".."
ABresult
1X1
011
000
237 77 ▶ next unless -f $p->[0] . '/' . $f;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $p->[0] . '/' . $f 31 46 77
238 # warn "f\n";
239 46 my $t = [$f, $p->[0] . '/' . $f];
240 46 ▶ if (!$self->{promiscuous}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"promiscuous"} or $self->{"filter"}->($t->[1])) 0 46 46
Condition33%1/3
$self->{"promiscuous"} or $self->{"filter"}->($t->[1])
l!l&&r!l&&!r
4600
MC/DC0%0/2
$self->{"promiscuous"} or $self->{"filter"}->($t->[1])
$self->{"promiscuous"} $self->{"filter"}->($t->[1])
Truth table33%1/3
A or B
A= $self->{"promiscuous"}B= $self->{"filter"}->($t->[1])
ABresult
1X1
011
000
241 and !$self->{filter}->($t->[1]))
242 {
243 0 next;
244 }
245 46 push @{$self->{_files}}, $t;
246 }
247 }
248 # XXX - I seem unable to specify the sort
249 # function directly.
250 47 my $sort = $self->{sort};
251 47 @{$self->{_files}} = sort $sort @{$self->{_files}};
252 # $self->{_current} = shift @{$self->{_files}} || [0,0];
253 }
254  
255 64 return $self;
256 }
257  
258 # Give a $qid, fetch pathname
259 sub _name
260 76 ▶{
Subroutine _name: called CC=9 SCAR=22.5
261 76 my $self = shift;
262 76 ▶ my $qid = shift || $self->{_current}->[0] || $self->next;
Condition33%2/6
shift() || $self->{'_current'}[0] || $self->next
l!l&&r!l&&!r
7600
shift() or $self->{"_current"}[0]
l!l&&r!l&&!r
7600
MC/DC0%0/3
shift() || $self->{'_current'}[0] || $self->next
shift() $self->{"_current"}[0] $self->next
Truth table25%1/4
A or B || C
A= shift()B= $self->{"_current"}[0]C= $self->next
ABCresult
1XX1
01X1
0011
0000
263  
264 # First, try to find this object in
265 # out cached structures
266  
267 76 ▶ for my $t (($self->{_current}->[1] ? $self->{_current} : ()),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_current'}[1] ? : 76 0 76
268 76 @{$self->{_files}})
269 {
270 82 ▶ if ($qid eq $t->[0]) { return $t->[1]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($qid eq $t->[0]) 46 36 82
271 }
272  
273 # As a last resort, attempt to find
274 # the objext in the fs
275  
276 30 for my $p (@{$self->{paths}})
277 {
278 42 $p->[1]->rewind;
279 42 ▶ while (my $n = $p->[1]->read)
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $p->[1]->read) 146 13 159
280 {
281 146 ▶ if ($n eq $qid)
Branch: 1 branch
BranchTrueFalseTotal
if ($n eq $qid) 29 117 146
282 {
283 29 return $p->[0] . '/' . $n;
284 }
285 }
286 }
287  
288 # Otherwise, we have to fail...
289  
290 1 return;
291 }
292  
293 sub _clean_locks
294 10 ▶{
Subroutine _clean_locks: called CC=7 SCAR=19.6
295 10 my $self = shift;
296
297 10 ▶ return unless $self->{lockdir};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"lockdir"} 0 10 10
298  
299 10 for my $p (@{$self->{paths}})
300 {
301 10 my $lock = $p->[0] . '/' . $self->{lockdir};
302 10 mkdir $lock;
303 10 my $d = new IO::Dir $lock;
304 10 ▶ while (my $f = $d->read)
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $d->read) 21 10 31
305 {
306 21 ▶ next if $f eq '.' or $f eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $f eq "." or $f eq ".." 20 1 21
Condition100%3/3
$f eq "." or $f eq ".."
l!l&&r!l&&!r
10101
MC/DC100%2/2
$f eq "." or $f eq ".."
$f eq "." $f eq ".."
Truth table100%3/3
A or B
A= $f eq "."B= $f eq ".."
ABresult
1X1
011
000
307 1 my $name = $lock . '/' . $f;
308 1 ▶ if ((stat($name))[9] + $self->{lockmax} < time)
Branch: 1 branch
BranchTrueFalseTotal
if ((stat $name)[9] + $self->{"lockmax"} < time) 0 1 1
309 {
310 0 unlink $name;
311 }
312 }
313 }
314  
315 }
316  
317 # The test below might seem redundant, but
318 # it's an attempt to improve in a lot of
319 # broken NFS locking implementations.
320  
321 sub _lock
322 47 ▶{
Subroutine _lock: called CC=15 SCAR=33.3
323 47 my $self = shift;
324 47 my $qid = shift;
325  
326 47 $self->{lockfh} = new IO::File;
327  
328 47 ▶ warn "_lock $qid\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 47 47
329  
330 47 ▶ return 1 unless $self->{lockdir};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"lockdir"} 32 15 47
331  
332 15 $self->{lockfile} = $self->{paths}->[(split(/\./, $qid))[1]]->[0];
333  
334 15 ▶ return unless $self->{lockfile};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"lockfile"} 0 15 15
335  
336 15 $self->{_key} = $self->{id} . '-' . $$ . '-' . int(rand(10000));
337 15 $self->{lockfile} .= '/' . $self->{lockdir} . '/' . $qid;
338  
339 15 ▶ warn "_lock lockfile is $self->{lockfile}\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 15 15
340  
341 15 ▶ if (-f $self->{lockfile})
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{"lockfile"}) 3 12 15
342 {
343 3 ▶ if ((stat(_))[9] + $self->{lockmax} < time)
Branch: 1 branch
BranchTrueFalseTotal
if ((stat _)[9] + $self->{'lockmax'} < time) { } 0 3 3
344 {
345 0 ▶ warn "_lock forcing unlink (stale) lockfile\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 0 0
346 0 unlink $self->{lockfile};
347 }
348 else
349 {
350 3 ▶ warn "_lock failing due to previous lock\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 3 3
351 3 return;
352 }
353 }
354 # Store our key in the lock file
355  
356 12 ▶ $self->{lockfh}->open($self->{lockfile}, O_RDWR | O_CREAT) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"lockfh"}->open($self->{"lockfile"}, 66) or return 12 0 12
357 12 $self->{lockfh}->autoflush(1);
358  
359 12 ▶ unless (flock $self->{lockfh}, LOCK_EX | LOCK_NB)
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $self->{"lockfh"}, 6) 0 12 12
360 {
361 0 $self->{lockfh}->close;
362 0 $self->{lockfh} = undef;
363 0 unlink $self->{lockfile};
364 0 $self->{lockfile} = undef;
365 0 return;
366 }
367 12 $self->{lockfh}->print($self->{_key});
368  
369 12 ▶ warn "_lock key $self->{_key} stored\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 12 12
370  
371 # Verify that the key is indeed in the
372 # lock file
373  
374 12 $self->{lockfh}->seek(0, 0);
375 12 chomp(my $rkey = $self->{lockfh}->getline);
376  
377 12 ▶ warn "_lock key $rkey recovered\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 12 12
378  
379 12 ▶ unless ($rkey eq $self->{_key})
Branch: 1 branch
BranchTrueFalseTotal
unless ($rkey eq $self->{"_key"}) 0 12 12
380 {
381 0 $self->{lockfh}->close;
382 0 $self->{lockfh} = undef;
383 0 unlink $self->{lockfile};
384 0 $self->{lockfile} = undef;
385 0 return;
386 }
387  
388 12 ▶ warn "_lock key matched\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 12 12
389  
390 # If all this passed, the lock is ours
391 12 return 1;
392 }
393  
394 =pod
395  
396 =item C<my ($fh, $qid) = $q-E<gt>store();>
397  
398 Store a file in the queue. Returns an array whose first element is an
399 C<IO::Handle> object for writing to the file. The second element is
400 the identifier of the object in the queue.
401  
402 If you created the C<Queue::Dir> object with locking enabled, you must
403 call C<-E<gt>unlock> after closing the file handle.
404  
405 =cut
406  
407 sub store
408 28 ▶{
Subroutine store: called CC=4 SCAR=14.1
Pod documented
409 28 my $self = shift;
410 28 my $fh = new IO::File;
411 28 my $queue = $self->{paths}->[$self->{_rr}];
412 28 my $qid = _timestamp . '.' . $self->{_rr} . '.' . $self->{id};
413 28 my $counter = 0;
414 28 my $pname;
415  
416 28 ▶ warn "Queue::Dir::store() qid=$qid\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 28 28
417  
418 28 $self->{_rr} ++;
419 28 $self->{_rr} %= @{$self->{paths}};
420  
421 28 ▶ while (-f ($pname = $queue->[0] . '/' . $qid . '.' . $counter))
Branch: 1 branch
BranchTrueFalseTotal
if (-f ($pname = $queue->[0] . '/' . $qid . '.' . $counter)) 0 28 28
422 {
423 0 ++ $counter;
424 }
425  
426 28 $qid .= '.' . $counter;
427  
428 28 ▶ $fh->open($pname, "w") or return;
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($pname, "w") or return 28 0 28
429 28 $self->{_current} = [$qid, $pname];
430  
431 28 $self->_lock($qid);
432  
433 28 return ($fh, $qid);
434 }
435  
436 =pod
437  
438 =item C<my $qid = $q-E<gt>next();>
439  
440 Returns the queue identifier of the next file to be processed. When
441 the queue is empty, returns undef.
442  
443 Note that if multiple consumers are working on the same queues in
444 promiscuous mode, the file referenced by the returned id might be
445 removed at any time so care must be used.
446  
447 Entries will be returned in an arbitrary order.
448  
449 =cut
450  
451 sub next
452 46 ▶{
Subroutine next: called CC=5 SCAR=16.1
Pod documented
453 46 my $self = shift;
454  
455 46 ▶ $self->_refresh unless @{$self->{_files}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"_files"};} 22 24 46
456  
457 46 ▶ $self->{_current} = shift @{$self->{_files}} || [0, 0];
Condition100%2/2
shift @{$$self{'_files'};} || [0, 0]
l!l
2521
MC/DC100%1/1
shift @{$$self{'_files'};} || [0, 0]
shift @{$$self{'_files'};}
Truth table100%2/2
A
A= shift @{$$self{'_files'};}
Aresult
11
00
458  
459 46 ▶ warn "Queue::Dir::next() current=", $self->{_current}->[0], "\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 46 46
460  
461 # warn "next: Current queue has\n";
462 # foreach (@{$self->{_files}})
463 # {
464 # warn " $_->[1]\n";
465 # }
466  
467 46 ▶ unless ($self->{_current}->[0])
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_current"}[0]) 21 25 46
468 {
469 21 $self->_refresh;
470 21 return;
471 }
472  
473 25 return $self->{_current}->[0];
474 }
475  
476 =pod
477  
478 =item C<my $fh = $q-E<gt>visit($mode, $qid);>
479  
480 On success, returns an C<IO::Handle> object, opened according to the
481 specified C<$mode> for the file with C<$qid>. If C<$mode> is not
482 specified, it defaults to a read from the start of the file. If
483 C<$qid> is not specified, it defaults to the next entry, as if
484 C<-E<gt>next()> were called. In order for the file to be eligible,
485 either the C<Queue::Dir> object is not created with locking enabled or
486 the file in the queue is not locked.
487  
488 It can fail in a number of situations. The obvious one, is when the
489 queue is empty. The second one, happens when the desired file is no
490 longer in the queue, which can happen if multiple consumers are
491 accessing the queue in promiscuous mode.
492  
493 To help disambiguate both scenarios, undef will be returned on an
494 empty queue. A defined but false value will be returned when the
495 desired file is missing but others remain in the queue.
496  
497 The object in the queue will be automatically locked if this option is
498 enabled when C<-E<gt>new> was called. In this case, you should call
499 the C<-E<gt>unlock> method.
500  
501 =cut
502  
503 sub visit
504 20 ▶{
Subroutine visit: called CC=14 SCAR=29.0
Pod documented
505 20 my $self = shift;
506 20 ▶ my $mode = shift || "r";
Condition100%2/2
shift() || 'r'
l!l
119
MC/DC100%1/1
shift() || 'r'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
507 20 ▶ my $qid = shift || $self->{_current}->[0] || $self->next;
Condition83%5/6
shift() || $self->{'_current'}[0] || $self->next
l!l&&r!l&&!r
1271
shift() or $self->{"_current"}[0]
l!l&&r!l&&!r
0128
MC/DC66%2/3
shift() || $self->{'_current'}[0] || $self->next
shift() $self->{"_current"}[0] $self->next
Truth table75%3/4
A or B || C
A= shift()B= $self->{"_current"}[0]C= $self->next
ABCresult
1XX1
01X1
0011
0000
508  
509 20 ▶ warn "Queue::Dir::visit() qid=$qid\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 20 20
510  
511 20 ▶ return unless $qid;
Branch: 1 branch
BranchTrueFalseTotal
unless $qid 1 19 20
512  
513 19 my $fh = new IO::File;
514 19 my $name;
515
516 19 ▶ until ($name = $self->_name($qid)
Branch: 1 branch
BranchTrueFalseTotal
unless ($name = $self->_name($qid) and -f $name and $self->_lock($qid) and $fh->open($name, $mode)) 3 16 19
Condition55%5/9
$name = $self->_name($qid) and -f $name and $self->_lock($qid) and $fh->open($name, $mode)
!ll&&!rl&&r
3016
$name = $self->_name($qid) and -f $name and $self->_lock($qid)
!ll&&!rl&&r
0316
$name = $self->_name($qid) and -f $name
!ll&&!rl&&r
0019
MC/DC25%1/4
$name = $self->_name($qid) and -f $name and $self->_lock($qid) and $fh->open($name, $mode)
$name = $self->_name($qid) -f $name $self->_lock($qid) $fh->open($name, $mode)
Truth table40%2/5
A and B and C and D
A= $name = $self->_name($qid)B= -f $nameC= $self->_lock($qid)D= $fh->open($name, $mode)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
517 and -f $name
518 and $self->_lock($qid)
519 and $fh->open($name, $mode))
520 {
521 3 ▶ unless ($qid = $self->next)
Branch: 1 branch
BranchTrueFalseTotal
unless ($qid = $self->next) 3 0 3
522 {
523 3 ▶ if (@{$self->{_files}})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'_files'};}) { } 3 0 3
524 {
525 3 ▶ warn "Queue::Dir::visit() ret undef\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 3 3
526 3 return undef;
527 }
528 else
529 {
530 0 ▶ warn "Queue::Dir::visit() ret 0\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 0 0
531 0 return 0;
532 }
533 }
534 }
535  
536 16 return $fh;
537 }
538  
539 =pod
540  
541 =item C<$q-E<gt>done($qid);>
542  
543 Disposes the queue file whose C<$qid> matches the given identifier as
544 well as its potential lock. If none is specified, defaults to the last
545 one used in a C<-E<gt>visit()>.
546  
547 It is a bad idea (or at least rough manners) to C<unlink()> the file
548 without invoking C<-E<gt>done>. Besides, C<-E<gt>done> will do it for
549 you.
550  
551 =cut
552  
553 sub done
554 27 ▶{
Subroutine done: called CC=7 SCAR=19.5
Pod documented
555 27 my $self = shift;
556 27 ▶ my $qid = shift || $self->{_current}->[0];
Condition66%2/3
shift() || $self->{'_current'}[0]
l!l&&r!l&&!r
2340
MC/DC0%0/2
shift() || $self->{'_current'}[0]
shift() $self->{'_current'}[0]
Truth table66%2/3
A || B
A= shift()B= $self->{'_current'}[0]
ABresult
1X1
011
000
557 27 my $wipe = 0;
558  
559 27 ▶ warn "Queue::Dir::done() qid=$qid\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 27 27
560  
561 27 ▶ return if $qid eq 0;
Branch: 1 branch
BranchTrueFalseTotal
if $qid eq 0 0 27 27
562  
563 27 my $name = $self->_name($qid);
564  
565 27 ▶ return unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 26 27
566  
567 26 $self->unlock($qid);
568  
569 26 unlink $name;
570  
571 26 ▶ for (my $i = 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"_files"};}) 10 24 34
572 34 $i < @{$self->{_files}};
573 $i ++)
574 {
575 10 ▶ if ($self->{_files}->[$i]->[0] eq $qid)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_files"}[$i][0] eq $qid) 2 8 10
576 {
577 2 splice(@{$self->{_files}}, $i, 1);
578 2 return;
579 }
580 }
581  
582 }
583  
584 =pod
585  
586 =item C<my $fname = $q-E<gt>name($qid);>
587  
588 Returns the full pathname of the queue file whose id matches
589 C<$qid>. If none is supplied, defaults to the last one obtained
590 through a C<-E<gt>store()>, C<-E<gt>next()> or C<-E<gt>visit()>.
591  
592 It could return C<undef> is the queue object no longer exists.
593  
594 =cut
595  
596 sub name
597 30 ▶{
Subroutine name: called CC=4 SCAR=16.4
Pod documented
598 30 my $self = shift;
599 30 ▶ my $qid = shift || $self->{_current}->[0] || $self->next;
Condition33%2/6
shift() || $self->{'_current'}[0] || $self->next
l!l&&r!l&&!r
3000
shift() or $self->{"_current"}[0]
l!l&&r!l&&!r
3000
MC/DC0%0/3
shift() || $self->{'_current'}[0] || $self->next
shift() $self->{"_current"}[0] $self->next
Truth table25%1/4
A or B || C
A= shift()B= $self->{"_current"}[0]C= $self->next
ABCresult
1XX1
01X1
0011
0000
600 30 ▶ warn "Queue::Dir::name() qid=$qid\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 30 30
601 30 return $self->_name($qid);
602 }
603  
604 =pod
605  
606 =item C<-E<gt>unlock($qid)>
607  
608 Removes any locks outstanding in the file identified by C<$qid>, or
609 the last C<visit()>ed file. Use of this method is only required if the
610 object is created with locking enabled.
611  
612 =cut
613  
614 sub unlock
615 33 ▶{
Subroutine unlock: called CC=5 SCAR=16.1
Pod documented
616 33 my $self = shift;
617 33 ▶ my $qid = shift || $self->{_current}->[0];
Condition66%2/3
shift() || $self->{'_current'}[0]
l!l&&r!l&&!r
2670
MC/DC0%0/2
shift() || $self->{'_current'}[0]
shift() $self->{'_current'}[0]
Truth table66%2/3
A || B
A= shift()B= $self->{'_current'}[0]
ABresult
1X1
011
000
618 33 my $fh = new IO::File;
619  
620 33 ▶ warn "unlock $qid\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Queue::Dir::Debug 0 33 33
621  
622 33 ▶ return 1 unless $self->{lockdir};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"lockdir"} 19 14 33
623 14 ▶ return 1 unless $self->{lockfh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"lockfh"} 5 9 14
624  
625 9 close $self->{lockfh};
626 9 $self->{lockfh} = undef;
627  
628 9 unlink $self->{lockfile};
629 9 $self->{lockfile} = undef;
630  
631 9 return 1;
632 }
633  
634 11 1;
635 __END__
↑ Summary flock-test.pl »