/usr/lib/x86_64-linux-gnu/perl5/5.24/Convert/Binary/C/Cached.pm is in libconvert-binary-c-perl 0.78-1+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | ################################################################################
#
# MODULE: Convert::Binary::C::Cached
#
################################################################################
#
# DESCRIPTION: Cached version of Convert::Binary::C module
#
################################################################################
#
# Copyright (c) 2002-2015 Marcus Holland-Moritz. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
################################################################################
package Convert::Binary::C::Cached;
use strict;
use Convert::Binary::C;
use Carp;
use vars qw( @ISA $VERSION );
@ISA = qw(Convert::Binary::C);
$VERSION = '0.78';
sub new
{
my $class = shift;
my $self = $class->SUPER::new;
$self->{cache} = undef;
$self->{parsed} = 0;
$self->{uses_cache} = 0;
@_ % 2 and croak "Number of configuration arguments to new must be even";
@_ and $self->configure(@_);
return $self;
}
sub configure
{
my $self = shift;
if (@_ < 2 and not defined wantarray) {
$^W and carp "Useless use of configure in void context";
return;
}
if (@_ == 0) {
my $cfg = $self->SUPER::configure;
$cfg->{Cache} = $self->{cache};
return $cfg;
}
elsif (@_ == 1 and $_[0] eq 'Cache') {
return $self->{cache};
}
my @args;
if (@_ == 1) {
@args = @_;
}
elsif (@_ % 2 == 0) {
while (@_) {
my %arg = splice @_, 0, 2;
if (exists $arg{Cache}) {
if ($self->{parsed}) {
croak 'Cache cannot be configured after parsing';
}
elsif (ref $arg{Cache}) {
croak 'Cache must be a string value, not a reference';
}
else {
if (defined $arg{Cache}) {
my @missing;
eval { require Data::Dumper };
$@ and push @missing, 'Data::Dumper';
eval { require IO::File };
$@ and push @missing, 'IO::File';
if (@missing) {
$^W and carp "Cannot load ", join(' and ', @missing), ", disabling cache";
undef $arg{Cache};
}
}
$self->{cache} = $arg{Cache};
}
}
else { push @args, %arg }
}
}
my $opt = $self;
if (@args) {
$opt = eval { $self->SUPER::configure(@args) };
$@ =~ s/\s+at.*?Cached\.pm.*//s, croak $@ if $@;
}
$opt;
}
sub clean
{
my $self = shift;
delete $self->{$_} for grep !/^(?:|cache|parsed|uses_cache)$/, keys %$self;
$self->{parsed} = 0;
$self->{uses_cache} = 0;
$self->SUPER::clean;
}
sub clone
{
my $self = shift;
unless (defined wantarray) {
$^W and carp "Useless use of clone in void context";
return;
}
my $clone = $self->SUPER::clone;
for (keys %$self) {
if ($_) {
$clone->{$_} = ref $_ eq 'ARRAY' ? [@{$self->{$_}}] : $self->{$_};
}
}
$clone;
}
sub parse_file
{
my $self = shift;
my($warn,$error) = $self->__parse('file', $_[0]);
for my $w ( @$warn ) { carp $w }
defined $error and croak $error;
defined wantarray and return $self;
}
sub parse
{
my $self = shift;
my($warn,$error) = $self->__parse('code', $_[0]);
for my $w ( @$warn ) { carp $w }
defined $error and croak $error;
defined wantarray and return $self;
}
sub dependencies
{
my $self = shift;
$self->{parsed} or croak "Call to dependencies without parse data";
unless (defined wantarray) {
$^W and carp "Useless use of dependencies in void context";
return;
}
$self->{files} || $self->SUPER::dependencies;
}
sub __uses_cache
{
my $self = shift;
$self->{uses_cache};
}
sub __parse
{
my $self = shift;
if (defined $self->{cache}) {
$self->{parsed} and croak "Cannot parse more than once for cached objects";
$self->{$_[0]} = $_[1];
if ($self->__can_use_cache) {
my @WARN;
{
local $SIG{__WARN__} = sub { push @WARN, $_[0] };
eval { $self->SUPER::parse_file($self->{cache}) };
}
unless ($@ or @WARN) {
$self->{parsed} = 1;
$self->{uses_cache} = 1;
return;
}
$self->clean;
}
}
$self->{parsed} = 1;
my(@warnings, $error);
{
local $SIG{__WARN__} = sub { push @warnings, $_[0] };
if ($_[0] eq 'file') {
eval { $self->SUPER::parse_file($_[1]) };
}
else {
eval { $self->SUPER::parse($_[1]) };
}
}
if ($@) {
$error = $@;
$error =~ s/\s+at.*?Cached\.pm.*//s;
}
else {
defined $self->{cache} and $self->__save_cache;
}
for (@warnings) { s/\s+at.*?Cached\.pm.*//s }
(\@warnings, $error);
}
sub __can_use_cache
{
my $self = shift;
my $fh = new IO::File;
unless (-e $self->{cache} and -s _) {
$ENV{CBCC_DEBUG} and print STDERR "CBCC: cache file '$self->{cache}' doesn't exist or is empty\n";
return 0;
}
unless ($fh->open($self->{cache})) {
$^W and carp "Cannot open '$self->{cache}': $!";
$ENV{CBCC_DEBUG} and print STDERR "CBCC: cannot open cache file '$self->{cache}'\n";
return 0;
}
my @warnings;
my @config = do {
my $config;
unless (defined($config = <$fh>)) {
$ENV{CBCC_DEBUG} and print STDERR "CBCC: cannot read configuration\n";
return 0;
}
unless ($config =~ /^#if\s+0/) {
$ENV{CBCC_DEBUG} and print STDERR "CBCC: invalid configuration\n";
return 0;
}
local $/ = $/.'#endif';
chomp($config = <$fh>);
$config =~ s/^\*//gms;
local $SIG{__WARN__} = sub { push @warnings, $_[0] };
eval $config;
};
# corrupt config
if ($@ or @warnings or @config % 2) {
$ENV{CBCC_DEBUG} and print STDERR "CBCC: broken configuration\n";
return 0;
}
my %config = @config;
my $what = exists $self->{code} ? 'code' : 'file';
unless (exists $config{$what} and
$config{$what} eq $self->{$what} and
__reccmp($config{cfg}, $self->configure)) {
if ($ENV{CBCC_DEBUG}) {
print STDERR "CBCC: configuration has changed\n";
print STDERR "CBCC: what='$what', \$config{$what}='$config{$what}' \$self->{$what}='$self->{$what}'\n";
my $dump = Data::Dumper->Dump([$config{cfg}, $self->configure], ['config', 'self']);
$dump =~ s/^/CBCC: /mg;
print STDERR $dump;
}
return 0;
}
while (my($file, $spec) = each %{$config{files}}) {
unless (-e $file) {
$ENV{CBCC_DEBUG} and print STDERR "CBCC: file '$file' deleted\n";
return 0;
}
my($size, $mtime, $ctime) = (stat(_))[7,9,10];
unless ($spec->{size} == $size and
$spec->{mtime} == $mtime and
$spec->{ctime} == $ctime) {
$ENV{CBCC_DEBUG} and print STDERR "CBCC: size/mtime/ctime of '$file' changed\n";
return 0;
}
}
$self->{files} = $config{files};
$ENV{CBCC_DEBUG} and print STDERR "CBCC: '$self->{cache}' is usable\n";
return 1;
}
sub __save_cache
{
my $self = shift;
my $fh = new IO::File;
$fh->open(">$self->{cache}") or croak "Cannot open '$self->{cache}': $!";
my $what = exists $self->{code} ? 'code' : 'file';
my $config = Data::Dumper->new([{ $what => $self->{$what},
cfg => $self->configure,
files => scalar $self->SUPER::dependencies,
}], ['*'])->Indent(1)->Dump;
$config =~ s/[^(]*//;
$config =~ s/^/*/gms;
print $fh "#if 0\n", $config, "#endif\n\n",
do { local $^W; $self->sourcify({ Context => 1 }) };
}
sub __reccmp
{
my($ref, $val) = @_;
!defined($ref) && !defined($val) and return 1;
!defined($ref) || !defined($val) and return 0;
ref $ref or return $ref eq $val;
if (ref $ref eq 'ARRAY') {
@$ref == @$val or return 0;
for (0..$#$ref) {
__reccmp($ref->[$_], $val->[$_]) or return 0;
}
}
elsif (ref $ref eq 'HASH') {
keys %$ref == keys %$val or return 0;
for (keys %$ref) {
__reccmp($ref->{$_}, $val->{$_}) or return 0;
}
}
else { return 0 }
return 1;
}
1;
__END__
=head1 NAME
Convert::Binary::C::Cached - Caching for Convert::Binary::C
=head1 SYNOPSIS
use Convert::Binary::C::Cached;
use Data::Dumper;
#------------------------
# Create a cached object
#------------------------
$c = Convert::Binary::C::Cached->new(
Cache => '/tmp/cache.c',
Include => [
'/usr/lib/gcc/i686-pc-linux-gnu/4.5.2/include',
'/usr/lib/gcc/i686-pc-linux-gnu/4.5.2/include-fixed',
'/usr/include',
],
);
#----------------------------------------------------
# Parse 'time.h' and dump the definition of timespec
#----------------------------------------------------
$c->parse_file('time.h');
print Dumper($c->struct('timespec'));
=head1 DESCRIPTION
Convert::Binary::C::Cached simply adds caching capability to
Convert::Binary::C. You can use it in just the same way that
you would use Convert::Binary::C. The interface is exactly
the same.
To use the caching capability, you must pass the C<Cache> option
to the constructor. If you don't pass it, you will receive
an ordinary Convert::Binary::C object. The argument to
the C<Cache> option is the file that is used for caching
this object.
The caching algorithm automatically detects when the cache
file cannot be used and the original code has to be parsed.
In that case, the cache file is updated. An update of the
cache file can be triggered by one or more of the following
factors:
=over 2
=item *
The cache file doesn't exist, which is obvious.
=item *
The cache file is corrupt, i.e. cannot be parsed.
=item *
The object's configuration has changed.
=item *
The embedded code for a L<C<parse>|Convert::Binary::C/"parse"> method
call has changed.
=item *
At least one of the files that the object depends on
does not exist or has a different size or a different
modification or change timestamp.
=back
=head1 LIMITATIONS
You cannot
call L<C<parse>|Convert::Binary::C/"parse"> or L<C<parse_file>|Convert::Binary::C/"parse_file"> more
that once when using a Convert::Binary::C::Cached object. This isn't
a big problem, as you usually don't call them multiple times.
If a dependency file changes, but the change affects neither
the size nor the timestamps of that file, the caching
algorithm cannot detect that an update is required.
=head1 COPYRIGHT
Copyright (c) 2002-2015 Marcus Holland-Moritz. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
See L<Convert::Binary::C>.
=cut
|