Checking for open files using fcntl: flock in Solaris 
Author Message
 Checking for open files using fcntl: flock in Solaris

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".

Is this the way to go, or is there an easier way to check for open
files?

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 .


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

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

Quote:
}

#end foreach

Quote:
}

__
Mike


Mon, 25 Jul 2005 11:40:37 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Flock on solaris using fcntl

2. flock and/or fcntl on solaris

3. GDBM, Solaris, and flock/fcntl

4. Using flock() or fcntl() as an arbiter

5. using fcntl for flock under SYS V

6. flock/fcntl with dbm files...

7. : Perl under Solaris 2.1 using GCC 2.4.5 and flock

8. Has anyone used Fcntl to handle file locking?

9. Req for example: file lock using fcntl

10. Solaris flock and bad file number

11. Help with using fcntl file locking

12. use Fcntl ':flock'

 

 
Powered by phpBB® Forum Software