Mozilla/mozilla/webtools/bonsai/processMail.pl
reed%reedloden.com db8da73ee3 Follow-up to bug 52573 -- make chdir() die if it fails so it is obvious if it has a problem. [r=cls]
git-svn-id: svn://10.0.0.236/trunk@252100 18797224-902f-48f8-a5cc-f745e15eee43
2008-06-04 02:41:53 +00:00

95 lines
2.4 KiB
Perl

#!/usr/bin/perl -w
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Netscape Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bonsai CVS tool.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s):
use strict;
my $bonsaidir = "@BONSAI_DIR@";
if (($#ARGV > 0) && (-d $ARGV[0])) {
$bonsaidir = $ARGV[0];
}
chdir $bonsaidir or die "Couldn't chdir to $bonsaidir";
require 'globals.pl';
use FileHandle;
use Fcntl qw(:DEFAULT :flock);
my $datadir;
my $lockfile;
my $debug = 0;
my $err = 0;
# borrowed from Tinderbox to use in processMail.pl
sub lock_datafile {
my ($file) = @_;
my $lock_fh = new FileHandle ">>$file"
or die "Couldn't open semaphore file, $file: $!";
# Get an exclusive lock with a non-blocking request
unless (flock($lock_fh, LOCK_EX|LOCK_NB)) {
die "Lock unavailable: $!";
}
return $lock_fh;
}
sub unlock_datafile {
my ($lock_fh) = @_;
flock $lock_fh, LOCK_UN; # Free the lock
close $lock_fh;
}
$datadir = $bonsaidir . "/data";
$lockfile = "$datadir/processMail.sem";
print "bonsaidir: $bonsaidir\n" if ($debug);
print "datadir: $datadir\n" if ($debug);
print "lockfile: $lockfile\n" if ($debug);
# Acquire a lock first so that we don't step on ourselves
my $lock = lock_datafile($lockfile);
opendir(DIR, shell_escape($datadir)) or $err++;
if ($err) {
unlock_datafile($lock);
unlink($lockfile);
die("Can't opendir($datadir): $!");
}
my @datafiles = sort(grep { /^bonsai\.\d+\.\d+$/ && -f "$datadir/$_" } readdir(DIR));
closedir(DIR);
print "Files: @datafiles\n" if ($debug);
for my $file (@datafiles) {
print "processing $file\n" if ($debug);
system("/usr/bin/perl", "-w", "addcheckin.pl", "$datadir/$file");
unlink("$datadir/$file");
}
unlock_datafile($lock);
unlink($lockfile);