tachyon has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that needs to update a flat text file by deleting some records from it. To avoid race conditions I need to generate a lockfile to guarantee that the text file is not changed while this update is being performed.

Here is the code

#!/usr/bin/perl -Tw use strict; use Fcntl (':flock'); .... sub DeleteRecords { no strict 'subs'; # strict does not like O_EXCL and O_CREAT?? my($file, @delete_these) = @_; # avoid race conditions, where subscriber.txt is changed between # reading contents with &GetRecords and writing new subscriber # list with &SetRecords. We create a lockfile only if it does # not already exist ie no other process performing &DeleteRecords my $count = 0; until (sysopen (LOCK, "$path_to_files/lockfile.txt", O_EXCL | O_CR +EAT | O_WRONLY)) { sleep 1; DieNice("Can't create lockfile '$path_to_files/lockfile.txt': +$!\n") if ++$count >= $timeout; } my @records = &GetRecords($file); for my $record (@delete_these) { @records = grep{ $_ ne $record }@records; } &SetRecords($file, @records); close LOCK; unlink "$path_to_files/lockfile.txt" or DieNice("Can't unlink lock +file: $!\n"); return; }

The code times out. The DieNice function reports the error as: Error Message: Can't create lockfile '/vs/mydomain.com/work/lockfile.txt': No such file or directory

Well I know the damn file does not exist (that's what I check for!) but it is not being created either.

So far I have confirmed that it is not a permissions problem as substituting a simple open for the sysopen works fine.

open (LOCK, ">$path_to_files/lockfile.txt")

In the camel book I note that this function is dependent on the open(2) and fdopen(3) C lib and that some MODE flags may not be available. These would seem pretty standard but I am waiting to hear back from my sysadmin :-)

Question: Is this syntax related or system related?

cheers

tachyon

Replies are listed 'Best First'.
Re: sysopen failing to work
by snowcrash (Friar) on Jun 25, 2001 at 09:08 UTC
    i guess you don't have the constants for the mode parameter defined. that's why it fails using strict subs. no strict "subs" wont help either, it just doesn't complain that the functions O_CREATE etc aren't defined. so just
    use Fcntl;
    or
    use Fcntl qw(:DEFAULT :flock);
    to pull in the flags your system supports.

    hope to help, cheers
    snowcrash //////

      Added the :DEFAULT to my flock and all was cured. Thanks.

      Cheers

      Tachyon