1 package DBM::Deep::File;
8 our $VERSION = q(1.0002);
10 use Fcntl qw( :DEFAULT :flock :seek );
25 #XXX Migrate this to the engine, where it really belongs.
26 filter_store_key => undef,
27 filter_store_value => undef,
28 filter_fetch_key => undef,
29 filter_fetch_value => undef,
32 # Grab the parameters we want to use
33 foreach my $param ( keys %$self ) {
34 next unless exists $args->{$param};
35 $self->{$param} = $args->{$param};
38 if ( $self->{fh} && !$self->{file_offset} ) {
39 $self->{file_offset} = tell( $self->{fh} );
42 $self->open unless $self->{fh};
50 # Adding O_BINARY should remove the need for the binmode below. However,
51 # I'm not going to remove it because I don't have the Win32 chops to be
52 # absolutely certain everything will be ok.
53 my $flags = O_CREAT | O_BINARY;
55 if ( !-e $self->{file} || -w _ ) {
63 sysopen( $fh, $self->{file}, $flags )
64 or die "DBM::Deep: Cannot sysopen file '$self->{file}': $!\n";
67 # Even though we use O_BINARY, better be safe than sorry.
70 if ($self->{autoflush}) {
93 unless ( defined $self->{inode} ) {
94 my @stats = stat($self->{fh});
95 $self->{inode} = $stats[1];
96 $self->{end} = $stats[7];
108 my $fh = $self->{fh};
109 if ( defined $loc ) {
110 seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
120 my ($loc, $size) = @_;
124 my $fh = $self->{fh};
125 if ( defined $loc ) {
126 seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
130 read( $fh, $buffer, $size);
148 #XXX Do I need to reset $self->{end} here? I need a testcase
149 my $loc = $self->{end};
150 $self->{end} += $size;
156 # If db locking is set, flock() the db file. If called multiple
157 # times before unlock(), then the same number of unlocks() must
158 # be called before the lock is released.
162 my ($obj, $type) = @_;
164 $type = LOCK_EX unless defined $type;
166 if (!defined($self->{fh})) { return; }
168 if ($self->{locking}) {
169 if (!$self->{locked}) {
170 flock($self->{fh}, $type);
172 # refresh end counter in case file has changed size
173 my @stats = stat($self->{fh});
174 $self->{end} = $stats[7];
176 # double-check file inode, in case another process
177 # has optimize()d our file while we were waiting.
178 if (defined($self->{inode}) && $stats[1] != $self->{inode}) {
183 $obj->{engine}->setup_fh( $obj );
185 flock($self->{fh}, $type); # re-lock
187 # This may not be necessary after re-opening
188 $self->{end} = (stat($self->{fh}))[7]; # re-end
200 # If db locking is set, unlock the db file. See note in lock()
201 # regarding calling lock() multiple times.
206 if (!defined($self->{fh})) { return; }
208 if ($self->{locking} && $self->{locked} > 0) {
210 if (!$self->{locked}) { flock($self->{fh}, LOCK_UN); }
221 # Flush the filehandle
222 my $old_fh = select $self->{fh};
223 my $old_af = $|; $| = 1; $| = $old_af;