blib/lib/Protocol/HAP/Store/File.pm

filter: Statementstmt 85.9% 141 / 164 Branchbran 66.6% 44 / 66 Conditioncond 58.8% 20 / 34 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 100% 29 / 29 Podpod 7.6% 1 / 13 totaltotal 73.7% 242 / 328 CC 48 SCAR 15.5
CC
48
Cov
78%
_write_atomic
11.6
_ensure_dir
8.3
load_pairings
7.0
CRAP 4.7
« lib/Protocol/HAP/HTTP.pm ↑ Summary blib/lib/Protocol/HAP/TLV.pm »
1 # ex:ts=8 sw=4:
2 # $OpenBSD$
3 #
4 # Copyright (c) 2026 Dick Olsson <hi@senzilla.io>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  
18 2 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 package Protocol::HAP::Store::File;
21 2 our $VERSION = '0.1.0';
22  
23 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Fcntl qw(O_CREAT O_EXCL O_TRUNC O_WRONLY);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Protocol::HAP;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Protocol::HAP::Store::File - the store contract over files.
30 #
31 # This is the durable implementation of the twelve store methods that
32 # Protocol/HAP/Store.pod documents. Everything here is HAP: the
33 # long-term key files, the pairings format, the counters, and the rule
34 # that every pairing change bumps the configuration number.
35 #
36 # The tier holds a sans-IO engine, and this module writes files. That
37 # is the point: sans-IO describes Protocol::HAP::Server, and the store
38 # is the injected seam that keeps the engine pure.
39 #
40 # The layout on disk is:
41 #
42 # <path>/accessory_ltsk mode 0600
43 # <path>/accessory_ltpk mode 0644
44 # <path>/pairings.db mode 0600
45 # <path>/state.json mode 0600, the counters
46 #
47 # Every write sets the mode at the open, before the first byte. A
48 # chmod after the write leaves a window in which the identity of the
49 # accessory is world-readable.
50  
51 # Protocol::HAP::Store::File->new(%args):
52 # path => $dir the state directory (required)
53 # logger => $obj a logger (default the null logger)
54 #
55 # The caller owns the path. This module holds no host policy, so
56 # it carries no default location.
57 15 ▶sub new ( $class, %args )
Subroutine new: called CC=5 SCAR=16.8
Pod documented
58 {
59 15 my $path = $args{path};
60 15 ▶ croak 'path parameter required'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 1 14 15
Condition66%2/3
defined $path and length $path
!ll&&!rl&&r
1014
MC/DC50%1/2
defined $path and length $path
defined $path length $path
Truth table66%2/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
61 unless defined $path && length $path;
62  
63 my $self = bless {
64 path => $path,
65 14 ▶ logger => $args{logger} // Protocol::HAP->null_logger,
Condition33%1/3
$args{"logger"} // "Protocol::HAP"->null_logger
l!l&&r!l&&!r
0140
MC/DC0%0/2
$args{"logger"} // "Protocol::HAP"->null_logger
$args{"logger"} "Protocol::HAP"->null_logger
Truth table33%1/3
A // B
A= $args{"logger"}B= "Protocol::HAP"->null_logger
ABresult
1X1
011
000
66 pairings_file => "$path/pairings.db",
67 accessory_ltsk_file => "$path/accessory_ltsk",
68 accessory_ltpk_file => "$path/accessory_ltpk",
69 state_file => "$path/state.json",
70 state => {},
71 }, $class;
72  
73 14 ▶ $self->_ensure_dir( $path, 0700 )
Branch: 1 branch
BranchTrueFalseTotal
$self->_ensure_dir($path, 448) or croak("Cannot create the state directory $path") 14 0 14
74 or croak "Cannot create the state directory $path";
75 14 $self->_load_state;
76  
77 14 return $self;
78 }
79  
80 # --- the accessory identity -----------------------------------------------
81  
82 4 ▶sub load_accessory_keys ($self)
Subroutine load_accessory_keys: called CC=3 SCAR=11.0
Pod undocumented
83 {
84 4 ▶ if ( -f $self->{accessory_ltsk_file}
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{"accessory_ltsk_file"} and -f $self->{"accessory_ltpk_file"}) 2 2 4
Condition66%2/3
-f $self->{"accessory_ltsk_file"} and -f $self->{"accessory_ltpk_file"}
!ll&&!rl&&r
202
MC/DC50%1/2
-f $self->{"accessory_ltsk_file"} and -f $self->{"accessory_ltpk_file"}
-f $self->{"accessory_ltsk_file"} -f $self->{"accessory_ltpk_file"}
Truth table66%2/3
A and B
A= -f $self->{"accessory_ltsk_file"}B= -f $self->{"accessory_ltpk_file"}
ABresult
0X0
100
111
85 && -f $self->{accessory_ltpk_file} )
86 {
87 2 $self->{logger}->debug('Loading accessory keys from storage');
88 2 my $ltsk = $self->_read( $self->{accessory_ltsk_file} );
89 2 my $ltpk = $self->_read( $self->{accessory_ltpk_file} );
90 2 return ( $ltsk, $ltpk );
91 }
92  
93 2 $self->{logger}->debug('No existing accessory keys found');
94 2 return ();
95 }
96  
97 4 ▶sub save_accessory_keys ( $self, $ltsk, $ltpk )
Subroutine save_accessory_keys: called CC=3 SCAR=11.4
Pod undocumented
98 {
99 4 $self->{logger}->debug('Generating and saving new accessory keys');
100  
101 4 ▶ $self->_write( $self->{accessory_ltsk_file}, $ltsk, 0600 )
Branch: 1 branch
BranchTrueFalseTotal
$self->_write($self->{"accessory_ltsk_file"}, $ltsk, 384) or croak("Cannot store the accessory secret key") 4 0 4
102 or croak 'Cannot store the accessory secret key';
103 4 ▶ $self->_write( $self->{accessory_ltpk_file}, $ltpk, 0644 )
Branch: 1 branch
BranchTrueFalseTotal
$self->_write($self->{"accessory_ltpk_file"}, $ltpk, 420) or croak("Cannot store the accessory public key") 4 0 4
104 or croak 'Cannot store the accessory public key';
105  
106 4 return;
107 }
108  
109 # --- the pairings ---------------------------------------------------------
110  
111 26 ▶sub load_pairings ($self)
Subroutine load_pairings: called CC=7 SCAR=19.5
Pod undocumented
112 {
113 26 ▶ return {} unless -f $self->{pairings_file};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->{"pairings_file"} 6 20 26
114  
115 20 $self->{logger}->debug('Loading pairings from storage');
116 20 my %pairings;
117 open my $fh, '<', $self->{pairings_file}
118 20 ▶ or die 'Cannot open pairings file: ' . _reason($!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"pairings_file"} or die 'Cannot open pairings file: ' . _reason($!) 20 0 20
119  
120 20 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 113 20 133
121 113 chomp $line;
122 113 ▶ next if $line =~ /^#/ || $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/u or $line =~ /^\s*$/u 80 33 113
Condition100%3/3
$line =~ /^#/u or $line =~ /^\s*$/u
l!l&&r!l&&!r
602033
MC/DC100%2/2
$line =~ /^#/u or $line =~ /^\s*$/u
$line =~ /^#/u $line =~ /^\s*$/u
Truth table100%3/3
A or B
A= $line =~ /^#/uB= $line =~ /^\s*$/u
ABresult
1X1
011
000
123  
124 # Format: controller_id:ltpk_hex:permissions
125 33 ▶ if ( $line =~ /^([^:]+):([^:]+):([01])$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^:]+):([^:]+):([01])$/u) 33 0 33
126 33 my ( $id, $ltpk_hex, $perms ) = ( $1, $2, $3 );
127 33 $pairings{$id} = {
128 ltpk => pack( 'H*', $ltpk_hex ),
129 permissions => $perms,
130 };
131 }
132 }
133  
134 20 close $fh;
135  
136 20 return \%pairings;
137 }
138  
139 11 ▶sub save_pairing ( $self, $controller_id, $ltpk, $permissions = 1 )
Subroutine save_pairing: called CC=2 SCAR=6.9
Pod undocumented
140 {
141 $self->{logger}
142 11 ->debug( 'Saving pairing for controller: %s', $controller_id );
143 11 my $pairings = $self->load_pairings;
144 11 $pairings->{$controller_id} = {
145 ltpk => $ltpk,
146 permissions => $permissions,
147 };
148  
149 11 $self->_save_pairings($pairings);
150 11 $self->increment_config_number();
151  
152 11 return;
153 }
154  
155 5 ▶sub remove_pairing ( $self, $controller_id )
Subroutine remove_pairing: called CC=1 SCAR=0.0
Pod undocumented
156 {
157 $self->{logger}
158 5 ->debug( 'Removing pairing for controller: %s', $controller_id );
159 5 my $pairings = $self->load_pairings;
160 5 delete $pairings->{$controller_id};
161  
162 5 $self->_save_pairings($pairings);
163 5 $self->increment_config_number();
164  
165 5 return;
166 }
167  
168 # remove_all_pairings() - Remove all pairings. This is a factory
169 # reset. The engine calls this method when it removes the last admin
170 # pairing (HAP-Pairing.md ยง7.2).
171 2 ▶sub remove_all_pairings ($self)
Subroutine remove_all_pairings: called CC=1 SCAR=0.0
Pod undocumented
172 {
173 2 $self->{logger}->debug('Removing all pairings');
174 2 $self->_save_pairings( {} );
175 2 $self->increment_config_number();
176  
177 2 return;
178 }
179  
180 18 ▶sub _save_pairings ( $self, $pairings )
Subroutine _save_pairings: called CC=3 SCAR=11.0
181 {
182 18 my $text =
183 "# OpenHAP Pairings Database\n"
184 . "# Format: controller_id:ltpk_hex:permissions\n"
185 . "# Permissions: 1=admin, 0=regular\n\n";
186  
187 18 for my $id ( sort keys %$pairings ) {
188 24 my $ltpk_hex = unpack( 'H*', $pairings->{$id}{ltpk} );
189 24 my $perms = $pairings->{$id}{permissions};
190 24 $text .= "$id:$ltpk_hex:$perms\n";
191 }
192  
193 # The file is 0600 from its first byte. A pairing record names
194 # every controller that may reach the accessory.
195 18 ▶ $self->_write( $self->{pairings_file}, $text, 0600 )
Branch: 1 branch
BranchTrueFalseTotal
$self->_write($self->{"pairings_file"}, $text, 384) or croak("Cannot write the pairings file") 18 0 18
196 or croak 'Cannot write the pairings file';
197  
198 18 return;
199 }
200  
201 # --- the counters ---------------------------------------------------------
202  
203 # The configuration number tells a controller that the accessory
204 # database changed [HAP-Accessories]. It only ever goes up: a
205 # controller that sees it go backwards drops the accessory and pairs
206 # again. Thus the value starts at 1, not at 0.
207 35 ▶sub get_config_number ($self)
Subroutine get_config_number: called CC=3 SCAR=11.0
Pod undocumented
208 {
209 35 my $number = $self->{state}{config_number};
210 35 ▶ return 1 unless defined $number && $number =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number and $number =~ /^\d+$/u 10 25 35
Condition66%2/3
defined $number and $number =~ /^\d+$/u
!ll&&!rl&&r
10025
MC/DC50%1/2
defined $number and $number =~ /^\d+$/u
defined $number $number =~ /^\d+$/u
Truth table66%2/3
A and B
A= defined $numberB= $number =~ /^\d+$/u
ABresult
0X0
100
111
211  
212 25 return $number;
213 }
214  
215 20 ▶sub increment_config_number ($self)
Subroutine increment_config_number: called CC=1 SCAR=0.0
Pod undocumented
216 {
217 20 my $number = $self->get_config_number + 1;
218 20 $self->_set( config_number => $number );
219  
220 20 return $number;
221 }
222  
223 4 ▶sub get_config_digest ($self)
Subroutine get_config_digest: called CC=1 SCAR=0.0
Pod undocumented
224 {
225 4 return $self->{state}{config_digest};
226 }
227  
228 2 ▶sub save_config_digest ( $self, $digest )
Subroutine save_config_digest: called CC=1 SCAR=0.0
Pod undocumented
229 {
230 2 $self->_set( config_digest => $digest );
231  
232 2 return;
233 }
234  
235 7 ▶sub get_auth_attempts ($self)
Subroutine get_auth_attempts: called CC=3 SCAR=11.0
Pod undocumented
236 {
237 7 my $count = $self->{state}{auth_attempts};
238 7 ▶ return 0 unless defined $count && $count =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count and $count =~ /^\d+$/u 3 4 7
Condition66%2/3
defined $count and $count =~ /^\d+$/u
!ll&&!rl&&r
304
MC/DC50%1/2
defined $count and $count =~ /^\d+$/u
defined $count $count =~ /^\d+$/u
Truth table66%2/3
A and B
A= defined $countB= $count =~ /^\d+$/u
ABresult
0X0
100
111
239  
240 4 return $count;
241 }
242  
243 6 ▶sub set_auth_attempts ( $self, $count )
Subroutine set_auth_attempts: called CC=1 SCAR=0.0
Pod undocumented
244 {
245 6 $self->_set( auth_attempts => $count );
246  
247 6 return;
248 }
249  
250 # --- the state file -------------------------------------------------------
251 #
252 # The counters live in one JSON file. The load tolerates a missing file
253 # and a corrupt one: a state file that a crash truncated must not stop
254 # the daemon that would rewrite it. The save writes through a temporary
255 # file and renames over the target, so the file is never the corrupt
256 # one that the next load has to tolerate.
257  
258 14 ▶sub _load_state ($self)
Subroutine _load_state: called CC=6 SCAR=18.2
259 {
260 14 $self->{state} = {};
261  
262 14 ▶ return $self unless -f $self->{state_file};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->{"state_file"} 11 3 14
263  
264 3 my $content = $self->_read( $self->{state_file} );
265 3 ▶ return $self if !defined $content || $content eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $content or $content eq "" 0 3 3
Condition33%1/3
not defined $content or $content eq ""
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $content or $content eq ""
not defined $content $content eq ""
Truth table33%1/3
not(A) or B
A= not defined $contentB= $content eq ""
ABresult
1X1
011
000
266  
267 3 my $data = eval { JSON::PP->new->utf8->decode($content) };
268 3 ▶ unless ( defined $data && ref $data eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data and ref $data eq "HASH") 1 2 3
Condition66%2/3
defined $data and ref $data eq "HASH"
!ll&&!rl&&r
102
MC/DC50%1/2
defined $data and ref $data eq "HASH"
defined $data ref $data eq "HASH"
Truth table66%2/3
A and B
A= defined $dataB= ref $data eq "HASH"
ABresult
0X0
100
111
269 $self->{logger}->warning( 'Cannot read state from %s: %s',
270 1 $self->{state_file}, _reason($@) );
271 1 return $self;
272 }
273  
274 2 $self->{state} = $data;
275  
276 2 return $self;
277 }
278  
279 28 ▶sub _save_state ($self)
Subroutine _save_state: called CC=2 SCAR=7.9
280 {
281 # The encoding is canonical, so the same data always gives the
282 # same bytes and a diff of two state files is readable.
283 my $json =
284 28 eval { JSON::PP->new->utf8->canonical->encode( $self->{state} ) };
285 28 ▶ unless ( defined $json ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $json) 0 28 28
286 $self->{logger}->error( 'Cannot encode %s: %s',
287 0 $self->{state_file}, _reason($@) );
288 0 return;
289 }
290  
291 28 return $self->_write_atomic( $self->{state_file}, $json, 0600 );
292 }
293  
294 28 ▶sub _set ( $self, $key, $value )
Subroutine _set: called CC=1 SCAR=0.0
295 {
296 28 $self->{state}{$key} = $value;
297  
298 28 return $self->_save_state;
299 }
300  
301 # --- the file operations --------------------------------------------------
302  
303 # $self->_read($path):
304 # Read a whole file and return its bytes, or undef.
305 7 ▶sub _read ( $self, $path )
Subroutine _read: called CC=3 SCAR=11.8
306 {
307 7 ▶ open my $fh, '<', $path or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or do { $self->{"logger"}->debug("Cannot read %s: %s", $path, $!); return } 7 0 7
308 0 $self->{logger}->debug( 'Cannot read %s: %s', $path, $! );
309 0 return;
310 };
311 7 binmode $fh;
312 7 my $content = do { local $/; <$fh> };
313 7 close $fh;
314  
315 7 ▶ return $content // '';
Condition50%1/2
$content // ""
l!l
70
MC/DC0%0/1
$content // ""
$content
Truth table50%1/2
A
A= $content
Aresult
11
00
316 }
317  
318 # $self->_write($path, $data, $mode):
319 # Write the bytes with the mode applied at the open. An existing
320 # file keeps its own mode through the open, so the method removes
321 # it first and the mode argument means what it says.
322 26 ▶sub _write ( $self, $path, $data, $mode )
Subroutine _write: called CC=4 SCAR=14.8
323 {
324 26 ▶ unlink $path if -e $path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $path 16 10 26
325  
326 26 ▶ sysopen my $fh, $path, O_CREAT | O_WRONLY | O_TRUNC, $mode or do {
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $path, 577, $mode or do { $self->{"logger"}->error("Cannot write %s: %s", $path, $!); return } 26 0 26
327 0 $self->{logger}->error( 'Cannot write %s: %s', $path, $! );
328 0 return;
329 };
330 26 binmode $fh;
331  
332 26 my $ok = $self->_write_all( $fh, $data, $path );
333 26 ▶ close $fh or $ok = undef;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or $ok = undef 26 0 26
334  
335 26 return $ok;
336 }
337  
338 # $self->_write_atomic($path, $data, $mode):
339 # Write through a temporary file in the same directory, then
340 # rename over the target. A reader sees the old content or the new
341 # content, never a half-written file. The rename must stay inside
342 # one filesystem, so the temporary name is a sibling.
343 28 ▶sub _write_atomic ( $self, $path, $data, $mode )
Subroutine _write_atomic: called CC=6 SCAR=24.5
344 {
345 # A hidden sibling, as the host helper used before the move. The
346 # state directory thus looks the same to an operator.
347 28 my $temp = $path =~ s{(^|/)([^/]+)$}{${1}.$2.$$.tmp}r;
348 28 ▶ unlink $temp if -e $temp;
Branch: 1 branch
BranchTrueFalseTotal
if -e $temp 0 28 28
349  
350 28 ▶ sysopen my $fh, $temp, O_CREAT | O_EXCL | O_WRONLY, $mode or do {
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $temp, 193, $mode or do { $self->{"logger"}->error("Cannot write %s: %s", $temp, $!); return } 28 0 28
351 0 $self->{logger}->error( 'Cannot write %s: %s', $temp, $! );
352 0 return;
353 };
354 28 binmode $fh;
355  
356 28 ▶ unless ( $self->_write_all( $fh, $data, $temp ) && close $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_write_all($fh, $data, $temp) and close $fh) 0 28 28
Condition33%1/3
$self->_write_all($fh, $data, $temp) and close $fh
!ll&&!rl&&r
0028
MC/DC0%0/2
$self->_write_all($fh, $data, $temp) and close $fh
$self->_write_all($fh, $data, $temp) close $fh
Truth table33%1/3
A and B
A= $self->_write_all($fh, $data, $temp)B= close $fh
ABresult
0X0
100
111
357 0 close $fh;
358 0 unlink $temp;
359 0 return;
360 }
361  
362 28 ▶ unless ( rename $temp, $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $temp, $path) 0 28 28
363 $self->{logger}
364 0 ->error( 'Cannot rename %s to %s: %s', $temp, $path, $! );
365 0 unlink $temp;
366 0 return;
367 }
368  
369 28 return 1;
370 }
371  
372 # $self->_write_all($fh, $data, $path):
373 # Write every byte. A short syswrite is not an error by itself, so
374 # the loop continues until the data is gone.
375 54 ▶sub _write_all ( $self, $fh, $data, $path )
Subroutine _write_all: called CC=4 SCAR=16.1
376 {
377 54 my $offset = 0;
378 54 ▶ while ( $offset < length $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $data) 54 54 108
379 54 my $n = syswrite $fh, $data, length($data) - $offset, $offset;
380 54 ▶ unless ( defined $n ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 54 54
381 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
382 $self->{logger}
383 0 ->error( 'Cannot write %s: %s', $path, $! );
384 0 return;
385 }
386 54 $offset += $n;
387 }
388  
389 54 return 1;
390 }
391  
392 # $self->_ensure_dir($path, $mode):
393 # Make sure the directory exists. The method refuses a symlink and
394 # refuses a path that exists as something else. Both are conditions
395 # that a daemon must not write through.
396 14 ▶sub _ensure_dir ( $self, $path, $mode )
Subroutine _ensure_dir: called CC=6 SCAR=21.2
397 {
398 14 ▶ if ( -l $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $path) 0 14 14
399 0 $self->{logger}->error( 'Directory is a symlink: %s', $path );
400 0 return;
401 }
402 14 ▶ if ( -e $path && !-d $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $path and not -d $path) 0 14 14
Condition66%2/3
-e $path and not -d $path
!ll&&!rl&&r
1130
MC/DC0%0/2
-e $path and not -d $path
-e $path not -d $path
Truth table66%2/3
A and not(B)
A= -e $pathB= not -d $path
ABresult
0X0
100
111
403 0 $self->{logger}->error( 'Path is not a directory: %s', $path );
404 0 return;
405 }
406 14 ▶ return 1 if -d $path;
Branch: 1 branch
BranchTrueFalseTotal
if -d $path 13 1 14
407  
408 1 eval { make_path( $path, { mode => $mode } ); 1 };
409 1 ▶ unless ( -d $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $path) 0 1 1
410  
411 # make_path dies with a message that names the module and
412 # the line. A daemon log wants the reason only.
413 $self->{logger}
414 0 ->error( 'Cannot create %s: %s', $path, _reason($@) );
415 0 return;
416 }
417  
418 1 return 1;
419 }
420  
421 # _reason($error):
422 # Reduce a die message to one line with no file and line number.
423 # A daemon log is not the place for a Perl backtrace.
424 1 ▶sub _reason ($error)
Subroutine _reason: called CC=3 SCAR=11.4
425 {
426 1 ▶ my $why = $error // 'unknown error';
Condition50%1/2
$error // "unknown error"
l!l
10
MC/DC0%0/1
$error // "unknown error"
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
427 1 $why =~ s/ at \S+ line \d+.*//s;
428 1 $why =~ s/\s+$//;
429  
430 1 ▶ return length($why) ? $why : 'unknown error';
Branch: 1 branch
BranchTrueFalseTotal
length $why ? : 1 0 1
431 }
432  
433 2 1;
« lib/Protocol/HAP/HTTP.pm ↑ Summary blib/lib/Protocol/HAP/TLV.pm »