Annotation of openbsd/OpenBSDTorrents/CurrentTorrents.pl, Revision 1.17
1.1 andrew 1: #!/usr/bin/perl -T
1.17 ! andrew 2: #$Id: CurrentTorrents.pl,v 1.16 2005/05/05 01:09:43 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.17 ! andrew 12: use BT::MetaInfo::Cached;
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.4 andrew 91: my $t;
1.17 ! andrew 92: eval { $t = BT::MetaInfo::Cached->new( $torrent ); };
1.13 andrew 93:
1.4 andrew 94: if ($@) {
95: warn "Error reading torrent $torrent\n";
96: next;
97: }
98:
1.13 andrew 99: $files{torrent}{$name}{$epoch}{comment} = $t->{comment};
1.4 andrew 100: my ($path) = $t->{comment} =~ /Files from ([^\n]+)\n/s;
101:
1.10 andrew 102: unless (-d $OBT->{DIR_FTP} . "/$path") {
1.13 andrew 103: #print "Deleting $files{torrent}{$name}{$epoch}{file} the path doesn't exist.\n";
104: push @delete, $files{torrent}{$name}{$epoch};
105: delete $files{torrent}{$name}{$epoch};
1.4 andrew 106: next;
107: }
108:
1.17 ! andrew 109: my $hash = $t->info_hash;
1.4 andrew 110: $hash = unpack("H*", $hash);
111:
1.16 andrew 112: undef $t;
113:
1.13 andrew 114: $files{torrent}{$name}{$epoch}{info_hash} = $hash;
1.4 andrew 115:
116:
1.2 andrew 117: if (exists $keep{$name}) {
118: if (exists $keep{$name}{$hash}) {
119: push @delete, $keep{$name}{$hash};
1.13 andrew 120: delete $files{torrent}{
121: $keep{$name}{$hash}{name}
122: }{
123: $keep{$name}{$hash}{epoch}
124: };
1.2 andrew 125: $keep{$name}{$hash} =
1.13 andrew 126: $files{torrent}{$name}{$epoch};
1.2 andrew 127: } else {
1.13 andrew 128: push @delete, $files{torrent}{$name}{$epoch};
129: delete $files{torrent}{$name}{$epoch};
1.2 andrew 130: }
131: } else {
132: $keep{$name}{$hash} =
1.13 andrew 133: $files{torrent}{$name}{$epoch};
1.2 andrew 134:
135: }
136: }
137: }
138:
139: #print Dump \%files, \%keep, \@delete;
140:
141: foreach (@delete) {
1.13 andrew 142: print "Deleting '$_->{path}'\n";
143: unlink $_->{path} or die "Couldn't unlink $_->{path}";
144: }
145:
146: foreach my $name (keys %{ $files{$OBT->{META_EXT} } }) {
147: foreach my $epoch (keys %{ $files{ $OBT->{META_EXT} }{$name} }) {
148: unless ( exists $files{torrent}{$name}{$epoch} ) {
149: my $path = $files{$OBT->{META_EXT}}{$name}{$epoch}{path};
150: print "Unlinking '$path'\n";
151: unlink $path or die "couldn't unlink '$path': $!";
152: }
153: }
154: }
155:
156:
157: #print Dump \%keep;
158: foreach my $name (keys %keep) {
159: foreach my $hash (keys %{ $keep{$name} }) {
160: my $file = $keep{$name}{$hash}{file};
161: my $dir = $keep{$name}{$hash}{dir };
162: if ($dir eq $OBT->{DIR_NEW_TORRENT}) {
163: print "Moving $file to current torrents\n";
164: rename("$dir/$file", $OBT->{DIR_TORRENT} . "/" . $file)
165: or die "Couldn't rename '$file': $!";
166:
167: my $name = $keep{$name}{$hash}{name};
168: my $epoch = $keep{$name}{$hash}{epoch};
169:
170: if (exists $files{txt}{$name}{$epoch}) {
171: my $m_file = $files{txt}{$name}{$epoch}{file};
172: my $m_dir = $files{txt}{$name}{$epoch}{dir };
173: rename(
174: "$m_dir/$m_file",
175: $OBT->{DIR_TORRENT} . "/" . $m_file
176: ) or die "Couldn't rename '$m_file': $!";
177: }
178: }
179: }
1.2 andrew 180: }
1.15 andrew 181:
182: sub Process_Dir
183: {
184: my $basedir = shift;
185:
186: my ($dirs, $files) = Get_Files_and_Dirs($basedir);
187: if (@$files) {
188: my $dir = $basedir;
189: $dir =~ s/^$OBT->{DIR_FTP}\///;
190: my $torrent = Name_Torrent($dir);
191: $torrent =~ s/-.*$//;
192: $Possible_Torrents{$torrent} = 1;
193: }
194:
195: foreach my $subdir (@$dirs) {
196: next if $subdir eq '.';
197: next if $subdir eq '..';
198: Process_Dir("$basedir/$subdir")
199: }
200: }
201:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>