1 package DBM::Deep::Storage::File;
6 use warnings FATAL => 'all';
8 use Fcntl qw( :DEFAULT :flock :seek );
10 use constant DEBUG => 0;
14 DBM::Deep::Storage::File
18 This is an internal-use-only object for L<DBM::Deep/>. It mediates the low-level
19 interaction with the storage mechanism.
21 Currently, the only storage mechanism supported is the file system.
25 This class provides an abstraction to the storage mechanism so that the Engine
26 (the only class that uses this class) doesn't have to worry about that.
47 #XXX Migrate this to the engine, where it really belongs.
48 filter_store_key => undef,
49 filter_store_value => undef,
50 filter_fetch_key => undef,
51 filter_fetch_value => undef,
54 # Grab the parameters we want to use
55 foreach my $param ( keys %$self ) {
56 next unless exists $args->{$param};
57 $self->{$param} = $args->{$param};
60 if ( $self->{fh} && !$self->{file_offset} ) {
61 $self->{file_offset} = tell( $self->{fh} );
64 $self->open unless $self->{fh};
71 This method opens the filehandle for the filename in C< file >.
73 There is no return value.
80 # Adding O_BINARY should remove the need for the binmode below. However,
81 # I'm not going to remove it because I don't have the Win32 chops to be
82 # absolutely certain everything will be ok.
83 my $flags = O_CREAT | O_BINARY;
85 if ( !-e $self->{file} || -w _ ) {
93 sysopen( $fh, $self->{file}, $flags )
94 or die "DBM::Deep: Cannot sysopen file '$self->{file}': $!\n";
97 # Even though we use O_BINARY, better be safe than sorry.
100 if ($self->{autoflush}) {
101 my $old = select $fh;
111 If the filehandle is opened, this will close it.
113 There is no return value.
130 This will return the size of the DB. If file_offset is set, this will take that into account.
137 return 0 unless $self->{fh};
138 return( (-s $self->{fh}) - $self->{file_offset} );
143 This will set the inode value of the underlying file object.
145 This is only needed to handle some obscure Win32 bugs. It reqlly shouldn't be
146 needed outside this object.
148 There is no return value.
155 unless ( defined $self->{inode} ) {
156 my @stats = stat($self->{fh});
157 $self->{inode} = $stats[1];
158 $self->{end} = $stats[7];
164 =head2 print_at( $offset, @data )
166 This takes an optional offset and some data to print.
168 C< $offset >, if defined, will be used to seek into the file. If file_offset is
169 set, it will be used as the zero location. If it is undefined, no seeking will
170 occur. Then, C< @data > will be printed to the current location.
172 There is no return value.
182 my $fh = $self->{fh};
183 if ( defined $loc ) {
184 seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
188 my $caller = join ':', (caller)[0,2];
189 my $len = length( join '', @_ );
190 warn "($caller) print_at( " . (defined $loc ? $loc : '<undef>') . ", $len )\n";
193 print( $fh @_ ) or die "Internal Error (print_at($loc)): $!\n";
198 =head2 read_at( $offset, $length )
200 This takes an optional offset and a length.
202 C< $offset >, if defined, will be used to seek into the file. If file_offset is
203 set, it will be used as the zero location. If it is undefined, no seeking will
204 occur. Then, C< $length > bytes will be read from the current location.
206 The data read will be returned.
212 my ($loc, $size) = @_;
216 my $fh = $self->{fh};
217 if ( defined $loc ) {
218 seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
222 my $caller = join ':', (caller)[0,2];
223 warn "($caller) read_at( " . (defined $loc ? $loc : '<undef>') . ", $size )\n";
227 read( $fh, $buffer, $size);
234 When the ::Storage::File object goes out of scope, it will be closed.
247 =head2 request_space( $size )
249 This takes a size and adds that much space to the DBM.
251 This returns the offset for the new location.
259 #XXX Do I need to reset $self->{end} here? I need a testcase
260 my $loc = $self->{end};
261 $self->{end} += $size;
268 This flushes the filehandle. This takes no parameters and returns nothing.
275 # Flush the filehandle
276 my $old_fh = select $self->{fh};
277 my $old_af = $|; $| = 1; $| = $old_af;
285 This takes no parameters. It returns a boolean saying if this filehandle is
288 Taken from L<http://www.perlmonks.org/?node_id=691054/>.
295 my $fh = $self->{fh};
296 return unless defined $fh;
297 return unless defined fileno $fh;
298 local $\ = ''; # just in case
299 no warnings; # temporarily disable warnings
300 local $^W; # temporarily disable warnings
304 =head2 copy_stats( $target_filename )
306 This will take the stats for the current filehandle and apply them to
307 C< $target_filename >. The stats copied are:
311 =item * Onwer UID and GID
321 my ($temp_filename) = @_;
323 my @stats = stat( $self->{fh} );
324 my $perms = $stats[2] & 07777;
327 chown( $uid, $gid, $temp_filename );
328 chmod( $perms, $temp_filename );
333 This is where the actual locking of the storage medium is performed.
334 Nested locking is supported.
336 B<NOTE>: It is unclear what will happen if a read lock is taken, then
337 a write lock is taken as a nested lock, then the write lock is released.
339 Currently, the only locking method supported is flock(1). This is a
340 whole-file lock. In the future, more granular locking may be supported.
341 The API for that is unclear right now.
343 The following methods manage the locking status. In all cases, they take
344 a L<DBM::Deep/> object and returns nothing.
348 =item * lock_exclusive( $obj )
350 Take a lock usable for writing.
352 =item * lock_shared( $obj )
354 Take a lock usable for reading.
356 =item * unlock( $obj )
358 Releases the last lock taken. If this is the outermost lock, then the
359 object is actually unlocked.
368 return $self->_lock( $obj, LOCK_EX );
374 return $self->_lock( $obj, LOCK_SH );
379 my ($obj, $type) = @_;
381 $type = LOCK_EX unless defined $type;
383 #XXX This is a temporary fix for Win32 and autovivification. It
384 # needs to improve somehow. -RobK, 2008-03-09
385 if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
389 if (!defined($self->{fh})) { return; }
391 #XXX This either needs to allow for upgrading a shared lock to an
392 # exclusive lock or something else with autovivification.
394 if ($self->{locking}) {
395 if (!$self->{locked}) {
396 flock($self->{fh}, $type);
398 # refresh end counter in case file has changed size
399 my @stats = stat($self->{fh});
400 $self->{end} = $stats[7];
402 # double-check file inode, in case another process
403 # has optimize()d our file while we were waiting.
404 if (defined($self->{inode}) && $stats[1] != $self->{inode}) {
409 $obj->{engine}->setup( $obj );
411 flock($self->{fh}, $type); # re-lock
413 # This may not be necessary after re-opening
414 $self->{end} = (stat($self->{fh}))[7]; # re-end
428 if (!defined($self->{fh})) { return; }
430 if ($self->{locking} && $self->{locked} > 0) {
433 if (!$self->{locked}) {
434 flock($self->{fh}, LOCK_UN);