Annotation of openbsd/OpenBSDTorrents/CurrentTorrents.pl, Revision 1.25
1.1 andrew 1: #!/usr/bin/perl -T
1.25 ! andrew 2: #$RedRiver: CurrentTorrents.pl,v 1.24 2007/11/02 02:36:01 andrew Exp $
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.23 andrew 9: use File::Basename;
10: #use YAML;
1.1 andrew 11:
1.9 andrew 12: use lib 'lib';
1.1 andrew 13: use OpenBSDTorrents;
1.17 andrew 14: use BT::MetaInfo::Cached;
1.1 andrew 15:
16: %ENV = ();
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: }
1.23 andrew 44: my $epoch = 0;
45: my $name = basename($_, '.torrent');
46:
47: if (my ($base, $year, $mon, $mday, $hour, $min) =
48: /^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/) {
1.2 andrew 49:
1.23 andrew 50: $mon--;
51: $epoch = timegm(0,$min,$hour,$mday,$mon,$year);
52: $name = $base;
53: }
1.2 andrew 54:
1.13 andrew 55: #print "Adding $_\n";
1.2 andrew 56:
1.13 andrew 57: $files{$ext}{$name}{$epoch} = {
58: file => $_,
59: dir => $DIR,
60: path => "$DIR/$_",
61: ext => $ext,
1.23 andrew 62: #year => $year,
63: #mon => $mon,
64: #mday => $mday,
65: #hour => $hour,
66: #min => $min,
1.13 andrew 67: name => $name,
68: epoch => $epoch,
69: };
70:
1.23 andrew 71: if (
72: $name =~ m/\A $OBT->{BASENAME} /xms &&
73: ! exists $Possible_Torrents{$name}
74: ) {
1.15 andrew 75: print "Would remove $_\n";
76: push @delete, $files{$ext}{$name}{$epoch};
77: }
1.13 andrew 78: }
79: closedir DIR;
1.2 andrew 80: }
81:
1.25 ! andrew 82: #print Dump \%files;
1.13 andrew 83: foreach my $name (keys %{ $files{torrent} }) {
1.12 andrew 84: next unless $name =~ /^$Name_Filter/;
1.22 andrew 85: #print "Checking $name\n";
1.4 andrew 86:
1.13 andrew 87: foreach my $epoch ( sort { $b <=> $a } keys %{ $files{torrent}{$name} } ) {
1.4 andrew 88: #print "\t$epoch\n";
1.13 andrew 89: my $torrent = $files{torrent}{$name}{$epoch}{path};
1.4 andrew 90:
1.14 andrew 91: if (
92: keys %{ $files{torrent}{$name} } == 1 &&
93: $files{torrent}{$name}{$epoch}{dir}
94: eq $OBT->{DIR_TORRENT}
95: ) {
1.25 ! andrew 96: print "Skipping torrent for $name there is only one.\n";
1.14 andrew 97: next;
98: }
99:
1.4 andrew 100: my $t;
1.18 andrew 101: eval {
102: $t = BT::MetaInfo::Cached->new(
103: $torrent,
104: {
1.22 andrew 105: cache_root => '/tmp/OBTFileCache'
106: #$OBT->{DIR_HOME} . '/FileCache'
1.18 andrew 107: }
108: );
109: };
1.13 andrew 110:
1.4 andrew 111: if ($@) {
112: warn "Error reading torrent $torrent\n";
1.24 andrew 113: push @delete, $files{torrent}{$name}{$epoch};
114: delete $files{torrent}{$name}{$epoch};
1.4 andrew 115: next;
116: }
117:
1.13 andrew 118: $files{torrent}{$name}{$epoch}{comment} = $t->{comment};
1.24 andrew 119: my ($path) = $t->{comment} =~ /($OBT->{BASENAME}\/[^\n]+)\n/s;
1.4 andrew 120:
1.24 andrew 121: unless (-e $OBT->{DIR_FTP} . "/$path") {
122: print "Deleting $files{torrent}{$name}{$epoch}{file} the path ($path) doesn't exist.\n";
1.13 andrew 123: push @delete, $files{torrent}{$name}{$epoch};
124: delete $files{torrent}{$name}{$epoch};
1.4 andrew 125: next;
126: }
127:
1.17 andrew 128: my $hash = $t->info_hash;
1.4 andrew 129: $hash = unpack("H*", $hash);
130:
1.16 andrew 131: undef $t;
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}) {
1.25 ! andrew 138: if ( $keep{$name}{$hash}{epoch} == $epoch ) {
! 139: next;
! 140: }
! 141: print "Removing [$name] [$hash]\n\t",
! 142: $keep{$name}{$hash}{path},
! 143: "\n";
1.2 andrew 144: push @delete, $keep{$name}{$hash};
1.13 andrew 145: delete $files{torrent}{
146: $keep{$name}{$hash}{name}
147: }{
148: $keep{$name}{$hash}{epoch}
149: };
1.2 andrew 150: $keep{$name}{$hash} =
1.13 andrew 151: $files{torrent}{$name}{$epoch};
1.25 ! andrew 152: print "Keeping additional instance of [$name] [$hash]\n\t",
! 153: $keep{$name}{$hash}{path},
! 154: "\n";
1.2 andrew 155: } else {
1.25 ! andrew 156: print "Removing old [$name] [$hash]\n\t",
! 157: $keep{$name}{$hash}{path},
! 158: "\n";
1.13 andrew 159: push @delete, $files{torrent}{$name}{$epoch};
160: delete $files{torrent}{$name}{$epoch};
1.2 andrew 161: }
162: } else {
1.25 ! andrew 163: print "Keeping first instance of $name [$hash]\n\t",
! 164: $files{torrent}{$name}{$epoch}{path},
! 165: "\n";
1.2 andrew 166: $keep{$name}{$hash} =
1.13 andrew 167: $files{torrent}{$name}{$epoch};
1.2 andrew 168:
169: }
170: }
171: }
172:
173: #print Dump \%files, \%keep, \@delete;
1.23 andrew 174: #exit;
1.2 andrew 175:
176: foreach (@delete) {
1.13 andrew 177: print "Deleting '$_->{path}'\n";
178: unlink $_->{path} or die "Couldn't unlink $_->{path}";
179: }
180:
181: foreach my $name (keys %{ $files{$OBT->{META_EXT} } }) {
182: foreach my $epoch (keys %{ $files{ $OBT->{META_EXT} }{$name} }) {
183: unless ( exists $files{torrent}{$name}{$epoch} ) {
184: my $path = $files{$OBT->{META_EXT}}{$name}{$epoch}{path};
185: print "Unlinking '$path'\n";
186: unlink $path or die "couldn't unlink '$path': $!";
187: }
188: }
189: }
190:
191:
1.20 andrew 192: #print Dump \%keep;
193: foreach my $name (keys %keep) {
194: foreach my $hash (keys %{ $keep{$name} }) {
195: my $file = $keep{$name}{$hash}{file};
196: my $dir = $keep{$name}{$hash}{dir };
197: if ($dir eq $OBT->{DIR_NEW_TORRENT}) {
198: print "Moving $file to current torrents\n";
199: rename("$dir/$file", $OBT->{DIR_TORRENT} . "/" . $file)
200: or die "Couldn't rename '$file': $!";
201:
202: my $name = $keep{$name}{$hash}{name};
203: my $epoch = $keep{$name}{$hash}{epoch};
204:
205: if (exists $files{txt}{$name}{$epoch}) {
206: my $m_file = $files{txt}{$name}{$epoch}{file};
207: my $m_dir = $files{txt}{$name}{$epoch}{dir };
208: rename(
209: "$m_dir/$m_file",
210: $OBT->{DIR_TORRENT} . "/" . $m_file
211: ) or die "Couldn't rename '$m_file': $!";
212: }
213: }
214: }
215: }
1.15 andrew 216:
217: sub Process_Dir
218: {
219: my $basedir = shift;
220:
221: my ($dirs, $files) = Get_Files_and_Dirs($basedir);
222: if (@$files) {
223: my $dir = $basedir;
224: $dir =~ s/^$OBT->{DIR_FTP}\///;
225: my $torrent = Name_Torrent($dir);
226: $torrent =~ s/-.*$//;
227: $Possible_Torrents{$torrent} = 1;
1.24 andrew 228: foreach my $file (@$files) {
229: if ($file =~ /$INSTALL_ISO_REGEX/) {
230: $torrent = Name_Torrent("$dir/$file");
231: $torrent =~ s/-.*$//;
232: $Possible_Torrents{$torrent} = 1;
233: }
234: }
1.15 andrew 235: }
236:
237: foreach my $subdir (@$dirs) {
238: next if $subdir eq '.';
239: next if $subdir eq '..';
240: Process_Dir("$basedir/$subdir")
241: }
242: }
243:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>