| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Cache::File::Entry - An entry in the file based implementation of Cache | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | See 'Cache::Entry' for a synopsis. | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | This module implements a version of Cache::Entry for the Cache::File variant | ||
| 12 | of Cache. It should not be created or used directly, please see | ||
| 13 | 'Cache::File' or 'Cache::Entry' instead. | ||
| 14 | |||
| 15 | =cut | ||
| 16 | package Cache::File::Entry; | ||
| 17 | |||
| 18 | require 5.006; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Cache::File; | ||
| 22 | use File::Spec; | ||
| 23 | use File::Path; | ||
| 24 | use File::Temp qw(tempfile); | ||
| 25 | use Fcntl qw(LOCK_EX LOCK_SH LOCK_NB); | ||
| 26 | use File::NFSLock; | ||
| 27 | use Symbol (); | ||
| 28 | use Carp; | ||
| 29 | |||
| 30 | use base qw(Cache::Entry); | ||
| 31 | use fields qw(dir path lockdetails); | ||
| 32 | |||
| 33 | our $VERSION = '2.11'; | ||
| 34 | |||
| 35 | # hash of locks held my the process, keyed on path. This is useful for | ||
| 36 | # catching potential deadlocks and warning the user, and for implementing | ||
| 37 | # LOCK_NONE (which still needs to do some synchronization). Each entry will | ||
| 38 | # be an hash of { lock, type, count, lock, lockfh, linkcount }. The | ||
| 39 | # filehandle and link count is for checking when the lock has been released by | ||
| 40 | # another process. | ||
| 41 | my %PROCESS_LOCKS; | ||
| 42 | |||
| 43 | |||
| 44 | sub new { | ||
| 45 | my Cache::File::Entry $self = shift; | ||
| 46 | |||
| 47 | $self = fields::new($self) unless ref $self; | ||
| 48 | $self->SUPER::new(@_); | ||
| 49 | |||
| 50 | # get file path and store full path and containing directory | ||
| 51 | my ($dir, $file) = $self->{cache}->cache_file_path($self->{key}); | ||
| 52 | |||
| 53 | $self->{dir} = $dir; | ||
| 54 | $self->{path} = File::Spec->catfile($dir, $file); | ||
| 55 | |||
| 56 | return $self; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub exists { | ||
| 60 | my Cache::File::Entry $self = shift; | ||
| 61 | |||
| 62 | # ensure pending expiries are removed | ||
| 63 | $self->{cache}->purge(); | ||
| 64 | |||
| 65 | return -e $self->{path}; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _set { | ||
| 69 | my Cache::File::Entry $self = shift; | ||
| 70 | my ($data, $expiry) = @_; | ||
| 71 | |||
| 72 | $self->_make_path() or return; | ||
| 73 | |||
| 74 | my ($fh, $filename) = tempfile('.XXXXXXXX', DIR => $self->{dir}); | ||
| 75 | binmode $fh; | ||
| 76 | print $fh $data; | ||
| 77 | close($fh); | ||
| 78 | |||
| 79 | my $time = time(); | ||
| 80 | my $cache = $self->{cache}; | ||
| 81 | my $key = $self->{key}; | ||
| 82 | |||
| 83 | # lock indexes | ||
| 84 | $cache->lock(); | ||
| 85 | |||
| 86 | my $exists = -e $self->{path}; | ||
| 87 | my $orig_size; | ||
| 88 | |||
| 89 | unless ($exists) { | ||
| 90 | # we're creating the entry | ||
| 91 | $cache->create_entry($key, $time); | ||
| 92 | $cache->change_count(1); | ||
| 93 | $orig_size = 0; | ||
| 94 | } | ||
| 95 | # only remove current size if there is no active write handle | ||
| 96 | elsif ($self->_trylock(LOCK_SH)) { | ||
| 97 | $orig_size = $self->size(); | ||
| 98 | $self->_unlock(); | ||
| 99 | } | ||
| 100 | else { | ||
| 101 | $orig_size = 0; | ||
| 102 | } | ||
| 103 | |||
| 104 | # replace existing data | ||
| 105 | rename($filename, $self->{path}); | ||
| 106 | |||
| 107 | # fix permissions of tempfile | ||
| 108 | my $mode = 0666 & ~($self->{cache}->cache_umask()); | ||
| 109 | chmod $mode, $self->{path}; | ||
| 110 | |||
| 111 | # invalidate any active handle locks | ||
| 112 | unlink($self->{path} . $Cache::File::LOCK_EXT); | ||
| 113 | delete $PROCESS_LOCKS{$self->{path}}; | ||
| 114 | |||
| 115 | $self->_set_expiry($expiry) if $expiry or $exists; | ||
| 116 | $cache->update_last_use($key, $time) if $exists; | ||
| 117 | |||
| 118 | $cache->change_size($self->size() - $orig_size); | ||
| 119 | # ensure pending expiries are removed | ||
| 120 | $cache->purge(); | ||
| 121 | |||
| 122 | $cache->unlock(); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _get { | ||
| 126 | my Cache::File::Entry $self = shift; | ||
| 127 | |||
| 128 | my $cache = $self->{cache}; | ||
| 129 | my $key = $self->{key}; | ||
| 130 | my $exists; | ||
| 131 | my $time = time(); | ||
| 132 | |||
| 133 | $cache->lock(); | ||
| 134 | |||
| 135 | if ($exists = $self->exists()) { | ||
| 136 | # update last used | ||
| 137 | $cache->update_last_use($key, $time); | ||
| 138 | |||
| 139 | # lock entry for reading | ||
| 140 | $self->_lock(LOCK_SH); | ||
| 141 | } | ||
| 142 | |||
| 143 | $cache->unlock(); | ||
| 144 | |||
| 145 | return undef unless $exists; | ||
| 146 | |||
| 147 | File::NFSLock::uncache($self->{path}) | ||
| 148 | if $cache->cache_lock_level() == Cache::File::LOCK_NFS(); | ||
| 149 | |||
| 150 | my $fh = Symbol::gensym(); | ||
| 151 | my $data; | ||
| 152 | my $oldmask = umask $self->{cache}->cache_umask(); | ||
| 153 | if (open($fh, $self->{path})) { | ||
| 154 | binmode $fh; | ||
| 155 | |||
| 156 | # slurp mode | ||
| 157 | local $/; | ||
| 158 | $data = <$fh>; | ||
| 159 | |||
| 160 | close($fh); | ||
| 161 | } | ||
| 162 | umask $oldmask; | ||
| 163 | |||
| 164 | # shared locks can be unlocked without holding cache lock | ||
| 165 | $self->_unlock(); | ||
| 166 | return $data; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub size { | ||
| 170 | my Cache::File::Entry $self = shift; | ||
| 171 | return -s $self->{path}; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub remove { | ||
| 175 | my Cache::File::Entry $self = shift; | ||
| 176 | |||
| 177 | my $cache = $self->{cache}; | ||
| 178 | my $key = $self->{key}; | ||
| 179 | |||
| 180 | $cache->lock(); | ||
| 181 | |||
| 182 | unless (-r $self->{path}) { | ||
| 183 | $cache->unlock(); | ||
| 184 | return; | ||
| 185 | } | ||
| 186 | |||
| 187 | my $index = $cache->get_index(); | ||
| 188 | my $index_entries = $cache->get_index_entries($key) | ||
| 189 | or warnings::warnif('Cache', "missing index entry for $key"); | ||
| 190 | delete $$index{$key}; | ||
| 191 | |||
| 192 | if ($$index_entries{age}) { | ||
| 193 | my $ageheap = $cache->get_age_heap(); | ||
| 194 | $ageheap->delete($$index_entries{age}, $key); | ||
| 195 | } | ||
| 196 | |||
| 197 | if ($$index_entries{lastuse}) { | ||
| 198 | my $useheap = $cache->get_use_heap(); | ||
| 199 | $useheap->delete($$index_entries{lastuse}, $key); | ||
| 200 | } | ||
| 201 | |||
| 202 | if ($$index_entries{expiry}) { | ||
| 203 | my $expheap = $cache->get_exp_heap(); | ||
| 204 | $expheap->delete($$index_entries{expiry}, $key) | ||
| 205 | } | ||
| 206 | |||
| 207 | my $size = 0; | ||
| 208 | if ($self->_trylock(LOCK_SH)) { | ||
| 209 | $size = (-s $self->{path}); | ||
| 210 | $cache->change_size(-$size); | ||
| 211 | $self->_unlock(); | ||
| 212 | } | ||
| 213 | $cache->change_count(-1); | ||
| 214 | |||
| 215 | unlink($self->{path}); | ||
| 216 | |||
| 217 | # obliterate any entry lockfile | ||
| 218 | unlink($self->{path} . $Cache::File::LOCK_EXT); | ||
| 219 | delete $PROCESS_LOCKS{$self->{path}}; | ||
| 220 | |||
| 221 | $cache->unlock(); | ||
| 222 | |||
| 223 | return $size; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub expiry { | ||
| 227 | my Cache::File::Entry $self = shift; | ||
| 228 | my $cache = $self->{cache}; | ||
| 229 | |||
| 230 | $cache->lock(); | ||
| 231 | my $index_entries = $cache->get_index_entries($self->{key}); | ||
| 232 | $cache->unlock(); | ||
| 233 | return $index_entries? $$index_entries{expiry} : undef; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub _set_expiry { | ||
| 237 | my Cache::File::Entry $self = shift; | ||
| 238 | my ($time) = @_; | ||
| 239 | |||
| 240 | my $cache = $self->{cache}; | ||
| 241 | my $key = $self->{key}; | ||
| 242 | |||
| 243 | $cache->lock(); | ||
| 244 | |||
| 245 | my $index_entries = $cache->get_index_entries($key); | ||
| 246 | |||
| 247 | unless ($index_entries) { | ||
| 248 | $cache->unlock(); | ||
| 249 | croak "Cannot set expiry on non-existant entry: $key"; | ||
| 250 | } | ||
| 251 | |||
| 252 | my $expheap = $cache->get_exp_heap(); | ||
| 253 | $expheap->delete($$index_entries{expiry}, $key) | ||
| 254 | if $$index_entries{expiry}; | ||
| 255 | $expheap->add($time, $key) if $time; | ||
| 256 | |||
| 257 | $$index_entries{expiry} = $time; | ||
| 258 | $cache->set_index_entries($key, $index_entries); | ||
| 259 | |||
| 260 | $cache->unlock(); | ||
| 261 | } | ||
| 262 | |||
| 263 | sub _handle { | ||
| 264 | my Cache::File::Entry $self = shift; | ||
| 265 | my ($mode, $expiry) = @_; | ||
| 266 | |||
| 267 | # a bit of magic! Since handles hold a lock indefinitely, and the entry | ||
| 268 | # lock code doesn't do recursion (its not necessary) we could get into | ||
| 269 | # trouble. So instead we just ensure that every handle has it's own entry | ||
| 270 | # associated with it. | ||
| 271 | $self = $self->{cache}->entry($self->{key}); | ||
| 272 | |||
| 273 | require Cache::File::Handle; | ||
| 274 | |||
| 275 | my $exists = -e $self->{path}; | ||
| 276 | my $writing = $mode =~ />|\+/; | ||
| 277 | |||
| 278 | unless ($exists) { | ||
| 279 | # return undef unless we're writing a new entry | ||
| 280 | $writing or return undef; | ||
| 281 | |||
| 282 | # make the path | ||
| 283 | $self->_make_path(); | ||
| 284 | } | ||
| 285 | |||
| 286 | my $time = time(); | ||
| 287 | my $cache = $self->{cache}; | ||
| 288 | my $key = $self->{key}; | ||
| 289 | |||
| 290 | # lock indexes | ||
| 291 | $cache->lock(); | ||
| 292 | |||
| 293 | # grab entry lock | ||
| 294 | $self->_lock($writing? LOCK_EX : LOCK_SH); | ||
| 295 | |||
| 296 | # create the attributes if the entry doesn't exist | ||
| 297 | unless ($exists) { | ||
| 298 | # we're creating the entry | ||
| 299 | $cache->create_entry($key, $time); | ||
| 300 | $cache->change_count(1); | ||
| 301 | } | ||
| 302 | |||
| 303 | # if truncating, reset expiry (or set it creating and its specified) | ||
| 304 | $cache->set_expiry($key, $expiry) | ||
| 305 | if ($expiry and not $exists) or ($mode =~/\+?>/); | ||
| 306 | $cache->update_last_use($key, $time) if $exists; | ||
| 307 | |||
| 308 | my $orig_size = $writing? ($exists? $self->size() : 0) : undef; | ||
| 309 | |||
| 310 | # open handle - entry lock will be held as self persists in the closure | ||
| 311 | my $oldmask = umask $cache->cache_umask(); | ||
| 312 | my $handle = Cache::File::Handle->new($self->{path}, $mode, undef, | ||
| 313 | sub { $self->_handle_closed(shift, $orig_size); } ); | ||
| 314 | umask $oldmask; | ||
| 315 | |||
| 316 | $handle or warnings::warnif('io', 'Failed to open '.$self->{path}.": $!"); | ||
| 317 | |||
| 318 | $cache->unlock(); | ||
| 319 | |||
| 320 | return $handle; | ||
| 321 | } | ||
| 322 | |||
| 323 | |||
| 324 | sub validity { | ||
| 325 | my Cache::File::Entry $self = shift; | ||
| 326 | |||
| 327 | my $cache = $self->{cache}; | ||
| 328 | $cache->lock(); | ||
| 329 | |||
| 330 | my $index_entries = $cache->get_index_entries($self->{key}); | ||
| 331 | |||
| 332 | $cache->unlock(); | ||
| 333 | |||
| 334 | return $index_entries? $$index_entries{validity} : undef; | ||
| 335 | } | ||
| 336 | |||
| 337 | sub set_validity { | ||
| 338 | my Cache::File::Entry $self = shift; | ||
| 339 | my ($data) = @_; | ||
| 340 | |||
| 341 | my $key = $self->{key}; | ||
| 342 | my $cache = $self->{cache}; | ||
| 343 | $cache->lock(); | ||
| 344 | |||
| 345 | my $index_entries = $cache->get_index_entries($key); | ||
| 346 | |||
| 347 | unless ($index_entries) { | ||
| 348 | $self->set(''); | ||
| 349 | $index_entries = $cache->get_index_entries($key); | ||
| 350 | } | ||
| 351 | |||
| 352 | $$index_entries{validity} = $data; | ||
| 353 | $cache->set_index_entries($key, $index_entries); | ||
| 354 | |||
| 355 | $cache->unlock(); | ||
| 356 | } | ||
| 357 | |||
| 358 | |||
| 359 | # UTILITY METHODS | ||
| 360 | |||
| 361 | sub _handle_closed { | ||
| 362 | my Cache::File::Entry $self = shift; | ||
| 363 | my ($handle, $orig_size) = @_; | ||
| 364 | |||
| 365 | unless (defined $orig_size) { | ||
| 366 | # shared locks can be unlocked without holding cache lock | ||
| 367 | $self->_unlock(); | ||
| 368 | return; | ||
| 369 | } | ||
| 370 | |||
| 371 | my $cache = $self->{cache}; | ||
| 372 | |||
| 373 | $cache->lock(); | ||
| 374 | |||
| 375 | # check if file still exists and our lock is still valid. this order is | ||
| 376 | # used to prevent a race between checking lock and getting size | ||
| 377 | my $new_size = $self->size(); | ||
| 378 | (defined $new_size and $self->_check_lock()) or $new_size = 0; | ||
| 379 | |||
| 380 | # release entry lock | ||
| 381 | $self->_unlock(); | ||
| 382 | |||
| 383 | # update sizes | ||
| 384 | if (defined $orig_size and $orig_size != $new_size) { | ||
| 385 | $cache->change_size($new_size - $orig_size); | ||
| 386 | } | ||
| 387 | |||
| 388 | $cache->unlock(); | ||
| 389 | } | ||
| 390 | |||
| 391 | sub _make_path { | ||
| 392 | my Cache::File::Entry $self = shift; | ||
| 393 | |||
| 394 | unless (-d $self->{dir}) { | ||
| 395 | my $oldmask = umask $self->{cache}->cache_umask(); | ||
| 396 | |||
| 397 | eval { mkpath($self->{dir}); }; | ||
| 398 | if ($@) { | ||
| 399 | warnings::warnif('io', | ||
| 400 | 'Failed to create path '.$self->{dir}.": $@"); | ||
| 401 | return 0; | ||
| 402 | } | ||
| 403 | |||
| 404 | umask $oldmask; | ||
| 405 | } | ||
| 406 | |||
| 407 | return 1; | ||
| 408 | } | ||
| 409 | |||
| 410 | sub _lock { | ||
| 411 | my Cache::File::Entry $self = shift; | ||
| 412 | my ($type, $tryonly) = @_; | ||
| 413 | $type ||= LOCK_EX; | ||
| 414 | |||
| 415 | # entry already has the lock? | ||
| 416 | $self->{lockdetails} and die "entry already holding a lock"; | ||
| 417 | |||
| 418 | my $path = $self->{path}; | ||
| 419 | my $lock_details = $PROCESS_LOCKS{$path}; | ||
| 420 | |||
| 421 | if ($lock_details) { | ||
| 422 | if ($$lock_details{type} != $type) { | ||
| 423 | $tryonly and return 0; | ||
| 424 | croak "process already holding entry lock of different type"; | ||
| 425 | } | ||
| 426 | $$lock_details{count}++; | ||
| 427 | $self->{lockdetails} = $lock_details; | ||
| 428 | return 1; | ||
| 429 | } | ||
| 430 | |||
| 431 | # create new entry | ||
| 432 | $lock_details = $PROCESS_LOCKS{$path} = {}; | ||
| 433 | |||
| 434 | # no need for any locking with LOCK_NONE | ||
| 435 | if ($self->{cache}->cache_lock_level() != Cache::File::LOCK_NONE()) { | ||
| 436 | local $File::NFSLock::LOCK_EXTENSION = $Cache::File::LOCK_EXT; | ||
| 437 | my $oldmask = umask $self->{cache}->cache_umask(); | ||
| 438 | |||
| 439 | my $lock = File::NFSLock->new({ | ||
| 440 | file => $path, | ||
| 441 | lock_type => $type | ($tryonly? LOCK_NB : 0), | ||
| 442 | stale_lock_timeout => $Cache::File::STALE_LOCK_TIMEOUT, | ||
| 443 | }); | ||
| 444 | |||
| 445 | unless ($lock) { | ||
| 446 | umask $oldmask; | ||
| 447 | $tryonly and return 0; | ||
| 448 | die "Failed to obtain lock on lockfile on '$path': ". | ||
| 449 | $File::NFSLock::errstr."\n"; | ||
| 450 | } | ||
| 451 | |||
| 452 | # count the number of hard links to the lockfile and open it | ||
| 453 | # if we can't reopen the lockfile then it has already been removed... | ||
| 454 | # we do the stat on the file rather than the filehandle, as otherwise | ||
| 455 | # there would be a race between opening the file and getting the link | ||
| 456 | # count (such that we could end up with a link count that is already 0). | ||
| 457 | my $fh = Symbol::gensym; | ||
| 458 | my $linkcount; | ||
| 459 | my $lockfile = $path . $Cache::File::LOCK_EXT; | ||
| 460 | if (($linkcount = (stat $lockfile)[3]) and open($fh, $lockfile)) { | ||
| 461 | $$lock_details{lock} = $lock; | ||
| 462 | $$lock_details{lockfh} = $fh; | ||
| 463 | $$lock_details{linkcount} = $linkcount; | ||
| 464 | } | ||
| 465 | else { | ||
| 466 | # lock failed - remove lock details | ||
| 467 | delete $PROCESS_LOCKS{$path}; | ||
| 468 | } | ||
| 469 | umask $oldmask; | ||
| 470 | } | ||
| 471 | |||
| 472 | # lock obtained | ||
| 473 | |||
| 474 | $$lock_details{type} = $type; | ||
| 475 | $$lock_details{count} = 1; | ||
| 476 | |||
| 477 | # use lock details reference as an internal lock check | ||
| 478 | $self->{lockdetails} = $lock_details; | ||
| 479 | |||
| 480 | return 1; | ||
| 481 | } | ||
| 482 | |||
| 483 | sub _trylock { | ||
| 484 | my Cache::File::Entry $self = shift; | ||
| 485 | my ($type) = @_; | ||
| 486 | return $self->_lock($type, 1); | ||
| 487 | } | ||
| 488 | |||
| 489 | sub _unlock { | ||
| 490 | my Cache::File::Entry $self = shift; | ||
| 491 | |||
| 492 | $self->{lockdetails} or die 'not locked'; | ||
| 493 | |||
| 494 | # is our lock still valid? | ||
| 495 | $self->_check_lock() or return; | ||
| 496 | |||
| 497 | $self->{lockdetails} = undef; | ||
| 498 | |||
| 499 | my $lock_details = $PROCESS_LOCKS{$self->{path}}; | ||
| 500 | --$$lock_details{count} == 0 | ||
| 501 | or return; | ||
| 502 | |||
| 503 | if ($self->{cache}->cache_lock_level() != Cache::File::LOCK_NONE()) { | ||
| 504 | $$lock_details{lock}->unlock; | ||
| 505 | } | ||
| 506 | delete $PROCESS_LOCKS{$self->{path}}; | ||
| 507 | } | ||
| 508 | |||
| 509 | # check that we still hold our lock | ||
| 510 | sub _check_lock { | ||
| 511 | my Cache::File::Entry $self = shift; | ||
| 512 | |||
| 513 | $self->{lockdetails} or return 0; | ||
| 514 | my $lock_details = $PROCESS_LOCKS{$self->{path}} | ||
| 515 | or return 0; | ||
| 516 | |||
| 517 | # check lock details reference still matches global | ||
| 518 | $self->{lockdetails} == $lock_details | ||
| 519 | or return 0; | ||
| 520 | |||
| 521 | if ($self->{cache}->cache_lock_level() != Cache::File::LOCK_NONE()) { | ||
| 522 | # check filehandle is still connected to filesystem | ||
| 523 | my $lockfh = $$lock_details{lockfh}; | ||
| 524 | if (((stat $lockfh)[3] || 0) < $$lock_details{linkcount}) { | ||
| 525 | # lock is gone | ||
| 526 | delete $PROCESS_LOCKS{$self->{path}}; | ||
| 527 | return 0; | ||
| 528 | } | ||
| 529 | } | ||
| 530 | |||
| 531 | return 1; | ||
| 532 | } | ||
| 533 | |||
| 534 | |||
| 535 | 1; | ||
| 536 | __END__ |