blib/lib/Ubic/AtomicFile.pm untested

filter: Statementstmt 0.0% 0 / 25 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 45 SCAR 17.9
CC
2
Cov
0%
store
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 6.0
1 package Ubic::AtomicFile;
2 $Ubic::AtomicFile::VERSION = '1.59';
3 use strict;
4 use warnings;
5  
6 # ABSTRACT: atomic file operations
7  
8  
9 use IO::Handle;
10 use Params::Validate qw(:all);
11  
12 sub store($$;$) {
13 my ($data, $file, @options) = @_;
14  
15 my $options = validate(@options, {
16 sync => { default => 1 },
17 });
18  
19 my $new_file = "$file.new";
20  
21 open my $fh, '>', $new_file or die "Can't open '$new_file' for writing: $!";
22 print {$fh} $data or die "Can't print to '$new_file': $!";
23 $fh->flush or die "Can't flush '$new_file': $!";
24  
25 if ($options->{sync}) {
26 # Here is a link which says why we should do sync too if we don't want to lose data:
27 # https://bugs.launchpad.net/ubuntu/+source/linux/+bug/317781/comments/54
28 #
29 # For some types of atomic files this is important, for others (pidfiles, temp files) it can be too big performance hit.
30 # Every part of ubic code decides for itself.
31 $fh->sync or die "Can't sync '$new_file': $!";
32 }
33  
34 close $fh or die "Can't close '$new_file': $!";
35 rename $new_file => $file or die "Can't rename '$new_file' to '$file': $!";
36 }
37  
38 1;
39  
40 __END__