Annotation of openbsd/OpenBSDTorrents/NewTorrents.pl, Revision 1.12
1.1 andrew 1: #!/usr/bin/perl -T
1.12 ! andrew 2: #$RedRiver: NewTorrents.pl,v 1.11 2007/02/07 23:09:05 andrew Exp $
1.1 andrew 3: use strict;
4: use warnings;
5: use diagnostics;
6:
7: use lib 'lib';
8: use OpenBSDTorrents;
9:
1.7 andrew 10: use POSIX qw / setsid :sys_wait_h /;
11: $SIG{CHLD} = \&REAPER;
12: my %Kids;
13: my %Kid_Status;
14: my %Need_Update;
1.3 andrew 15:
1.1 andrew 16: %ENV = ();
17:
18: my $last_dir = '';
19: while (<>) {
1.11 andrew 20: print;
1.1 andrew 21: chomp;
1.11 andrew 22: if (my ($message, $file, $xfer, $size) =
23: m#(.*)\s+\`([^']+)'\s+(\d+)\s+(\d+)#) {
1.10 andrew 24: next if $message eq 'Making directory';
1.11 andrew 25: next unless $xfer;
1.10 andrew 26:
27: my $dir = '';
28: if ($file =~ m#^(.*)/([^/]+)#) {
29: ($dir, $file) = ($1, $2);
30: }
31: #print "$message - $dir - $file\n";
1.1 andrew 32: if ($last_dir && $last_dir ne $dir) {
1.2 andrew 33: StartTorrent($last_dir);
1.1 andrew 34: }
35: $last_dir = $dir;
36: }
1.2 andrew 37: }
1.7 andrew 38:
39: # Regen just the new ones now
40: sleep(1) while (keys %Kids > 0);
1.2 andrew 41: StartTorrent($last_dir);
42:
1.7 andrew 43: # after the new ones are done, regen all, just to make sure
44: sleep(1) while (keys %Kids > 0);
1.3 andrew 45: StartTorrent('skip');
46:
1.7 andrew 47: sub REAPER {
48: my $child;
49: while (($child = waitpid(-1,WNOHANG)) > 0) {
50: $Kid_Status{$child} = $?;
51: delete $Kids{$child};
52: }
53: $SIG{CHLD} = \&REAPER; # still loathe sysV
54: }
1.3 andrew 55:
1.2 andrew 56: sub StartTorrent
57: {
58: my $dir = shift;
1.3 andrew 59: return undef unless $dir;
1.12 ! andrew 60: $dir =~ s/^.*$OBT->{BASENAME}\///;
1.2 andrew 61:
1.7 andrew 62: my $should_fork = 1;
63:
64: if ($dir eq 'skip') {
1.8 andrew 65: #$dir = '';
1.7 andrew 66: %Need_Update = ();
67: $should_fork = 0;
68: } else {
1.6 andrew 69: $dir = $OBT->{BASENAME} . "/$dir";
1.7 andrew 70: $Need_Update{$dir} = 1;
1.3 andrew 71: }
72:
1.7 andrew 73: if (keys %Kids > 0) {
74: print "Not making torrents for $dir now, already running\n";
75: return undef;
76: }
1.3 andrew 77:
1.7 andrew 78: my @now_update = keys %Need_Update;
79: %Need_Update = ();
1.3 andrew 80:
1.7 andrew 81: if ($should_fork) {
82: defined(my $pid = fork) or die "Can't fork: $!";
1.3 andrew 83:
1.7 andrew 84: if ($pid) {
85: $Kids{$pid} = 1;
86: return undef;
87: }
1.3 andrew 88:
1.7 andrew 89: }
90:
91: if (@now_update) {
92: print "Making torrents for ", join(" ", @now_update), "\n";
93: } else {
94: print "Remaking all torrents\n";
1.8 andrew 95: push @now_update, $dir;
1.7 andrew 96: }
97: exec($OBT->{DIR_HOME} . '/regen.sh', @now_update);
1.1 andrew 98: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>