lib/IPC/Transit/Internal.pm

filter: Statementstmt 89.4% 85 / 95 Branchbran 67.5% 27 / 40 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 81.1% 133 / 164 CC 26 SCAR 16.2
CC
26
Cov
81%
_load_transit_config
8.3
_get_queue_id
6.8
_write_transit_config
6.2
CRAP 5.0
« blib/lib/IPC/Transit/Serialize.pm ↑ Summary Build »
1 package IPC::Transit::Internal;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use IPC::SysV;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use IPC::Msg;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 3 use vars qw(
10 $config
11 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 {
14 3 my $queue_cache = {};
15 sub
16 _initialize_queue {
17 384 ▶ my %args = @_;
Subroutine _initialize_queue: called CC=3 SCAR=11.0
18 384 my $qid = _get_queue_id(%args);
19 381 ▶ if(not $queue_cache->{$qid}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($queue_cache->{$qid}) 3 378 381
20 3 ▶ $queue_cache->{$qid} = IPC::Msg->new($qid, _get_flags('create_ipc'))
Branch: 1 branch
BranchTrueFalseTotal
$queue_cache->{$qid} = "IPC::Msg"->new($qid, _get_flags("create_ipc")) or die "failed to _initialize_queue: failed to create queue_id ${qid}: $!\n" 0 3 3
21 or die "failed to _initialize_queue: failed to create queue_id $qid: $!\n";
22 }
23 381 return $queue_cache->{$qid};
24 }
25 }
26  
27 sub
28 3 _get_transit_config_file {
29 15 ▶ my $cf = $IPC::Transit::config_file || 'transit.conf';
Subroutine _get_transit_config_file: called CC=3 SCAR=11.7
Condition50%1/2
$IPC::Transit::config_file || 'transit.conf'
l!l
015
MC/DC0%0/1
$IPC::Transit::config_file || 'transit.conf'
$IPC::Transit::config_file
Truth table50%1/2
A
A= $IPC::Transit::config_file
Aresult
11
00
30 15 ▶ my $dir = $IPC::Transit::config_dir || '/tmp/';
Condition50%1/2
$IPC::Transit::config_dir || '/tmp/'
l!l
015
MC/DC0%0/1
$IPC::Transit::config_dir || '/tmp/'
$IPC::Transit::config_dir
Truth table50%1/2
A
A= $IPC::Transit::config_dir
Aresult
11
00
31 15 return "$dir/$cf";
32 }
33  
34 sub
35 _lock_config_file {
36 4 ▶ my $lock_file = _get_transit_config_file() . '.lock';
Subroutine _lock_config_file: called CC=4 SCAR=15.5
37 4 my ($have_lock, $fh);
38 4 for (1..10) {
39 4 ▶ if(sysopen($fh, $lock_file, _get_flags('exclusive_lock'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen $fh, $lock_file, _get_flags("exclusive_lock")) 4 0 4
40 4 $have_lock = 1;
41 4 last;
42 }
43 0 sleep 1;
44 }
45 4 ▶ if(not $have_lock) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $have_lock) 0 4 4
46 0 _unlock_config_file();
47 0 sysopen($fh, $lock_file, _get_flags('exclusive_lock'));
48 }
49 #we have the advisory lock for sure now
50 }
51  
52  
53 sub
54 _unlock_config_file {
55 7 ▶ my $lock_file = _get_transit_config_file() . '.lock';
Subroutine _unlock_config_file: called CC=2 SCAR=6.9
56 7 ▶ unlink $lock_file or die "_unlock_config_file: failed to unlink $lock_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $lock_file or die "_unlock_config_file: failed to unlink ${lock_file}: $!" 3 4 7
57 }
58  
59 sub
60 _load_transit_config {
61 3 ▶ my $cf = _get_transit_config_file();
Subroutine _load_transit_config: called CC=8 SCAR=21.2
62 3 ▶ if(not -r $cf) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $cf) 1 2 3
63 1 my $previous_umask = umask 0000;
64 1 ▶ open my $fh, '>', $cf or die "failed to open '$cf' for writing: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $cf or die "failed to open '${cf}' for writing: $!\n" 0 1 1
65 1 ▶ print $fh '' or die "failed to write to '$cf': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
print $fh "" or die "failed to write to '${cf}': $!\n" 0 1 1
66 1 ▶ close $fh or die "failed to close '$cf': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "failed to close '${cf}': $!" 0 1 1
67 1 umask $previous_umask;
68 }
69 3 my $queues = {};
70 3 ▶ open my $fh, '<', $cf or die "failed to open '$cf' for writing: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $cf or die "failed to open '${cf}' for writing: $!\n" 0 3 3
71 3 ▶ while(my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 2 3 5
72 2 chomp $line;
73 2 my ($qname, $qid, @others) = split ':', $line;
74 2 $queues->{$qname} = { qid => $qid, @others };
75 }
76 3 ▶ close $fh or die "failed to close '$cf': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "failed to close '${cf}': $!" 0 3 3
77 3 $config->{queues} = $queues;
78 3 return $config;
79 }
80  
81 sub
82 _write_transit_config {
83 1 ▶ my $cf = _get_transit_config_file();
Subroutine _write_transit_config: called CC=6 SCAR=18.2
84 1 my $previous_umask = umask 0000;
85 1 _lock_config_file();
86 1 eval {
87 1 ▶ open my $fh, '>', $cf or die "failed to open '$cf' for writing: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $cf or die "failed to open '${cf}' for writing: $!\n" 0 1 1
88 1 ▶ while (my($qname, $rec) = each %{$config->{queues}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$IPC::Transit::Internal::config{"queues"};}) 1 1 2
89 1 ▶ print $fh "$qname:$rec->{qid}\n" or die "failed to write to '$cf': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
print $fh qq[${qname}:$rec->{"qid"}\n] or die "failed to write to '${cf}': $!\n" 0 1 1
90 }
91 1 ▶ close $fh or die "failed to close '$cf': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "failed to close '${cf}': $!" 0 1 1
92 };
93 1 _unlock_config_file();
94 1 umask $previous_umask;
95 1 ▶ die "_write_transit_config failed: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
96 1 return $config;
97 }
98  
99  
100 sub
101 _get_queue_id {
102 384 ▶ my %args = @_;
Subroutine _get_queue_id: called CC=6 SCAR=19.1
103 384 my $qname = $args{qname};
104 return $config->{queues}->{$qname}->{qid}
105 384 ▶ if $config->{queues} and $config->{queues}->{$qname};
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname} 381 3 384
Condition66%2/3
$IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname}
!ll&&!rl&&r
30381
MC/DC50%1/2
$IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname}
$IPC::Transit::Internal::config->{"queues"} $IPC::Transit::Internal::config->{"queues"}{$qname}
Truth table66%2/3
A and B
A= $IPC::Transit::Internal::config->{"queues"}B= $IPC::Transit::Internal::config->{"queues"}{$qname}
ABresult
0X0
100
111
106  
107 3 _lock_config_file();
108 3 eval {
109 3 _load_transit_config();
110 3 ▶ if($config->{queues} and $config->{queues}->{$qname}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname}) 2 1 3
Condition66%2/3
$IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname}
!ll&&!rl&&r
012
MC/DC50%1/2
$IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname}
$IPC::Transit::Internal::config->{"queues"} $IPC::Transit::Internal::config->{"queues"}{$qname}
Truth table66%2/3
A and B
A= $IPC::Transit::Internal::config->{"queues"}B= $IPC::Transit::Internal::config->{"queues"}{$qname}
ABresult
0X0
100
111
111 2 _unlock_config_file();
112 2 return $config->{queues}->{$qname}->{qid};
113 }
114 1 my $next_number;
115 1 ▶ if(scalar keys %{$config->{queues}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$IPC::Transit::Internal::config{'queues'};}) { } 0 1 1
116 0 { my @current_numbers = sort {$a <=> $b} values %{$config->{queues}};
117 0 my $highest_number = pop @current_numbers;
118 0 $next_number = $highest_number++;
119 }
120 } else {
121 #$config->{queues}->{$qname} = { qid => 1 };
122 1 $next_number = 1;
123 }
124 1 $config->{queues}->{$qname} = { qid => $next_number };
125 1 _unlock_config_file();
126 1 _write_transit_config();
127 };
128  
129 3 _unlock_config_file();
130 0 return $config->{queues}->{$qname}->{qid};
131 }
132  
133 #gnarly looking UNIX goop hidden below
134 {
135 3 my $flags = {
136 create_ipc => IPC::SysV::S_IRUSR() |
137 IPC::SysV::S_IWUSR() |
138 IPC::SysV::S_IRGRP() |
139 IPC::SysV::S_IWGRP() |
140 IPC::SysV::S_IROTH() |
141 IPC::SysV::S_IWOTH() |
142 IPC::SysV::IPC_CREAT(),
143  
144 nowait => IPC::SysV::IPC_NOWAIT(),
145  
146 exclusive_lock => POSIX::O_RDWR() |
147 POSIX::O_CREAT() |
148 POSIX::O_EXCL(),
149  
150 nonblock => POSIX::O_NONBLOCK(),
151 };
152  
153 sub
154 _get_flags {
155 388 ▶ my $name = shift;
Subroutine _get_flags: called CC=1 SCAR=0.0
156 388 return $flags->{$name};
157 }
158 }
159 3 1;
« blib/lib/IPC/Transit/Serialize.pm ↑ Summary Build »