blib/lib/WebSphere/MQTT/Persist/File.pm

filter: Statementstmt 83.9% 120 / 143 Branchbran 61.2% 38 / 62 Conditioncond 70.8% 17 / 24 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 95.6% 22 / 23 Podpod 0.0% 0 / 15 totaltotal 71.9% 203 / 282 CC 40 SCAR 17.9
CC
40
Cov
76%
__ANON__
10.5
updReceivedMessage
10.1
mkdir_p
8.0
CRAP 6.0
« blib/script/publish.pl ↑ Summary blib/script/subscribe.pl »
1 package WebSphere::MQTT::Persist::File;
2  
3 ################
4 #
5 # MQTT: WebSphere MQ Telemetry Transport persistence object
6 #
7 # Re-written in Perl since the IBM C source (mspfp.c) is not in
8 # the list of redistributable files.
9 #
10 # Brian Candler
11 # B.Candler@pobox.com
12 #
13  
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Fcntl qw(SEEK_END SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
19 1 my $class = shift;
20 1 my $basedir = shift;
21 1 ▶ $basedir = '/tmp/wmqtt' unless defined $basedir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $basedir 0 1 1
22  
23 # Store parameters
24 1 my $self = {
25 'basedir' => $basedir,
26 };
27
28 # Bless the hash into an object
29 1 bless $self, $class;
30  
31 1 return $self;
32 }
33  
34 1 ▶sub open {
Subroutine open: called CC=1 SCAR=0.0
Pod undocumented
35 1 my ($self, $clientid, $broker, $port) = @_;
36  
37 $self->{'sentdir'} = sprintf("%s/%s/%s_%s/sent",
38 1 $self->{'basedir'}, $clientid, $broker, $port);
39 $self->{'rcvddir'} = sprintf("%s/%s/%s_%s/rcvd",
40 1 $self->{'basedir'}, $clientid, $broker, $port);
41 1 mkdir_p($self->{'sentdir'}, 0740);
42 1 mkdir_p($self->{'rcvddir'}, 0740);
43 1 0;
44 }
45  
46 2 ▶sub mkdir_p {
Subroutine mkdir_p: called CC=7 SCAR=20.7
Pod undocumented
47 2 my ($name, $mode) = @_;
48 2 ▶ $mode = 0777 unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 2 2
49  
50 2 my @dirs = split('/', $name);
51 2 my $path = '';
52 2 ▶ while(@dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 10 2 12
53 10 $path .= shift(@dirs).'/';
54 10 ▶ next if $path eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "/" 0 10 10
55 1 ▶ next if mkdir($path, $mode) || $!{EEXIST} || $!{EISDIR};
Subroutine BEGIN: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
if mkdir $path, $mode or $!{"EEXIST"} or $!{"EISDIR"} 10 0 10
Condition50%3/6
mkdir $path, $mode or $!{"EEXIST"} or $!{"EISDIR"}
l!l&&r!l&&!r
1000
mkdir $path, $mode or $!{"EEXIST"}
l!l&&r!l&&!r
550
MC/DC0%0/3
mkdir $path, $mode or $!{"EEXIST"} or $!{"EISDIR"}
mkdir $path, $mode $!{"EEXIST"} $!{"EISDIR"}
Truth table50%2/4
A or B or C
A= mkdir $path, $modeB= $!{"EEXIST"}C= $!{"EISDIR"}
ABCresult
1XX1
01X1
0011
0000
56 0 croak("mkdir_p: $!");
57 }
58 }
59  
60 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod undocumented
61 0 0;
62 }
63  
64 # Called if clean_start=1 on connect. Delete all files in
65 # the sent and rcvd directories
66  
67 1 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
68 1 my $self = shift;
69 1 ▶ forallfiles($self->{'sentdir'}, sub { unlink $_[0] });
Subroutine __ANON__: called CC=1 SCAR=0.0
70 1 ▶ forallfiles($self->{'rcvddir'}, sub { unlink $_[0] });
Subroutine __ANON__: called CC=1 SCAR=0.0
71 1 0;
72 }
73  
74 # Run the passed function for all files in a directory
75 # (can you tell I prefer Ruby? :-)
76  
77 14 ▶sub forallfiles {
Subroutine forallfiles: called CC=5 SCAR=16.1
Pod undocumented
78 14 my $dir = shift;
79 14 my $callback = shift;
80 14 ▶ opendir(DIR,$dir) || croak("opendir $dir: $!");
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or croak("opendir ${dir}: $!") 14 0 14
81 14 ▶ while (my $f = readdir(DIR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $f = readdir DIR)) 58 14 72
82 58 ▶ next if ($f eq '.' || $f eq '..');
Branch: 1 branch
BranchTrueFalseTotal
if $f eq "." or $f eq ".." 28 30 58
Condition100%3/3
$f eq "." or $f eq ".."
l!l&&r!l&&!r
141430
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
83 30 $callback->("$dir/$f");
84 }
85 14 closedir(DIR);
86 }
87  
88 # Called if clean_start=0 on connect. Load all received messages found on
89 # disk into an array of [id,message,id,message,...]
90  
91 6 ▶sub getAllReceivedMessages {
Subroutine getAllReceivedMessages: called CC=1 SCAR=0.4
Pod undocumented
92 6 my $self = shift;
93  
94 6 my @res = ();
95 10 ▶ forallfiles($self->{'rcvddir'}, sub { BLOCK: {
Subroutine __ANON__: called CC=5 SCAR=19.0
96 10 ▶ last BLOCK unless $_[0] =~ /\/(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ m[/(\d+)$] 3 7 10
97 7 my $key = $1;
98 7 ▶ unless (CORE::open(F, '<', $_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::open F, "<", $_[0]) 0 7 7
99 0 warn("open $_[0]: $!\n");
100 0 last BLOCK;
101 }
102 7 my @stat = stat(F);
103 7 ▶ unless (@stat) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@stat) 0 7 7
104 0 warn("stat $_[0]: $!\n");
105 0 CORE::close(F);
106 0 last BLOCK;
107 }
108 7 my $len = $stat[7];
109 7 my $data = "";
110 7 ▶ if (sysread(F, $data, $len) != $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread(F, $data, $len) != $len) 0 7 7
111 0 warn("sysread $_[0]: $!\n");
112 0 CORE::close(F);
113 0 last BLOCK;
114 }
115 7 push @res, $key, $data;
116 7 CORE::close(F);
117 6 }});
118 6 @res;
119 }
120  
121 # Called if clean_start=0 on connect. Load all received messages found on
122 # disk into an array of [id,message,id,message,...]. Note that the library
123 # may not actually attempt to send these messages until 'retry_interval'
124 # seconds have passed.
125 #
126 # When recovering sent messages we need to ensure that we only restore the
127 # latest message associated with a particular key. When updSentMessage is
128 # called to replace a PUBLISH with a PUBREL there is a small overlap where
129 # both files (NNN and NNNu) are present. Failure at this point would result
130 # in both messages be available for recovery, which would result in
131 # duplication. So if both a PUBLISH and PUBREL are found for the same key,
132 # we need to ensure that only the PUBREL is recovered.
133  
134 6 ▶sub getAllSentMessages {
Subroutine getAllSentMessages: called CC=1 SCAR=0.1
Pod undocumented
135 6 my $self = shift;
136  
137 6 my @res = ();
138 6 my %seen = ();
139 10 ▶ forallfiles($self->{'sentdir'}, sub { BLOCK: {
Subroutine __ANON__: called CC=9 SCAR=23.5
140 10 ▶ last BLOCK unless $_[0] =~ /\/(\d+)(u?)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ m[/(\d+)(u?)$] 1 9 10
141 9 my ($key, $u) = ($1, $2);
142 # This is NNN and we've already seen NNNu? Ignore it
143 9 ▶ last BLOCK if ($u eq '' && $seen{$key});
Branch: 1 branch
BranchTrueFalseTotal
if $u eq "" and $seen{$key} 1 8 9
Condition100%3/3
$u eq "" and $seen{$key}
!ll&&!rl&&r
441
MC/DC100%2/2
$u eq "" and $seen{$key}
$u eq "" $seen{$key}
Truth table100%3/3
A and B
A= $u eq ""B= $seen{$key}
ABresult
0X0
100
111
144 # Read in the file
145 8 ▶ unless (CORE::open(F, '<', $_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::open F, "<", $_[0]) 0 8 8
146 0 warn("open $_[0]: $!");
147 0 last BLOCK;
148 }
149 8 my @stat = stat(F);
150 8 ▶ unless (@stat) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@stat) 0 8 8
151 0 warn("stat $_[0]: $!\n");
152 0 CORE::close(F);
153 0 last BLOCK;
154 }
155 8 my $len = $stat[7];
156 8 my $data = "";
157 8 ▶ if (sysread(F, $data, $len) != $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread(F, $data, $len) != $len) 0 8 8
158 0 warn("sysread $_[0]: $!\n");
159 0 CORE::close(F);
160 0 last BLOCK;
161 }
162 8 CORE::close(F);
163 # This is NNNu and we've already seen NNN? Replace it
164 8 ▶ if ($u eq 'u' && $seen{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u eq "u" and $seen{$key}) 1 7 8
Condition100%3/3
$u eq "u" and $seen{$key}
!ll&&!rl&&r
431
MC/DC100%2/2
$u eq "u" and $seen{$key}
$u eq "u" $seen{$key}
Truth table100%3/3
A and B
A= $u eq "u"B= $seen{$key}
ABresult
0X0
100
111
165 1 $res[$seen{$key}] = $data;
166 1 last BLOCK;
167 }
168 7 push @res, $key, $data;
169 7 $seen{$key} = $#res;
170 6 }});
171 6 @res;
172 }
173  
174 # The actual writing of a message file. We take a bit of care here
175 # to ensure only whole messages are left in the filesystem
176  
177 5 ▶sub addMessage {
Subroutine addMessage: called CC=5 SCAR=18.3
Pod undocumented
178 5 my ($self, $key, $data, $dir, $suffix) = @_;
179 5 my $tmp = "$dir/tmp$key${suffix}_$$";
180 5 ▶ CORE::open(F,'>',$tmp) || return 1;
Branch: 1 branch
BranchTrueFalseTotal
CORE::open F, ">", $tmp or return 1 5 0 5
181 5 binmode(F);
182 5 ▶ goto FAIL unless syswrite(F, $data) == length($data);
Branch: 1 branch
BranchTrueFalseTotal
unless syswrite(F, $data) == length $data 0 5 5
183 5 ▶ goto FAIL2 unless CORE::close(F);
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::close F 0 5 5
184 5 ▶ goto FAIL2 unless rename($tmp,"$dir/$key$suffix");
Branch: 1 branch
BranchTrueFalseTotal
unless rename $tmp, "$dir/$key$suffix" 0 5 5
185 5 return 0;
186 0 FAIL:
187 CORE::close(F);
188 0 FAIL2:
189 unlink($tmp);
190 0 return 1;
191 }
192  
193 2 ▶sub addSentMessage {
Subroutine addSentMessage: called CC=1 SCAR=0.0
Pod undocumented
194 2 my ($self, $key, $data) = @_;
195 2 $self->addMessage($key,$data,$self->{'sentdir'}, '');
196 }
197  
198 1 ▶sub updSentMessage {
Subroutine updSentMessage: called CC=2 SCAR=7.0
Pod undocumented
199 1 my ($self, $key, $data) = @_;
200 1 my $rc = $self->addMessage($key,$data,$self->{'sentdir'}, 'u');
201 1 ▶ unlink("$self->{'sentdir'}/$key") if $rc == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rc == 0 1 0 1
202 1 $rc;
203 }
204  
205 2 ▶sub delSentMessage {
Subroutine delSentMessage: called CC=5 SCAR=17.0
Pod undocumented
206 2 my $self = shift;
207 2 my $key = shift;
208 2 ▶ return 1 if (unlink("$self->{'sentdir'}/$key") == 0 && ! $!{ENOENT});
Branch: 1 branch
BranchTrueFalseTotal
if unlink(qq[$self->{"sentdir"}/$key]) == 0 and not $!{"ENOENT"} 0 2 2
Condition66%2/3
unlink(qq[$self->{"sentdir"}/$key]) == 0 and not $!{"ENOENT"}
!ll&&!rl&&r
110
MC/DC0%0/2
unlink(qq[$self->{"sentdir"}/$key]) == 0 and not $!{"ENOENT"}
unlink(qq[$self->{"sentdir"}/$key]) == 0 not $!{"ENOENT"}
Truth table66%2/3
A and not(B)
A= unlink(qq[$self->{"sentdir"}/$key]) == 0B= not $!{"ENOENT"}
ABresult
0X0
100
111
209 2 ▶ return 1 if (unlink("$self->{'sentdir'}/${key}u") == 0 && ! $!{ENOENT});
Branch: 1 branch
BranchTrueFalseTotal
if unlink(qq[$self->{"sentdir"}/${key}u]) == 0 and not $!{"ENOENT"} 0 2 2
Condition66%2/3
unlink(qq[$self->{"sentdir"}/${key}u]) == 0 and not $!{"ENOENT"}
!ll&&!rl&&r
110
MC/DC0%0/2
unlink(qq[$self->{"sentdir"}/${key}u]) == 0 and not $!{"ENOENT"}
unlink(qq[$self->{"sentdir"}/${key}u]) == 0 not $!{"ENOENT"}
Truth table66%2/3
A and not(B)
A= unlink(qq[$self->{"sentdir"}/${key}u]) == 0B= not $!{"ENOENT"}
ABresult
0X0
100
111
210 2 0;
211 }
212  
213 2 ▶sub addReceivedMessage {
Subroutine addReceivedMessage: called CC=1 SCAR=0.0
Pod undocumented
214 2 my ($self, $key, $data) = @_;
215 2 $self->addMessage($key,$data,$self->{'rcvddir'}, '');
216 }
217  
218 # This is the weird one. We have to OR the last byte of the file with 0x01
219  
220 1 ▶sub updReceivedMessage {
Subroutine updReceivedMessage: called CC=8 SCAR=23.2
Pod undocumented
221 1 my $self = shift;
222 1 my $key = shift;
223  
224 1 ▶ CORE::open(F,'+<',"$self->{'rcvddir'}/$key") || return 1;
Branch: 1 branch
BranchTrueFalseTotal
CORE::open F, "+<", qq[$self->{"rcvddir"}/$key] or return 1 1 0 1
225 1 binmode(F);
226 1 my $pos = sysseek(F, -1, Fcntl::SEEK_END);
227 1 ▶ goto FAIL unless defined $pos && $pos >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos and $pos >= 0 0 1 1
Condition33%1/3
defined $pos and $pos >= 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $pos and $pos >= 0
defined $pos $pos >= 0
Truth table33%1/3
A and B
A= defined $posB= $pos >= 0
ABresult
0X0
100
111
228 1 my $d = '';
229 1 ▶ goto FAIL unless sysread(F, $d, 1) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless sysread(F, $d, 1) == 1 0 1 1
230 1 $d = chr(ord($d)|0x01);
231 1 my $pos2 = sysseek(F, $pos, Fcntl::SEEK_SET);
232 1 ▶ goto FAIL unless $pos2 == $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos2 == $pos 0 1 1
233 1 ▶ goto FAIL unless syswrite(F, $d, 1) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless syswrite(F, $d, 1) == 1 0 1 1
234 1 ▶ return 1 unless CORE::close(F);
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::close F 0 1 1
235 1 return 0;
236 0 FAIL:
237 CORE::close(F);
238 0 1;
239 }
240  
241 2 ▶sub delReceivedMessage {
Subroutine delReceivedMessage: called CC=1 SCAR=0.0
Pod undocumented
242 2 my $self = shift;
243 2 my $key = shift;
244 2 return unlink("$self->{'rcvddir'}/$key") != 1;
245 }
246  
247 1 1;
248  
249 __END__
« blib/script/publish.pl ↑ Summary blib/script/subscribe.pl »