| 1 | # Copyright 2004-2026, Paul Johnson (paul@pjcj.net) | ||
| 2 | |||
| 3 | # This software is free. It is licensed under the same terms as Perl itself. | ||
| 4 | |||
| 5 | # The latest version of this software should be available from my homepage: | ||
| 6 | # https://pjcj.net | ||
| 7 | |||
| 8 | package Devel::Cover::DB::Structure; | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | use Digest::MD5; | ||
| 15 | |||
| 16 | use Devel::Cover::DB; | ||
| 17 | use Devel::Cover::DB::IO; | ||
| 18 | use Devel::Cover::Dumper; | ||
| 19 | |||
| 20 | # For comprehensive debug logging. | ||
| 21 | use constant DEBUG => 0; | ||
| 22 | |||
| 23 | our $VERSION = '1.52'; # VERSION | ||
| 24 | our $AUTOLOAD; | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | my $class = shift; | ||
| 28 | my $self = {@_}; | ||
| 29 | bless $self, $class | ||
| 30 | } | ||
| 31 | |||
| 32 | sub DESTROY { } | ||
| 33 | |||
| 34 | sub AUTOLOAD { | ||
| 35 | my $self = $_[0]; | ||
| 36 | my $func = $AUTOLOAD; | ||
| 37 | $func =~ s/.*:://; | ||
| 38 | my ($function, $criterion) = $func =~ /^(add|get)_(.*)/; | ||
| 39 | croak "Undefined subroutine $func called" | ||
| 40 | unless $criterion && grep $_ eq $criterion, @Devel::Cover::DB::Criteria, | ||
| 41 | qw( sub_name file line ); | ||
| 42 | no strict "refs"; | ||
| 43 | if ($function eq "get") { | ||
| 44 | my $c = $criterion eq "time" ? "statement" : $criterion; | ||
| 45 | if (grep $_ eq $c, qw( sub_name file line )) { | ||
| 46 | *$func = sub { shift->{$c} }; | ||
| 47 | } else { | ||
| 48 | *$func = sub { | ||
| 49 | my $self = shift; | ||
| 50 | my $digest = shift; | ||
| 51 | # print STDERR "file: $digest, condition: $c\n"; | ||
| 52 | for my $fval (values %{ $self->{f} }) { | ||
| 53 | return $fval->{$c} if $fval->{digest} eq $digest; | ||
| 54 | } | ||
| 55 | return | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } else { | ||
| 59 | *$func = sub { | ||
| 60 | my $self = shift; | ||
| 61 | my $file = shift; | ||
| 62 | push @{ $self->{f}{$file}{$criterion} }, @_; | ||
| 63 | }; | ||
| 64 | } | ||
| 65 | goto &$func | ||
| 66 | } | ||
| 67 | |||
| 68 | sub debuglog { | ||
| 69 | my $self = shift; | ||
| 70 | my $dir = "$self->{base}/debuglog"; | ||
| 71 | unless (mkdir $dir) { | ||
| 72 | confess "Can't mkdir $dir: $!" unless -d $dir; | ||
| 73 | } | ||
| 74 | |||
| 75 | local $\; | ||
| 76 | # One log file per process, as we're potentially dumping out large amounts, | ||
| 77 | # and might exceed the atomic write size of the OS | ||
| 78 | open my $fh, '>>', "$dir/$$" or confess "Can't open $dir/$$: $!"; | ||
| 79 | print $fh "----------------" . gmtime() . "----------------\n"; | ||
| 80 | print $fh ref $_ ? Dumper($_) : $_; | ||
| 81 | print $fh "\n"; | ||
| 82 | close $fh or confess "Can't close $dir/$$: $!"; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub add_criteria { | ||
| 86 | my $self = shift; | ||
| 87 | @{ $self->{criteria} }{@_} = (); | ||
| 88 | $self | ||
| 89 | } | ||
| 90 | |||
| 91 | sub criteria { | ||
| 92 | my $self = shift; | ||
| 93 | keys %{ $self->{criteria} } | ||
| 94 | } | ||
| 95 | |||
| 96 | sub set_subroutine { | ||
| 97 | my $self = shift; | ||
| 98 | my ($sub_name, $file, $line, $scount) | ||
| 99 | = @{$self}{qw( sub_name file line scount )} = @_; | ||
| 100 | |||
| 101 | # When new code is added at runtime, via a string eval in some guise, we | ||
| 102 | # need information about where structure information for the subroutine | ||
| 103 | # is. This information is stored in $self->{f}{$file}{start} keyed on the | ||
| 104 | # filename, line number, subroutine name and the count, the count being | ||
| 105 | # for when there are multiple subroutines of the same name on the same | ||
| 106 | # line (such subroutines generally being called BEGIN). | ||
| 107 | |||
| 108 | # print STDERR "set_subroutine start $file:$line $sub_name($scount) ", | ||
| 109 | # Dumper $self->{f}{$file}{start}; | ||
| 110 | $self->{additional} = 0; | ||
| 111 | if ($self->reuse($file)) { | ||
| 112 | # reusing a structure | ||
| 113 | if (exists $self->{f}{$file}{start}{$line}{$sub_name}[$scount]) { | ||
| 114 | # sub already exists - normal case | ||
| 115 | # print STDERR "reuse $file:$line:$sub_name\n"; | ||
| 116 | $self->{count}{$_}{$file} | ||
| 117 | = $self->{f}{$file}{start}{$line}{$sub_name}[$scount]{$_} | ||
| 118 | for $self->criteria; | ||
| 119 | } else { | ||
| 120 | # sub doesn't exist, for example a conditional C<eval "use M"> | ||
| 121 | $self->{additional} = 1; | ||
| 122 | if (exists $self->{additional_count}{ ($self->criteria)[0] }{$file}) { | ||
| 123 | # already had such a sub in module | ||
| 124 | # print STDERR "reuse additional $file:$line:$sub_name\n"; | ||
| 125 | $self->{count}{$_}{$file} | ||
| 126 | = $self->{f}{$file}{start}{$line}{$sub_name}[$scount]{$_} | ||
| 127 | = ($self->add_count($_))[0] | ||
| 128 | for $self->criteria; | ||
| 129 | } else { | ||
| 130 | # first such a sub in module | ||
| 131 | # print STDERR "reuse first $file:$line:$sub_name\n"; | ||
| 132 | $self->{count}{$_}{$file} = $self->{additional_count}{$_}{$file} | ||
| 133 | = $self->{f}{$file}{start}{$line}{$sub_name}[$scount]{$_} | ||
| 134 | = $self->{f}{$file}{start}{-1}{"__COVER__"}[$scount]{$_} | ||
| 135 | for $self->criteria; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } else { | ||
| 139 | # first time sub seen in new structure | ||
| 140 | # print STDERR "new $file:$line:$sub_name\n"; | ||
| 141 | $self->{count}{$_}{$file} | ||
| 142 | = $self->{f}{$file}{start}{$line}{$sub_name}[$scount]{$_} | ||
| 143 | = $self->get_count($file, $_) | ||
| 144 | for $self->criteria; | ||
| 145 | } | ||
| 146 | # print STDERR "set_subroutine start $file:$line $sub_name($scount) ", | ||
| 147 | # Dumper $self->{f}{$file}{start}; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub store_counts { | ||
| 151 | my $self = shift; | ||
| 152 | my ($file) = @_; | ||
| 153 | $self->{count}{$_}{$file} = $self->{f}{$file}{start}{-1}{__COVER__}[0]{$_} | ||
| 154 | = $self->get_count($file, $_) | ||
| 155 | for $self->criteria; | ||
| 156 | # print STDERR "store_counts: ", Dumper $self->{f}{$file}{start}; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub reuse { | ||
| 160 | my $self = shift; | ||
| 161 | my ($file) = @_; | ||
| 162 | exists $self->{f}{$file}{start}{-1}{"__COVER__"} | ||
| 163 | # TODO - exists $self->{f}{$file}{start}{-1} | ||
| 164 | } | ||
| 165 | |||
| 166 | sub set_file { | ||
| 167 | my $self = shift; | ||
| 168 | my ($file) = @_; | ||
| 169 | $self->{file} = $file; | ||
| 170 | my $digest = $self->digest($file); | ||
| 171 | if ($digest) { | ||
| 172 | # print STDERR "Adding $digest for $file\n"; | ||
| 173 | $self->{f}{$file}{digest} = $digest; | ||
| 174 | push @{ $self->{digests}{$digest} }, $file; | ||
| 175 | } | ||
| 176 | $digest | ||
| 177 | } | ||
| 178 | |||
| 179 | sub digest { | ||
| 180 | my $self = shift; | ||
| 181 | my ($file) = @_; | ||
| 182 | |||
| 183 | # print STDERR "Opening $file for MD5 digest\n"; | ||
| 184 | |||
| 185 | my $digest; | ||
| 186 | if (open my $fh, "<", $file) { | ||
| 187 | binmode $fh; | ||
| 188 | $digest = Digest::MD5->new->addfile($fh)->hexdigest; | ||
| 189 | } else { | ||
| 190 | print STDERR "Devel::Cover: Warning: can't open $file " | ||
| 191 | . "for MD5 digest: $!\n" | ||
| 192 | unless lc $file eq "-e" | ||
| 193 | or $Devel::Cover::Silent | ||
| 194 | or $file =~ $Devel::Cover::DB::Ignore_filenames; | ||
| 195 | # require "Cwd"; print STDERR Carp::longmess("in " . Cwd::cwd()); | ||
| 196 | } | ||
| 197 | $digest | ||
| 198 | } | ||
| 199 | |||
| 200 | sub get_count { | ||
| 201 | my $self = shift; | ||
| 202 | my ($file, $criterion) = @_; | ||
| 203 | $self->{count}{$criterion}{$file} | ||
| 204 | } | ||
| 205 | |||
| 206 | sub add_count { | ||
| 207 | my $self = shift; | ||
| 208 | # warn Carp::longmess("undefined file") unless defined $self->{file}; | ||
| 209 | return unless defined $self->{file}; # can happen during self_cover | ||
| 210 | my ($criterion) = @_; | ||
| 211 | $self->{additional_count}{$criterion}{ $self->{file} }++ | ||
| 212 | if $self->{additional}; | ||
| 213 | ( | ||
| 214 | $self->{count}{$criterion}{ $self->{file} }++, | ||
| 215 | !$self->reuse($self->{file}) || $self->{additional}, | ||
| 216 | ) | ||
| 217 | } | ||
| 218 | |||
| 219 | sub delete_file { | ||
| 220 | my $self = shift; | ||
| 221 | my ($file) = @_; | ||
| 222 | delete $self->{f}{$file}; | ||
| 223 | } | ||
| 224 | |||
| 225 | # TODO - concurrent runs updating structure? | ||
| 226 | |||
| 227 | sub write { | ||
| 228 | my $self = shift; | ||
| 229 | my ($dir) = @_; | ||
| 230 | # print STDERR Dumper $self; | ||
| 231 | $dir .= "/structure"; | ||
| 232 | unless (mkdir $dir) { | ||
| 233 | confess "Can't mkdir $dir: $!" unless -d $dir; | ||
| 234 | } | ||
| 235 | chmod 0777, $dir if $self->{loose_perms}; | ||
| 236 | for my $file (sort keys %{ $self->{f} }) { | ||
| 237 | $self->{f}{$file}{file} = $file; | ||
| 238 | my $digest = $self->{f}{$file}{digest}; | ||
| 239 | $digest = $1 if defined $digest && $digest =~ /(.*)/; # ie tainting | ||
| 240 | unless ($digest) { | ||
| 241 | print STDERR "Can't find digest for $file" | ||
| 242 | unless $Devel::Cover::Silent | ||
| 243 | || $file =~ $Devel::Cover::DB::Ignore_filenames | ||
| 244 | || ($Devel::Cover::Self_cover && $file =~ q|/Devel/Cover[./]|); | ||
| 245 | next; | ||
| 246 | } | ||
| 247 | my $df_final = "$dir/$digest"; | ||
| 248 | my $df_temp = "$dir/.$digest.$$"; | ||
| 249 | # TODO - determine if Structure has changed to save writing it | ||
| 250 | # my $f = $df; my $n = 1; $df = $f . "." . $n++ while -e $df; | ||
| 251 | my $io = Devel::Cover::DB::IO->new; | ||
| 252 | $io->write($self->{f}{$file}, $df_temp); # unless -e $df; | ||
| 253 | unless (rename $df_temp, $df_final) { | ||
| 254 | unless ($Devel::Cover::Silent) { | ||
| 255 | if (-e $df_final) { | ||
| 256 | print STDERR "Can't rename $df_temp to $df_final " | ||
| 257 | . "(which exists): $!"; | ||
| 258 | $self->debuglog( | ||
| 259 | "Can't rename $df_temp to $df_final " . "(which exists): $!") | ||
| 260 | if DEBUG; | ||
| 261 | } else { | ||
| 262 | print STDERR "Can't rename $df_temp to $df_final: $!"; | ||
| 263 | $self->debuglog("Can't rename $df_temp to $df_final: $!") if DEBUG; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | unless (unlink $df_temp) { | ||
| 267 | print STDERR "Can't remove $df_temp after failed rename: $!" | ||
| 268 | unless $Devel::Cover::Silent; | ||
| 269 | $self->debuglog("Can't remove $df_temp after failed rename: $!") | ||
| 270 | if DEBUG; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | sub read { | ||
| 277 | |||
| 278 | my $self = shift; | ||
| 279 | my ($digest) = @_; | ||
| 280 | my $file = "$self->{base}/structure/$digest"; | ||
| 281 | my $io = Devel::Cover::DB::IO->new; | ||
| 282 | my $s = eval { $io->read($file) }; | ||
| 283 | |||
| 284 | if ($@ or !$s) { | ||
| 285 | $self->debuglog("read retrieve $file failed: $@") if DEBUG; | ||
| 286 | die $@; | ||
| 287 | } | ||
| 288 | if (DEBUG) { | ||
| 289 | foreach my $key (qw(file digest)) { | ||
| 290 | if (!defined $s->{$key}) { | ||
| 291 | $self->debuglog("retrieve $file had no $key entry. Got:\n", $s); | ||
| 292 | } | ||
| 293 | } | ||
| 294 | } | ||
| 295 | my $d = $self->digest($s->{file}); | ||
| 296 | # print STDERR "reading $digest from $file: ", Dumper $s; | ||
| 297 | if (!$d) { | ||
| 298 | # No digest implies that we can't read the file. Likely this is because | ||
| 299 | # it's stored with a relative path. In which case, it's not valid to | ||
| 300 | # assume that the file has been changed, and hence that we need to | ||
| 301 | # "update" the structure database on disk. | ||
| 302 | } elsif ($d eq $s->{digest}) { | ||
| 303 | $self->{f}{ $s->{file} } = $s; | ||
| 304 | } else { | ||
| 305 | print STDERR "Devel::Cover: Deleting old coverage ", | ||
| 306 | "for changed file $s->{file}\n"; | ||
| 307 | if (unlink $file) { | ||
| 308 | $self->debuglog( | ||
| 309 | "Deleting old coverage $file for changed " | ||
| 310 | . "$s->{file} $s->{digest} vs $d. Got:\n", | ||
| 311 | $s, "Have:\n", $self->{f}{$file} | ||
| 312 | ) if DEBUG; | ||
| 313 | } else { | ||
| 314 | print STDERR "Devel::Cover: can't delete $file: $!\n"; | ||
| 315 | $self->debuglog( | ||
| 316 | "Failed to delete coverage $file for changed " | ||
| 317 | . "$s->{file} ($!) $s->{digest} vs $d. Got:\n", | ||
| 318 | $s, "Have:\n", $self->{f}{$file} | ||
| 319 | ) if DEBUG; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | $self | ||
| 323 | } | ||
| 324 | |||
| 325 | sub read_all { | ||
| 326 | my ($self) = @_; | ||
| 327 | my $dir = $self->{base}; | ||
| 328 | $dir .= "/structure"; | ||
| 329 | opendir D, $dir or return; | ||
| 330 | for my $d (sort grep $_ !~ /\./, readdir D) { | ||
| 331 | $d = $1 if $d =~ /(.*)/; # Die tainting | ||
| 332 | $self->read($d); | ||
| 333 | } | ||
| 334 | closedir D or die "Can't closedir $dir: $!"; | ||
| 335 | $self | ||
| 336 | } | ||
| 337 | |||
| 338 | sub merge { | ||
| 339 | my $self = shift; | ||
| 340 | my ($from) = @_; | ||
| 341 | Devel::Cover::DB::_merge_hash($self->{f}, $from->{f}, "noadd"); | ||
| 342 | } | ||
| 343 | |||
| 344 | 1 | ||
| 345 | |||
| 346 | __END__ |