]> Dogcows Code - chaz/p5-File-KDBX/blobdiff - lib/File/KDBX/Key/File.pm
Don't open already-open files on Windows
[chaz/p5-File-KDBX] / lib / File / KDBX / Key / File.pm
index 335b31e367fdf91bff45a4a13a69701577fd9826..0b6093f985771182f1d9f048f695223c39e27f99 100644 (file)
@@ -140,6 +140,7 @@ Write a key file. Available options:
 * C<filepath> - Where to save the file (default: value of L</filepath>)
 * C<fh> - IO handle to write to (overrides C<filepath>, one of which must be defined)
 * C<raw_key> - Raw key (default: value of L</raw_key>)
+* C<atomic> - Write to the filepath atomically (default: true)
 
 =cut
 
@@ -156,18 +157,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;
         }
     }
 
This page took 0.021965 seconds and 4 git commands to generate.