1 package CGI::Session::Driver::file;
3 # $Id: file.pm 351 2006-11-24 14:16:50Z markstos $
9 use Fcntl qw( :DEFAULT :flock :mode );
10 use CGI::Session::Driver;
11 use vars qw( $FileName $NoFlock $UMask $NO_FOLLOW );
14 # keep historical behavior
18 *FileName = \$CGI::Session::File::FileName;
21 @CGI::Session::Driver::file::ISA = ( "CGI::Session::Driver" );
22 $CGI::Session::Driver::file::VERSION = "4.20";
23 $FileName = "cgisess_%s";
26 $NO_FOLLOW = eval { O_NOFOLLOW } || 0;
30 $self->{Directory} ||= File::Spec->tmpdir();
32 unless ( -d $self->{Directory} ) {
34 unless ( File::Path::mkpath($self->{Directory}) ) {
35 return $self->set_error( "init(): couldn't create directory path: $!" );
39 $self->{NoFlock} = $NoFlock unless exists $self->{NoFlock};
40 $self->{UMask} = $UMask unless exists $self->{UMask};
47 return File::Spec->catfile($self->{Directory}, sprintf( $FileName, $sid ));
54 my $path = $self->_file($sid);
56 return 0 unless -e $path;
58 # make certain our filehandle goes away when we fall out of scope
63 return $self->set_error("retrieve(): '$path' appears to be a symlink and I couldn't remove it: $!");
64 return 0; # we deleted this so we have no hope of getting back anything
66 sysopen(FH, $path, O_RDONLY | $NO_FOLLOW ) || return $self->set_error( "retrieve(): couldn't open '$path': $!" );
68 $self->{NoFlock} || flock(FH, LOCK_SH) or return $self->set_error( "retrieve(): couldn't lock '$path': $!" );
82 my ($sid, $datastr) = @_;
84 my $path = $self->_file($sid);
86 # make certain our filehandle goes away when we fall out of scope
89 my $mode = O_WRONLY|$NO_FOLLOW;
91 # kill symlinks when we spot them
94 return $self->set_error("store(): '$path' appears to be a symlink and I couldn't remove it: $!");
97 $mode = O_RDWR|O_CREAT|O_EXCL unless -e $path;
99 sysopen(FH, $path, $mode, $self->{UMask}) or return $self->set_error( "store(): couldn't open '$path': $!" );
101 # sanity check to make certain we're still ok
103 return $self->set_error("store(): '$path' is a symlink, check for malicious processes");
106 # prevent race condition (RT#17949)
107 $self->{NoFlock} || flock(FH, LOCK_EX) or return $self->set_error( "store(): couldn't lock '$path': $!" );
108 truncate(FH, 0) or return $self->set_error( "store(): couldn't truncate '$path': $!" );
111 close(FH) or return $self->set_error( "store(): couldn't close '$path': $!" );
120 my $directory = $self->{Directory};
121 my $file = sprintf( $FileName, $sid );
122 my $path = File::Spec->catfile($directory, $file);
123 unlink($path) or return $self->set_error( "remove(): couldn't unlink '$path': $!" );
132 unless ( $coderef && ref($coderef) && (ref $coderef eq 'CODE') ) {
133 croak "traverse(): usage error";
136 opendir( DIRHANDLE, $self->{Directory} )
137 or return $self->set_error( "traverse(): couldn't open $self->{Directory}, " . $! );
139 my $filename_pattern = $FileName;
140 $filename_pattern =~ s/\./\\./g;
141 $filename_pattern =~ s/\%s/(\.\+)/g;
142 while ( my $filename = readdir(DIRHANDLE) ) {
143 next if $filename =~ m/^\.\.?$/;
144 my $full_path = File::Spec->catfile($self->{Directory}, $filename);
145 my $mode = (stat($full_path))[2]
146 or return $self->set_error( "traverse(): stat failed for $full_path: " . $! );
147 next if S_ISDIR($mode);
148 if ( $filename =~ /^$filename_pattern$/ ) {
152 closedir( DIRHANDLE );
169 CGI::Session::Driver::file - Default CGI::Session driver
173 $s = new CGI::Session();
174 $s = new CGI::Session("driver:file", $sid);
175 $s = new CGI::Session("driver:file", $sid, {Directory=>'/tmp'});
180 When CGI::Session object is created without explicitly setting I<driver>, I<file> will be assumed.
181 I<file> - driver will store session data in plain files, where each session will be stored in a separate
184 Naming conventions of session files are defined by C<$CGI::Session::Driver::file::FileName> global variable.
185 Default value of this variable is I<cgisess_%s>, where %s will be replaced with respective session ID. Should
186 you wish to set your own FileName template, do so before requesting for session object:
188 $CGI::Session::Driver::file::FileName = "%s.dat";
189 $s = new CGI::Session();
191 For backwards compatibility with 3.x, you can also use the variable name
192 C<$CGI::Session::File::FileName>, which will override the one above.
194 =head2 DRIVER ARGUMENTS
196 If you wish to specify a session directory, use the B<Directory> option, which denotes location of the directory
197 where session ids are to be kept. If B<Directory> is not set, defaults to whatever File::Spec->tmpdir() returns.
198 So all the three lines in the SYNOPSIS section of this manual produce the same result on a UNIX machine.
200 If specified B<Directory> does not exist, all necessary directory hierarchy will be created.
202 By default, sessions are created with a umask of 0660. If you wish to change the umask for a session, pass
203 a B<UMask> option with an octal representation of the umask you would like for said session.
207 If your OS doesn't support flock, you should understand the risks of going without locking the session files. Since
208 sessions tend to be used in environments where race conditions may occur due to concurrent access of files by
209 different processes, locking tends to be seen as a good and very necessary thing. If you still want to use this
210 driver but don't want flock, set C<$CGI::Session::Driver::file::NoFlock> to 1 or pass C<< NoFlock => 1 >> and this
211 driver will operate without locks.
215 For support and licensing see L<CGI::Session|CGI::Session>