Annotation of openbsd/OpenBSDTorrents/CurrentTorrents.pl, Revision 1.24
1.1 andrew 1: #!/usr/bin/perl -T
1.24 ! andrew 2: #$RedRiver: CurrentTorrents.pl,v 1.23 2007/10/01 20:17:23 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.13 andrew 82: foreach my $name (keys %{ $files{torrent} }) {
1.12 andrew 83: next unless $name =~ /^$Name_Filter/;
1.22 andrew 84: #print "Checking $name\n";
1.4 andrew 85:
1.13 andrew 86: foreach my $epoch ( sort { $b <=> $a } keys %{ $files{torrent}{$name} } ) {
1.4 andrew 87: #print "\t$epoch\n";
1.13 andrew 88: my $torrent = $files{torrent}{$name}{$epoch}{path};
1.4 andrew 89:
1.14 andrew 90: if (
91: keys %{ $files{torrent}{$name} } == 1 &&
92: $files{torrent}{$name}{$epoch}{dir}
93: eq $OBT->{DIR_TORRENT}
94: ) {
95: #print "Skipping torrent for $name there is only one.\n";
96: next;
97: }
98:
1.4 andrew 99: my $t;
1.18 andrew 100: eval {
101: $t = BT::MetaInfo::Cached->new(
102: $torrent,
103: {
1.22 andrew 104: cache_root => '/tmp/OBTFileCache'
105: #$OBT->{DIR_HOME} . '/FileCache'
1.18 andrew 106: }
107: );
108: };
1.13 andrew 109:
1.4 andrew 110: if ($@) {
111: warn "Error reading torrent $torrent\n";
1.24 ! andrew 112: push @delete, $files{torrent}{$name}{$epoch};
! 113: delete $files{torrent}{$name}{$epoch};
1.4 andrew 114: next;
115: }
116:
1.13 andrew 117: $files{torrent}{$name}{$epoch}{comment} = $t->{comment};
1.24 ! andrew 118: my ($path) = $t->{comment} =~ /($OBT->{BASENAME}\/[^\n]+)\n/s;
1.4 andrew 119:
1.24 ! andrew 120: unless (-e $OBT->{DIR_FTP} . "/$path") {
! 121: print "Deleting $files{torrent}{$name}{$epoch}{file} the path ($path) doesn't exist.\n";
1.13 andrew 122: push @delete, $files{torrent}{$name}{$epoch};
123: delete $files{torrent}{$name}{$epoch};
1.4 andrew 124: next;
125: }
126:
1.17 andrew 127: my $hash = $t->info_hash;
1.4 andrew 128: $hash = unpack("H*", $hash);
129:
1.16 andrew 130: undef $t;
131:
1.13 andrew 132: $files{torrent}{$name}{$epoch}{info_hash} = $hash;
1.4 andrew 133:
134:
1.2 andrew 135: if (exists $keep{$name}) {
136: if (exists $keep{$name}{$hash}) {
137: push @delete, $keep{$name}{$hash};
1.13 andrew 138: delete $files{torrent}{
139: $keep{$name}{$hash}{name}
140: }{
141: $keep{$name}{$hash}{epoch}
142: };
1.2 andrew 143: $keep{$name}{$hash} =
1.13 andrew 144: $files{torrent}{$name}{$epoch};
1.2 andrew 145: } else {
1.13 andrew 146: push @delete, $files{torrent}{$name}{$epoch};
147: delete $files{torrent}{$name}{$epoch};
1.2 andrew 148: }
149: } else {
150: $keep{$name}{$hash} =
1.13 andrew 151: $files{torrent}{$name}{$epoch};
1.2 andrew 152:
153: }
154: }
155: }
156:
157: #print Dump \%files, \%keep, \@delete;
1.23 andrew 158: #exit;
1.2 andrew 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:
1.20 andrew 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: }
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;
1.24 ! andrew 212: foreach my $file (@$files) {
! 213: if ($file =~ /$INSTALL_ISO_REGEX/) {
! 214: $torrent = Name_Torrent("$dir/$file");
! 215: $torrent =~ s/-.*$//;
! 216: $Possible_Torrents{$torrent} = 1;
! 217: }
! 218: }
1.15 andrew 219: }
220:
221: foreach my $subdir (@$dirs) {
222: next if $subdir eq '.';
223: next if $subdir eq '..';
224: Process_Dir("$basedir/$subdir")
225: }
226: }
227:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>