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


In reply to sysopen failing to work by tachyon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.