Annotation of openbsd/OpenBSDTorrents/NewTorrents.pl, Revision 1.7
1.1 andrew 1: #!/usr/bin/perl -T
1.7 ! andrew 2: #$Id: NewTorrents.pl,v 1.6 2005/04/06 23:00:31 andrew Exp andrew $
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: use YAML;
19:
1.3 andrew 20: # *** This requires --log-format="%t [%p] %o %f %l" on the rsync command
21:
1.1 andrew 22: my $last_dir = '';
23: while (<>) {
24: chomp;
25: if (my ($year, $mon, $mday, $time, $pid, $oper, $file, $size) =
26: m#^(\d{4})/(\d{2})/(\d{2}) (\d{2}:\d{2}:\d{2}) \[(\d+)\] (\S+) (.+) (\d+)$# ) {
27: #print "($year, $mon, $mday, $time, $pid, $oper, $file, $size)\n";
28: my ($dir, $file) = $file =~ m#^(.*)/([^/]+)#;
29: #print "$dir - $file\n";
30: if ($last_dir && $last_dir ne $dir) {
1.2 andrew 31: StartTorrent($last_dir);
1.1 andrew 32: }
33: $last_dir = $dir;
34: } else {
35: #print $_;
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') {
! 64: $dir = '';
! 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: }
1.3 andrew 89:
1.7 ! andrew 90: chdir $OBT->{DIR_HOME} or die "Can't chdir to $OBT->{DIR_HOME}: $!";
! 91:
! 92: if (@now_update) {
! 93: print "Making torrents for ", join(" ", @now_update), "\n";
! 94: } else {
! 95: print "Remaking all torrents\n";
! 96: }
! 97: exec($OBT->{DIR_HOME} . '/regen.sh', @now_update);
1.1 andrew 98: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>