/usr/share/perl5/Cache/File/Entry.pm is in libcache-perl 2.11-1.
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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | =head1 NAME
Cache::File::Entry - An entry in the file based implementation of Cache
=head1 SYNOPSIS
See 'Cache::Entry' for a synopsis.
=head1 DESCRIPTION
This module implements a version of Cache::Entry for the Cache::File variant
of Cache. It should not be created or used directly, please see
'Cache::File' or 'Cache::Entry' instead.
=cut
package Cache::File::Entry;
require 5.006;
use strict;
use warnings;
use Cache::File;
use File::Spec;
use File::Path;
use File::Temp qw(tempfile);
use Fcntl qw(LOCK_EX LOCK_SH LOCK_NB);
use File::NFSLock;
use Symbol ();
use Carp;
use base qw(Cache::Entry);
use fields qw(dir path lockdetails);
our $VERSION = '2.11';
# hash of locks held my the process, keyed on path. This is useful for
# catching potential deadlocks and warning the user, and for implementing
# LOCK_NONE (which still needs to do some synchronization). Each entry will
# be an hash of { lock, type, count, lock, lockfh, linkcount }. The
# filehandle and link count is for checking when the lock has been released by
# another process.
my %PROCESS_LOCKS;
sub new {
my Cache::File::Entry $self = shift;
$self = fields::new($self) unless ref $self;
$self->SUPER::new(@_);
# get file path and store full path and containing directory
my ($dir, $file) = $self->{cache}->cache_file_path($self->{key});
$self->{dir} = $dir;
$self->{path} = File::Spec->catfile($dir, $file);
return $self;
}
sub exists {
my Cache::File::Entry $self = shift;
# ensure pending expiries are removed
$self->{cache}->purge();
return -e $self->{path};
}
sub _set {
my Cache::File::Entry $self = shift;
my ($data, $expiry) = @_;
$self->_make_path() or return;
my ($fh, $filename) = tempfile('.XXXXXXXX', DIR => $self->{dir});
binmode $fh;
print $fh $data;
close($fh);
my $time = time();
my $cache = $self->{cache};
my $key = $self->{key};
# lock indexes
$cache->lock();
my $exists = -e $self->{path};
my $orig_size;
unless ($exists) {
# we're creating the entry
$cache->create_entry($key, $time);
$cache->change_count(1);
$orig_size = 0;
}
# only remove current size if there is no active write handle
elsif ($self->_trylock(LOCK_SH)) {
$orig_size = $self->size();
$self->_unlock();
}
else {
$orig_size = 0;
}
# replace existing data
rename($filename, $self->{path});
# fix permissions of tempfile
my $mode = 0666 & ~($self->{cache}->cache_umask());
chmod $mode, $self->{path};
# invalidate any active handle locks
unlink($self->{path} . $Cache::File::LOCK_EXT);
delete $PROCESS_LOCKS{$self->{path}};
$self->_set_expiry($expiry) if $expiry or $exists;
$cache->update_last_use($key, $time) if $exists;
$cache->change_size($self->size() - $orig_size);
# ensure pending expiries are removed
$cache->purge();
$cache->unlock();
}
sub _get {
my Cache::File::Entry $self = shift;
my $cache = $self->{cache};
my $key = $self->{key};
my $exists;
my $time = time();
$cache->lock();
if ($exists = $self->exists()) {
# update last used
$cache->update_last_use($key, $time);
# lock entry for reading
$self->_lock(LOCK_SH);
}
$cache->unlock();
return undef unless $exists;
File::NFSLock::uncache($self->{path})
if $cache->cache_lock_level() == Cache::File::LOCK_NFS();
my $fh = Symbol::gensym();
my $data;
my $oldmask = umask $self->{cache}->cache_umask();
if (open($fh, $self->{path})) {
binmode $fh;
# slurp mode
local $/;
$data = <$fh>;
close($fh);
}
umask $oldmask;
# shared locks can be unlocked without holding cache lock
$self->_unlock();
return $data;
}
sub size {
my Cache::File::Entry $self = shift;
return -s $self->{path};
}
sub remove {
my Cache::File::Entry $self = shift;
my $cache = $self->{cache};
my $key = $self->{key};
$cache->lock();
unless (-r $self->{path}) {
$cache->unlock();
return;
}
my $index = $cache->get_index();
my $index_entries = $cache->get_index_entries($key)
or warnings::warnif('Cache', "missing index entry for $key");
delete $$index{$key};
if ($$index_entries{age}) {
my $ageheap = $cache->get_age_heap();
$ageheap->delete($$index_entries{age}, $key);
}
if ($$index_entries{lastuse}) {
my $useheap = $cache->get_use_heap();
$useheap->delete($$index_entries{lastuse}, $key);
}
if ($$index_entries{expiry}) {
my $expheap = $cache->get_exp_heap();
$expheap->delete($$index_entries{expiry}, $key)
}
my $size = 0;
if ($self->_trylock(LOCK_SH)) {
$size = (-s $self->{path});
$cache->change_size(-$size);
$self->_unlock();
}
$cache->change_count(-1);
unlink($self->{path});
# obliterate any entry lockfile
unlink($self->{path} . $Cache::File::LOCK_EXT);
delete $PROCESS_LOCKS{$self->{path}};
$cache->unlock();
return $size;
}
sub expiry {
my Cache::File::Entry $self = shift;
my $cache = $self->{cache};
$cache->lock();
my $index_entries = $cache->get_index_entries($self->{key});
$cache->unlock();
return $index_entries? $$index_entries{expiry} : undef;
}
sub _set_expiry {
my Cache::File::Entry $self = shift;
my ($time) = @_;
my $cache = $self->{cache};
my $key = $self->{key};
$cache->lock();
my $index_entries = $cache->get_index_entries($key);
unless ($index_entries) {
$cache->unlock();
croak "Cannot set expiry on non-existant entry: $key";
}
my $expheap = $cache->get_exp_heap();
$expheap->delete($$index_entries{expiry}, $key)
if $$index_entries{expiry};
$expheap->add($time, $key) if $time;
$$index_entries{expiry} = $time;
$cache->set_index_entries($key, $index_entries);
$cache->unlock();
}
sub _handle {
my Cache::File::Entry $self = shift;
my ($mode, $expiry) = @_;
# a bit of magic! Since handles hold a lock indefinitely, and the entry
# lock code doesn't do recursion (its not necessary) we could get into
# trouble. So instead we just ensure that every handle has it's own entry
# associated with it.
$self = $self->{cache}->entry($self->{key});
require Cache::File::Handle;
my $exists = -e $self->{path};
my $writing = $mode =~ />|\+/;
unless ($exists) {
# return undef unless we're writing a new entry
$writing or return undef;
# make the path
$self->_make_path();
}
my $time = time();
my $cache = $self->{cache};
my $key = $self->{key};
# lock indexes
$cache->lock();
# grab entry lock
$self->_lock($writing? LOCK_EX : LOCK_SH);
# create the attributes if the entry doesn't exist
unless ($exists) {
# we're creating the entry
$cache->create_entry($key, $time);
$cache->change_count(1);
}
# if truncating, reset expiry (or set it creating and its specified)
$cache->set_expiry($key, $expiry)
if ($expiry and not $exists) or ($mode =~/\+?>/);
$cache->update_last_use($key, $time) if $exists;
my $orig_size = $writing? ($exists? $self->size() : 0) : undef;
# open handle - entry lock will be held as self persists in the closure
my $oldmask = umask $cache->cache_umask();
my $handle = Cache::File::Handle->new($self->{path}, $mode, undef,
sub { $self->_handle_closed(shift, $orig_size); } );
umask $oldmask;
$handle or warnings::warnif('io', 'Failed to open '.$self->{path}.": $!");
$cache->unlock();
return $handle;
}
sub validity {
my Cache::File::Entry $self = shift;
my $cache = $self->{cache};
$cache->lock();
my $index_entries = $cache->get_index_entries($self->{key});
$cache->unlock();
return $index_entries? $$index_entries{validity} : undef;
}
sub set_validity {
my Cache::File::Entry $self = shift;
my ($data) = @_;
my $key = $self->{key};
my $cache = $self->{cache};
$cache->lock();
my $index_entries = $cache->get_index_entries($key);
unless ($index_entries) {
$self->set('');
$index_entries = $cache->get_index_entries($key);
}
$$index_entries{validity} = $data;
$cache->set_index_entries($key, $index_entries);
$cache->unlock();
}
# UTILITY METHODS
sub _handle_closed {
my Cache::File::Entry $self = shift;
my ($handle, $orig_size) = @_;
unless (defined $orig_size) {
# shared locks can be unlocked without holding cache lock
$self->_unlock();
return;
}
my $cache = $self->{cache};
$cache->lock();
# check if file still exists and our lock is still valid. this order is
# used to prevent a race between checking lock and getting size
my $new_size = $self->size();
(defined $new_size and $self->_check_lock()) or $new_size = 0;
# release entry lock
$self->_unlock();
# update sizes
if (defined $orig_size and $orig_size != $new_size) {
$cache->change_size($new_size - $orig_size);
}
$cache->unlock();
}
sub _make_path {
my Cache::File::Entry $self = shift;
unless (-d $self->{dir}) {
my $oldmask = umask $self->{cache}->cache_umask();
eval { mkpath($self->{dir}); };
if ($@) {
warnings::warnif('io',
'Failed to create path '.$self->{dir}.": $@");
return 0;
}
umask $oldmask;
}
return 1;
}
sub _lock {
my Cache::File::Entry $self = shift;
my ($type, $tryonly) = @_;
$type ||= LOCK_EX;
# entry already has the lock?
$self->{lockdetails} and die "entry already holding a lock";
my $path = $self->{path};
my $lock_details = $PROCESS_LOCKS{$path};
if ($lock_details) {
if ($$lock_details{type} != $type) {
$tryonly and return 0;
croak "process already holding entry lock of different type";
}
$$lock_details{count}++;
$self->{lockdetails} = $lock_details;
return 1;
}
# create new entry
$lock_details = $PROCESS_LOCKS{$path} = {};
# no need for any locking with LOCK_NONE
if ($self->{cache}->cache_lock_level() != Cache::File::LOCK_NONE()) {
local $File::NFSLock::LOCK_EXTENSION = $Cache::File::LOCK_EXT;
my $oldmask = umask $self->{cache}->cache_umask();
my $lock = File::NFSLock->new({
file => $path,
lock_type => $type | ($tryonly? LOCK_NB : 0),
stale_lock_timeout => $Cache::File::STALE_LOCK_TIMEOUT,
});
unless ($lock) {
umask $oldmask;
$tryonly and return 0;
die "Failed to obtain lock on lockfile on '$path': ".
$File::NFSLock::errstr."\n";
}
# count the number of hard links to the lockfile and open it
# if we can't reopen the lockfile then it has already been removed...
# we do the stat on the file rather than the filehandle, as otherwise
# there would be a race between opening the file and getting the link
# count (such that we could end up with a link count that is already 0).
my $fh = Symbol::gensym;
my $linkcount;
my $lockfile = $path . $Cache::File::LOCK_EXT;
if (($linkcount = (stat $lockfile)[3]) and open($fh, $lockfile)) {
$$lock_details{lock} = $lock;
$$lock_details{lockfh} = $fh;
$$lock_details{linkcount} = $linkcount;
}
else {
# lock failed - remove lock details
delete $PROCESS_LOCKS{$path};
}
umask $oldmask;
}
# lock obtained
$$lock_details{type} = $type;
$$lock_details{count} = 1;
# use lock details reference as an internal lock check
$self->{lockdetails} = $lock_details;
return 1;
}
sub _trylock {
my Cache::File::Entry $self = shift;
my ($type) = @_;
return $self->_lock($type, 1);
}
sub _unlock {
my Cache::File::Entry $self = shift;
$self->{lockdetails} or die 'not locked';
# is our lock still valid?
$self->_check_lock() or return;
$self->{lockdetails} = undef;
my $lock_details = $PROCESS_LOCKS{$self->{path}};
--$$lock_details{count} == 0
or return;
if ($self->{cache}->cache_lock_level() != Cache::File::LOCK_NONE()) {
$$lock_details{lock}->unlock;
}
delete $PROCESS_LOCKS{$self->{path}};
}
# check that we still hold our lock
sub _check_lock {
my Cache::File::Entry $self = shift;
$self->{lockdetails} or return 0;
my $lock_details = $PROCESS_LOCKS{$self->{path}}
or return 0;
# check lock details reference still matches global
$self->{lockdetails} == $lock_details
or return 0;
if ($self->{cache}->cache_lock_level() != Cache::File::LOCK_NONE()) {
# check filehandle is still connected to filesystem
my $lockfh = $$lock_details{lockfh};
if (((stat $lockfh)[3] || 0) < $$lock_details{linkcount}) {
# lock is gone
delete $PROCESS_LOCKS{$self->{path}};
return 0;
}
}
return 1;
}
1;
__END__
=head1 SEE ALSO
Cache::Entry, Cache::File
=head1 AUTHOR
Chris Leishman <chris@leishman.org>
Based on work by DeWitt Clinton <dewitt@unto.net>
=head1 COPYRIGHT
Copyright (C) 2003-2006 Chris Leishman. All Rights Reserved.
This module is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
either expressed or implied. This program is free software; you can
redistribute or modify it under the same terms as Perl itself.
$Id: Entry.pm,v 1.8 2006/01/31 15:23:58 caleishm Exp $
=cut
|