Annotation of openbsd/OpenBSDTorrents/CurrentTorrents.pl, Revision 1.16
1.1 andrew 1: #!/usr/bin/perl -T
1.16 ! andrew 2: #$Id: CurrentTorrents.pl,v 1.15 2005/05/04 00:48:35 andrew Exp andrew $
1.1 andrew 3: use strict;
4: use warnings;
5: use diagnostics;
6:
1.2 andrew 7: use Time::Local;
1.16 ! andrew 8: use Fcntl ':flock';
1.1 andrew 9:
1.9 andrew 10: use lib 'lib';
1.1 andrew 11: use OpenBSDTorrents;
1.13 andrew 12: use BT::OBTMetaInfo;
1.1 andrew 13:
14: %ENV = ();
15:
1.13 andrew 16: use YAML;
1.6 andrew 17:
1.15 andrew 18: #justme();
1.1 andrew 19:
1.12 andrew 20: my $Name_Filter = shift || '';
21: if ($Name_Filter =~ /^(\w*)$/) {
22: $Name_Filter = $1;
23: } else {
24: die "Invalid filter: $Name_Filter";
25: }
26:
1.15 andrew 27: my %Possible_Torrents;
28: Process_Dir($OBT->{DIR_FTP});
29:
1.2 andrew 30: my %files;
1.15 andrew 31: my %keep;
32: my @delete;
1.13 andrew 33: foreach my $DIR ($OBT->{DIR_NEW_TORRENT}, $OBT->{DIR_TORRENT}) {
34: opendir DIR, $DIR
35: or die "Couldn't opendir $DIR: $!";
36: foreach (readdir DIR) {
37: next unless my ($ext) = /\.(torrent|$OBT->{META_EXT})$/;
1.2 andrew 38:
1.13 andrew 39: if (/^([^\/]+)$/) {
40: $_ = $1;
41: } else {
42: die "Invalid character in $_: $!";
43: }
44: my ($name, $year, $mon, $mday, $hour, $min) =
45: /^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/;
1.2 andrew 46:
1.13 andrew 47: $mon--;
48: my $epoch = timegm(0,$min,$hour,$mday,$mon,$year);
1.2 andrew 49:
1.13 andrew 50: #print "Adding $_\n";
1.2 andrew 51:
1.13 andrew 52: $files{$ext}{$name}{$epoch} = {
53: file => $_,
54: dir => $DIR,
55: path => "$DIR/$_",
56: ext => $ext,
57: year => $year,
58: mon => $mon,
59: mday => $mday,
60: hour => $hour,
61: min => $min,
62: name => $name,
63: epoch => $epoch,
64: };
65:
1.15 andrew 66: unless (exists $Possible_Torrents{$name}) {
67: print "Would remove $_\n";
68: push @delete, $files{$ext}{$name}{$epoch};
69: }
1.13 andrew 70: }
71: closedir DIR;
1.2 andrew 72: }
73:
1.13 andrew 74: foreach my $name (keys %{ $files{torrent} }) {
1.12 andrew 75: next unless $name =~ /^$Name_Filter/;
76: print "Checking $name\n";
1.4 andrew 77:
1.13 andrew 78: foreach my $epoch ( sort { $b <=> $a } keys %{ $files{torrent}{$name} } ) {
1.4 andrew 79: #print "\t$epoch\n";
1.13 andrew 80: my $torrent = $files{torrent}{$name}{$epoch}{path};
1.4 andrew 81:
1.14 andrew 82: if (
83: keys %{ $files{torrent}{$name} } == 1 &&
84: $files{torrent}{$name}{$epoch}{dir}
85: eq $OBT->{DIR_TORRENT}
86: ) {
87: #print "Skipping torrent for $name there is only one.\n";
88: next;
89: }
90:
1.16 ! andrew 91: my $meta_file = $torrent;
! 92: $meta_file =~ s/\.torrent$/.$OBT->{META_EXT}/;
1.14 andrew 93:
1.16 ! andrew 94: my $hash = undef;
! 95: if (-e $meta_file) {
! 96: #print "Reading meta file: $meta_file\n";
! 97: open my $meta, $meta_file
! 98: or die "Couldn't open $meta_file: $!";
! 99: flock($meta, LOCK_SH);
! 100: binmode $meta;
! 101:
! 102: $hash = do { local $/; <$meta> };
! 103:
! 104: flock($meta, LOCK_UN);
! 105: close $meta;
! 106: } else {
! 107:
1.4 andrew 108: my $t;
1.13 andrew 109: eval { $t = BT::OBTMetaInfo->new( $torrent ); };
110:
1.4 andrew 111: if ($@) {
112: warn "Error reading torrent $torrent\n";
113: next;
114: }
115:
1.13 andrew 116: $files{torrent}{$name}{$epoch}{comment} = $t->{comment};
1.4 andrew 117: my ($path) = $t->{comment} =~ /Files from ([^\n]+)\n/s;
118:
1.10 andrew 119: unless (-d $OBT->{DIR_FTP} . "/$path") {
1.13 andrew 120: #print "Deleting $files{torrent}{$name}{$epoch}{file} the path doesn't exist.\n";
121: push @delete, $files{torrent}{$name}{$epoch};
122: delete $files{torrent}{$name}{$epoch};
1.4 andrew 123: next;
124: }
125:
1.16 ! andrew 126: $hash = $t->info_hash_cached($torrent);
1.4 andrew 127: $hash = unpack("H*", $hash);
128:
1.16 ! andrew 129: undef $t;
! 130:
! 131: }
! 132:
1.13 andrew 133: $files{torrent}{$name}{$epoch}{info_hash} = $hash;
1.4 andrew 134:
135:
1.2 andrew 136: if (exists $keep{$name}) {
137: if (exists $keep{$name}{$hash}) {
138: push @delete, $keep{$name}{$hash};
1.13 andrew 139: delete $files{torrent}{
140: $keep{$name}{$hash}{name}
141: }{
142: $keep{$name}{$hash}{epoch}
143: };
1.2 andrew 144: $keep{$name}{$hash} =
1.13 andrew 145: $files{torrent}{$name}{$epoch};
1.2 andrew 146: } else {
1.13 andrew 147: push @delete, $files{torrent}{$name}{$epoch};
148: delete $files{torrent}{$name}{$epoch};
1.2 andrew 149: }
150: } else {
151: $keep{$name}{$hash} =
1.13 andrew 152: $files{torrent}{$name}{$epoch};
1.2 andrew 153:
154: }
155: }
156: }
157:
158: #print Dump \%files, \%keep, \@delete;
159:
160: foreach (@delete) {
1.13 andrew 161: print "Deleting '$_->{path}'\n";
162: unlink $_->{path} or die "Couldn't unlink $_->{path}";
163: }
164:
165: foreach my $name (keys %{ $files{$OBT->{META_EXT} } }) {
166: foreach my $epoch (keys %{ $files{ $OBT->{META_EXT} }{$name} }) {
167: unless ( exists $files{torrent}{$name}{$epoch} ) {
168: my $path = $files{$OBT->{META_EXT}}{$name}{$epoch}{path};
169: print "Unlinking '$path'\n";
170: unlink $path or die "couldn't unlink '$path': $!";
171: }
172: }
173: }
174:
175:
176: #print Dump \%keep;
177: foreach my $name (keys %keep) {
178: foreach my $hash (keys %{ $keep{$name} }) {
179: my $file = $keep{$name}{$hash}{file};
180: my $dir = $keep{$name}{$hash}{dir };
181: if ($dir eq $OBT->{DIR_NEW_TORRENT}) {
182: print "Moving $file to current torrents\n";
183: rename("$dir/$file", $OBT->{DIR_TORRENT} . "/" . $file)
184: or die "Couldn't rename '$file': $!";
185:
186: my $name = $keep{$name}{$hash}{name};
187: my $epoch = $keep{$name}{$hash}{epoch};
188:
189: if (exists $files{txt}{$name}{$epoch}) {
190: my $m_file = $files{txt}{$name}{$epoch}{file};
191: my $m_dir = $files{txt}{$name}{$epoch}{dir };
192: rename(
193: "$m_dir/$m_file",
194: $OBT->{DIR_TORRENT} . "/" . $m_file
195: ) or die "Couldn't rename '$m_file': $!";
196: }
197: }
198: }
1.2 andrew 199: }
1.15 andrew 200:
201: sub Process_Dir
202: {
203: my $basedir = shift;
204:
205: my ($dirs, $files) = Get_Files_and_Dirs($basedir);
206: if (@$files) {
207: my $dir = $basedir;
208: $dir =~ s/^$OBT->{DIR_FTP}\///;
209: my $torrent = Name_Torrent($dir);
210: $torrent =~ s/-.*$//;
211: $Possible_Torrents{$torrent} = 1;
212: }
213:
214: foreach my $subdir (@$dirs) {
215: next if $subdir eq '.';
216: next if $subdir eq '..';
217: Process_Dir("$basedir/$subdir")
218: }
219: }
220:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>