/usr/share/perl5/Dancer2/Core/Role/SessionFactory/File.pm is in libdancer2-perl 0.152000+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | package Dancer2::Core::Role::SessionFactory::File;
#ABSTRACT: Role for file-based session factories
$Dancer2::Core::Role::SessionFactory::File::VERSION = '0.152000';
use strict;
use warnings;
use Carp 'croak';
use Dancer2::Core::Types;
use Dancer2::FileUtils qw(path set_file_mode);
use Fcntl ':flock';
use Moo::Role;
with 'Dancer2::Core::Role::SessionFactory';
#--------------------------------------------------------------------------#
# Required by classes consuming this role
#--------------------------------------------------------------------------#
requires '_suffix'; # '.yml', '.json', etc.
requires '_thaw_from_handle'; # given handle, return session 'data' field
requires '_freeze_to_handle'; # given handle and data, serialize it
#--------------------------------------------------------------------------#
# Attributes and methods
#--------------------------------------------------------------------------#
has session_dir => (
is => 'ro',
isa => Str,
default => sub { path( '.', 'sessions' ) },
);
sub BUILD {
my $self = shift;
if ( !-d $self->session_dir ) {
mkdir $self->session_dir
or croak "Unable to create session dir : "
. $self->session_dir . ' : '
. $!;
}
}
sub _sessions {
my ($self) = @_;
my $sessions = [];
opendir( my $dh, $self->session_dir )
or croak "Unable to open directory " . $self->session_dir . " : $!";
my $suffix = $self->_suffix;
while ( my $file = readdir($dh) ) {
next if $file eq '.' || $file eq '..';
if ( $file =~ /(\w+)\Q$suffix\E/ ) {
push @{$sessions}, $1;
}
}
closedir($dh);
return $sessions;
}
sub _retrieve {
my ( $self, $id ) = @_;
my $session_file = path( $self->session_dir, $id . $self->_suffix );
return unless -f $session_file;
open my $fh, '+<', $session_file or die "Can't open '$session_file': $!\n";
flock $fh, LOCK_SH or die "Can't lock file '$session_file': $!\n";
my $data = $self->_thaw_from_handle($fh);
close $fh or die "Can't close '$session_file': $!\n";
return $data;
}
sub _destroy {
my ( $self, $id ) = @_;
my $session_file = path( $self->session_dir, $id . $self->_suffix );
return if !-f $session_file;
unlink $session_file;
}
sub _flush {
my ( $self, $id, $data ) = @_;
my $session_file = path( $self->session_dir, $id . $self->_suffix );
open my $fh, '>', $session_file or die "Can't open '$session_file': $!\n";
flock $fh, LOCK_EX or die "Can't lock file '$session_file': $!\n";
seek $fh, 0, 0 or die "Can't seek in file '$session_file': $!\n";
truncate $fh, 0 or die "Can't truncate file '$session_file': $!\n";
set_file_mode($fh);
$self->_freeze_to_handle( $fh, $data );
close $fh or die "Can't close '$session_file': $!\n";
return $data;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Dancer2::Core::Role::SessionFactory::File - Role for file-based session factories
=head1 VERSION
version 0.152000
=head1 DESCRIPTION
This is a specialized SessionFactory role for storing session
data in files.
This role manages the files. Classes consuming it only need to handle
serialization and deserialization.
Classes consuming this must satisfy three requirements: C<_suffix>,
C<_freeze_to_handle> and C<_thaw_from_handle>.
package Dancer2::SessionFactory::XYX
use Moo;
has _suffix => (
is => 'ro',
isa => 'Str',
default => sub { '.xyz' },
);
with 'Dancer2::Core::Role::SessionFactory::File';
sub _freeze_to_handle {
my ($self, $fh, $data) = @_;
# ... do whatever to get data into $fh
return;
}
sub _thaw_from_handle {
my ($self, $fh) = @_;
my $data;
# ... do whatever to get data from $fh
return $data;
}
1;
=head1 ATTRIBUTES
=head2 session_dir
Where to store the session files. Defaults to "./sessions".
=head1 AUTHOR
Dancer Core Developers
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Alexis Sukrieh.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|