-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vyacheslav Matyukhin
committed
Jun 14, 2011
1 parent
bf6c61b
commit da29374
Showing
5 changed files
with
54 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package Ubic::AtomicFile; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
# ABSTRACT: atomic file operations | ||
|
||
=head1 SYNOPSIS | ||
use Ubic::AtomicFile; | ||
Ubic::AtomicFile::store("blah\n" => "/var/lib/blah"); | ||
=head1 FUNCTIONS | ||
=over | ||
=item B<store($data, $file)> | ||
Store C<$data> into C<$file> atomically. Temporary C<$file.new> will be created and then renamed to C<$file>. | ||
=cut | ||
sub store($$) { | ||
my ($data, $file) = @_; | ||
|
||
my $new_file = "$file.new"; | ||
|
||
# here is an interesting link explaining why we need to do it this way: | ||
# https://bugs.launchpad.net/ubuntu/+source/linux/+bug/317781/comments/54 | ||
open my $fh, '>', $new_file or die "Can't open '$new_file' for writing: $!"; | ||
print {$fh} $data or die "Can't print to '$new_file': $!"; | ||
$fh->flush or die "Can't flush '$new_file': $!"; | ||
close $fh or die "Can't close '$new_file': $!"; | ||
rename $new_file => $file or die "Can't rename '$new_file' to '$file': $!"; | ||
} | ||
=back | ||
=cut | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters