in reply to What the flock!? Concurrency issues in file writing.
As the two 'rename' operations are atomic, this might work, and the file inode is preserved, so all should be well. Change the "$file.$$" for other (better) tempfilename if you expect concurrent accesses by the same process or thru the network.use strict; use warnings; sub report($@) { my $file = shift; my $temp = "$file.$$"; 1 until -w $file; rename $file, $temp or die "rename [$file] [$temp]: $!"; open my $t, '+<', $temp or die "open [$temp]: $!"; seek $t, 0, 2 or die "seek [$temp]: $!"; print $t @_; close $t; rename $temp, $file or die "rename [$temp] [$file]: $!" }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: What the flock!? Concurrency issues in file writing.
by bluto (Curate) on Oct 01, 2008 at 17:02 UTC |