version 1.3, 2005/05/02 22:46:48 |
version 1.7, 2005/05/05 21:29:13 |
|
|
# $Id$ |
# $Id$ |
use strict; |
use strict; |
|
|
package BT::OBTMetaInfo; |
package BT::MetaInfo::Cached; |
|
|
require 5.6.0; |
require 5.6.0; |
use vars qw( $VERSION @ISA ); |
use vars qw( $VERSION @ISA ); |
|
|
use Digest::SHA1 qw(sha1); |
use Cache::FileCache; |
use Fcntl ':flock'; # import LOCK_* constants |
use File::Basename; |
|
|
use BT::MetaInfo; |
use BT::MetaInfo; |
use base 'BT::MetaInfo'; |
use base 'BT::MetaInfo'; |
|
|
use OpenBSDTorrents; |
|
|
|
use Data::Dumper; |
|
|
|
$VERSION = do { my @r = (q$Id$ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; |
$VERSION = do { my @r = (q$Id$ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; |
|
|
sub new |
sub new |
{ |
{ |
my $classname = shift; |
my $class = shift; |
return $classname->SUPER::new(@_); |
my $file = shift; |
} |
my $cache_settings = shift; |
|
|
|
$cache_settings->{namespace} ||= 'BT::MetaInfo::Cached'; |
|
|
sub info_hash_cached |
my $cache = new Cache::FileCache( $cache_settings ); |
{ |
|
my $self = shift; |
|
my $torrent = shift; |
|
|
|
return $self->SUPER::info_hash unless $torrent; |
my $obj = (defined($file)) ? _load($file, $cache) : {}; |
|
|
my $meta_file = $torrent; |
$obj->{cache} = $cache; |
$meta_file =~ s/\.torrent$/.$OBT->{META_EXT}/; |
|
|
|
my $hash = undef; |
return(bless($obj, $class)); |
|
} |
|
|
if (-e $meta_file) { |
sub _load { |
#print "Reading meta file: $meta_file\n"; |
my $file = shift; |
open my $meta, $meta_file or die "Couldn't open $meta_file: $!"; |
my $cache = shift; |
flock($meta, LOCK_SH); |
|
binmode $meta; |
|
|
|
$hash = do { local $/; <$meta> }; |
my $basename = basename($file); |
|
|
|
my $info = $cache->get( $basename ); |
|
|
flock($meta, LOCK_UN); |
unless (defined $info) { |
close $meta; |
$info = BT::MetaInfo::_load($file); |
} else { |
$cache->set( $basename, $info ); |
$hash = $self->SUPER::info_hash; |
} |
#print "Writing meta file: $meta_file\n"; |
return $info; |
open my $meta, '>', $meta_file |
} |
or die "Couldn't open $meta_file: $!"; |
|
flock($meta, LOCK_EX); |
|
binmode $meta; |
|
|
|
print $meta $hash; |
|
|
|
flock($meta, LOCK_UN); |
sub save |
close $meta; |
{ |
|
my ($self, $file) = @_; |
|
my $basename = basename($file); |
|
|
} |
$self->SUPER::save($file, @_); |
#my $text_hash = unpack("H*", $hash); |
|
#print "INFO_HASH: $text_hash\n"; |
|
|
|
return $hash; |
|
} |
|
|
|
1 |
my %info_hash = %$self; # unbless |
|
$self->cache->set->($basename, \%info_hash) |
|
} |