=================================================================== RCS file: /cvs/openbsd/OpenBSDTorrents/CurrentTorrents.pl,v retrieving revision 1.29 retrieving revision 1.35 diff -u -r1.29 -r1.35 --- openbsd/OpenBSDTorrents/CurrentTorrents.pl 2010/01/08 17:18:35 1.29 +++ openbsd/OpenBSDTorrents/CurrentTorrents.pl 2010/03/08 20:19:35 1.35 @@ -1,5 +1,5 @@ #!/usr/bin/perl -T -#$RedRiver: CurrentTorrents.pl,v 1.28 2010/01/05 19:55:22 andrew Exp $ +#$RedRiver: CurrentTorrents.pl,v 1.34 2010/03/03 18:31:57 andrew Exp $ use strict; use warnings; use diagnostics; @@ -63,7 +63,7 @@ $files{$ext}{$name}{$epoch} = { file => $_, dir => $DIR, - path => "$DIR/$_", + #path => "$DIR/$_", ext => $ext, #year => $year, @@ -78,7 +78,7 @@ if ( $name =~ m/\A $OBT->{BASENAME} /xms && !exists $Possible_Torrents{$name} ) { - print "Would remove $_\n"; + #print "Would remove $_\n"; push @delete, $files{$ext}{$name}{$epoch}; } } @@ -92,6 +92,7 @@ foreach my $name ( sort keys %{ $files{torrent} } ) { next unless $name =~ /^$Name_Filter/; + #next if $name !~ /songs/xms; #next if $name =~ /_packages_/xms; #print "Checking $name\n"; @@ -99,7 +100,7 @@ EPOCH: foreach my $epoch ( sort { $b <=> $a } keys %{$cn} ) { my $ct = $cn->{$epoch}; - my $cf = $ct->{path}; + my $cf = $ct->{dir} . '/' . $ct->{file}; #print "\t$epoch - $cf\n"; @@ -113,7 +114,6 @@ if ($@) { warn "Error reading torrent $cf\n"; push @delete, $ct; - delete $cn->{$epoch}; next EPOCH; } @@ -126,7 +126,6 @@ $cn->{$epoch}{file}, ' the path (', $path, ") doesn't exist.\n"; push @delete, $ct; - delete $cn->{$epoch}; next EPOCH; } @@ -136,23 +135,15 @@ undef $t; if ( $seen{$name} && $seen{$name} ne $hash ) { - print "Removing older [$name] [$hash]\n"; - if ( $keep{$hash}{path} ) { - print "\t", $keep{$hash}{path}, "\n"; - } + print "Removing older [$name] [$hash]\n\t", + $cf, + "\n"; + $ct->{reason} = 'older'; push @delete, $ct; - delete $cn->{$epoch}; next EPOCH; } - $seen{$name} = $hash; - - if ( keys %{$cn} == 1 && $ct->{dir} eq $OBT->{DIR_TORRENT} ) { - $keep{$hash} = $ct; - - #print "Keeping only instance of [$name] [$hash]\n\t", - # $ct->{path}, - # "\n"; - next EPOCH; + elsif ( keys %{$cn} == 1 && $ct->{dir} eq $OBT->{DIR_TORRENT} ) { + $ct->{reason} = 'only'; } elsif ( $keep{$hash} ) { if ( $keep{$hash}{epoch} == $epoch ) { @@ -160,27 +151,24 @@ } print "Removing duplicate [$name] [$hash]\n\t", - $keep{$hash}{path}, "\n"; - push @delete, $keep{$hash}; - delete $files{torrent}{ $keep{$hash}{name} } - { $keep{$hash}{epoch} }; + $keep{$hash}{file}, "\n"; - $keep{$hash} = $ct; - print "Keeping additional instance of [$name] [$hash]\n\t", - $ct->{path}, - "\n"; + $keep{$hash}{reason} = 'duplicate'; + $ct->{reason} = 'duplicate'; + + push @delete, $keep{$hash}; } else { - $keep{$hash} = $ct; - print "Keeping first instance of [$name] [$hash]\n\t", - $ct->{path}, - "\n"; - + $ct->{reason} = 'first'; } + + $keep{$hash} = $ct; + $seen{$name} = $hash; } } #print Dump \%files, \%keep, \@delete; +#print Dump \%keep, \@delete; #exit; my $client = Transmission::Client->new; @@ -203,25 +191,35 @@ foreach my $hash ( keys %keep ) { my $file = $keep{$hash}{file} || q{}; my $dir = $keep{$hash}{dir} || q{}; + + my $name = $keep{$hash}{name}; + my $epoch = $keep{$hash}{epoch}; + my $reason = $keep{$hash}{reason} ? $keep{$hash}{reason} . q{ } : q{}; + + #if ($reason && $reason ne 'only') { + # print "Keeping $reason instance of [$file] [$hash]\n", + # "\t", $file, "\n"; + #} + if ( $dir eq $OBT->{DIR_NEW_TORRENT} ) { print "Moving $file to current torrents\n"; rename( "$dir/$file", $OBT->{DIR_TORRENT} . "/" . $file ) or die "Couldn't rename '$file': $!"; - my $name = $keep{$hash}{name}; - my $epoch = $keep{$hash}{epoch}; $dir = $OBT->{DIR_TORRENT}; + $keep{$hash}{dir} = $dir; if ( exists $files{txt}{$name}{$epoch} ) { my $m_file = $files{txt}{$name}{$epoch}{file}; my $m_dir = $files{txt}{$name}{$epoch}{dir}; rename( "$m_dir/$m_file", $OBT->{DIR_TORRENT} . "/" . $m_file ) or die "Couldn't rename '$m_file': $!"; + $files{txt}{$name}{$epoch}{dir} = $OBT->{DIR_TORRENT}; } } if ( !$seeding{$hash} ) { - print "Starting seed of [$file] [$hash]\n"; + print 'Starting seed of ' . $reason . "[$file] [$hash]\n"; if (!$client->add( filename => "$dir/$file", download_dir => $OBT->{DIR_FTP}, @@ -230,18 +228,18 @@ { #warn $client->error, ": $dir/$file\n"; - print "Removing invalid torrent\n\t", $keep{$hash}{path}, "\n"; + print "Removing invalid torrent\n\t", $keep{$hash}{file}, "\n"; push @delete, $keep{$hash}; - delete $files{torrent}{ $keep{$hash}{name} } - { $keep{$hash}{epoch} }; } } } foreach (@delete) { - if ( $_->{path} ) { - print "Deleting '$_->{path}'\n"; - unlink $_->{path} or die "Couldn't unlink $_->{path}"; + my $path = $_->{dir} . '/' . $_->{file}; + if ( -e $path ) { + print "Deleting '$path'\n"; + unlink $path or die "Couldn't delete $path"; + delete $files{torrent}{ $_->{name} }{ $_->{epoch} }; } else { use Data::Dumper; @@ -252,7 +250,10 @@ foreach my $name ( keys %{ $files{ $OBT->{META_EXT} } } ) { foreach my $epoch ( keys %{ $files{ $OBT->{META_EXT} }{$name} } ) { unless ( exists $files{torrent}{$name}{$epoch} ) { - my $path = $files{ $OBT->{META_EXT} }{$name}{$epoch}{path}; + my $path = $files{ $OBT->{META_EXT} }{$name}{$epoch}{dir} + . '/' + . $files{ $OBT->{META_EXT} }{$name}{$epoch}{file}; + print "Unlinking '$path'\n"; unlink $path or die "couldn't unlink '$path': $!"; } @@ -268,14 +269,10 @@ if (@$files) { my $dir = $basedir; $dir =~ s/^$OBT->{DIR_FTP}\///; - my $torrent = Name_Torrent($dir); - $torrent =~ s/-.*$//; - $Possible_Torrents{$torrent} = 1; + Make_Possible($dir); foreach my $file (@$files) { if ( $file =~ /$INSTALL_ISO_REGEX/ ) { - $torrent = Name_Torrent("$dir/$file"); - $torrent =~ s/-.*$//; - $Possible_Torrents{$torrent} = 1; + Make_Possible("$dir/$file"); } } } @@ -287,3 +284,12 @@ } } +sub Make_Possible { + my ($path) = @_; + + my $torrent = Name_Torrent($path); + $torrent =~ s/-.*$//; + $Possible_Torrents{$torrent} = 1; + + return $torrent; +}