]> Dogcows Code - chaz/p5-File-KDBX/blobdiff - lib/File/KDBX/Key/File.pm
Version 0.901
[chaz/p5-File-KDBX] / lib / File / KDBX / Key / File.pm
index 6e4173e987bd883261685b9d8b3639733537f51d..2223870ab7bf8fe14b105cfdfe370d087dcfcfe6 100644 (file)
@@ -17,7 +17,7 @@ use namespace::clean;
 
 extends 'File::KDBX::Key';
 
-our $VERSION = '0.900'; # VERSION
+our $VERSION = '0.901'; # VERSION
 
 
 has 'type',     is => 'ro';
@@ -99,18 +99,24 @@ sub save {
     my $version     = $args{version} // $self->version // 2;
     my $filepath    = $args{filepath} // $self->filepath;
     my $fh          = $args{fh};
+    my $atomic      = $args{atomic} // 1;
 
     my $filepath_temp;
     if (!openhandle($fh)) {
         $filepath or throw 'Must specify where to safe the key file to';
 
-        require File::Temp;
-        ($fh, $filepath_temp) = eval { File::Temp::tempfile("${filepath}-XXXXXX", CLEANUP => 1) };
-        if (!$fh or my $err = $@) {
-            $err //= 'Unknown error';
-            throw sprintf('Open file failed (%s): %s', $filepath_temp, $err),
-                error       => $err,
-                filepath    => $filepath_temp;
+        if ($atomic) {
+            require File::Temp;
+            ($fh, $filepath_temp) = eval { File::Temp::tempfile("${filepath}-XXXXXX", UNLINK => 1) };
+            if (!$fh or my $err = $@) {
+                $err //= 'Unknown error';
+                throw sprintf('Open file failed (%s): %s', $filepath_temp, $err),
+                    error       => $err,
+                    filepath    => $filepath_temp;
+            }
+        }
+        else {
+            open($fh, '>:raw', $filepath) or throw "Open file failed ($filepath): $!", filepath => $filepath;
         }
     }
 
@@ -256,7 +262,7 @@ File::KDBX::Key::File - A file key
 
 =head1 VERSION
 
-version 0.900
+version 0.901
 
 =head1 SYNOPSIS
 
@@ -377,6 +383,10 @@ C<fh> - IO handle to write to (overrides C<filepath>, one of which must be defin
 
 C<raw_key> - Raw key (default: value of L</raw_key>)
 
+=item *
+
+C<atomic> - Write to the filepath atomically (default: true)
+
 =back
 
 =head1 BUGS
This page took 0.023218 seconds and 4 git commands to generate.