Hello
I have an issue with Solaris 8 and flock. What I'm trying to do is copy files from an
ftp dropbox to another directory, but skip open files (files that are currently being
ftp'd into the dropbox). Seems that the line :
if ( ! (flock(LOCK, LOCK_EX | LOCK_NB))) {
close LOCK;
might be the problem, this code works great on BSD and Linux boxen but seems to have
problems on a Solaris box. The log file says that all the files "seem to be locked".
Any help would be greatly appreciated.
============================================================
#!/usr/bin/perl
use Fcntl ':flock'; # import LOCK_* constants
#
# Mover.pl
#
# This program will pull together a listing of files in $somedir
# For each file in $somedir that is not "open", it will then move it to $targetdir
# It will leave alone any open files in $somedir
#
$somedir = "/Users/ftp";
$targetdir = "/Users/ftp2";
$logfile = "/Users/log/mover.log";
open (LOG, ">>$logfile") || die "cant open $logfile: $!";
# date stuff
my ($seconds,$minutes,$hour, $monthday,$month,$year,
$weekday,$yearday,$dst_flag)=localtime(time);
my $monthspell=(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[(localtime(time))[4]];
my $yearright = $year + 1900;
print LOG "$monthspell $monthday, $yearright $hour:$minutes:$seconds- Start
ftpit.pl\n";
# Open the directory you want a listing from.
opendir(DIR, $somedir) || die "cant opendir $somedir: $!";
# Pull the list of files (and directories) into an array, excluding . and ..
@filez = grep ! /^\.\.?$/, readdir(DIR);
$count = @filez;
print LOG "$monthspell $monthday, $yearright $hour:$minutes:$seconds - read listing of
$count files from $somedir\n";
close DIR;
# Act on each file in the array
foreach (@filez) {
open (LOCK, "$somedir/$_") or die "Could not open file $_ $!";
if (!(flock(LOCK, LOCK_EX | LOCK_NB))) {
close LOCK;
print LOG "$monthspell $monthday, $yearright $hour:$minutes:$seconds - $_ file
seems to be locked\n"}
else {
close LOCK;
print LOG "$monthspell $monthday, $yearright $hour:$minutes:$seconds - $_ not
locked, try to mv from $somedir to $targetdir\n";
`mv $somedir/$_
$targetdir/$yearright-$monthspell-$monthday-$hour-$minutes-$seconds-$_`;
#end else
}
#end foreach
}
__
Mike Lewis