Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

more abstract way to atomic write

  • Loading branch information...
commit 6ead34234f7b35aa5c29373feb11c66d763ef1de 1 parent edb00c0
@jamhed authored
Showing with 15 additions and 9 deletions.
  1. +13 −0 lib/Dancer/FileUtils.pm
  2. +2 −9 lib/Dancer/Session/YAML.pm
View
13 lib/Dancer/FileUtils.pm
@@ -5,6 +5,9 @@ use warnings;
use File::Basename ();
use File::Spec;
+use File::Temp qw(tempfile);
+use File::Copy;
+
use Carp;
use Cwd 'realpath';
@@ -16,6 +19,7 @@ use vars '@EXPORT_OK';
@EXPORT_OK = qw(
dirname open_file path read_file_content read_glob_content
path_or_empty set_file_mode normalize_path
+ atomic_write
);
# path should not verify paths
@@ -143,6 +147,15 @@ sub _trim_UNC {
return @args;
}
+sub atomic_write {
+ my ($path, $file, $data) = @_;
+ my ($fh, $filename) = tempfile("tmpXXXXXXXXX", DIR => $path);
+ set_file_mode($fh);
+ print $fh $data;
+ close $fh or die "Can't close '$file': $!\n";
+ move($filename, $file) or die "Can't move '$filename' to '$file'";
+}
+
1;
__END__
View
11 lib/Dancer/Session/YAML.pm
@@ -8,12 +8,9 @@ use base 'Dancer::Session::Abstract';
use Dancer::Logger;
use Dancer::ModuleLoader;
use Dancer::Config 'setting';
-use Dancer::FileUtils qw(path set_file_mode);
+use Dancer::FileUtils qw(path set_file_mode atomic_write);
use Dancer::Exception qw(:all);
-use File::Temp qw(tempfile);
-use File::Copy;
-
# static
@@ -93,11 +90,7 @@ sub flush {
my $self = shift;
my $session_file = yaml_file( $self->id );
- my ($fh, $filename) = tempfile( "tmpXXXXXXXXX", DIR => setting('session_dir') );
- set_file_mode($fh);
- print $fh YAML::Dump($self);
- close $fh or die "Can't close '$session_file': $!\n";
- move($filename, $session_file);
+ atomic_write( setting('session_dir'), yaml_file($self->id), YAML::Dump($self) );
return $self;
}
Please sign in to comment.
Something went wrong with that request. Please try again.