=================================================================== RCS file: /cvs/openbsd/OpenBSDTorrents/lib/OpenBSDTorrents.pm,v retrieving revision 1.2 retrieving revision 1.7 diff -u -r1.2 -r1.7 --- openbsd/OpenBSDTorrents/lib/OpenBSDTorrents.pm 2005/03/22 23:44:43 1.2 +++ openbsd/OpenBSDTorrents/lib/OpenBSDTorrents.pm 2005/03/28 23:04:42 1.7 @@ -1,5 +1,5 @@ package OpenBSDTorrents; -#$Id: OpenBSDTorrents.pm,v 1.2 2005/03/22 23:44:43 andrew Exp $ +#$Id: OpenBSDTorrents.pm,v 1.7 2005/03/28 23:04:42 andrew Exp $ use 5.008005; use strict; use warnings; @@ -12,17 +12,20 @@ our @EXPORT = qw( $BaseDir + $HomeDir $TorrentDir $BaseName $Tracker &Name_Torrent &Get_Files_and_Dirs + &justme ); -our $BaseDir = '/home/ftp/pub'; -our $TorrentDir = '/home/andrew/torrents'; -our $BaseName = 'OpenBSD'; -our $Tracker = 'http://OpenBSD.somedomain.net/announce.php'; +our $BaseDir = '/home/ftp/pub'; +our $HomeDir = '/home/OpenBSDTorrents'; +our $TorrentDir = '/home/torrentsync/torrents'; +our $BaseName = 'OpenBSD'; +our $Tracker = 'http://OpenBSD.somedomain.net/announce.php'; # These are regexes that tell what files to skip; our $SkipDirs = qr/\/patches$/; @@ -47,7 +50,7 @@ { my $basedir = shift; opendir DIR, $basedir or die "Couldn't opendir $basedir: $!"; - my @contents = grep { ! /^\.\.$/ } grep { ! /^\.$/ } readdir DIR; + my @contents = sort grep { ! /^\.\.$/ } grep { ! /^\.$/ } readdir DIR; closedir DIR; my @dirs = grep { -d "$basedir/$_" } @contents; @@ -79,6 +82,40 @@ } return join '-', ($year, $mon, $mday, $hour . $min); } + +# "There can be only one." --the Highlander +sub justme { + + my $myname; + + if ($0 =~ m#([^/]+$)#) { + $myname = $1; + } else { + die "Couldn't figure out myname"; + } + + my $SEMA = "$HomeDir/run/$myname.pid"; + if (open SEMA, "<", $SEMA) { + my $pid = ; + if (defined $pid) { + chomp $pid; + if ($pid =~ /^(\d+)$/) { + $pid = $1; + } else { + die "invalid pid read '$pid'"; + } + if (kill(0, $pid)) { + print "$0 already running (pid $pid), bailing out\n"; + exit 253; + } + } + close SEMA; + } + open (SEMA, ">", $SEMA) or die "can't write $SEMA: $!"; + print SEMA "$$\n"; + close(SEMA) or die "can't close $SEMA: $!"; +} + 1; __END__