make projects.list (and some other files) writes atomic

The KDE folks have a projects.list file that gets picked up by rsync and
sent elsewhere in some cron-ish manner.  They noticed that occasionally
the file that went across was truncated, which indicates that maybe it
got picked up while it was still being written.

oh and this change also affects any files that get written using
wrap_print()
This commit is contained in:
Sitaram Chamarty 2011-05-24 20:24:42 +05:30
parent 3d9b4fae9f
commit 936f8719ce
2 changed files with 4 additions and 2 deletions

View file

@ -101,9 +101,10 @@ sub wrap_open {
sub wrap_print { sub wrap_print {
my ($file, @text) = @_; my ($file, @text) = @_;
my $fh = wrap_open(">", $file); my $fh = wrap_open(">", "$file.$$");
print $fh @text; print $fh @text;
close($fh) or die "$ABRT close $file failed: $! at ", (caller)[1], " line ", (caller)[2], "\n"; close($fh) or die "$ABRT close $file failed: $! at ", (caller)[1], " line ", (caller)[2], "\n";
rename "$file.$$", $file;
} }
sub slurp { sub slurp {

View file

@ -560,11 +560,12 @@ for my $repo (@phy_repos) {
# write out the project list, but not if GL_NO_DAEMON_NO_GITWEB is set # write out the project list, but not if GL_NO_DAEMON_NO_GITWEB is set
unless ($GL_NO_DAEMON_NO_GITWEB) { unless ($GL_NO_DAEMON_NO_GITWEB) {
my $projlist_fh = wrap_open( ">", $PROJECTS_LIST); my $projlist_fh = wrap_open( ">", "$PROJECTS_LIST.$$");
for my $proj (sort keys %projlist) { for my $proj (sort keys %projlist) {
print $projlist_fh "$proj\n"; print $projlist_fh "$proj\n";
} }
close $projlist_fh; close $projlist_fh;
rename "$PROJECTS_LIST.$$", $PROJECTS_LIST;
} }
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------