1 package DBM::Deep::File;
6 use warnings FATAL => 'all';
8 use Fcntl qw( :DEFAULT :flock :seek );
10 use constant DEBUG => 0;
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 (the
26 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 needed outside
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 set, it will be used
169 as the zero location. If it is undefined, no seeking will occur. Then, C< @data > will be printed to
170 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 set, it will be used
203 as the zero location. If it is undefined, no seeking will occur. Then, C< $length > bytes will be
204 read from the current location.
206 The data read will be returned.
212 my ($loc, $size) = @_;
214 my $fh = $self->{fh};
215 if ( defined $loc ) {
216 seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
220 my $caller = join ':', (caller)[0,2];
221 warn "($caller) read_at( " . (defined $loc ? $loc : '<undef>') . ", $size )\n";
225 read( $fh, $buffer, $size);
232 When the ::File object goes out of scope, it will be closed.
245 =head2 request_space( $size )
247 This takes a size and adds that much space to the DBM.
249 This returns the offset for the new location.
257 #XXX Do I need to reset $self->{end} here? I need a testcase
258 my $loc = $self->{end};
259 $self->{end} += $size;
266 This flushes the filehandle. This takes no parameters and returns nothing.
273 # Flush the filehandle
274 my $old_fh = select $self->{fh};
275 my $old_af = $|; $| = 1; $| = $old_af;
283 This takes no parameters. It returns a boolean saying if this filehandle is
286 Taken from L<http://www.perlmonks.org/?node_id=691054/>.
293 my $fh = $self->{fh};
294 return unless defined $fh;
295 return unless defined fileno $fh;
296 local $\ = ''; # just in case
297 no warnings; # temporarily disable warnings
298 local $^W; # temporarily disable warnings
302 =head2 copy_stats( $target_filename )
304 This will take the stats for the current filehandle and apply them to
305 C< $target_filename >. The stats copied are:
309 =item * Onwer UID and GID
319 my ($temp_filename) = @_;
321 my @stats = stat( $self->{fh} );
322 my $perms = $stats[2] & 07777;
325 chown( $uid, $gid, $temp_filename );
326 chmod( $perms, $temp_filename );
331 This is where the actual locking of the storage medium is performed.
332 Nested locking is supported.
334 B<NOTE>: It is unclear what will happen if a read lock is taken, then
335 a write lock is taken as a nested lock, then the write lock is released.
337 Currently, the only locking method supported is flock(1). This is a
338 whole-file lock. In the future, more granular locking may be supported.
339 The API for that is unclear right now.
341 The following methods manage the locking status. In all cases, they take
342 a L<DBM::Deep/> object and returns nothing.
346 =item * lock_exclusive( $obj )
348 Take a lock usable for writing.
350 =item * lock_shared( $obj )
352 Take a lock usable for reading.
354 =item * unlock( $obj )
356 Releases the last lock taken. If this is the outermost lock, then the
357 object is actually unlocked.
366 return $self->_lock( $obj, LOCK_EX );
372 return $self->_lock( $obj, LOCK_SH );
377 my ($obj, $type) = @_;
379 $type = LOCK_EX unless defined $type;
381 #XXX This is a temporary fix for Win32 and autovivification. It
382 # needs to improve somehow. -RobK, 2008-03-09
383 if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
387 if (!defined($self->{fh})) { return; }
389 #XXX This either needs to allow for upgrading a shared lock to an
390 # exclusive lock or something else with autovivification.
392 if ($self->{locking}) {
393 if (!$self->{locked}) {
394 flock($self->{fh}, $type);
396 # refresh end counter in case file has changed size
397 my @stats = stat($self->{fh});
398 $self->{end} = $stats[7];
400 # double-check file inode, in case another process
401 # has optimize()d our file while we were waiting.
402 if (defined($self->{inode}) && $stats[1] != $self->{inode}) {
407 $obj->{engine}->setup_fh( $obj );
409 flock($self->{fh}, $type); # re-lock
411 # This may not be necessary after re-opening
412 $self->{end} = (stat($self->{fh}))[7]; # re-end
426 if (!defined($self->{fh})) { return; }
428 if ($self->{locking} && $self->{locked} > 0) {
431 if (!$self->{locked}) {
432 flock($self->{fh}, LOCK_UN);