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 | |
by tachyon (Chancellor) on Jun 25, 2001 at 15:10 UTC |