Annotation of openbsd/OpenBSDTorrents/NewTorrents.pl, Revision 1.11
1.1 andrew 1: #!/usr/bin/perl -T
1.11 ! andrew 2: #$RedRiver: NewTorrents.pl,v 1.10 2006/07/24 18:03:53 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.2 andrew 60:
1.7 andrew 61: my $should_fork = 1;
62:
63: if ($dir eq 'skip') {
1.8 andrew 64: #$dir = '';
1.7 andrew 65: %Need_Update = ();
66: $should_fork = 0;
67: } else {
1.6 andrew 68: $dir = $OBT->{BASENAME} . "/$dir";
1.7 andrew 69: $Need_Update{$dir} = 1;
1.3 andrew 70: }
71:
1.7 andrew 72: if (keys %Kids > 0) {
73: print "Not making torrents for $dir now, already running\n";
74: return undef;
75: }
1.3 andrew 76:
1.7 andrew 77: my @now_update = keys %Need_Update;
78: %Need_Update = ();
1.3 andrew 79:
1.7 andrew 80: if ($should_fork) {
81: defined(my $pid = fork) or die "Can't fork: $!";
1.3 andrew 82:
1.7 andrew 83: if ($pid) {
84: $Kids{$pid} = 1;
85: return undef;
86: }
1.3 andrew 87:
1.7 andrew 88: }
89:
90: if (@now_update) {
91: print "Making torrents for ", join(" ", @now_update), "\n";
92: } else {
93: print "Remaking all torrents\n";
1.8 andrew 94: push @now_update, $dir;
1.7 andrew 95: }
96: exec($OBT->{DIR_HOME} . '/regen.sh', @now_update);
1.1 andrew 97: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>