When you need to update a file that will continue to be "live", you have to create a new file and then rename it over the old one so that the data appears to be atomically updated.

However, if you naively use flock, you run the risk of getting exclusive access to the now-dead file, rather than the live file, so you need to verify that you really have the live file exclusively flocked. Here's a template to do that.

The checks for the device/inode number verify that the filehandle FILE is really the same as the file $file.

{   open FILE, $file or die;   my ($dev,$ino) = stat(FILE);   flock FILE, 2 or die;   my ($dev1, $ino1) = stat($file);   redo unless "$dev $ino" eq "$dev1 $ino1"; } open FILE_OUT, ">$file.tmp" or die; ### ... process FILE into FILE_OUT close FILE_OUT; rename "$file.tmp", "$file" or die; close FILE;

Replies are listed 'Best First'.
$$, perhaps?
by gryng (Hermit) on Aug 22, 2000 at 21:35 UTC
    Even though this is a snippet, wouldn't it have been better to have used $$ (the process id) as part of the temporary filename? And/or I think there is a module for this sort of temporary file creation?

    (but still, kudos on the nice code :) )

    Ciao,
    Gryn

      Not needed, because you'll be the only one creating the temp file if you make it that far. And no, there's no module at the moment that does this just like this. Odd that you should say that, because I was thinking of making a small module to do just that. Interface would be something like:
      use File::UpdateExclusively; { my $handle = File::UpdateExclusively->on("/some/file/name") or die; my @data = <$handle>; ## process @data print $handle @data; }
      The return value would be a tied handle. Reading from the handle would read from the beginning of the original file (now flocked). Writing to the handle writes to the new location. Closing the handle or letting it go out of scope does the rename shuffle.

      Now to code that... {grin}

      -- Randal L. Schwartz, Perl hacker

Re: Exclusively updating a file that continues to be repeatedly read
by halley (Prior) on Feb 18, 2004 at 18:05 UTC
    Just a caveat; the device/inode pair is not an assured canonical identity for files on all platforms. The inode concept does not carry to Win32, for example, and will appear as 0 for almost any file on Win32 platforms.

    --
    [ e d @ h a l l e y . c c ]