[BACK]Return to Cached.pm CVS log [TXT][DIR] Up to [local] / openbsd / OpenBSDTorrents / lib / BT / MetaInfo

Annotation of openbsd/OpenBSDTorrents/lib/BT/MetaInfo/Cached.pm, Revision 1.3

1.3     ! andrew      1: # $Id: OBTMetaInfo.pm,v 1.2 2005/05/02 19:43:44 andrew Exp andrew $
1.2       andrew      2: use strict;
                      3:
1.1       andrew      4: package BT::OBTMetaInfo;
                      5:
                      6: require 5.6.0;
1.2       andrew      7: use vars qw( $VERSION @ISA );
1.1       andrew      8:
1.3     ! andrew      9: use Digest::SHA1 qw(sha1);
        !            10: use Fcntl ':flock'; # import LOCK_* constants
        !            11:
1.1       andrew     12: use BT::MetaInfo;
1.3     ! andrew     13: use base 'BT::MetaInfo';
1.1       andrew     14:
1.3     ! andrew     15: use OpenBSDTorrents;
        !            16:
1.2       andrew     17: use Data::Dumper;
1.1       andrew     18:
1.3     ! andrew     19: $VERSION = do { my @r = (q$Id: OBTMetaInfo.pm,v 1.2 2005/05/02 19:43:44 andrew Exp andrew $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
1.2       andrew     20:
1.1       andrew     21: sub new
                     22: {
                     23:        my $classname   = shift;
1.3     ! andrew     24:        return $classname->SUPER::new(@_);
1.1       andrew     25: }
                     26:
                     27:
1.3     ! andrew     28: sub info_hash_cached
        !            29: {
        !            30:        my $self = shift;
        !            31:        my $torrent = shift;
        !            32:
        !            33:        return $self->SUPER::info_hash unless $torrent;
        !            34:
        !            35:        my $meta_file = $torrent;
        !            36:        $meta_file =~ s/\.torrent$/.$OBT->{META_EXT}/;
        !            37:
        !            38:        my $hash = undef;
        !            39:
        !            40:        if (-e $meta_file) {
        !            41:                #print "Reading meta file: $meta_file\n";
        !            42:                open my $meta, $meta_file or die "Couldn't open $meta_file: $!";
        !            43:                flock($meta, LOCK_SH);
        !            44:                binmode $meta;
        !            45:
        !            46:                $hash = do { local $/; <$meta> };
        !            47:
        !            48:                flock($meta, LOCK_UN);
        !            49:                close $meta;
        !            50:        } else {
        !            51:                $hash = $self->SUPER::info_hash;
        !            52:                #print "Writing meta file: $meta_file\n";
        !            53:                open my $meta, '>', $meta_file
        !            54:                        or die "Couldn't open $meta_file: $!";
        !            55:                flock($meta, LOCK_EX);
        !            56:                binmode $meta;
        !            57:
        !            58:                print $meta $hash;
        !            59:
        !            60:                flock($meta, LOCK_UN);
        !            61:                close $meta;
        !            62:
        !            63:        }
        !            64:        #my $text_hash = unpack("H*", $hash);
        !            65:        #print "INFO_HASH: $text_hash\n";
        !            66:
        !            67:        return $hash;
        !            68: }
1.1       andrew     69:
1.2       andrew     70: 1

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>